0

I am creating a table row dynamically in android and creating a button in that row. All are rendering fine but the created button fills the parent, i.e. occupies the full width of the parent row. I want it as a fixed width array i.e. 150px.

My Code

TableRow rows = new TableRow(this);
TableLayout.LayoutParams tableRowParamss=
     new TableLayout.LayoutParams
     (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

     rows.setBackgroundColor(Color.parseColor("#62b0ff"));
     tableRowParamss.setMargins(0, 0, 0, 40);
     rows.setLayoutParams(tableRowParamss);

     Button ib = new Button(this);
     //ib.setBackgroundResource(R.drawable.accept);
     final float scale = getResources().getDisplayMetrics().density;
     int heightDp = (int) (33 * scale + 0.5f);
     int widthDp = (int) (60 * scale + 0.5f); 
     ViewGroup.LayoutParams bLp = new ViewGroup.LayoutParams(widthDp,heightDp);

     rows.addView(ib);

     table.addView(rows);

How can I make the button width fixed 150px and align to right side of row?

CompanyDroneFromSector7G
  • 4,291
  • 13
  • 54
  • 97
ramesh
  • 4,008
  • 13
  • 72
  • 117

4 Answers4

0

Try this for fixed size button in the row --

TableRow.LayoutParams bLp = new TableRow.LayoutParams();
    // Set the height and width as per your requirement
    bLp.width=100;
    bLp.height=50;      

    rows.addView(ib,bLp);
Anukool
  • 5,301
  • 8
  • 29
  • 41
  • hi I tried this.. but still my button filling the parent row ... any idea ? – ramesh May 10 '13 at 03:45
  • I created a linearlayout inside to that table row. Thats how I solved the problem. By the way there is any option for refreshing activity on resume ( means after quit and reopen ) ? – ramesh May 10 '13 at 04:53
  • try this - http://stackoverflow.com/questions/4311097/how-can-i-refresh-the-activity – Anukool May 10 '13 at 04:56
  • hello ..please have a look at http://stackoverflow.com/questions/16475684/restart-android-activity-on-relaunch-of-application – ramesh May 10 '13 at 05:23
0
    RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
    params.width = 150;
    params.height = 100;
    button.setLayoutParams(params);
Android Developer
  • 1,039
  • 1
  • 9
  • 33
0
        Import for TableRowParam
      //  import android.widget.TableRow.LayoutParams;    


       @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.text);

            TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);

    TableRow rows = new TableRow(this);
    TableLayout.LayoutParams tableRowParamss=
            new TableLayout.LayoutParams
                 (TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT);

    rows.setBackgroundColor(Color.parseColor("#62b0ff"));
    tableRowParamss.setMargins(0, 0, 0, 40);
    rows.setLayoutParams(tableRowParamss);

    Button ib = new Button(this);
    //ib.setBackgroundResource(R.drawable.accept);
            // import android.widget.TableRow.LayoutParams;

    LayoutParams rowParam = new LayoutParams(150, LayoutParams.WRAP_CONTENT);
     ib.setLayoutParams(rowParam);

     rows.addView(ib);

     table.addView(rows);


}



 <?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:gravity="center"
  android:orientation="vertical" >

    <TableLayout
       android:id="@+id/tableLayout1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

    </TableLayout>

</LinearLayout>
Pawan Yadav
  • 1,772
  • 19
  • 17
  • Hello, Use TableLayout using xml then inflate in onCreate and add tableRow dynamically. I test this code and it work fine to me. – Pawan Yadav May 09 '13 at 12:41
  • hi I tried this.. but still my button filling the parent row ... any idea ? – ramesh May 10 '13 at 03:45
  • can you send me your xml layout in which you dynamically add table row – Pawan Yadav May 10 '13 at 03:47
  • I am just created a table layout in my xml and dynamically adding rows in to that table layout. Only problem is the created button filling the parent row – ramesh May 10 '13 at 03:57
  • 1
    // import android.widget.TableRow.LayoutParams; Insure you apply LayoutParam for button is android.widget.TableRow.LayoutParams; It work for me – Pawan Yadav May 10 '13 at 04:15
0
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
button.setLayoutParams(params);
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57