Reputation: 11
I was trying to navigate to Layout2(which is an ImageView), from layout2(contains 8 image buttons), but still I couldn't connect to the desired layout. I guess there is something lagging in the code, but couldn't fetch the missing condition.
The current layout I'm in is explored_homie.xml
Layout2 Java code:
setContentView(R.layout.explored_homie);
gallery1 = (ImageButton)findViewById(R.id.gallery1);
gallery1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.gallery1:
setContentView(R.layout.gallery_layout);
break;
case R.id.upcoming1:
setContentView(R.layout.rough);
break;
which should call the following layout: gallery_layout.xml
Layout1 just contains an image button on clicking that, will take me to explored_homie layout2 which again contains 8 imagebuttons and I was trying to access the button gallery(id: galler1) from the above layout2(explored_homie), but still I'm not able to navigate to the desired page, can anyone help me to fix this?
Upvotes: 1
Views: 111
Reputation: 236
It's a bad practice to call setcontentview more than once in an activity. The simple approach is to put both layouts in the same xml where one is at visibility gone and when needed switch the visibility. Off-course this is an ugly way, a more elegant way is to use some sort of view switcher or perhaps the Fragments API (create two fragments one for each layout and use FragmentManager to change the views)
Upvotes: 1
Reputation: 901
You should use LayoutInfalter class instead doing it.Then if you want to remove view use removeView() method inside your code.
Upvotes: 0