Is it possible to have two different layouts for different cases in the same activity or do I have to use intent to call another activity with a different layout
Yes its possible. You can use as many layouts as possible for a single activity but obviously not simultaneously. You can use something like:
if (Case_A)
setContentView(R.layout.layout1);
else if (Case_B)
setContentView(R.layout.layout2);
and so on...
Yes this is also possible with switch case
I already tried this code....
switch (condition) {
case 1:
setContentView(R.layout.layout1);
break;
case 2:
setContentView(R.layout.layout2);
break;
case 3:
setContentView(R.layout.layout3);
break;
default:
setContentView(R.layout.main);
break;
}
I suggest using Fragments
It will be helpful if you can explain more to find other solutions if your not ok with fragments
Edit
Use android support libraries for supporting lower OS versions
Edit2
if you want to use two xml you can combine two xml into one and use it
<include layout="#layout/YOURXMLNAME1" />
<include layout="#layout/YOURXMLNAME2" />
this is also useful while using layout again in many cases
There are a number of ways to go about this. The other answers include at least two approaches - using setContentView depending on the case and using fragments. There is one more I'd like to talk about. Say for instance, you include two layouts
<include
android:id = "#+id/layout1"
layout = .../>
<include
android:id = "#+id/layout2"
layout = ...
android:visibility = "gone"/>
In your java code, you can then hide or show your layouts depending on the use case. For instance, setting the content view to show the layout above shows layout1. When user clicks next button, you can then get a reference to layout1 and set it's visibility to gone and layout2's visibility to visible.
LinearLayout layout1 = findViewById(R.id.layout1);
LinearLayout layout2 = findViewById(R.id.layout2);
buttonNext.setOnClickListener(new View.OnClickListener()
{
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
});
Here is best solution for you ViewFlipper.
ViewFlipper is a Simple ViewAnimator that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval. Here is good example of viewflipper.
You can also look at this.
EDIT: -One StackoverFlow answer for you
Related
I have designed the red highlighted layout like the picture:
What I have used is LinearLayout with orientation. Here an ImageView and Two Textviews are repeated in four times. But as far my knowledge I designed it using LinearLayout. So I have to write every time the same design code for four times.
Is there any better way to design it so that I have to write it one time instead of four times.
My code for the highlighted portion is [here](https://pastebin.com/C9ZHDaZV).
Create one layout called for example weather_layout.xml and place your ImageView and Two Textviews inside, then just use include four times in your final LinearLayout like this:
<LinearLayout
... >
<include
id="+#/top_left"
layout="#layout/weather_layout"
... />
<LinearLayout/>
And then you can access like this:
LinearLayout topLeft = (LinearLayout) mainView.findViewbyId(R.id.top_left)
ImageView v = (ImageView) topLeft.findViewbyId(R.id.imageView)
Use Grid Layout. That way you won't need to handle the spacing and partitioning yourself. One line of of code will suffice.
I have a layout which is mainly a linearlayout with a Button and another layout which is is a linearlayout again with an EditText and two buttons : Cancel and Submit.
What I want is initially the first layout to be displayed. When user clicks the button this layout is being replaced by the 2nd layout(the one with the two buttons).
I have already tried ViewFlipper but the problem is that it takes the height of the one with bigger height. So when first layout is displayed
it has a big white space between the button and the content below it.
If you want to keep using a ViewFlipper you can fix the height issue by adding this to your ViewFlipper in XML:
android:measureAllChildren="false"
Layout1.setVisibility(INVISIBLE)
Layout2.setVisibility(VISIBLE)
Try use only one layout and set VISIBLE or GONE:
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
Or If you Have two layouts, use Intent:
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i)
I was wondering if someone can advise what the best way to do the following: I'm creating an application where I'm asking the user a question and the user either answers the question or proceeds to the next question with fade in and fade out animation using animator instead of anim. As of now, I have one activity and in the activity I would load the layout that I have created in XML and then remove the view and load a new view.
I'm not sure if this is the best way to present multiple views in one Activity and prevent the need to use multiple activities. The reason why I'm doing this is because I have several objects that I'm storing data into based on the category of the question (whether is math, science, social studies, etc).
Here is a picture if it helps visualize:
If there is a better way to do this, please let me know. The only issue I'm having with this is that the activity A java class is growing in code because I have to handle everything there despite having the classes of the objects defined in other java files. Thank you.
I think you can build layout like this
RelativeLayout(see the structure) Set layout1 android:visibility="visible" and layout2 android:visibility="invisible" at first in .xml
layout1 = findViewById(R.id.layout1);
layout1.setVisibility(View.VISIBLE);
layout2 = findViewById(R.id.layout2);
layout2.setVisibility(View.GONE);
Button buttonNext = (Button)findViewById(R.id.buttonNext);
buttonNext.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
layout1.setVisibility(View.GONE);
layout2.setVisibility(View.VISIBLE);
viewTransAnimation();
}
});
And set your animation in viewTransAnimation(), example:
private void viewTransAnimation() {
layout2.setScaleX(0.1f);
layout2.setPivotX(layout2.getX() + layout2.getLeft() );
layout2.animate()
.scaleX(1)
.setDuration(500)
.setInterpolator(new AccelerateInterpolator())
.start();
}
Then it will change to layout2 after you click it. (use some flag make the animation only work once for your requirement.)
You can also replace 2 view with 2 RelativeLayout and do it in the same way.
I have a requirement where there are 2 programatically generated screens and 2 xml layouts. Now i need to on the fly combine, these layouts multiple times.
For ex, i have screen 1 - programatically created, screen 2 - programatically created, screen 3- from a xml layout, screen 4 - from a xml layout
My final layout design should be a single screen with screen1, screen2, screen 3, screen 4, screen 2... with all screens sharing equal screen space based on the number of screen i input. Please let me know the approach. Some screens are having relative layout and some linear ones. So it should combine these.
You'll need to invoke addView() on the primary layout. Once the primary layout is built (which holds all the other layouts), the addView() method will add new views to the existing primary layout.
To add the new layout, you'll need to inflate it first.
LinearLayout primaryLayout;
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
LinearLayout newLayout = (LinearLayout)layoutInflater.inflate(R.layout.your_new_layout, null, false);
primaryLayout.addView(newLayout);
AddView also provides an index option to place the new layout at a specific point in the primary layout.
Try starting with a blank, XML layout (say called primary_layout):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/primaryLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
Then, as your activity starts, set that first, then inflate and add as desired:
setContentView(R.layout.primary_layout);
LinearLayout primaryLayout = (LinearLayout) findViewById(R.id.primaryLayout);
Then you can add your new views to that one. As for adding multiple times, I believe that it's done by reference, so it only sees a single view. Try building the view in a method, and just returning the view. Such as:
private View buildNewView(){
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
LinearLayout newView = (LinearLayout)layoutInflater.inflate( R.layout.my_new_view null, false );
return newView ;
}
And call it via primaryLayout.addView(buildNewView();.
You could look into Fragments. They seem to do exactly what you need. Here are the links to the Training and API Guides on them.In your xml file, you can specify 4 child layouts inside a LinearLayout parent, each with an attribute android:layout_weight="1", so each child layout would only take up the same amount of space. If in portrait orientation, it is suggested to set android:layout_width="match_parent and android:layout_height="0dp" Now, you can label the id's of each child layout as id1, id2, id3, etc, but you can also label the two layouts you will create as something likeandroid:id="#+id/fragment_container_first and android:id="#+id/fragment_container_second.In the Java code, you would set the contentView as the id of the xml file (setContentView(R.layout.myXMLLayout);), create two instances of a Fragment by following the Training guide link I provided above, and add those views to the containers you setup earlier inside your xml files by using something like getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_first, firstFragment).commit(); and getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container_second, secondFragment).commit();(If you are using the support library, which is what the training guides use).I really hope this helps you out. You can build a really flexible UI with Fragments. For instance, later on, you can replace the first two fragments with other fragments at runtime, increasing flexibility. You can even setup different UIs for different screen sizes, with a more compact view on a phone, but with much more to offer on a larger screen like a tablet.I'd love to hear back if this helped you out!
Suppose I have 2 XML files and my activity will setContentView the appropriate one based on some button press from the user. Is it possible to change the transition animation for the changing of content view?
So far I see super.overridePendingTransition() which is suitable for starting new activities, however my example does not start a new activity, it just changes the layout in the current one.
Mathias Lin has explained it very well.
You can always use default stock animations supplied by Android framework.
Heres an example code:
boolean isFirstXml=evaluatingConditionFunction();
LayoutInflater inflator=getLayoutInflater();
View view=inflator.inflate(isFirstXml?R.layout.myfirstxml:R.layout.myseconxml, null, false);
view.startAnimation(AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right));
setContentView(view);
Call this from any of your activity which holds your Parent View.
For custom animations you can visit developer docs. Heres the documentation link.
Yes, you can apply an animation on almost any view you like. Just via view.startAnimation(animation);
Take the outer viewgroup of your respective layout (content view) and apply the animation to it. Depending what kind of animation you want to do, it might make sense to inflate/load both layouts but hide one of them and then swap. Please specify what kind of transition you have in mind.
For example: if you do an alpha transition, you would run the alphaAnimation on the current layout, when when the animation ends (AnimationListener), you set the content view to the new layout, and fade the content back in, via another alphaAnimation.
A better solution is using ViewFlipper: it is a FrameLayout, that can do animations when changing the views.
<ViewFlipper
android:id="#+id/[your_id_here]"
android:inAnimation="..."
android:outAnimation="..."
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
<!--Your first layout -->
</RelativeLayout>
<RelativeLayout
<!--Your second layout -->
</RelativeLayout>
</ViewFlipper>
Then, switch the views with setDisplayedChild(int) or showNext() or showPrevious. If you want to have different animation for left and right movement, you have to set inAnimation and outAnimation in the code before transition.
More complete example is here.