Cannot add weight param to fragment in android studio? - android

I am trying to add weight param to fragment xml in android studio so that my two fragements can take half screen each but adding weight have no effect on the layout. what interesting is i still can add weight to normal activity views here is the screen shot
Here is my layout file
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.designcoder.vivz.lifecycle.xml.ActivityFragmentXml">
<fragment
android:id="#+id/fragment_xml_a"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:layout="#layout/fragment_xml_a"
/>
<fragment
android:id="#+id/fragment_xml_b"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlB"
android:layout_width="match_parent"
android:layout_below="#id/fragment_xml_a"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_xml_b"
/>
</RelativeLayout>

remember that: weight param can only work in linearlayout. if you want to let the two fragments share the screen in horizontal, make the orientation of linearlayout horizontal and make the width of each fragment "0dp" or "wrap_content" so that you can weight them. The same way in vertical. You can look my codes below for example.
the weight example
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="com.example.dysaniazzz.testdemo.MainActivity">
//the background only to distinguish the fragment
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_red_light" />
<fragment
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_green_light" />
</LinearLayout>

You try using code below:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="cungxunu.cunghoangdao.cheng.myapplication.MainActivity">
<fragment
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<fragment
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</LinearLayout>

You should not use Relative layout as weight would not work there.
So User Linear layout for that.
<?xml version="1.0" encoding="utf-8"?>
<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:weightSum="2"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="io.designcoder.vivz.lifecycle.xml.ActivityFragmentXml">
<fragment
android:id="#+id/fragment_xml_a"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlA"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
tools:layout="#layout/fragment_xml_a"
/>
<fragment
android:id="#+id/fragment_xml_b"
class="io.designcoder.vivz.lifecycle.xml.FragmentXmlB"
android:layout_width="match_parent"
android:layout_below="#id/fragment_xml_a"
android:layout_height="0dp"
tools:layout="#layout/fragment_xml_b"
/>
</LinearLayout>

Related

How to set my Child Layout also starts from parent's position?

I try to set my LinearLayout starts from screen edge(like Parent layout).I dont want the White Space between two layouts.
i need to start from left side from screen edge and top also edge . or follow the parent left and top properties.
NOTE:
suppose i dont want to change the Relative layout properties only changes in linear layout means how to fix it?
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.zd.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/preview"
android:focusable="true">
<requestFocus />
</LinearLayout>
</RelativeLayout>
DO LIKE BELOW.
<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.example.zd.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/preview"
android:focusable="true">
<requestFocus />
</LinearLayout>
</RelativeLayout>
remove these lines.. these are creating padding for the child.
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
But according your question.
<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/gray"
android:clipToPadding="false" // this will do the trick here
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
// suppose you have 16dp padding
<LinearLayout
android:layout_marginTop="-16dp"
android:layout_marginBottom="-16dp"
android:layout_marginLeft="-16dp"
android:layout_marginRight="-16dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/preview"
android:background="#color/green"
android:focusable="true">
<requestFocus />
</LinearLayout>
</RelativeLayout>

how to have 2 relative layouts divided by half in activity?

i want to divide my screen in half vertically, and do a different color in each half,
i was trying to use this solution mentioned in here>
Android: 2 relative layout divided in half screen
but it doesn't work for me.
this is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".activities.alignmentActivities.Add_New_Project_Activity"
tools:showIn="#layout/app_bar_add__new__project_">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#f00000"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:background="#00b0f0"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
how can i achieve the desired effect of 2 layouts each takes half the screen vertical \ what am i doing wrong then the example mentioned in the link ?
You may Try to use this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="horizontal"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#f00000"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:background="#00b0f0"
android:layout_height="match_parent"
android:layout_weight="1">
</RelativeLayout>
Output :
Add the following attribute in your LinearLayout
android:weightSum="2"
After adding this your code will work.
WeightSum attribute defines the total number of equal divisions you want it to be divided.If you want it to be divided into 3 parts, change its value as 3.
<?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:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/red">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/white">
</RelativeLayout>
</LinearLayout>
**Preview Image Link**
[1]: http://i.stack.imgur.com/6JKTP.png
If you want to divide screen vertically than you need to use android:orientation="vertical" and in child layout use android:layout_width="match_parent"
check below xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:weightSum="2"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f00000" >
</RelativeLayout >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00b0f0" >
</RelativeLayout >
</LinearLayout >

I want the ViewPager to be in full screen - Android Studio

I have some issues with the ViewPager I am using. I want it to be in full screen, but for some reason there are some edges that won't disappear. How can I solve this?
Code from the XML-file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Main.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewPager"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can try this:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Try to remove paddings from all four sides. By default when you create the activity using AS, AS will add padding to your main view. Below is the layout without external padding.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Main.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewPager"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can try as like following way
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Main.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
remove all of android:padding from RelativeLayout
and remove android:layout_centerVertical and android:layout_centerHorizontal
from ViewPager

RelativeLayout: how to delete border around second layout?

I have an xml file which contains two RelativeLayouts. The trouble is that the second Relativelayout does not go up to the display limit. It looks like a border around the layout.
How can I display the second layer also on the border of the display?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:background="#color/colorAccent"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="newservice"
tools:showIn="#layout/activity_newservice">
<RelativeLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
android:layout_alignBottom="#+id/viewmap"
android:layout_centerHorizontal="true">
</RelativeLayout>
</RelativeLayout>
Remove this line on the top layout:
android:paddingTop="#dimen/activity_vertical_margin"
You are putting a padding on the top, so the second layout will not fill the first like you want.
Regards!
Remove your
android:paddingXXX from parent RelatvieLayout
that is making issue
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="newservice"
tools:showIn="#layout/activity_newservice">
<RelativeLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryDark"
android:layout_alignBottom="#+id/viewmap"
android:layout_centerHorizontal="true">
</RelativeLayout>
</RelativeLayout>
Try this

Android application with a webview in Main Activity and Fragment[activity2: HeaderActivity]

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.

Categories

Resources