I'm trying to programmatically resize an EditText
when the orientation of the phone changes, here's what I have so far:
private final float editTextWidthLandscap = 380;
private final float editTextWidthPortrait = 220;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Resources r = getResources();
float px;
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthLandscap, r.getDisplayMetrics());
input.setWidth((int) px); // input is an EditText
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthPortrait, r.getDisplayMetrics());
input.setWidth((int) px); // input is an EditText
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
I also have this in my manifest file (in case it matters):
<activity
android:name=".Results"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateHidden" >
</activity>
The strange thing is that it displays the Toast
just fine but it seems to ignore the input.setWidth()
statement. If anyone could explain why the EditText
is not being resized and/or give me pointers as to how i would be able to this correctly it would be greatly appreciated.
Edit:
This is a part of the XML file in which the EditText resides
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@color/purple"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<EditText
android:id="@+id/input"
android:layout_width="220dp"
android:layout_height="50dp"
android:background="@drawable/search_bar"
android:hint="@string/input_hint"
android:inputType="text|textNoSuggestions"
android:maxLines="1"
android:minHeight="50dp"
android:minWidth="220dp"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textColor="@color/black_grey"
android:textSize="16.67sp" />
<Button
android:id="@+id/search_button"
android:layout_width="65dp"
android:layout_height="50dp"
android:background="@drawable/search_button" />
</LinearLayout>
Edit 2: (solution)
I still don't know what input.setWidth()
does exactly but i found out i had to use LayoutParams
for this issue. Below is the code that is working for me now:
private final float editTextWidthLandscap = 380;
private final float editTextWidthPortrait = 220;
private final float editTextHeight = 50;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Resources r = getResources();
float widthpx;
float heightpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextHeight, r.getDisplayMetrics());;
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthLandscap, r.getDisplayMetrics());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) Math.round(widthpx), (int) Math.round(heightpx));
trademarkTextfield.setLayoutParams(lp);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthPortrait, r.getDisplayMetrics());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) Math.round(widthpx), (int) Math.round(heightpx));
trademarkTextfield.setLayoutParams(lp);
}
}