Loading XML layout - android

I have a problem loading previously created layout. I would like to load it and change text on buttons inside, then show it to the user. It will be quiz question and I have to show it many times during one activity. I don't want to create new class for my layout.
What do I have to use? I read something about Inflate class, but I think it is used only to create new classes. I tried setContentView() method, but app stops when method doing this load starts:
LinearLayout layout = (LinearLayout) findViewById(R.id.CapitalQuestionLayout);
setContentView((View) layout);
Can someone give some hints?

Try using the layout field not the id field, when you call from the R class, like so :
LinearLayout layout = (LinearLayout) findViewById(R.layout.CapitalQuestionLayout);
setContentView((View) layout);

For creating a "Quiz" app your basic requirement is:
a layout which has a TextView for Question and 4 Buttons for options.
a set of questions; pretty obvious :-).
You can create a Custom Class - Questions that will hold Text for a question and its associated options (as Strings).
Now, whenever you want to display a new question with different text for buttons just do the following:
If user clicks on right answer then display a Right-Answer-Activity to the user (that has a next-question-Button).
When user clicks on next-question-button you can display Question-Activity and populate the layout-views with a randomly picked question-object's attributes (i.e. Question's text and options' text).
Hope this helps.

This is quite common in Android apps. Do your fields/buttons have an id? For the parent activity, you mostly do
Button someButton = (Button) findViewById(R.id.the_id_to_the_button_you_want_to_change);
This allows you to do things like
someButton.setText("What is the ultimate answer to life, the universe, and everything?");
Look up the Andorid documentation if you want to do other things like set the color. If you have a viewgroup of some sort (RelativeLayout / LinearLayout / etc), you can specify that specific one
Button someButton = (Button)viewGroup.findViewById(R.id.awesomely_named_button_identifier);
The above someOtherButton is used more often with inflated viewGroups

Related

Duplicate a View Android

I would like to clone a View, graphicaly and functionaly (event). For exemple I have a button on my RelativeView, I get it with findViewById(View) and copy the info inside another button, and finaly put it (the clone) on the relativeView. Like that I could have two buttons and when I click on the copy it triggers the same event. I already did clone = findViewById(View) but the app crash. I don't know if it's possible. I would like to know also if it could be possible to make a generic class. Because the view could be a button but also a Text or An Image. I want to know it's possible to avoid to make three differents methods. But if I have to copy info by info, i will need to make three methods.I'm a begginer in Android ;).
Thanks for you answers.
you are smart Sir, unfortunately that is the way to go though, quick tip when you are implementing Button extends TextView so can always call text functions on a Button like View.setText(""), so forget about it being Button or TextView and go for a boolean attack on ImageView or TextView
hope i am smart too
I would recommend to implement custom views which have the desired properties.
Then you can do this:
LinearLayout myLayout = root.findViewByID(R.id.main);
CustomView myView = new CustomView(this);
myLayout.addView(myView);

How to handle adding/removing views in LinearLayout on Android

I have one of those screens where you fill user info, some other EditTexts etc. and send it to server. But there is section something like add note (just example). User can add as many notes as he want. At the beginning there is just button add note. When user press it then new TextView spawns above and he can fill it, or remove it. Then he can touch add note again and whole process repeats. You sure know what I mean. At the end I take all filed notes and send it to server.
I know that the way to do it is - on btn press inflate my TextView + delete btn layout and add it to linear layout. On btnPress then get its parent or whatever and remove it from layout.
However this is pretty annoying thing to do as it requires inflating, children views managing and all the other stuff. What I am asking is - is there some other way to do that? Maybe some library, or possibility to set adapter to linear layout etc.
Thanks :)

How to reuse Android XML layouts with minor differences?

Let's say i have the Android XML file home_page.xml.
on this home_page.xml i have some variations that i want to show at different activities, and i'd like to reuse the same main layout home_page.xml.
For example, imagine variations on the page such as:
there's 2 more buttons if the user is in state A
there's 1 more editText field if the user is in state B (same activity as state A)
there's a different arrangement of layout on the Z-axis in a frame layout if the user is in state C (same activity as state A)
i know it's possible to programmatically say hide views and set views as visible. but is there a better way to do this via xml or something?
Android recommends using 2 Tags for re-using the layouts across different screens.
Include
When to Use ?
If you already know the layout that you want to re-use, create a new XML file and define the layout. Use tag to re-use it.
Merge
When to Use ?
To eliminate redundant view groups in your view hierarchy when including one layout within another, we can use tag.
Refer to this link - http://developer.android.com/training/improving-layouts/reusing-layouts.html for code sample and more details.
You can hide views but using the Visibility flag.
View v = findViewById(R.Id.my_view);
v.setVisiblity(View.GONE); //etc.
I've tried stuff like this before. I had mixed results. This is fine if you are doing things, like asking the user for a name, then showing an address input or something. But if you find yourself with like 3 or 4 conditions for one editText and then different ones for a button in the same class you might want to just use different layouts. A lot easier to manage.

Multiple Activities Single Layout

I have many activities that do the same thing overall. Is it possible to have them use the same layout? I tried using the code below, but the layout would be shifted way to left to the point where buttons would be off the screen.
setContentView(R.layout.activity_enter_pin);
TextView SecondEP = (TextView) findViewById(R.id.EnterPin);
SecondEP.setText("Enter NEW Pin");
There shouldn't be any problem with that, but if you are using the same layout to do the same thing in different Activity instances, you may want to look into setting up your doodad as a Fragment so it can be reused in both Activitys.
See the Android Fragments Developer Guide for more information.

Android Dynamic Content Design

I want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);

Categories

Resources