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.
Related
Check Following Image.
![background activity content showing while opening keyboard or close. first layout goes up it take mili second to open keyboard.
Below Address field white part there is background activity content.]1
Manifest File
android:windowSoftInputMode="stateAlwaysHidden"
Here its my layout top Views. Help me to resolve this issue.
<?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"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:clickable="true">
<include
android:id="#+id/top_bar"
layout="#layout/top_bar_steps_form" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/top_bar">
<FrameLayout
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:visibility="visible">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">
<LinearLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|bottom"
android:orientation="vertical"
android:visibility="visible">
</LinearLayout>
</RelativeLayout>
</ScrollView>
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
i was using Transparent theme Theme.AppCompat.Translucent
i just change theme and it fixed
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>
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;
How to make a webview is -10dp width and height of the parent layout with a background image look like the following image:
Updated:
The blue box is the screen.
The black box is the outer layout(#+id/wrapper) and the red box is the webview(#+id/webview)
The green box is some other layout.
I have tried with the following code but have no success.
If the content in the webview is too long, the outer layout will stretch. I want a fixed size webview inside the layout but I don't know how to achieve this.
Ps. The background image of the black box is not full screen.
Would somebody give me some hints?
<?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:background="#drawable/common_background" >
... some other layout ...
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/wrapper_background" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
</LinearLayout>
... some other layout ...
</RelativeLayout>
use
android:padding="10dip" to the parent of webview.
Use this code instead
it will look like As shown in image
xml Code:
<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="#drawable/ic_launcher" >
... some other layout ...
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="#drawable/ic_launcher" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
</LinearLayout>
... some other layout ...
</RelativeLayout>
Not tested but you can try this:
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:background="#drawable/wrapper_background" >
Try out this:
<?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:background="#drawable/common_background" >
... some other layout ...
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:padding="10dip" <---- Apply this in your layout.
android:background="#drawable/wrapper_background" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
... some other layout ...
</RelativeLayout>
Use this code instead
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/someOtherLayout"
android:layout_centerInParent="true"
android:background="#drawable/wrapper_background" >
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
</LinearLayout>
<Some Other Layout
android:id="#+id/someOtherLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
I have a RelativeLayout with ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="-1sp"
android:paddingLeft="16dp"
android:paddingRight="16dp"/>
<include
android:id="#+id/commentFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/post_message" />
</RelativeLayout>
where the include called post_message is a RelativeLayout with an EditText and a Button.
The problem is the include appears above the list items at the bottom of the list. How can I make it appear below the list?
You have try this i checked it works for you.
& show like As shown in image
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="-1sp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:layout_above="#+id/commentFooter"
android:layout_alignParentTop="true"/>
<include
android:id="#+id/commentFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/article_view"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Have you tried android:layout_below="" ?