I am creating an app that will control a microcontroler via bluetooth. (It will be a 8x32 Led matrix). The idea is to create an 8 row 32 columns array of Imagebuttons each element represents on real LED on the board.
After some documentation I decided to use TableLayout and fill it with ImageButtons. But can't get it to work.
This is what I have done so far:
public class DrawerMode extends Activity implements View.OnTouchListener{
private static int numberOfColumns = 32;
private static int numberOfRows = 8;
private TableLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Assign content
setContentView(R.layout.activity_draw_mod);
tableLayout = (TableLayout) findViewById(R.id.drawer_table);
fillTable(numberOfColumns,numberOfRows, tableLayout);
}
private void fillTable(final int numberOfColumns,final int numberOfRows, TableLayout tableLayout) {
//removing child views
tableLayout.removeAllViews();
TableLayout.LayoutParams params = new TableLayout.LayoutParams( TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
//initiate rows
TableRow[] tableRow = new TableRow[numberOfRows];
for(int iter_R = 0; iter_R < numberOfRows; iter_R++){
//fill rows with buttons
tableRow[iter_R] = new TableRow(this);
ImageButton[] imageButton = new ImageButton[numberOfColumns];
for(int iter_C = 0; iter_C < numberOfColumns; iter_C++){
imageButton[iter_C] = new ImageButton(this);
imageButton[iter_C].setBackgroundColor(Color.WHITE);
imageButton[iter_C].setLayoutParams(params);
//[iter_C]
imageButton[iter_C].setOnTouchListener(this);
imageButton[iter_C].setId(iter_C);
imageButton[iter_C].setTag(iter_C);
//add an instance of a button
tableRow[iter_R].addView(imageButton[iter_C]);
}
//add instance of row
tableLayout.addView(tableRow[iter_R]);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
int selected_element = (Integer) v.getId();
Toast.makeText(getApplicationContext(), "It's"+ selected_element, Toast.LENGTH_LONG).show();
return false;
}
}
Here is the content of the xml file to:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@android:color/holo_green_dark">
<TextView
android:id="@+id/draw_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/draw_mode_name"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:textColor="@android:color/white"
/>
<TableLayout
android:id="@+id/drawer_table"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/draw_title"
>
</TableLayout>
What's the best way for me to do this?