Reputation: 636
I've created a layout. All done programmatically. Now I have a button that's set to change the VISIBILITY of a view when clicked. Frankly, I don't know how to reference the view.
Here's part of the code:
//my button to show pop-up
Button btn_showPop = new Button(this);
btn_showPop.setText("Pop-up");
btn_showPop.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btn_showPop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
hsv.post(new Runnable() {
@Override
public void run() {
if (Menu_Displayed) {
//pop-up
li_pop.setVisibility(View.VISIBLE); //how do I reference li_pop
} else {
//do nothing
}
}
});
}
});
// pop-up:
final LinearLayout li_pop = new LinearLayout(this);
li_pop.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
li_pop.setOrientation(1);// 1 is vertical
li_pop.setBackgroundColor(Color.LTGRAY);
li_pop.setVisibility(View.GONE);
li_pop.setClickable(true);
Upvotes: 2
Views: 822
Reputation: 43728
Move the line
final LinearLayout li_pop = new LinearLayout(this);
before the reference.
Upvotes: 0
Reputation: 132972
Change your code as for getting reference to dynamically created View
first decalre li_pop
at class level
LinearLayout li_pop;
second set id for li_pop
at time of creation as :
li_pop = new LinearLayout(this);
li_pop.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
li_pop.setId(599980); // set LinearLayout id here
now you can reference li_pop
LinearLayout as on button click :
@Override
public void onClick(View v) {
hsv.post(new Runnable() {
@Override
public void run() {
if (Menu_Displayed) {
//pop-up
li_pop.setVisibility(View.VISIBLE);
//OR
LinearLayout li_popnew=(LinearLayout)findViewById(599980);
li_popnew.setVisibility(View.VISIBLE);
} else {
//do nothing
}
}
});
Upvotes: 1