Skip to content Skip to sidebar Skip to footer

Android EditText Resize Programmatically

I have a view in which I have two LinearLayouts - one with Text, EditText and a Button and one with just Text and EditText. I am trying to get the width of the second EditText (on

Solution 1:

I solved it in a bit of a different way that makes the most sense to me. What I did was get the width of the pencil button via getWidth and then set the right-padding of the LinearLayout (each row is its own LinearLayout) to the width of that button. This seems to work perfectly and allows for the button to change size (based on screen size and DP) and the padding to react accordingly.

imageButtonWidth = (ImageButton) findViewById(R.id.imageButtonEdit);
linearLayoutPointsUsed= (LinearLayout) findViewById(R.id.linearPointsUsed);

widthOfEditButton = imageButtonWidth.getWidth();

linearLayoutPointsUsed.setPadding(5, 0, widthOfEditButton, 0);

Solution 2:

you probably want to do this

getWidthBox.getLayoutParams().width=getWidthBox.getWidth();
 setWidthBox.setWidth(getWidthBox.getWidth());

Solution 3:

I would just use relative layout and alignRight the second TextView to the first

try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/TV01"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:text="Test Text1: "
        android:textSize="18dp" />

    <EditText
        android:id="@+id/ET01"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_toLeftOf="@+id/Btn"
        android:layout_toRightOf="@+id/TV01"
        android:text="test text" />

    <Button
        android:id="@+id/Btn"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:text="myBtn" />

    <TextView
        android:id="@+id/TV02"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_below="@+id/TV01"
        android:layout_marginTop="8dp"
        android:text="Text2: "
        android:textSize="18dp" />

    <EditText
        android:id="@+id/ET02"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/ET01"
        android:layout_alignRight="@+id/ET01"
        android:layout_below="@+id/TV01"
        android:text="test text" />

</RelativeLayout>

Post a Comment for "Android EditText Resize Programmatically"