I have two layouts, what is the best way to switch between the two layouts when a user clicks on a button?
You could call setContentView(R.layout.layout2) on ButtonClick
The best way is to use android.widget.ViewFlipper. With it you can create different layout from xml and then switch among them with simple method like this:
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.myViewFlipper);
// you can switch between next and previous layout and display it
viewFlipper.showNext();
viewFlipper.showPrevious();
// or you can switch selecting the layout that you want to display
viewFlipper.setDisplayedChild(1);
viewFlipper.setDisplayedChild(viewFlipper.indexOfChild(findViewById(R.id.secondLayout)
Xml example with tree layouts:
<ViewFlipper
android:id="#+id/myViewFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
<LinearLayout
android:id="#+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
<LinearLayout
android:id="#+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
[...]
</LinearLayout>
</ViewFlipper>
Use ViewSwitcher.
make one layout file that includes two layouts. your two layouts should be place in viewswitcher.
associate an onclick listener that switch two layout with a button.
if you separate two layouts in different file, you can use tag in layout xml file.
Use "fragment manager" after creating fragments and putting your layouts into it on run time or "view pager" as it can also add swapping effect. Do not use setContentView(R.layout.your_layout) without clearing the previous layout (use "gone" or "clear") for changing layout on run time as it slows down your app (because now there are two layout running) and even creates confusion for the app.
Related
I am a beginner at Android Studio.
What I am trying here is to make two fragments and the second fragment has a button.
I was wondering why this XML code below is not working..
I got this notification..
Rendering Problems: A <fragment> tag allows a layout file to dynamically include different layouts at runtime. At layout editing time the specific layout to be used is not known. You can choose which layout you would like previewed while editing the layout.
My questions..
Is it okay to add Button element inside Fragment?
Can I add Frame inside Fragment as well?
How can I solve this problem?
Thank you for your help!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--first fragment(left screen) -->
<fragment android:name="com.example.android.fragments.ArticleListFragment"
android:id="#+id/headlines_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >
</fragment>
<!--second fragment(right screen) -->
<fragment android:name="com.example.android.fragments.ArticleFragment"
android:id="#+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent">
<Button
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/article_new_fragment"
android:text="OK!" />
</fragment>
</LinearLayout>
You can add any view inside a fragment, just like you would do in an activity.
For what I can tell, that button is inside the activity layout.
So:
Go To com.example.android.fragments.ArticleFragment
Go to its method onCreateView (ctrl+F to look for it)
It should have a return statement, something like
inflater.inflate(R.layout.fragment_layout, container, false);
At this point, you should understand that this method returns the Fragment's layout.
Go to the layout specified in that line (in this case, it would be
R.layout.fragment_layout).
Put in that layout the button, run the app and you should see it.
Answering your questions:
Is it okay to add Button element inside Fragment?
Yes but in its own layout file. You define where is the layout file in you java fragment class.
Can I add Frame inside Fragment as well?
If you mean frameLayout, yes but also in the layout file of the fragment.
I have a fragment defined by the following xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_example"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_example"/>
</RelativeLayout>
Now I would like to reuse the ImageButton (and other buttons I will add) in another fragment, ideally
without copying its definition
by keeping the definition in xml and not adding it programmatically
I.e. the goal is to overlay the same set of buttons in different fragments.
Is there a way to define all buttons in a separate xml file and load them programmatically on fragment creation?
Yes, You can.
Define all buttons in different xml say layout_buttons.xml
and add them in each fragment layout using
<include layout="#layout/layout_buttons" />
I want to display two different activities in a single screen how can i do that in android?Please if anybody has idea share it.And I don't wanna use fragments.
I want to display a screen which contains some fields and below(at the bottom of the screen) I want another screen with some buttons.
Is this possible in android?
If so, How can i do this ?
You can't have two activities in one screen. You can have only one. So, ultimate solution is Fragments.
An activity is not directly a visual component, so I'm thinking that what you're really asking is how to have a single activity display different views.
There's nothing that says you can't rerun setContentView() with a different layout/view ID. But there's another non-fragments way of doing what your probably want.
You can define more than one full-size (match_parent) view in a layout. What you want to do is set the visibility for one of them to "visible" with android:visibility="visible" and all the others to "gone" with android:visibility="gone".
Then when you want to switch the displayed view, you'll run setVisibility(View.GONE) on the outgoing view and setVisibility(View.VISIBLE) on the incoming. It's important to use GONE and not INVISIBLE or the layouts won't render correctly.
Sample layout file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<SurfaceView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</FrameLayout>
Sample Code to switch view:
video.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
web.setVisibility(View.GONE);
That said, you probably want to learn how to use fragments since you can handle switching the view along with other state in a single unit of work (a transaction). But the above approach above does work for simple view changes.
I have a main layout being hosted by an activity (setContentView method).
I have a button in the layout. I want to be able to click this button and a new layout will slide down from the left/top until the middle of the screen. So, the screen now will have two layouts where one is on top and another one is just beneath it.
This is something like the UI in Android Jelly Bean where you can pull the settings layout down using a touch gesture.
What are the possible implementations?
A method I have tried:
add the slide down layout into main.xml but set its visibility to gone
when button is clicked, run some code that will set the layout visibility to
VISIBLE and add some animations.
the result of this implementation is that this layout will push the rest of the
views down in order to have a "squeeze" space for itself which is not
what I intended to do (see above)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<LinearLayout
android:id="#+id/PR_slidedown" <---this is the intended slidedown layout
android:layout_width="8dp"
android:layout_height="40dp"
android:visibility="gone"
android:orientation="vertical" >
</LinearLayout>
....
Your implementation is partially correct, the other part is to host your two layouts in a FrameLayout so they can overlap with each other. Search for FrameLayout in here, there are a lot of example on how to use it.
Or try the SlidingDrawer
Quick question: at runtime I do a boolean check, if it returns true I would like to have two buttons in a relative layout on my MainActivity class. If its false I want to instead have two other widgets where those buttons would be (or near enough). How do I do that?
you could also implement a ViewSwitcher where a more complicated set of buttons/widgets can be switched out very easily with a single call to
ViewSwitcher mViewSwitcher = (ViewSwitcher) findViewById(R.id.viewswitcher);
if (some_logic == true) {
mViewSwitcher.showNext();
}
Set up your XML like this and the above will switch between the two LinearLayouts:
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
--- buttons, Views, whatever---
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
--- buttons, Views, whatever---
</LinearLayout>
If you have just those two alternatives put them both in your layout and hide / show the one you want. View#setVisibility()
If you want it more dynamic you can add and remove widgets programmatically. ViewGroup#addView()
Modifying a RelativeLayout during runtime is quite complicated since you need to set all those layout parameters so you could add a simple layout like a FrameLayout in the place where the buttons should go and put them inside the frame. Has the advantage that you can setup all the relative layout parameters for the frame in xml.