Android Views using Templates - android

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.

Related

change xml on layout view android

I'm new to android. I wanna create something like this. two buttons to go "Next" and "Previous" content. middle content should be changed by pressing "Next" & "Previous" buttons. I think I can use fragments for this. but how to just change only middle area of a layout without changing whole layout.
thank you

Android: How to change layout in code

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!

Using a layout inside a layout?

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

Android: Dynamically vary number of buttons in a custom View

I am learning Android by implementing a clone of Mastermind. I want to break up the screen (or View) into three parts: the board with the users guesses so far and feedback, a series of control buttons, and a series of buttons to pick the color of the next peg.
My instinct is to do this is a modular way. The layout files uses nested LinearLayouts (I know not the most efficient thing to do, but this is an educational experience.)
The "board" is a custom View where I do a lot of drawing with a Canvas. The buttons on the bottom are declared in the layout file. Notice the orange strip to the right?
Right now that is another custom View. I want to add a variable number of buttons to that custom View based on the number of colors the player can choose from. A button press would select the color for the next peg in the player's guess. (There are 3 versions of the game, easy, medium, and hard each with a different number of colors.)
So, how do I add a variable number of buttons to the custom View I am creating? Or am I approaching this in the wrong way? Should I use a prebuilt layout? If so, which one and how could I dynamically change the number of buttons in the layout?
Thanks for any help. Cheers!
You can do this in two ways:
Using a predefined layout and setting initially the property
"visibility" of all the buttons to "gone", then programatically you
can set the "visibility" of the buttons you need to "visible". The
"gone" property makes the button invisible and also not consumes
space in the layout.
Adding dinamically buttons to the main layout, first you will have to
create or "inflate" them.
The second options is more powerful, but also more difficult if you are learning.

Hiding and showing android layouts - How do you mark active layout?

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?

Categories

Resources