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);
}
}
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) {
}
}
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..
And also add
android:visibility="visible"
in your mainlayout xml. then use this
mainlayout.setVisibility(View.GONE);
in your code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With