How to Split a Layout into two equal rows? - android

This is the code written in main Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical"
>
</LinearLayout>
I need two linear layouts inside this parent layout such that the parent layout gets split into two halves..

Use this code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
//Other Stuff
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
//Other Stuff
</LinearLayout>
</LinearLayout>

You can use below 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="100dp"
android:weightSum="2"
android:orientation="vertical">
<LinearLayout
android:background="#android:color/black"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--child 1-->
</LinearLayout>
<LinearLayout
android:background="#android:color/holo_blue_dark"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--child 2-->
</LinearLayout>
</LinearLayout>

we have use layout_weight to create these type of layout.
try this 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="100dp"
android:weightSum="1"
android:orientation="vertical">
<!--First Row-->
<LinearLayout
android:background="#android:color/black"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5">
</LinearLayout>
<!--Second Row-->
<LinearLayout
android:background="#android:color/holo_blue_dark"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".5">
</LinearLayout>
</LinearLayout>

Related

LinearLayout inside ScrollView is autoscrolled to bottom

I've got a ScrollView which occupies the bottom half of the screen.
Inside this ScrollView a put a LinearLayout (vertical) with a lot of content.
When I start the activity, the content somewhy automatically scrolls itself down so that it starts at the top of the window. But I need it to start at the top of the ScrollView (i.e. at the center of the window).
What am I doing wrong?
<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"
tools:context="ru.intrigue.activities.EditActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:visibility="visible">
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:background="#color/colorDarkBack">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<!-- content -->
</LinearLayout>
</ScrollView>
</LinearLayout>
you can do like below:
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:orientation="vertical">
<ImageView
android:id="#+id/frag_home_iv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/demo" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:fadeScrollbars="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.26"
android:gravity="center"
android:orientation="horizontal"
android:padding="#dimen/padding_5dp"
android:weightSum="1">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/demo" />
<TextView
android:id="#+id/fragment_home_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:paddingLeft="#dimen/padding_5dp"
android:text="#string/demo"
android:textColor="#color/BlackColor" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Your problem solve by below code :
When open your activity call below code:
your_scroll_view.post(new Runnable() {
#Override
public void run() {
your_scroll_view.fullScroll(View.FOCUS_DOWN);
}
});
Best of luck
You can achieve the desired results by using layout_weight property:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- your content here-->
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- your content here-->
</LinearLayout>
</ScrollView>
</LinearLayout>
Let me know if it helped you.

How to display the listview at the bottom of my activity ? There are 3 linear layouts above the listview

i am dividing my android screen into four vertical parts and i want to display the listview at the bottom of my screen, but whenever i placed it at bottom it wont aligned to the bottom. Even after i stretched it to the bottom it takes full screen. how to do that ?
You can display listview at the bottom by the following desing.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--First Part-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--Second Part-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--Third Part-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--Fourth Part-->
<ListView
android:id="#+id/xListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
this may help you
<?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="wrap_content"
android:weightSum="4"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//first view....
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//second view....
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//third view....
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
//List view goes here....
</LinearLayout>
If the listview which you want at bottom always need to be seen then you should use rlativelayout. It will help you to place it properly at bottom by using this attribute android:layout_alignParentBottom="true" in your listview.
Linear layout with vertical orientation will be more helpful instead of
relative layout
<?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="wrap_content"
android:weightSum="4"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<ListView
android:id="#+id/mListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>

XML Layout: how can we make an xml layout like this Duolingo app

I am a beginner in Android. So, I want to know how can an xml layout be achieved like this in the picture.
here is the code of what you want..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#color/colorAccent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#CDDC39"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#F8BBD0"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:background="#727272"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#212121"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:background="#E91E63"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:background="#795548"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
just copy whole code in your xml file and you will know.. and change by your requirement. now you can add textview, imageview, button and else inside particular layout. this is all about layout-weight attribute.. try to understand my code. and if you dont know about anything than try to write each layout line by line and you will know how its work..

linearlayout height matching other linearLayout

how can i make sure my text behind the checkbox is the same height as the check box and image?
I don't want to use any DIP or anything, as i want it to run on multiple devices. But if there is no other way so be it.
Here is an example of what it is now
see Image here :
XML Code :
<LinearLayout
android:id="#+id/storeParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/storetest"
android:layout_width="177dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dp" />
<LinearLayout
android:id="#+id/storesingleStore"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/storesettings"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
</LinearLayout>
Please note that i add margins programmatically .
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.topMargin=2;
And that this is a fragment.
Use fillviewport= true in scrollview parent layout
<?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="fill_parent"
android:orientation="vertical"
android:fillViewport="true"
android:layout_weight="1">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:id="#+id/storetest"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="10dp" />
<LinearLayout
android:id="#+id/storesingleStore"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/storesettings"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:id="#+id/storetest1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="10dp" />
<LinearLayout
android:id="#+id/storesingleStore2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/storesettings3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

How to set both width and height of layouts as percentages in android?

I need to create a horizontal layout as follows :
I can set the total width of the layout of C using android:layout_width="match_parent" but I'm completely stumped at setting the rest of the layouts. Any help is highly appreciated.
The whole app is set to run on a horizontal layout only, if that helps in decide what method to set the layouts.
<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="fill_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="90"
android:orientation="horizontal"
android:weightSum="100" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#FF0000" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#00FF00" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:background="#FFFF00" >
</LinearLayout>
</LinearLayout>
The trick is to use LinearLayout's with a certain weight like 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:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<View
android:id="#+id/view_A"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:id="#+id/view_B"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<View
android:id="#+id/view_C"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="9" />
</LinearLayout>

Categories

Resources