Multiple Activities Single Layout - android

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.

Related

Android Studio Single activity acting as many

I have looked everywhere but don't seem to be able to find what I am looking for. I have an activity with multiple buttons, each button opens a new activity with an identical recyclerview layout, but different data. I am wondering if it is possible to use one activity and layout instead of multiple? this way instead of having 10+ activities (one for each button) I only have to manage one when a button is clicked and simply pass the necessary list data to it.
I believe you could set the intents for each button to call the same activity but with an integer such as 1-10, in the activity it takes the value and decides which list should be presented? If anyone thinks of how this could be done or a simpler way I would greatly appreciate it!
Yes, there are various ways to do that.
You could use multiple fragments on the same activity. Then add/remove fragments on each button click.
You can have multiple layouts within your activity. Say you have two buttons, and you have three layouts layout1, layout2, layout3, sequentially one after the another. So if initially, layout1 is visible and the rest are gone using layout.setVisiblity(View.GONE), if you click button1, ypu can do layout1.setVisiblity(View.GONE); layout3.setVisiblity(View.GONE); layout2.setVisiblity(View.VISIBLE) and vice-versa for pressing button2.
Are all the activities opened by the buttons similar? If so, you could only take care of the changes in the elements of the layout & specify conditions.
For instance, if you click a button, instead of changing the whole thing, you only go into the buttons & change their texts with btn.setText("..."). You could define different conditional statements inside the onClickListener of that button.
It could be something like:
if(btn.getText().equals("a certain text that you set to the button")){
doSomething();
else if(btn.getText().equals("another option")){
doSomethingElse();
Following this logic, you could continually update the elements in your layout & your code will decide what to do depending on what's stored in these elements.
The second option that comes to my mind would be creating different xml layout files & simply changing the layout of your MainActivity to the appropriate one depending on what stage of your process you are at.
I hope this helps,

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.

Two Custom ListViews inside an activity for larger displays

Android 2.3.3
I have an activity where I display "Device's Contacts" in my custom view(imageview, 3 textboxes, checkbox) inside a ListView. What I want to do is, display two listviews with both showing alternate contacts (Splitting the listview into two) side by side. This is because, I want to utilize space on landscape mode of larger displays.
I haven't seen many questions on SO and somewhere I have read that, this approach will mess up scrolling of listviews and will get messy.
So, can someone explain why/how would it get messy and is there another way to use the space in larger displays in my scenario.
Thanks.
This sounds like exactly what the Building a Dynamic UI with Fragments android tutorial was designed to address.
As #ooops mentions you will need to put each ListView within its own container. In the tutorial I linked to, they accomplished this by using 2 Fragment instances, each that loaded different content. Whether you copy their example and use the <fragment> tab (admitidly I don't remember which API level that works on) or use a different container like a regular LinearLayout you should be able to acomplish this easily.
You could do this, but each ListView should be in it's own container.
For examlple How to use multiple listviews in a single activity on android?
But be aware that #Rarw is more correct in the way of good programming practice. Fragments are recommended for such purpose.

Multiple layouts for the Fragment

Can I choose different layout for the Fragment based on the Button that has been clicked on the Activity, or should I create one Fragment class for each Button?
I know that Fragments exist to accommodate different UI but in my case I have many buttons that on click display a FrameLayout and I was thinking if it is possible to save time from copy/paste ?
I'd say that depends on how your layouts/fragments look like. If they are huge and very similar it could be better to have one layout. You could also think about using <merge /> or <include/> in that case.
This way you can maintain equal parts in (sub-)layouts without the need of code repetition.
Have a look at Googles site here.

Loading XML layout

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

Categories

Resources