Resizing EditText for no reason

Seeing rather strange behavior in relative layout.

This is the initial state: enter image description here

Defined as:

<EditText android:id="@+id/bleedCount" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_toLeftOf="@+id/abrMult" android:layout_below="@+id/textView4" android:layout_marginRight="10dp" android:ems="10" android:inputType="number" > <requestFocus /> </EditText> <TextView android:id="@+id/abrMult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/abrSubmit" android:layout_alignBaseline="@+id/abrSubmit" android:layout_marginRight="10dp" android:text="x12" /> <Button android:id="@+id/abrSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView4" android:layout_alignParentRight="true" android:text="Calculate" /> 

In onItemSelected from the dropdown menu, it changes as follows:

 abrSubmit.setText(pos == 1 ? "Calculate" : "Submit"); abrMult.setVisibility(pos == 1 ? View.VISIBLE : View.GONE); bleedCount.setHint(pos == 1 ? "# of Meow/month" : "# of Meow/year"); 

and turns into this:
enter image description here

Notice how EditText bleedCount much higher in the second snapshot. The value of bleedCount.getHeight() changes from 72 to 95, and I cannot figure out what causes it.

+4
source share
3 answers

Resizing bleedCount EditText is due to the fact that your tooltip text becomes longer than one line if (pos == 1) .

If you comment on the following line in your code, resizing will stop:

 // bleedCount.setHint(pos == 1 ? "# of Meow/month" : "# of Meow/year"); 

Perhaps you can make it shorter / smaller to prevent resizing?

+1
source

It is associated with android:ems="10"
When EditText changed its width, after displaying the view with x12 it had to be split into two lines.

ems is the size of one letter for a given font.

I think you do not need ems .
Set EditText as single line: android:singleLine="true"

+2
source

Well, since I do not have the entire code base, I am doing something simple to reproduce what you are doing. Just FYI, I am using ICS 4.1. I did not have the problems you have, so maybe this is an API problem. Perhaps you can look at my code base and see where there are differences between it and your own. There may be a solution.

XML:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_rl" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Top TextView" /> <Spinner android:layout_below="@+id/textView4" android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_below="@+id/spinner1" android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Above EditText" /> <EditText android:id="@+id/bleedCount" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView5" android:layout_marginRight="10dp" android:layout_toLeftOf="@+id/abrMult" android:ems="10" android:inputType="number" > </EditText> <TextView android:id="@+id/abrMult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView5" android:layout_alignBaseline="@+id/abrSubmit" android:layout_marginRight="10dp" android:layout_toLeftOf="@+id/abrSubmit" android:text="x12" android:visibility="gone" /> <Button android:id="@+id/abrSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/textView5" android:text="Submit" /> </RelativeLayout> 

code:

 public class ExampleActivity extends Activity implements OnItemSelectedListener { private Button submitButton; private TextView tv; private Spinner spinner; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_example); submitButton = (Button) findViewById(R.id.abrSubmit); tv = (TextView) findViewById(R.id.abrMult); spinner = (Spinner) findViewById(R.id.spinner1); // create the data array for the spinner String[] strings = { "This", "That", "The Other" }; // create the spinner adapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, strings); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // set the adapter on the spinner spinner.setAdapter(adapter); // set the event listener for the spinner spinner.setOnItemSelectedListener(this); } public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { if (submitButton.getText().equals("Calculate")) { submitButton.setText("Submit"); tv.setVisibility(View.GONE); } else { submitButton.setText("Calculate"); tv.setVisibility(View.VISIBLE); } } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } } 

Hope this helps ...

0
source

All Articles