0

I am able to control the height but somehow it does not make any difference to the button width.

    // Table Row for Previous button
    TableRow tableRow1 = new TableRow(this);

    tableRow1.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT));
    tableRow1.setPadding(5, 5, 5, 5);

    // Button - Previous
    Button btnPrev = new Button(this);
    btnPrev.setText("< Previous");
    btnPrev.setGravity(Gravity.CENTER);
    btnPrev.setOnClickListener(prevListener);

    tableRow1.addView(btnPrev);

    android.view.ViewGroup.LayoutParams params = btnPrev.getLayoutParams();
    params.height = 20;
    params.width = 60;

    btnPrev.setLayoutParams(params);


    // TextView for Previous button
    TextView textView11 = new TextView(this);
    textView11.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
    textView11.setGravity(Gravity.CENTER_VERTICAL);
    textView11.setGravity(Gravity.RIGHT);
    textView11.setPadding(0, 0, 20, 0);
    textView11.setText("New Entry Form");

    tableRow1.addView(textView11);

    android.view.ViewGroup.LayoutParams params1 = textView11
            .getLayoutParams();
    params1.height = (int) dipHeight;
    textView11.setLayoutParams(params1);

    tableLayout.addView(tableRow1);

I tried changing few things here and there but no look.

Can someone from you take a time and help me on this?

Richard Paul
  • 1
  • 1
  • 1

4 Answers4

1
// replace this code
tableRow1.addView(btnPrev);

android.view.ViewGroup.LayoutParams params = btnPrev.getLayoutParams();
params.height = 20;
params.width = 60;
btnPrev.setLayoutParams(params);

             to
tableRow1.addView(btnPrev,new android.view.ViewGroup.LayoutParams(20,60));
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

to set button width & height use below code

btnPrev.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnPrev.setwidth(60);
btnPrev.setheight(20);
Kailash Ahirwar
  • 771
  • 3
  • 14
  • This gives runtime error: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.TableRow$LayoutParams – Richard Paul Sep 18 '13 at 18:06
0

You can set button width in two ways:

Via java :

 Button.setWidth(int width);

Via XML properties :

<Button android:layout_height="wrap_content"
 android:id="@+id/ButtonPrev" 
 android:layout_below="@id/output" 
 android:text="7" 
 android:minWidth="100dp">
</Button>

The property : android:minWidth will set the width of the button.

Reference : Set width of Button in Android

Community
  • 1
  • 1
The Dark Knight
  • 5,455
  • 11
  • 54
  • 95
0

View.requestLayout() maybe what you want?

afpro
  • 1,103
  • 7
  • 12