Search code examples
androiddynamicviewfragmentconditional-statements

Android - change layout with condition


I am making an app, where I have 10+ categories, but they would use kinda the same layout, but with different data shown in it. For easier understanding, I have categ1 and categ2, both of them uses the same fields, but with different data showing. I am new to android and I was thinking of making an activity and view for every category, but since I have about 12 of these, It might be tiring and I am not sure the memory management would be good if the app keeps open every page in it.

I would like to know any idea about which way might work out the best, but still easy for a newcomer? =) What I had in mind, but not sure how to do it is, when I click on a button for a category, it fires an intent with the "extra" string of the name of the category, so a page could load a dynamic view with that layout. Is this possible at all, and if yes, could someone explain it a bit? Including code is not needed! =)

In case It was too blurry what I said: You have X buttons for X categories, you click on a button, and it shows the page of that category. I would like to make this dynamic with only 1 layout, so when I click on the category, it loads the layout for that.


Solution

  • You just use like this...Simple all the best..

     if(condition 1) 
     {
         setContentView(R.layout.layout1);
     } else {
         setContentView(R.layout.layout2);
     }