27661

Android EditText Resize Programmatically

Question:

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 the second line) to match the width of the first line. I have tried making it a TableLayout instead of just using LinearLayouts and now am trying to set the width programmatically. I get the width of the first EditText and try to setWidth of the second EditText, but it doesn't change sizes.

<strong>* JAVA RESIZE ATTEMPT *</strong>

@Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); getWidthBox = (EditText) findViewById(R.id.editText10); setWidthBox = (EditText) findViewById(R.id.EditText01); Toast.makeText(this, "Meaured Width: " + Integer.toString(getWidthBox.getMeasuredWidth()), Toast.LENGTH_SHORT).show(); Toast.makeText(this, "Get Width: " + Integer.toString(getWidthBox.getWidth()), Toast.LENGTH_SHORT).show(); //Neither of these setWidth Lines Work Correctly setWidthBox.getLayoutParams().width=getWidthBox.getWidth(); setWidthBox.setWidth(getWidthBox.getWidth()); }

<strong>* XML LAYOUT *</strong>

<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.15" android:paddingLeft="5dp" > <TextView android:id="@+id/textView2" android:layout_width="110dp" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:text="Points Allowed: " android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="15sp" /> <EditText android:id="@+id/editText10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="number" > </EditText> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:src="@drawable/editpencil" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.15" android:paddingLeft="5dp" > <TextView android:id="@+id/TextView01" android:layout_width="110dp" android:layout_height="wrap_content" android:text="Points Used: " android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="15sp" /> <EditText android:id="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="number" /> </LinearLayout>

<strong>* SCREENSHOT FROM EMULATOR *</strong> <img alt="Screenshot from Emulator" class="b-lazy" data-src="https://i.stack.imgur.com/zOVQe.png" data-original="https://i.stack.imgur.com/zOVQe.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer1:

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);

Answer2:

you probably want to do this

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

Answer3:

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>

Recommend

  • Differences between: myView.getLayoutParams().height = screenHeight and myView.setLayoutParams(lp)?
  • Setting/adding custom content view for ProgressDialog: requestfeature() must be called
  • Custom ScaleAnimation / applytransformation is not called
  • How To Dynamically Set EditText Height
  • Checkbox in AlertDialog “pushed out” by TextView
  • Change icon size of tabs
  • Slide image from left to right and right to left
  • Sticky Footer Below ListView in Android Layout
  • Background image crashes android
  • Float Label hint (TextInputLayout) not works with Android Data Binding
  • Setting attributes of an EditText added dynamically in Android
  • EditText ignores the ImeActionLabel
  • Android: How do you create an EditText field in java class
  • Gray line inside cardview :)?
  • Splash Screen will not display
  • WPF version of .ScaleControl?
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • Multicolored edittext hint
  • Redux Form - Not able to type anything in input
  • How Lists (specifically, RecyclerView with CardViews) in Android work
  • How can I sort a a table with VBA with given text condition?
  • Play WS (2.2.1): post/put large request
  • Disable Enter in editText android
  • Cannot resolve symbol 'MyApi'
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • All Classes Conforming to Protocol Inherit Default Implementation
  • Deserializing XML into class C#
  • Release, debug version and Authorization Google?
  • align graphs with different xlab
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?