Android Java, Arduino Control
Automatically Generate a Button Array Interface
without XML
Introduction
Under ordinary circumstances you use XML to create layouts for Android, and do the programming in Java. However, there are circumstances when this isn't convenient - you may need to do it on the fly, or it may be just easier to create layouts in Java. Below is some Java code to do just this, and it shows how easy it really is!
Tricolour LED grid
I wanted to produce an array of buttons which could be used as an interface to control an Arduino over blue-tooth. Maybe it could be for a grid of LED's or an array of relays/ mosfets / switches etc... For Blue-Tooth see this post: http://gampageek.blogspot.co.uk/2013/12/set-paireddevices-mbluetoothadapter.html
The key class here is TableLayout():
// Table
myTableLayout = new TableLayout(this);
TableLayout.LayoutParams myParams = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
myTableLayout.setLayoutParams(myParams);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
-------------------------------------------------------------------------------------------------------------------
Full code:
package biz.consett.ArduinoLEDgrid;
/**
* Create an array of buttons and chose their colours in Android without using an XML layout
*
* Could be used in combination with Bluetooth classes
* eg [http://gampageek.blogspot.co.uk/2013/12/set-paireddevices-mbluetoothadapter.html]
* to communicate with Arduino over Blue-tooth to control an LED grid / relay / mosfet switch grid
* ETC
*
***Disclaimer***
* Please note this code is provided for free and AS IS. There is no support and no guarantee
* from the Author.
*****************
*
* @author Craig Turner
*
* Please use it, modify it, and enjoy it.
* If you find it useful please link to my website:
* http://gampageek.blogspot.co.uk, where you can also find
* the project write up and Arduino code, and other hacks.
*
*
*/
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Point;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.LinearLayout.LayoutParams;
public class ButtonGrid extends Activity implements OnClickListener
{
int LedID = 0;
int LedColor = AColor.WHITE; //null color
// dimensions of grid
final static int Xsize = 8;
final static int Ysize = 8 ;
static int ButtonID = 0;
static int LedList [] = new int [Xsize * Ysize];
static final Button ButtonArray[][] = new Button[Xsize][Ysize];
TableRow rowArray[] = new TableRow[Ysize];
TableRow rowColorButtons;
TableLayout myTableLayout;
TextView myTextViewDebug1;
TextView myEditTextBT;
static Button BlackButton = null; // 0
static Button GreenButton = null; // 1
static Button RedButton = null; // 2
static Button YellowButton = null; // 3
static ToggleButton BTconnectButton = null;
static int SelectedColor = 0; // black
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
// get screen size to calc width of buttons
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//---------------------------------------------------------------------
//layouts*************************************************
// Table
myTableLayout = new TableLayout(this);
TableLayout.LayoutParams myParams = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
myTableLayout.setLayoutParams(myParams);
TableLayout.LayoutParams rowLayout = new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// debug1 text view
myTextViewDebug1 = new TextView(this);
myTextViewDebug1.setLayoutParams(rowLayout);
myTextViewDebug1.setTextColor(AColor.YELLOW);
myTextViewDebug1.setText("Debug1: " + "null");
// BT connection edit text
myEditTextBT = new EditText(this);
myEditTextBT.setTextColor(AColor.WHITE);
myEditTextBT.setTextSize(10);
myEditTextBT.setHint("BT Connect");
// Color Row
rowColorButtons = new TableRow(this);
rowColorButtons.setLayoutParams(rowLayout);
//**************************************************************************
// Create an array of buttons, Create row views and add buttons to
// views
for (int y = 0; y < Ysize; y++)
{
rowArray[y] = new TableRow(this);
rowArray[y].setLayoutParams(rowLayout);
}
for (int x = 0; x < Xsize; x++)
{
for (int y = 0; y < Ysize; y++)
{
ButtonArray[x][y] = new Button(this);
ButtonArray[x][y].setOnClickListener(this);
ButtonArray[x][y].setWidth(width / Xsize);
ButtonArray[x][y].setPadding(0, 0, 0, 0);
ButtonArray[x][y].setHeight(50);
ButtonArray[x][y].setTextSize(10);
}
}
// array rows
for (int y = 0; y < Ysize; y++)
{
for (int x = 0; x < Xsize; x++)
{
rowArray[y].setBackgroundColor(AColor.BLACK);
rowArray[y].addView(ButtonArray[x][y]);
String LedCoord = ("(" + String.valueOf(x) + ","
+ String.valueOf(y) + ")");
ButtonArray[x][y].setText(String.valueOf(LedCoord));
ButtonArray[x][y].setTextColor(AColor.PURPLE);
ButtonArray[x][y].setBackgroundColor(AColor.BLACK);
ButtonArray[x][y].setId(LedID);
LedID = LedID + 1;
}
myTableLayout.addView(rowArray[y]);
}
myTableLayout.addView(myTextViewDebug1);
myTableLayout.setBackgroundColor(AColor.BLACK);
//color buttons
// add a Green Button
GreenButton = new Button(this);
GreenButton.setWidth(width / Xsize);
GreenButton.setHeight(50);
GreenButton.setTextSize(11);
GreenButton.setText("Green");
GreenButton.setBackgroundColor(AColor.GREY);
///GreenButton.setOnClickListener(this);
rowColorButtons.addView(GreenButton);
// add a Red Button
RedButton = new Button(this);
RedButton.setWidth(width / Xsize);
RedButton.setHeight(50);
RedButton.setTextSize(11);
RedButton.setText("Red");
RedButton.setBackgroundColor(AColor.GREY);
rowColorButtons.addView(RedButton);
// add a Yellow Button
YellowButton = new Button(this);
YellowButton.setWidth(width / Xsize);
YellowButton.setHeight(50);
YellowButton.setTextSize(11);
YellowButton.setText("Yellow");
YellowButton.setBackgroundColor(AColor.GREY);
rowColorButtons.addView(YellowButton);
// add a Black Button (turn off the colour)
BlackButton = new Button(this);
BlackButton.setWidth(width / Xsize);
BlackButton.setHeight(50);
BlackButton.setTextSize(11);
BlackButton.setText("Black");
BlackButton.setTextColor(AColor.WHITE);
BlackButton.setBackgroundColor(AColor.GREY);
rowColorButtons.addView(BlackButton);
// add a toggle button to open and close the BT connection
BTconnectButton = new ToggleButton(this);
BTconnectButton.setWidth(width / Xsize);
BTconnectButton.setHeight(50);
BTconnectButton.setTextSize(11);
BTconnectButton.setPadding(0, 0, 0, 0);
//BTconnectButton.setText("Connect");
BTconnectButton.setTextColor(AColor.WHITE);
//BTconnectButton.setBackgroundColor(AColor.GREY);
rowColorButtons.addView(myEditTextBT);
rowColorButtons.addView(BTconnectButton);
//add the rows to the Table
myTableLayout.addView(rowColorButtons);
// action onClick for colour buttons
BlackButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
BlackButton.setBackgroundColor(AColor.BLACK);
SelectedColor = 0; // black
RedButton.setBackgroundColor(AColor.GREY);
YellowButton.setBackgroundColor(AColor.GREY);
GreenButton.setBackgroundColor(AColor.GREY);
} catch (Exception ex)
{
}
}
});
RedButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
RedButton.setBackgroundColor(AColor.RED);
SelectedColor = 2; // red
BlackButton.setBackgroundColor(AColor.GREY);
YellowButton.setBackgroundColor(AColor.GREY);
GreenButton.setBackgroundColor(AColor.GREY);
} catch (Exception ex)
{
}
}
});
YellowButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
YellowButton.setBackgroundColor(AColor.YELLOW);
SelectedColor = 3; // yellow
BlackButton.setBackgroundColor(AColor.GREY);
RedButton.setBackgroundColor(AColor.GREY);
GreenButton.setBackgroundColor(AColor.GREY);
} catch (Exception ex)
{
}
}
});
GreenButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
GreenButton.setBackgroundColor(AColor.GREEN);
SelectedColor = 1; // green
BlackButton.setBackgroundColor(AColor.GREY);
YellowButton.setBackgroundColor(AColor.GREY);
RedButton.setBackgroundColor(AColor.GREY);
} catch (Exception ex)
{
}
}
});
// end of action Onclick for color buttons
// set content view - Make the view visible on-screen
setContentView(myTableLayout);
}
// add onClick function to button array view
public void onClick(View view)
{
switch (SelectedColor)
{
case 0:
LedColor = AColor.BLACK; //black
break;
case 1:
LedColor = AColor.GREEN; //green
break;
case 2:
LedColor = AColor.RED; // red
break;
case 3:
LedColor = AColor.YELLOW; // yellow
break;
default:
LedColor = AColor.WHITE; // white
}
view.setBackgroundColor(LedColor);
ButtonID = view.getId();
LedList [ButtonID] = LedColor - 0xFF000000; // set color of led] = color; // set color of led
myTextViewDebug1.setText("Debug1: " + Integer.toString(ButtonID) +" = " + Integer.toHexString(LedList[ButtonID])); // for debug
// so now we can send ButtonID and it's color over BT to arduino */
}//end of add onClick function to button array view
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
end code:----------------------------------------------------------------
No comments:
Post a Comment