I'm just writing my first android app and in one layout xml file I have four buttons that will display a different layout when pressed. Instead of creating four seperate screens, I've just put the four layouts in the same layout as the buttons with android:visibility="GONE", and then when a button is pressed, I set it's corresponding layout to being visible.
My question is, is there a best practice or suggested method for keeping track of the active layout so that when a button is pressed you can set the active layout back to visibility="GONE" before making the new one visible. I thought I could just set a string value with the ID of the active layout, but then findViewById wont take the string to get a hold of the layout. Any help or suggestions would be great. Thanks!
Why do you use a string to save the ID of the layout? Can you not just use an int since the ID's are such? Or set the visibility of the layouts from the onClickListener you attached to each button?
Related
I've got multiple screens in my applications, which are nearly the same:
Basically there's a heading on the top and a view at the bottom including buttons. If I click on a button, the text of the heading should change. Then, when clicked on determined buttons, there should appear a second header, under the first one, in some cases a third. Each of them includes buttons. When clicking on a button of the second header, the third header should change, when clicking on the third header, only the content below the headers should change.
My target is also to nest (3) different templates, but to define each of them only for once (in xml). Is there a way to do this?
Don't know, if the image works, here's the link to dropbox.
Your main layout should be 3rd template layout.
You just have to hide the buttons or the button layout using attribute setvisibility="gone" in xml layout and in java when you click the button you can set visiblity to visible and you can also do set text to textview to change the header text to whatever you want on click of the buttons.
Hope this helps you
One solution is to declare different xml Views, which could be included by using the 'include' tag in other xml files.
I am developing application in android.What I want is ,my activity should represent two xml layouts files.concept is like,
->when the activity is started it should show one layout(screen)
->when I click on the button exist on the first layout, it should show 2nd layout in the bottom of the screen,keeping first layout visible.
Have both the layout in a single XML. Keep the visibility of the the second layout to secondLayoutObject.setVisibility(View.GONE) initially and then on the click of the button change its visibility to secondLayoutObject.setVisibility(View.Visible).
On method to call two xml files in on activity is by using layoutmanager and assign the screen ratio for both xml files. Use relative layout in both xml. Small code snippet is
RelativeLayout layleft = (RelativeLayout)inf.inflate(R.layout.firstxml,null);
RelativeLayout layright = (RelativeLayout)inf.inflate(R.layout.secondxml,null);
for detail info Layout Reuse help
For this you have to use the concept of visibility. Initially set visibility of second layout as GONE and when you press button set Its visibility True.
you can try use the bellow example:
https://github.com/AdilSoomro/Iphone-Tab-in-Android
this source code to change layout like button click to load another layout!
I have basic knowledge of layouts and so far in my applications I always had one static layout for each Activity. Now Im trying to create a single Activity with a basic framework (Title, some buttons) and a Layout I need to change after clicking a specified button.
Example:
I have a Title bar, a Linear Layout under it and 3 buttons under it. After clicking one of the 3 buttons I change the title text and the button appearance. But I also need to change my Linear Layout between Title bar and buttons. I didnt know how to do this exactly, so I made it Linear Layout in my main.xml, but after clicking one button, I actualy need it to be a Table Layout (or a List View), after clicking another button I need it to be a Relative layout. I hope you get the picture. In all cases I need to add some elements into it (table rows, some views etc.)
My question is, what is the best way to acomplish this? Thanks!
i have to create two layers and switch between them by clicking buttons. for example it should be approximately like this
can anyone tel me where and how to start. i dont have any idea about this.. i want an idea about this
thanks..
You can create two different layout with different id. Then to select the second layout set first layout visibility gone and to select first layout make second layout gone and first layout visible.
youViewLayout.setVisibility(View.GONE);
Which is the best way to change the content inside a layout by pressing buttons?
I want 6 buttons and different content for each push.
I cannot use tab layout because i already used it so..
I would suggest using the gridview that is set up in API Demos. You can import it in as a new project from the Android SDK.
It works effectively like the list of 'All Apps'. However you can change the way it lays out the buttons/icons/text.
In an app I'm working on, I have a list of message types as buttons. Clicking a button changes the display to a layout for composing the selected message. To do this, I have a FrameLayout for the area I want to change. I reference this view as 'compose_content'. When I want to change the content, I run the following code:
compose_content.removeAllViews();
LayoutInflater.from(activity).inflate(R.layout.new_content,compose_content, true);
This will change the FrameLayout content to the content from the specified layout.
One solution can be to have all the 6 views inside your inflated xml and depending on the button pressed set the visibility of that particular view visible and the rest gone