Hide and show layout based on conditions in Android
Asked Answered
L

5

6

I am new to Android. I want to hide and show Linearlayout based on if else conditions. In my application I have taken 1 spinner. Based on selected spinner values I want my next layout hide or visible, but if it is once gone it not comes visible again.

My code is:

    public class Expense extends Activity{
    Spinner datype;

    public void onCreate(Bundle b){
    super.onCreate(b);
    setContentView(R.layout.expense);
    mainlayout=(LinearLayout)this.findViewById(R.id.layout1);
    datype=(Spinner)findViewById(R.id.da_type);
    List<String>data1=new ArrayList<String>();
    data1.add("Local");
    data1.add("Ex-Station Double Side");
    data1.add("Ex-Station Single Side");
    data1.add("Out-Station Double Side");
    data1.add("Out-Station Single Side");
    ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,data1);
    adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
    datype.setAdapter(adapter);
    if(datype.getSelectedItem().toString().equals("Local")){

        mainlayout.setVisibility(LinearLayout.GONE);
    }
    else 
        mainlayout.setVisibility(LinearLayout.VISIBLE);


}

}
Lakesha answered 31/8, 2013 at 6:25 Comment(1)
it is not working.....in my spinner first value is set by-default to "local". so layout is not visible. but when i select some other values from spinner. the layout is not comes in view... that is the problem...Lakesha
R
9

You need to set OnItemSelectedListener and change your layout there too. Here is an example where your activity implements OnItemSelectedListener.

public class Expense extends Activity implements OnItemSelectedListener {

private Spinner datype;
private LinearLayout mainlayout;

public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.expense);
    mainlayout=(LinearLayout)this.findViewById(R.id.layout1);
    datype=(Spinner)findViewById(R.id.da_type);
    List<String>data1=new ArrayList<String>();
    data1.add("Local");
    data1.add("Ex-Station Double Side");
    data1.add("Ex-Station Single Side");
    data1.add("Out-Station Double Side");
    data1.add("Out-Station Single Side");
    ArrayAdapter<String>adapter=new ArrayAdapter<String (this,android.R.layout.simple_spinner_item,data1);
    adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
    datype.setAdapter(adapter);
    if(datype.getSelectedItem().toString().equals("Local")){
        mainlayout.setVisibility(LinearLayout.GONE);
    }
    else {
        mainlayout.setVisibility(LinearLayout.VISIBLE);
    } 

    // here we set the listener
    datatype.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    // here we change layout visibility again
    if(datype.getSelectedItem().toString().equals("Local")){
        mainlayout.setVisibility(LinearLayout.GONE);
    }
    else {
        mainlayout.setVisibility(LinearLayout.VISIBLE);
    } 
}

public void onNothingSelected(AdapterView<?> parent) {
}
}
Rob answered 31/8, 2013 at 6:45 Comment(1)
If you put a breakpoint into onItemSelected() and check it. What is the value of datype.getSelectedItem()?Rob
U
8

you can use this

instead

mainlayout.setVisibility(LinearLayout.GONE);

of

mainlayout.setVisibility(View.GONE);

you have to hide and show your Code after click event of Spinner just this OnItemSelectedListener method..

Uredium answered 31/8, 2013 at 6:40 Comment(1)
it is not working.....in my spinner first value is set by-default to "local". so layout is not visible. but when i select some other values from spinner. the layout is not comes in view... that is the problemLakesha
R
3

And also add

android:visibility="visible"

in your mainlayout xml. then use this

mainlayout.setVisibility(View.GONE);

in your code

Rochelrochell answered 12/11, 2015 at 6:10 Comment(0)
E
2

Instead of linear layout.gone try View.GONE

Excipient answered 31/8, 2013 at 6:37 Comment(0)
C
0

rather than hiding the layout you can hide the entire view as below

  public class Expense extends Activity implements OnItemSelectedListener {

private Spinner datype;
private LinearLayout mainlayout;

public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.expense);
    mainlayout=(LinearLayout)this.findViewById(R.id.layout1);
    datype=(Spinner)findViewById(R.id.da_type);
    List<String>data1=new ArrayList<String>();
    data1.add("Local");
    data1.add("Ex-Station Double Side");
    data1.add("Ex-Station Single Side");
    data1.add("Out-Station Double Side");
    data1.add("Out-Station Single Side");
    ArrayAdapter<String>adapter=new ArrayAdapter<String (this,android.R.layout.simple_spinner_item,data1);
    adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
    datype.setAdapter(adapter);
    if(datype.getSelectedItem().toString().equals("Local")){
        mainlayout.setVisibility(View.GONE);
    }
    else {
        mainlayout.setVisibility(View.VISIBLE);
    } 

    // here we set the listener
    datatype.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    // here we change layout visibility again
    if(datype.getSelectedItem().toString().equals("Local")){
        mainlayout.setVisibility(LinearLayout.GONE);
    }
    else {
        mainlayout.setVisibility(LinearLayout.VISIBLE);
    } 
}

public void onNothingSelected(AdapterView<?> parent) {
}
}
Chavaree answered 2/6, 2019 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.