4

Observing 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" />

On onItemSelected from the dropdown it is being changed like this:

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 is way taller on the second picture. The value of bleedCount.getHeight() is changing from 72 to 95, and I can't understand what is causing it.

Ilia G
  • 10,043
  • 2
  • 40
  • 59
  • I tried to reproduce your problem. Copy/paste the code you posted, run that on a Cupcake emulator and I don't see any problem, the editText box keeps the same height. What version of Android are you using? Are you doing anything else to customize that EditText box ? Are you using any particular theme? – Matthieu Jul 14 '12 at 23:47
  • why don't you use weight for all 3 elements (edit text, text view, button) and put them into a horizontal linear layout having some weightSum? – Yogesh Somani Jul 16 '12 at 08:40

3 Answers3

2

It's connected with android:ems="10"
When EditText changed it's width, after showing up of view with x12, it must have been splited into two lines.

ems has size of one letter for given font.

I think you don't need ems.
Set EditText as single lined: android:singleLine="true"

pawelzieba
  • 16,082
  • 3
  • 46
  • 72
1

The bleedCount EditText resizing is due to your hint text becoming longer than a single line when (pos == 1).

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

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

Maybe you can make it shorter/smaller to prevent the resizing?

Community
  • 1
  • 1
Joe
  • 14,039
  • 2
  • 39
  • 49
0

Okay, since I don't have your entire code base I put something simple together to replicate what you are doing. Just FYI, I am using ICS 4.1. I didn't have any of the problems you are having, so perhaps this is an API issue. Perhaps you can look at my code base and see where there are differences between it and your own. Therein may lie the 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 it helps...

Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37