I have a fragment which fill all of the screen. Now, If i add a new fragment i would like that each fragment fill half of the screen. How can I do this?
I tried this layout and programmatically added fragments. But it doesn't work.
<RelativeLayout 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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="tech.test.org.game.Game$PlaceholderFragment">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="50">
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_below="#+id/relativeLayout"
android:layout_weight="50"
>
</RelativeLayout>
</RelativeLayout>
Thanks.
The answer given by Kat-hat is also correct but it takes to much load, to load the UI which may hamper the performance.
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:visibility="gone" >
</RelativeLayout>
</LinearLayout>
when you are adding second fragment do programatically set layout2 visibility to visible.
findViewById(R.id.relativeLayout2).setVisibility(View.VISIBLE);
Use Linear layout instead of Relative layout and set layout as
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="100" >
<fragment
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</fragment>
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:visibility="gone" >
<fragment
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</fragment>
</LinearLayout>
</LinearLayout>
And when you are adding second fragment do programatically set layout2 visibility to visible and layout1 weight to 50
LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
android.widget.LinearLayout.LayoutParams par = layout1.getLayoutParam();
par.weight =50;
Related
I want to place a view via include on top of a SlidingPaneLayout. In the layout preview of AndroĆd Studio it is displayed correctly but on my device the view won't show - why?
Here is the complete layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/online_state"
android:layout_height="5dp"
android:layout_width="wrap_content"/>
<android.support.v4.widget.SlidingPaneLayout
android:id="#+id/content_view_spl"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/color_background_primary"
android:orientation="vertical" >
<de.timroes.android.listview.EnhancedListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="?attr/color_background_primary"
android:divider="#color/black"
android:dividerHeight="1dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/selected_item"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
Ok, got it...
I have to place the SlidingPaneLayout below the view...
Stupid question... sorry ;)
I was adding a Bottom Bar below the ViewPager (on the bottom of the screen) by using include but it does not work. I have tried all the methods by adding LinearLayout/ RelativeLayout to wrap the include tag, even ViewPager; or adding code like layout_alignParentBottom=true, layout_gravity=bottom.
Is there any method to put it at the bottom, or any problem in my code?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical" >
<include layout="#layout/Bottom_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<android.support.v4.view.ViewPager
android:id="#+id/id_viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
just try like this:
<?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"
>
<include layout="#layout/Bottom_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"/>
<android.support.v4.view.ViewPager
android:id="#+id/id_viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</RelativeLayout>
Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eee"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/id_viewpager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
<include layout="#layout/Bottom_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
LinearLayout draws every child by the order it is declared, so if Bottom_bar is declared before id_viewpager, it will draw above ViewPager. If you switch the order, you'll get what you want.
I want to display MainActivity having webview(webview is already in MainActivity) and a fragment say Activity2. here's is the code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="chronical.com.sayc.HeaderFragment"
android:id="#+id/fragment"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:layout="#layout/activity_header_fragment" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
The problem is that it is not displaying the HeaderActivity. When the app starts, it is displaying only the webview.
your webview height is match_parent
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
please change height of webview
and set the linear layout orientation
android:orientation="vertical"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1" >
<fragment
android:id="#+id/fragment"
android:name="chronical.com.sayc.HeaderFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="0.2"
tools:layout="#layout/activity_header_fragment" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8" />
</LinearLayout>
You can use the **android:layout_weight** property to divide your parent view depending on the weight of its child view like for example 20% of your parent view will be covered by fragment and 80% by the WebView.
I have a RelativeLayout that contains 3 children (LinearLayout) :
one at the top.
one at the bottom.
and one between the 2 last views.
The code i use for it is :
<RelativeLayout 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"
android:background="#color/white"
tools:context="fr.caudoux.onlinegames.activities.GameMainFragment">
<LinearLayout
android:orientation="vertical"
android:background="#ff44f0c3"
android:layout_alignParentTop="true"
android:id="#+id/game_main_actionbar"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#F01234"
android:orientation="vertical"
android:id="#+id/game_main_top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/game_main_actionbar"
android:layout_above="#+id/game_main_chat_container">
</LinearLayout>
<LinearLayout
android:background="#ff2c3ff0"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:id="#id/game_main_chat_container"
android:layout_marginBottom="70dp"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
</RelativeLayout>
Since the api 21 the attribut 'android:layout_above' is not working : the second LinearLayout take all the space below the first one, and the third LinearLayout is over the second.
On api lower than 21 it works : the second part take only the space between the first and the third Linearlayout.
Does anyone known why it is not working and hos to do ?
Thanks
Change the ordering to have your top and bottom view declared before the view that is in between the two:
<RelativeLayout 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"
android:background="#color/white"
tools:context="fr.caudoux.onlinegames.activities.GameMainFragment">
<LinearLayout
android:orientation="vertical"
android:background="#ff44f0c3"
android:layout_alignParentTop="true"
android:id="#+id/game_main_actionbar"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#ff2c3ff0"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:id="#id/game_main_chat_container"
android:layout_marginBottom="70dp"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#F01234"
android:orientation="vertical"
android:id="#+id/game_main_top_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/game_main_actionbar"
android:layout_above="#+id/game_main_chat_container">
</LinearLayout>
</RelativeLayout>
As mentioned, this same layout is easier to do as a LinearLayout, which specializes in stacked elements:
<LinearLayout 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"
android:background="#color/white"
android:orientation="vertical"
tools:context="fr.caudoux.onlinegames.activities.GameMainFragment">
<LinearLayout
android:orientation="vertical"
android:background="#ff44f0c3"
android:id="#+id/game_main_actionbar"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
<LinearLayout
android:background="#F01234"
android:orientation="vertical"
android:id="#+id/game_main_top_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:background="#ff2c3ff0"
android:orientation="vertical"
android:id="#id/game_main_chat_container"
android:layout_marginBottom="70dp"
android:layout_width="match_parent"
android:layout_height="50dp">
</LinearLayout>
</LinearLayout>
Note how only the middle element (the element that needs to fill the remaining space) has a layout_weight on it. This ensures that all remaining space is allocated to that view while the other views are of fixed height.
You cannot have both above and below set in one view. Use below on second view and then below again on last view.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linear1"
android:background="#FF0000"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:background="#00FF00"
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="200dip"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:background="#0000FF"
android:id="#+id/linear3"
android:layout_width="match_parent"
android:layout_height="100dip"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
This is my layout and I expected to see a red background (because linear1 have background red and have properties to fill the parent) , and two others layout with over the linear1 with green and blue bacgroudns
but what I actually see is black bacground from the scrollview and green and blue from linear2 and linear3 but no red backgrund from linear1
namely the linear is acting like android:layout_height="wrap_content" is set not android:layout_height="match_parent"
any ideas ?
You need set fillViewport:
<ScrollView
android:id="#+id/scrollView1"
android:background="#000000"
android:layout_width="match_parent"
android:fillViewport="true" <!-- here -->
android:layout_height="match_parent" >
...
</ScrollView>
information