Conditionally switch between layouts in Android - android

How do you switch between layouts in an activity and populate fields based on layout currently used?
For example if there is a logic in a view to load a view:
if(category == 1){
setContentView(R.layout.layout1);
}else{
setContentView(R.layout.layout2);
TextView title = (TextView) findViewById(R.id.titleTV);
title.setText("myTitle");
}
If the else code is called the view never sets the title TextView to the String.
How do I accomplish conditionally going between the two views?

Another alternative would be to use Fragment. The advantage over the choices dimitris mentioned is that each fragment has it's own lifecycle, so you can delegate all code related to each view to its fragment, and keep your main activity clean.
To do this, simply use a FrameLayout in the Activity as a placeholder for the fragments. Fragments can communicate with the activity by using the listener pattern.

In case you want to flip across difference Views you can examine various possibilities, such as:
ViewPager via the compatibility library (http://android-developers.blogspot.com/2011/08/horizontal-view-swiping-with-viewpager.html)
ViewFlipper or ViewSwitcher (http://developer.android.com/reference/android/widget/ViewFlipper.html, http://developer.android.com/reference/android/widget/ViewSwitcher.html)
Or even a simple FrameLayout (http://developer.android.com/reference/android/widget/FrameLayout.html)
Hope this helps for now! In case you need anything specific please shoot it!

BTW have you considered Fragments. They are meant to deal with conditional layouts. Their basic motivation is screen sizes etc. But they can also fit your problem.
Refer to:
Fragments

Related

Which Layout to use with Fragments?

Recently delve into fragments and from what I understand to create a fragment you need a java class and the fragments layout. This makes sense. However what I cant seem to wrap my head around is what "container", or layout do I use to store/insert the fragment? In android studio you can use this to insert fragments, or you can use any other of the layouts. But which one is ideal to use?
Also I saw in a reddit post that I shouldn't be using fragments at all and that its preferred to instead use Frame layouts and play around with your views visibility for the desired effects. Is this true?
You're slightly over-complicating the concept of Fragments.
Fragments, like Activities, don't actually need a dedicated fragments layout xml file. If you choose to do so, you can create the entire layout through Java code, not that I will understand why you'll choose to do so.
So for Fragments, you don't need a Java class and a fragment's layout file. The only requirement is the Java class, and the layouts file is just the preferred approach to inflating a layout, similar to how it is for Activities.
As for your question about the container of the fragments, it's really a matter of your app's design.
You can add a Fragment to your Activity or other Fragments, through the FragmentManager in code or through the <fragment> tag in your layout.xml files.
Neither of those are the best way or the preferred way, since it really depends on what your app needs.
Using the <fragment> tag will cause that fragment to always be added whenever the layout is inflated. This is actually VERY bad if your Activity requires dynamically switching Fragments due to your use of things like ViewPagers, Tabs, Drawer Navigation, or etc. However, it's GREAT if there's no need to dynamically switch fragments and for that specific Activity or parent Fragment, this fragment is a fragment that's always loaded.
For example, let's say you designed a flexible AddNew Fragment that's used in a Dialog and an AddNewActivity. Due to reusing the same screen and code, you decide to make this part of your code a fragment so you can insert it inside a DialogFragment or into another Activity. But, for those DialogFragments and Activities, the only Fragment it'll have is the AddNewFragment, so it'll make sense to just insert that fragment into the Dialog layout and Activity layout through the <fragment> tag.
As for the option with Java code, the preferred approach is to use a FrameLayout. But there's no need to play around with any View visibilities!
The common approach is to just use:
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
A FrameLayout is used because it's going to be the container of the Fragment. In other words, the Fragment will be stored inside of this layout.
So in Java code, you can simply use this code to replace the Fragment inside the container with your new one:
getSupportFragmentManager().beginTransaction().replace(R.id.container, AddNewFragment.newInstance()).commit();
Optionally, you can use add() instead of replace() if you want the fragment to be placed ontop of another fragment within the FrameLayout container.
So yes, to give a decisive answer to your question, there's no ideal way to add a Fragment to an Activity or another Fragment. Each option has it's benefits and drawbacks, with some working better for certain situations and others working better for others.
In the end, it really depends on what your App needs. If you need your Fragments to be flexible, so you can switch Fragments, then this must be done through Java code, because fragments added through the <fragment> tag can't be removed at runtime. However, if you don't need your Fragment to be replaced and it's definitely always going to be showing the same Fragment, then using the <fragment> tag removes the need to write extra Java code to load the dedicated Fragment.
One thing I really do need to point out is... that reddit page you read about is wrong. The 'preferred' way to use Fragments is not to use FrameLayouts and play around with View visibilities. I actually have no idea why there's even a need to change View visibilities.
You can use the new androidx.fragment.app.FragmentContainerView to get a better performance than FrameLayout.
Here more information.
You could use the Fragment layout, but this isn't very flexible. If you're just showing one Fragment, it should work, but using a FrameLayout and inserting your Fragment into it works better as this allows you to change them on the fly.
You may see a FrameLayout with the id R.id.container or something similar, and what this is used for is the Transaction.
For example, if you want to insert FragmentOne into your layout, you can just do this and it'll put it into R.id.container.
Fragment fragment = new FragmentOne();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commit();

Changing the appearance of a fragment

Is it possible to change the appearance of a fragment (set a different View) during its operation or is it possible only in the onCreateView () method?
You can possibly do it this way:
Return some placeholder view (e.g. FrameLayout) in your onCreateView() and keep reference to it.
Then, you can add/remove other views from your placeholder view, when you need to do so.
However, I think it is not how Fragments are supposed to be used. (changing its views rapidly during runtime)
I'll answer my own question.
Not to create chaos in the management of widgets. It is better to use child fragments or show/hide necessary widgets. The answer found here.

How can I add multiple activity to the same screen in android

I want to divide my screen into four parts and add activities in each part. I am not interested in using fragment. Each activity should behave independent of other. Attached photo is showing what I exactly want to do.
In each child activity I want to add VideoView or WebView depending on the selection from menu item.
How can I do it. I didn't find any way to add activity to an activity.
Thanks :)
PS: Activity means Activity not fragment.
i strongly recommend 'using fragments' in your case as ActivityGroup is deprecated in API 13..
The only way you can do it by using fragments. Here is the simple example on how to add multiple fragments on single activity Link
Edit link
You can achieve this design by using four fragments, one for each child view inside a single activity.
The recommended way is to create a single XML layout for your activity, and create four fragments inside that layout.

Do we need an activity class for each page?

I am a newbie in Android. I want to ask that do I need to create an activity class for each page in my app? If not what can I do to bring new pages on screen? Is layout inflater a good choice for this?
Thank you.
Lets say, I have a list view which contains questions. When you click one of them you will see the answer o another page. This means lots of activity class. Then I am looking for another solution.
No, you don't need different activities for this. You can make your "Answer activity" dynamic. It can change data according to which ListView item was clicked. Here's a simple solution to your problem. Create a new int variable and change it accordingly to which ListView item was clicked. If item at index 0 was clicked, then this int should be 0. Pass it with extras to your answer activity and in your answer activity, implement an IF statement.
if (intVariable == 0)
{
//show answer 0
}
else if (intVariable == 1)
{
//show answer 1
}
Do you get the concept?
So, if you want to change the layout accordingly to which ListView item was clicked, this might help you.
Are you changing only image sources or actual layout positions? If you're changing positions, you might aswell create another activity because changing layout positions programatically would take too much time and it's too complicated.
Do you only want image sources to change but layout positions to stay the same? Then this is easier. This code might help you:
int position = //get int extras here
switch (position)
case 0:
imageView1.setImageResources(r.id.yourPicture);
case 1:
imageView1.setImageResources(r.id.yourPic2);
So as you can see, your image resource changed accordingly to ListView item clicked.
Yes, to use activities is the most common way to display content. You can bring new content, that is new activities on the screen by starting new activities via Intent. Therefore, make sure to check out the activity lifecycle
http://developer.android.com/training/basics/activity-lifecycle/index.html.
On the other hand, it sometimes is useful to use fragments to display content on the screen. Fragments are basically "multiple screens" that are hosted by one parent activity. By using fragments you can achieve views like the one in the Skype android app.
Here is more on fragments: http://developer.android.com/training/basics/fragments/index.html
All in all: Use activities to display static content and fragments to make your views more dynamic.
Not necessary, it depends on your design. A typical example is Android Gallery, which is implemented in OpenGL. All pages of Gallery are in one Activity. The advantage is the switching speed is fast, and UI seems much slick. Starting a new Activity may cost much and cause delay. So how to implement it this? You need to create a stack for storing all pages, and manually control(add or remove) them while switching different pages. Anyway, it depends on how you want to design your app(some pages could be displayed in new Activity, some pages not). You have to consider cost and performance.

Android Tab inside fragment containing simple views

I have read some examples, tutorials, question on stackoverflow, but till now I don't think
it's quite clear if what I want to do should be simpler or done anyway!
I will post a draft design for better understanding.
As you can see I just need to have a fragment like the above which will inflated by an Activity and this fragment should contain some custom compound controls and other simple Views.
Now I need as you can see on the left of the draft a Tab (control) which will simply contain Views into each tab area and interact with them through the fragment.
I think its so redundant using different activity for every tab or even different fragment.
I thought of a solution with buttons and visible/hide views.
Any examples, tutorials solving such a problem welcome.
Thank you.
You can just create buttons in place of tabs and you can just change the
setContentView(yourlayout)
accordingly Example
public void onClick(View v) {
switch (v.getId()) {
case R.id.tab1:
setContentView(R.layout.tab1);
// perform other changes if needed
break;
case R.id.tab2:
setContentView(R.layout.tab2);
break;
case R.id.tab3:
setContentView(R.layout.tab3);
break;
}
this will be useful if you have minimum level of functionality to perform
There are several ways of achieving this. You could use ViewPager + ViewPagerIndicator. Or display the content as separate Fragments in a FrameLayout, and replace it with FragmentTransaction's when user selects a different tab. One bonus with the ViewPager implementation is that the user can swipe between the different tabs.

Categories

Resources