How to give a dynamic generated button an id? - android

Hello i used a code to generate some buttons in a tablelayout. The buttons are generated by this layout, so none of them has an id yet. So here is my question. How can i give a specific button an id? (For example the button in the first row, second colum should have the id "button2")
public class MainActivity extends ActionBarActivity {
private static final int NUM_ROWS = 3;
private static final int NUM_COLS = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateButtons();
}
private void populateButtons() {
TableLayout table = (TableLayout) findViewById(R.id.TableLayout);
for (int row =0; row < NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col < NUM_COLS; col++) {
Button button = new Button(this);
tableRow.addView(button);
}
}
}

You can use View.setId(), however I guess you don't have to find the Views by id because you already know them.

Related

How to add buttons dynamically?

I want to add buttons dynamically. I am adding multiple buttons dynamically but I want to add the buttons in following pattern:
[BUTTON1] [BUTTON2]
[BUTTON3] [BUTTON4]
[BUTTON5] [BUTTON6]
That means I want to add only 2 buttons in a row that is dynamically.
I have tried many options.
one of them is:
LinearLayout ll = (LinearLayout) findViewById(R.id.buttonlayout);
Button[][] buttonArray = new Button[count][count];
TableLayout table = new TableLayout(this);
for (int row = 0; row < count; row++) {
TableRow currentRow = new TableRow(this);
for (int button = 0; button < row; button++) {
Button currentButton = new Button(this);
// you could initialize them here
currentButton.setOnClickListener(this);
// you can store them
buttonArray[row][button] = currentButton;
// and you have to add them to the TableRow
currentRow.addView(currentButton);
}
// a new row has been constructed -> add to table
table.addView(currentRow);
}
and finally takes that new table and add it to your layout. ll.addView(table);
Note: count of button could be random.
How can I do that?
Use vertical LinearLayout in XML. Then programmatically create horizontal LinearLayout and add buttons in horizontal layout. For each line, create and add new horizontal layout.
XML:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/buttonlayout">
</LinearLayout>
ACTIVITY:
public class dynamicButtons extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int numberOfRows = 3;
int numberOfButtonsPerRow = 2;
int buttonIdNumber = 0;
final LinearLayout verticalLayout= LinearLayout)findViewById(R.id.buttonlayout);
for(int i=0;i<numberOfRows;i++){
LinearLayout newLine = new LinearLayout(this);
newLine.setLayoutParams(params);
newLine.setOrientation(LinearLayout.HORIZONTAL);
for(int j=0;j<numberOfButtonsPerRow;j++){
Button button=new Button(this);
// You can set button parameters here:
button.setWidth(20);
button.setId(buttonIdNumber);
button.setLayoutParams(params);
button.setText("Button Name");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent is = new Intent(getApplicationContext(), someOtherApplication.class);
is.putExtra("buttonVariable", buttonIdNumber);
startActivity(is);
}
});
newLine.addView(button);
buttonIdNumber++;
}
verticalLayout.addView(newLine);
}
}
}
try this code:
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int i=0; i<2; i++) { //number of rows
TableRow tr = new TableRow(this);
for (int j=0; j<2; j++) { //number of columns
Button b = new Button (this);
b.setText("Button:"+i+j);
b.setTextSize(10.0f);
b.setOnClickListener(this);
tr.addView(b, 30,30);
}
layout.addView(tr);
}
As your number of button is random u can use:
int total = 20; //random number of buttons
int column = 3; //specify the column number
int row = total / column;
now use the column and row value to dynamically show buttons
Do something like this:
LinearLayout ll_Main = new LinearLayout(getActivity());
LinearLayout ll_Row01 = new LinearLayout(getActivity());
LinearLayout ll_Row02 = new LinearLayout(getActivity());
ll_Main.setOrientation(LinearLayout.VERTICAL);
ll_Row01.setOrientation(LinearLayout.HORIZONTAL);
ll_Row02.setOrientation(LinearLayout.HORIZONTAL);
final Button button01 = new Button(getActivity());
final Button button02 = new Button(getActivity());
final Button button03 = new Button(getActivity());
final Button button04 = new Button(getActivity());
ll_Row01.addView(button01);
ll_Row01.addView(button02);
ll_Row02.addView(button03);
ll_Row02.addView(button04);
ll_Main.addView(ll_Row01);
ll_Main.addView(ll_Row02);
button04.setVisibility(View.INVISIBLE);
button04.setVisibility(View.VISIBLE);
Create a listview/recyclerview with custom item that hold 2 button as you mentioned in your question, than populate that listView with the buttons (inside the adapter if item index % 2 == 0 it will take the left position otherwise the right position).

How to add multiple table row?

Only show one row(last data)
I did not get multiple row.There is showing only one row. How to solve this problem.
//Activity
public class TableViewActivity extends AppCompatActivity {
TableLayout tableLayout;
TableRow tableRow;
TextView textView1, textView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table_view);
tableLayout = (TableLayout) findViewById(R.id.tableLayout);
tableRow = (TableRow) findViewById(R.id.tableRow2);
textView1 = (TextView) findViewById(R.id.TextViewRowTwo1);
textView2 = (TextView) findViewById(R.id.TextViewRowTwo2);
List<Student> students = new ArrayList<>();
students.add(new Student(1, "Mahmud"));
students.add(new Student(2, "Saiful"));
students.add(new Student(3, "Monir"));
students.add(new Student(4, "Milton"));
for (int j = 0; j< students.size(); j++) {
textView1.setText(students.get(j).getId()+"");
textView2.setText(students.get(j).getName());
// TEXTVIEW
if(textView1.getParent()!=null)
((ViewGroup)textView1.getParent()).removeView(textView1); // <- fix
tableRow.addView(textView1);
if(textView2.getParent()!=null)
((ViewGroup)textView2.getParent()).removeView(textView2); // <- fix
tableRow.addView(textView2);
if(tableRow.getParent()!=null)
((ViewGroup)tableRow.getParent()).removeView(tableRow); // <- fix
tableLayout.addView(tableRow);
}
setContentView(tableLayout);
}
}
You need to create the TableRow Dynamically and whenever you put the values in tablerow you need to add that tablerow to tablelayut.
for (int j = 0; j< students.size(); j++) {
TableRow tableRow = new TableRow(this);
textView1.setText(students.get(j).getId()+"");
textView2.setText(students.get(j).getName());
// TEXTVIEW
if(textView1.getParent()!=null)
((ViewGroup)textView1.getParent()).removeView(textView1); // <- fix
tableRow.addView(textView1);
tableLayout.addView(tableRow);
if(textView2.getParent()!=null)
((ViewGroup)textView2.getParent()).removeView(textView2); // <- fix
tableRow.addView(textView2);
tableLayout.addView(tableRow);
if(tableRow.getParent()!=null)
((ViewGroup)tableRow.getParent()).removeView(tableRow); // <- fix
tableLayout.addView(tableRow);
}
Hope this will work for you.
I think that in your case is better for you to use a ListView. It's more easy and is designed to show a list of objects like your students.
https://developer.android.com/guide/topics/ui/layout/listview.html

pass value from stringbuilder to textview

I am trying to get words from several EditTexts and display them using TextView without using any layout. But the program is force closed every-time I run it.
LOGCAT: java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.tablelayout/com.example.tablelayout.MainActivity}:
java.lang.NullPointerException
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
linearLayout.setLayoutParams(params);
linearLayout.setOrientation(VERTICAL);
int count = 10;
linearLayout.addView(tableLayout(count));
linearLayout.addView(submitButton());
setContentView(linearLayout);
}
private Button submitButton() {
Button button = new Button(this);
button.setHeight(WRAP_CONTENT);
button.setText("Submit");
button.setOnClickListener(submitListener);
return button;
}
TextView txt=new TextView(this);
// Access the value of the EditText
private View.OnClickListener submitListener = new View.OnClickListener() {
public void onClick(View view) {
StringBuilder stringBuilder = new StringBuilder();
for (EditText editText : editTextList) {
stringBuilder.append(editText.getText().toString());
}
txt.setText(stringBuilder.toString().trim());
}
};
// Using a TableLayout as it provides you with a neat ordering structure
private TableLayout tableLayout(int count) {
TableLayout tableLayout = new TableLayout(this);
tableLayout.setStretchAllColumns(true);
int noOfRows = count / 5;
for (int i = 0; i < noOfRows; i++) {
int rowId = 5 * i;
tableLayout.addView(createOneFullRow(rowId));
}
int individualCells = count % 5;
tableLayout.addView(createLeftOverCells(individualCells, count));
return tableLayout;
}
private TableRow createLeftOverCells(int individualCells, int count) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
int rowId = count - individualCells;
for (int i = 1; i <= individualCells; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private TableRow createOneFullRow(int rowId) {
TableRow tableRow = new TableRow(this);
tableRow.setPadding(0, 10, 0, 0);
for (int i = 1; i <= 5; i++) {
tableRow.addView(editText(String.valueOf(rowId + i)));
}
return tableRow;
}
private EditText editText(String hint) {
EditText editText = new EditText(this);
editText.setId(Integer.valueOf(hint));
editText.setHint(hint);
editTextList.add(editText);
return editText;
}
}
TextView txt=new TextView(this);
you can't declare and initialize a class member View at the same time, because the Context is not yet valid
change it to:
public class MainActivity extends Activity {
private List<EditText> editTextList = new ArrayList<EditText>();
private TextView txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txt = new TextView(this);
// other code
also I noticed that you are not adding this View to any of the containers that your provided to setContentView. The suggestion will fix the crash but you will not see the TextView's content
Exception shows problem is in your manifest file.
Your package name is manifest tag and your activity are in different locations.

tablelayout delete tablerow in android

final TableLayout table = (TableLayout) findViewById(R.id.tableLayout);
TableRow row = new TableRow(this);
TextView t2 = new TextView(this);
t2.setText("test");
row.addView(t2);
Button bu = new Button(this);
bu.setBackgroundResource(R.drawable.del);
bu.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//I need to delete the tablerow
//how to do?
}
});
row.addView(bu);
table.addView(row, new TableLayout.LayoutParams(WC, WC));
**
i want to delete tablerow in bu.setOnClickListener
how to do removeViewAt() ,i cant find indexId
**
use removeView for removing tablerow as:
table.removeView(row);
NOTE: If they don't have unique id then use:
table.removeView(rowIndex);
and by using removeViewAt
for(int i = 0, j < table.getChildCount(); i < j; i++){
// then, you can remove the the row you want...
// for instance...
TableRow row = getChildAt(i);
if( something you want to check ) {
removeViewAt(i);
// or...
removeView(row);
}
}

Tableview in android [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
show data in table view in android
how to data set in tableview.
how to show data in tableview.
A quick example:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TableRow.LayoutParams params = new TableRow.LayoutParams(100, 100);
TableLayout table = new TableLayout(this);
for (int i = 0; i < 3; i++) {
TableRow row = new TableRow(this);
for (int j = 0; j < 3; j++) {
TextView text = new TextView(this);
text.setLayoutParams(params);
text.setText(String.format("(%d, %d)", i, j));
row.addView(text);
}
table.addView(row);
}
setContentView(table);
}
}
If you can get away with it, GridView and ListView can be much easier to work with using adapters such as ArrayAdapter and CursorAdapter.

Categories

Resources