How to add multiple buttons to a row dynamically in Android - android

Does anybody know how to add multiple buttons to a table row dynamically in Android?

You can try this and see if it's what your are looking for.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<Button android:text="Button" android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TableLayout android:id="#+id/tableLayout1"
android:layout_height="wrap_content" android:layout_width="fill_parent" />
</LinearLayout>
Your Activity class:
public class mainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.main );
Button b = (Button) findViewById( R.id.button1 );
b.setOnClickListener( this );
}
#Override
public void onClick(View v) {
TableLayout table = (TableLayout) findViewById( R.id.tableLayout1 );
int buttonsInRow = 0;
int numRows = table.getChildCount();
TableRow row = null;
if( numRows > 0 ){
row = (TableRow) table.getChildAt( numRows - 1 );
buttonsInRow = row.getChildCount();
}
if( numRows == 0 || buttonsInRow == 3 ){
row = new TableRow( this );
table.addView( row );
buttonsInRow = 0;
}
if( buttonsInRow < 3 ){
Button b = new Button( this );
row.addView( b, 100, 50 );
}
}
}
Hope it helps.

Here layout is a TableLayout.If you want to add a row dynamically and buttons in that row can use the follwoing code
TableRow tr1=new TableRow(this);
Button tv=new Button(this);
tv.setText("");
tr1.addView(tv,250,30);
Button tv1=new Button(this);
tv1.setText("");
tr1.addView(tv1,100,30);
layout.addView(tr1);
If you already have row in the layout then just fetch the row and add buttons to the row

After searching for 30 sec I found http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-a-tablelayout-programatically - this should help you, just change TextViews to Buttons.

Related

Dynamically add TableRow to above of TableLayout

I want to dynamically add TableRow to above of TableLayout. I use blow code:
In my activity:
TableLayout table = (TableLayout)ChatActivity.this.findViewById(R.id.tblChats);
for(int i = 1; i < 10; ++i)
{
TableRow row = (TableRow)LayoutInflater.from(ChatActivity.this).inflate(R.layout.chat_row, null);
((TextView)row.findViewById(R.id.attrib_name)).setText("A");
((TextView)row.findViewById(R.id.attrib_value)).setText(i + "");
table.addView(row);
}
table.requestLayout();
chat_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/attrib_name"
android:textStyle="bold"/>
<TextView android:id="#+id/attrib_value"
android:gravity="right"
android:textStyle="normal"/>
</TableRow>
As you know these codes adds TableRow in the bottom of the TableLayout.
Is there any way to add it to the above of TableLayout?
yes you can add row as runtime in table layout.
use below code like chart.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tblChats"
android:layout_width="match_parent"
android:layout_height="match_parent"> </TableLayout>
and make activity like below ..
public class ChartActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chart_layout);
TableLayout table = findViewById(R.id.tblChats);
for (int i = 0; i < 5; i++) {
TableRow row = new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
TextView textView = new TextView(this);
textView.setText("Name" + " " + i);
EditText editText = new EditText(this);
row.addView(textView);
table.addView(row, i);
}
}
}
According to Android Team's answer I realized I must change this line:
table.addView(row);
to this:
table.addView(row, 0);

add buttons to TableRow programatically and fill in screen

I can add TableRow and also some buttons in it programaticaly like below image:
but I want to figure something like this that button fill in screen:
and here is my codes:
<TableLayout
android:id="#+id/Tb1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r3"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
</TableLayout>
and:
Button btn1;
private TableLayout buttonTableLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
buttonTableLayout = (TableLayout) findViewById(R.id.Tb1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int row = 0; row < 3; row++) {
TableRow currentTableRow = getTableRow(row);
for (int column = 0; column < 5; column++) {
Button newGuessButton = (Button) inflater.inflate(R.layout.my_button, null);
String myName = new String(String.valueOf((row * 5) + column + 1));
newGuessButton.setText(myName);
currentTableRow.addView(newGuessButton);
}
}
}
});
}
private TableRow getTableRow(int row) {
return (TableRow) buttonTableLayout.getChildAt(row);
}
and the xml(my_button):
<Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/newButton" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"></Button>
please give me some advice to solve it.
Android TableRow class actually extends LinearLayout class, so you can use all of LinearLayout's features here.
In this case you can use android:layout_weight to equally stretch those buttons. Where you're adding those buttons just add the following piece of code.
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1;
button.setLayoutParams(p);
Or alternatively add these lines to your button.xml layout file:
android:layout_width="0dp"
android:layout_weight="1"
Docs say:
Equally weighted children
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".

Create buttons programmatically goes new line

I created a layout which programmatically created buttons (see code). With this layout I have a column of buttons, but I would create something like the picture below. I tried something with linear layout but the buttons didn't go to new line automatically and didn't show, so I change it to table layout. How can I create something like the picture programmatically?
Thank you.
protected TableLayout layout;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout = (TableLayout) findViewById(R.id.workflows_activity_layout);
private void createButton() {
loading.setVisibility(View.VISIBLE);
layout.removeAllViews();
if (items.length == 0) {
TableRow row = new TableRow(this);
TextView no_item = new TextView(this);
no_questions.setText("there are no items");
no_questions.setTextSize(35);
row.addView(no_item);
layout.addView(row);
} else {
for (int i = 0; i < items.length; i++) {
TableRow row = new TableRow(this);
final Button btn = new Button(this);
TextView tw = new TextView(this);
tw.setText(items[i].getName());
tw.setTextSize(25);
btn.setBackgroundResource(R.drawable.button_image);
btn.setId(i);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = btn.getId();
Intent openItempage = new Intent(MainActivity.this, ItemsActivity.class);
startActivity(openItempage);
}
});
row.addView(btn);
row.addView(tw);
layout.addView(row, params);
items_buttons.add(btn);
items_nameText.add(tw);
}
}
loading.setVisibility(View.GONE);
}
There's an easy way to achieve what you want. Look at this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="#+id/tab_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
TabLayout tab_lay = (TableLayout)view.findViewById(R.id.tab_lay);
for(int i = 1; i <= 4; ){
TableRow a = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
a.setLayoutParams(param);
a.setGravity(Gravity.CENTER_VERTICAL);
for(int y = 0; (y < 2) && (i <= 4); y++) {
Button x = new Button(getActivity());
x.setText("Text " + i);
x.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
x.setLayoutParams(par);
int ids = i;
x.setId(ids);
x.setOnClickListener(this);
a.addView(x);
i++;
}
tab_lay.addView(a);
}
If you want more buttons, then just change 4 which is compare to i to your value. Hope I help.
You can implement this using labraries, for instance
FlowLayout
Usage:
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Another solution is:
AndroidFlowLayout
Usage:
<com.liangfeizc.flowlayout.FlowLayout
android:id="#+id/flow_layout"
flowlayout:vertical_spacing="10dp"
flowlayout:horizontal_spacing="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Layout Problems with Buttons

I'm trying to add table rows programatically (see code following)
Each row contains some textfields and terminates with sometimes a Button
But the button is not aligned like textfields..(see image of output)
I would like to have the same height for textfield and Button
How can i do that ?
Many thanks for your support
Jcarlier
Brussels
public class MainActivity extends Activity {
............
TableLayout tl = (TableLayout) findViewById(R.id.table);
// heading
TableRow tr_head = new TableRow(this);
TextView label_club= new TextView(this);
label_club.setText("REMARQUE");
label_club.setGravity(Gravity.CENTER);
label_club.setTextColor(getResources().getColor(R.color.OrangeRed));
label_club.setBackground(getResources().getDrawable(R.drawable.border));
tr_head.addView(label_club);
// etc..
// iterate sqlite and show in table TableLayout
Integer icount=0;
Integer j =c.getColumnCount()-1;
if (c != null && c.getCount() > 0){
if (c.moveToFirst()) {
do {
TableRow tr = new TableRow(this);
tr.setBaselineAligned(false);
//tr.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for( int i=0;i<j+1;i++)
{
if ( (i == j) & ( c.getString(i).length() > 4) ) {
//map.put(icount, c.getString(i).toString());
Button btn=new Button(this);
btn.setId(icount);
//btn.setMaxWidth(10);
btn.setVisibility(View.VISIBLE);
btn.setText("Tableaux");
btn.setClickable(true);
//btn.setHeight(20);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// blabla
}
}
});
//btn.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
//btn.setLayoutParams (new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//btn.setGravity(Gravity.CENTER_HORIZONTAL);
// btn.setBackground(getResources().getDrawable(R.drawable.border));
tr.addView(btn); }
else {
TextView text=new TextView(this);
switch(c.getType(i))
{
case Cursor.FIELD_TYPE_INTEGER:
text.setText(String.valueOf(c.getInt(i)));
break;
case Cursor.FIELD_TYPE_STRING:
text.setText(c.getString(i));
break;
}
text.setPadding(0,0,10,0);
//text.setHeight(20);
//text.setBackground(getResources().getDrawable(R.drawable.border));
tr.addView(text);
}
}
// add row to table
tl.addView(tr);
icount++;
} while (c.moveToNext());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:id="#+id/layout"
android:scrollbars="horizontal|vertical"
android:layout_alignParentBottom="true"
android:layout_height="fill_parent"
>
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="2dp"
android:stretchColumns="*">
</TableLayout>
</HorizontalScrollView>
</ScrollView>

set two radio buttons in one row then next two radio is on second row and so on

i want to set 2 two radiobuttons one row , then next 2 radiobuttons on second row dynamically(Programmatically) I am new to android any body have idea or sample code of it. What i have do is:
RadioGroup radiogroup = (RadioGroup)findViewById(R.id.RadioGroup01);
for(int i=0;i<10;i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(i);
rdbtn.setText("test");
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(rdbtn);
radiogroup.addView(rdbtn);
ll.addView(radiogroup);
//i%2 == 0
}
You can use below code:
Activity code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int row=0; row < 5; row++)
{
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
for(int i = 0; i < 2; i++) {
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId((row * 2) + i);
rdbtn.setText("test " + rdbtn.getId());
ll.addView(rdbtn);
}
((ViewGroup)findViewById(R.id.radiogroup)).addView(ll);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RadioGroup
android:id="#+id/radiogroup"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
Output on screen:
Hope this is what you were looking for. Cheers!

Categories

Resources