i try to display a two-pane UI design. I try to add two fragments in a linear layout with a weight. However, the linear layout seems to ignore the weight. How do i correct it?? Thanks
[Link:] http://dl.dropbox.com/u/78582670/twopanes.png
my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8">
</fragment>
</LinearLayout>
Solution:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="8">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
</fragment>
</LinearLayout>
always , when you use weights , set the width/height (of the children) to 0px ,and prefer integers for the weights .
so , the solution is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="2">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="8">
</fragment>
</LinearLayout>
With me is working by putting 20,80 .. try also 0.8,0.2:
<fragment
class="com.usci.view.fragment.CatalogFragment"
android:id="#+id/fragment_catalog"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.8">
</fragment>
<fragment
class="com.usci.education.TestFragment1"
android:id="#+id/fragment_test"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.2">
</fragment>
Set android:layout_width and android:layout_height value match_parent, then for example set android:layout_weight (1 fragment and 2 fragment) value 1
Related
I have this code:
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeContainer"
xmlns:design="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:weightSum="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="#style/Theme.AppCompat"
tools:context="com.tag.instagramdemo.example.MainActivity"
android:background="#drawable/background1">
<ListView
android:id="#+id/lvRelationShip"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_gravity="bottom"
android:background="#ffffff"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:itemIconTint="#color/text"
android:enabled="false"
app:itemTextColor="#color/text"
android:animateLayoutChanges="true"
android:clickable="false"
design:menu = "#menu/menu_main"
android:contextClickable="false"/>
When I use this layout. I can't see the bottom bar and I can only see the listview. At the top there is the list and nothing else.
I tried to resolve your issue.
I hope it will work for you :)
Please have a look and tried
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:design="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_above="#+id/bottomBar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:theme="#style/Theme.AppCompat"
android:weightSum="1">
<ListView
android:id="#+id/lvRelationShip"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:animateLayoutChanges="true"
android:background="#ffffff"
android:clickable="false"
android:contextClickable="false"
android:enabled="false"
design:menu="#menu/navigation" />
</RelativeLayout>
If you have not added a parent layout, do add a parent layout. Enclose the listview inside swiperefreshlayout and enclose the bottombar & swiperefreshlayout inside a parent layout (for eg., relative layout)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:design="http://schemas.android.com/apk/res-auto"
....>
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
...>
<ListView
android:id=... >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_gravity="bottom"
android:background="#ffffff"
android:enabled="false"
app:itemIconTint="#color/colorAccent"
app:itemTextColor="#color/colorPrimary"
android:animateLayoutChanges="true"
android:clickable="false"
android:contextClickable="false"
android:layout_below="#id/swipeContainer"
app:menu = "#menu/menu_main"/>
Also use
app:menu
instead of design:menu as suggested in
https://medium.com/#chornenkyy.v/first-look-at-bottomnavigationview-from-android-design-library-8244de85b953
BottomNavigationView has a method inflateMenu(menu) and if you pass
your menu resource file inside it will work. Right after idea with the
wrong namespace appear in my mind so I tried to replace ‘design’ with
‘app’ namespace which automatically resolves the attributes. And it
actually works. The same story for other three attributes which exist
specifically for this view: itemBackground, itemIconTint,
itemTextColor.
I have been struggling for hours to get this layout to work.
Here is my code:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainPreviewActivity">
<fragment
android:name="com.apps.foo.CleanPreviewFragment"
android:id="#+id/clean_preview_fragment"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
<fragment
android:name="com.apps.foo.pointop.DirtyPreviewFragment"
android:id="#+id/filters_fragment"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
<fragment
android:name="com.apps.foo.pointop.ProcessedPreviewFragment"
android:id="#+id/processed_preview_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
</fragment>
</LinearLayout>
Every fragment is a simple RelativeLayout (all have the same view):
<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"
tools:context="com.apps.<whatever the fragment name is>">
</RelativeLayout>
Now I want make it work like this:
1) No nested layout_weight
2) No Nesting at all (for example nest the 2 first fragments etc
etc)
3) Not using code to do it programmatically after the view has
rendered.
In my opinion the cleanest most readable way of doing this, would be to set the orientation of fragment 1 and fragment 2 to horizontal, and fragment 3 to vertical, but it does not work.
I've also checked this answer,
but the guy, uses a layout_weight with a RelativeLayout. RelativeLayouts do ignore weights this will not work.
Any help will be appreciated?
You can add a dummy view of 0dp width and height in the center of the layout using a RelativeLayout, and position your Fragments accordingly:
<RelativeLayout
... >
<View
android:id="#+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<fragment
android:layout_toLeftOf="#id/center_anchor"
android:layout_above="#id/center_anchor"
... />
<fragment
android:toLeftOf="#id/center_anchor"
android:layout_below="#id/center_anchor"
... />
<fragment
android:toRightOf="#id/center_anchor"
... />
</RelativeLayout>
I am having a layout with four Fragment layouts as shown below.
Now I need to access only two or three Fragments depending upon my selection which is from the database.
How to access only the desired Fragments?
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context="com.example.rknikhil.myapplication.fragment_sample">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<fragment
android:id="#+id/list_Fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.ProfileFragment" >
</fragment>
<fragment
android:id="#+id/list_Fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.Notificationfragment">
</fragment>
<fragment
android:id="#+id/list_Fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.ProfileFragment">
</fragment>
<fragment
android:id="#+id/list_Fragment3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
class="com.example.rknikhil.myapplication.Notificationfragment" >
</fragment>
</LinearLayout>
</FrameLayout>
</ScrollView>
You can simple show and hide the fragment on the basis of the selection.
Use below code in OnCreateView() method . i.e
Fragment f= getFragmentManager().findFragmentById(R.id.list_Fragment);
Fragment f1= getFragmentManager().findFragmentById(R.id.list_Fragment1);
Fragment f2= getFragmentManager().findFragmentById(R.id.list_Fragment2);
f1.getView().setVisibility(View.GONE);
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;
I've been searching for a while and only came up with methods to NOT leave an open space when hiding fragments.
I have a 3-panel application and I would like that the remaining panel stay the same sizes when I hide one of them.
Here is my xml code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/fragment_container"
android:background="#140506">
<fragment
android:name="tets.twopane.FragmentA"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/titles"
android:layout_weight="1"
>
</fragment>
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<fragment
android:name="tets.twopane.FragmentB"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/viewers"
android:layout_weight="1"
>
</fragment>
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
<fragment
android:name="tets.twopane.FragmentC"
android:layout_width="0dp"
android:layout_height="match_parent"
android:id="#+id/pass"
android:layout_weight="1"
>
</fragment>
</LinearLayout>
And this is the function in my main activity who hides the fragment:
public void Hide3() {
final FragmentTransaction ft = this.getFragmentManager()
.beginTransaction();
final Fragment myFrag = this.getFragmentManager().findFragmentById(
R.id.pass);
ft.hide(myFrag);
ft.commit();
}
Any help will be welcomed
I think you could probably wrap each of your fragments with a container such as a linear layout and you can transfer the layout weight to it. So for example wrapping FragmentA this way:
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<fragment
android:name="tets.twopane.FragmentA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/titles"
>
</fragment>
</LinearLayout>