Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android fixed width EditText within TableRow

I'm completely stuck trying to get fixed width EditText widgets in a TableRow. I am attempting to place two EditText side by side with equal width (about 20dip) but no matter which property I try and set the first EditText is way to long and apparently cannot be resized.

Many thanks:

<TableRow 
  android:layout_height="wrap_content"
  android:baselineAligned="false" 
  android:id="@+id/tableRow3" 
  android:gravity="center"
  android:stretchColumns="1" 
  android:layout_width="match_parent">
  <TextView 
    android:id="@+id/textView6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="1" 
    android:paddingLeft="36dip">
  </TextView>
  <EditText
    android:layout_height="wrap_content" 
    android:id="@+id/editText2" 
    android:inputType="number" 
    android:layout_width="20dip">
  </EditText>
  <EditText 
    android:layout_height="wrap_content" 
    android:id="@+id/editText1" 
    android:inputType="number"
    android:layout_width="wrap_content">
    <requestFocus></requestFocus>
  </EditText>
</TableRow>
like image 959
Phil Avatar asked Nov 30 '22 16:11

Phil


1 Answers

I don't know that a TableLayout is the best way to do this, it can be cumbersome unless you're displaying large amounts of data and need to use it.

One of the best ways I've found to ensure that form objects have length distributed the way I want them is by using weight rather than explicitly declaring width.

Try the following:

<LinearLayout ... android:orientation="horizontal" ... 
android:layout_width="match_parent" android:layout_height="wrap_content"
<TextView ... android:layout_width="0dp" ... android:layout_weight="50" />
<TextView ... android:layout_width="0dp" ... android:layout_weight="50" />
</LinearLayout>

Make sure to declare the layout width as 0, this will let the layout fill to the weight.

This should create two TextViews next to each other on the screen, both filling 50% of the screen. You can play with different percentages. You can also use a LinearLayout as a placeholder with a weight of whatever % you would like to place hold.

Make sure that your "weights" add up to 100 in order to ensure the view will look exactly as you want it to. It's not necessary, but it's a good convention to know what % of the screen width it will take up.

like image 107
Codeman Avatar answered Dec 23 '22 16:12

Codeman