Reputation: 5459
I have a simple layout which includes 2 textviews and 2 edittexts, along with a couple spinners. My issue is that, depending on the value of the first spinner, I want to show or hide certain views or change the text on certain labels. My implementation works fine except that whenever setVisible(View.INVISIBLE) is called, the edittext and textview will not actually dissappear until I click on something else on the screen. Once I click on, for example, the edit text which I am not hiding, the views that I want hidden will go ahead and dissappear. Ive tried calling invalidate() on them as I've seen in other posts to refresh a view but it is not working. I've posted the XML layout and relevant java code below. All help is appreciated.
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.greg.android.youcast.FieldDisplayActivity"
tools:showIn="@layout/activity_field_display">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<Spinner
android:id="@+id/run_or_play_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<TextView
android:id="@+id/passer_name_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="@string/passer_name_label"/>
<EditText
android:id="@+id/passer_name_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:hint="@string/passer_name_edit_text"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<TextView
android:id="@+id/receiver_name_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="@string/receiver_name_label"/>
<EditText
android:id="@+id/receiver_name_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:hint="@string/receiver_name_edit_text"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<TextView
android:id="@+id/yards_gained_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="@string/yards_gained"/>
<Spinner
android:id="@+id/yards_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<Button
android:id="@+id/submit_play_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/submit_play_button"/>
</LinearLayout>
</LinearLayout>
and the pertinent java code:
...
runOrPlaySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String choice = (String) parent.getItemAtPosition(position);
if (choice.equals("Run")) {
setRunPlayUIFields();
} else if (choice.equals("Pass")) {
setPassPlayUIFields();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {}
});
....
private void setRunPlayUIFields() {
passerNameLabel.setText("Runner Name: ");
receiverNameField.setVisibility(View.INVISIBLE);
receiverNameLabel.setVisibility(View.INVISIBLE);
receiverNameField.refreshDrawableState();
passerNameField.setHint("Runner Name");
this.receiverNameField.invalidate();
this.receiverNameLabel.invalidate();
}
private void setPassPlayUIFields() {
passerNameLabel.setText(R.string.passer_name_label);
receiverNameLabel.setVisibility(View.VISIBLE);
receiverNameField.setVisibility(View.VISIBLE);
passerNameField.setHint("Passer Name");
this.receiverNameField.invalidate();
this.receiverNameLabel.invalidate();
}
Upvotes: 0
Views: 164
Reputation: 166
In your XML Layout, make it so that you set your
android:id="@+id/receiver_name_text_view"
has the property of
android:visibility="visible"
You can try that, or if you want you can try - in your Java code - to do the following:
receiverNameField.setAlpha(0.0f);
to make it invisible
receiverNameField.setAlpha(1.0f);
to make it visible
Upvotes: 2