android using layout height 0dp in both child and parent - android

I want to achieve the following:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.42">
<ListView
android:id="#+id/ist"
android:layout_width="match_parent"
android:layout_height="0dp" <!-- GETTING ERROR : Suspicious size: this will make the view invisible, probably intended for layout_width -->
android:layout_weight="1"
android:background="#color/Bkgd"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#android:color/white" >
<Button android:id="#+id/Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/input_btn"
android:background="#android:color/white" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.58"
android:background="#color/colorPrimary" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:columnWidth="30dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
I have marked in the above code where Android Studio shows error in layout.
Using above layout, I'm getting output as:
What changes should I make to have layout like first picture?

Well had surprising difficulty with the linear layout so if you aren't apposed to relative layout this can do something similar.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="0.42">
<ListView
android:id="#+id/ist"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8"
android:layout_alignParentTop="true"
android:layout_above="#+id/linearLayoutHolder"
android:background="#color/Bkgd"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/linearLayoutHolder"
android:layout_alignParentBottom="true"
android:layout_weight=".2"
android:background="#android:color/white" >
<Button android:id="#+id/Btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/input_btn"
android:background="#android:color/white" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.58"
android:background="#color/colorPrimary" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:columnWidth="30dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
</LinearLayout>

Actually you've forgot to define orientation in Layout with weight 0.42.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.42"
android:orientation="vertical">
But I'm not sure if that will work with older APIs, so using of RelativeLayouts gives more stable result.

Globally, you should avoid use nested weight (recommended by google and show like warning in Android Studio)
You can create your view with only one imbrication like this (Place the button before the linearlayout) :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="0.42">
<Button
android:id="#+id/Btn"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:src="#drawable/input_btn"
android:background="#android:color/white" />
<ListView
android:id="#+id/ist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/Btn"
android:background="#color/Bkgd"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true" />
</RelativeLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.58"
android:background="#color/colorPrimary" >
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:columnWidth="30dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
</LinearLayout>

Related

Items inside scroll view are not showing up on mobile screen

I have a layout file as given below. I have one scroll view inside that one listview and one linear layout. When i install the app in mobile only listview is visible properly but all other layouts are invisible. I have two other textview which are not visible. How to solve this problem?
<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:orientation="vertical"
tools:context="com.example.app.Fragments.LFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/l_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:scrollbars="none"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
<android.support.v7.widget.CardView
android:id="#+id/assmt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Assessment"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
<TextView
android:id="#+id/assmt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="sample"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
</LinearLayout>
The Parent View(ScrollView) have a separate scroll option and ListView have a separate scroll so when you try to use scroll listView that scroll option only working this case parent scroll not working.
Please try to use NestedScrollView.
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html
Example : https://inducesmile.com/android-tips/android-recyclerview-inside-nestedscrollview-example/
Put the ScrollView as a parent like this:
<ScrollView 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"
tools:context="com.example.app.Fragments.LFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/l_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:scrollbars="none"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
<android.support.v7.widget.CardView
android:id="#+id/assmt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Assessment"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
<TextView
android:id="#+id/assmt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="sample"
android:textColor="#color/colorBlack"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
Use android:layout_weight property for views inside LinearLayout.
<?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:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<ListView
android:id="#+id/l_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8"
android:divider="#null"
android:scrollbars="none"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true">
</ListView>
<android.support.v7.widget.CardView
android:id="#+id/assmt"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
android:layout_margin="10dp"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Assessment"
android:textSize="16sp"/>
<TextView
android:id="#+id/assmt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="sample"
android:textSize="16sp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>

Android Layout Weight Exactly Twice Of Others

I would like to simply divide a Linear Layout to 3 piece. I did something as in the code below. If I change the weight of second inner layout anything else than 2, it works. But if I change it to 2(two), the second inner layout doesn't appear in the design ?
<LinearLayout
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
>
<ImageView
android:id="#+id/HomePageIcon"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="33dp"
android:paddingBottom="30dp"
>
</LinearLayout>
Use it like below height set to 0dp when orientation is vertical.
<LinearLayout
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorAccent"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#color/background_dark"
android:gravity="center"
>
<ImageView
android:id="#+id/HomePageIcon"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorPrimaryDark"
android:orientation="vertical"
android:paddingHorizontal="33dp"
android:paddingBottom="30dp"
>
</LinearLayout>
Use the Constraint Layout and forget the Linear and Relative layout.
Pls try this,
<LinearLayout
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:id="#+id/HomePageIcon"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:paddingHorizontal="33dp"
android:paddingBottom="30dp"
>
</LinearLayout>
As you are using layout_weight that means your inner layouts will take the height as much as is available to it. So, in that case there is no need of using layout_height = "wrap_content" just change it to android:layout_height="0dp"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center">
<ImageView
android:id="#+id/HomePageIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="30dp"
android:paddingHorizontal="33dp"></LinearLayout>
</LinearLayout>
When you are using weight than use 0dp to android:layout_width or android:layout_height which you want for affect using weight. And its good to use android:weightSum in parent layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center">
<ImageView
android:id="#+id/HomePageIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="30dp"
android:paddingHorizontal="33dp"></LinearLayout>

Set two button below grid View

Actually i want to set the two button below the grid view and i try Relative Layout and other things but the problem is not solve
XML code:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/widget"
android:layout_height="match_parent">
<include layout="#layout/toolbar"
android:id="#+id/toolbar">
</include>
<GridView
android:id="#+id/gridView1"
android:layout_width="363dp"
android:layout_height="442dp"
android:columnWidth="90dp"
android:gravity="bottom"
android:layout_weight="1"
android:layout_marginTop="70dp"
android:layout_below="#+id/toolbar"
android:horizontalSpacing="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="0dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"></GridView>
</android.support.design.widget.CoordinatorLayout>
try to this hope this can help you..
<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=".MainActivity">
<LinearLayout
android:id="#+id/rlListChariTrust"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ll"
android:orientation="vertical">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<GridView
android:id="#+id/gridView1"
android:layout_width="363dp"
android:layout_height="442dp"
android:layout_below="#+id/toolbar"
android:layout_marginLeft="10dp"
android:layout_marginRight="0dp"
android:layout_marginTop="70dp"
android:layout_weight="1"
android:columnWidth="90dp"
android:gravity="bottom"
android:horizontalSpacing="0dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="bt1" />
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="bt2" />
</LinearLayout></RelativeLayout>
<?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"
android:orientation="vertical">
<!--add ur tool bar here -->
<!--gride view -->
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/linearLayout"
android:background="#color/colorAccent"></GridView>
<!--two buttons in LinLay-->
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<!--button 1-->
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="button 1" />
<!--button 2-->
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Buttodddn 2" />
</LinearLayout>
</RelativeLayout>
Add footer to the Gridview that way It could be managed more efficiently, you can add footer like this.
Create a layout which you want to add to the bottom of gridview and do this
View footerView = (LayoutInflater.from(this).inflate(R.layout.footer_layout, null, false);
GridView.addFooterView(footerView);
This would solve your problem.
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridlayout"
android:layout_above="#+id/linear">
</GridLayout>
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/linear"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_weight="1" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>

Android Design Issues on Phone and Tablet

I am new to Android and was working on designing an app. I had worked for my phone device hoping it would work on tablet (Nexus 7) too. I used view pager and frames to make tabs. But it works fine on phone and not on tablet.
The upper one is for phone and the lower one is for tablet. Please suggest what wrong I did.
Here's my activity_main.xml file:-
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivityNew"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
<com.meroanswer.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:textAlignment="textStart"
android:elevation="2dp"
android:background="#color/primary"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
</LinearLayout>
<!--<android.support.v7.widget.RecyclerView
android:id="#+id/RecyclerView"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#ffffff"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>-->
<ListView
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/windowBackgroundColor"/>
</android.support.v4.widget.DrawerLayout>
Here's tab_subject.xml file:-
<?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">
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar"
android:padding="4dp"
android:clipToPadding="false"
android:columnWidth="#dimen/item_width"
android:numColumns="auto_fit"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"
android:stretchMode="columnWidth"/>
</RelativeLayout>
Here's my grid_items.xml file:-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_height="2dp"
android:layout_width="fill_parent"
android:background="#88cdcdcd" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:baselineAligned="false">
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3">
<ImageView
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:scaleType="centerCrop"
android:layout_marginLeft="10dp"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="match_parent"
android:layout_gravity="left"
android:gravity="center">
<TextView
android:id="#+id/subject_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Physics"
android:textSize="20dp"
android:textColor="#color/black_translucent"/>
<TextView
android:id="#+id/chapters_completed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:text="50 chapters"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_weight="0.3"
android:layout_height="80dp"
android:layout_gravity="right"
android:gravity="center">
<TextView
android:id="#+id/progress_gained"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#color/progressColor"
android:padding="#dimen/spacing_large"
android:layout_gravity="center" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
The activity_main.xml file is the main activity layout, tab_subjects.xml is the layout file for the Model Exam tab currently. And then the grid_items.xml file is later added on the gridview.
Found a solution. It was because I had added fixed width on gridview as suggested by AndroidWeblineindia.

Dividing screen into two equal parts , can not set layout_weight

I guess question is silly, but I am missing something in this and want to understand.
I am dividing the screen into two equal parts. I know this can be done by setting weight. But I do not know why I can not set weight in Linear layout. Instead I am setting weightSum, but what I understood about weightSum is, it is space on screen left after setting width and height I guess.
here is what I am doing
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="#+id/main_panel">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/top"
android:weightSum="100">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/top"
android:weightSum="50"
android:layout_alignParentBottom="true"
android:id="#+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="#drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="#drawable/line" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
PS: I cannot set layout_weight property using android lollipop
Try with below code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="#+id/main_panel">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="#+id/top"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_below="#id/top"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:id="#+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="#drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="#drawable/line" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/color_in_palette"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFFFF"
android:text="Button"
android:layout_weight="50"
android:textSize="0sp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50">
<TextView
android:id="#+id/color_id"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="(204,255,255)"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:numColumns="13"
android:layout_weight="48" >
</GridView>
</LinearLayout>
</LinearLayout>
In this button will occupy the half of the screen , and the other half is occupied by another Linear Layout , which contains a TextView & GridView
Problem:
You cant add a weightSum within a Relative layout that it is only bounded to linearlayout layout params itself.
solution:
add another linearlayout inside your relatie layout as a parent to both of the inner linear layout.
sample:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="#+id/main_panel">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parent"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/top"
android:weightSum="0.5">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/list"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/top"
android:weightSum="0.5"
android:layout_alignParentBottom="true"
android:id="#+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="#drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="#drawable/line" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Weightsum is property used by parent to find the total weight sum by its children.This property is only applicable to LinearLayout. Its childrens should specify its weight with width or height as 0dp. You can find the tutorial here. So your layout.xml will be
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="50"
android:divider="#null"
android:dividerHeight="0dp" />
<FrameLayout
android:id="#+id/data"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:background="#D3D3D3"
android:orientation="vertical" >
<View
android:id="#+id/drag_line"
android:layout_width="50dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal|top"
android:visibility="visible" />
</FrameLayout>
</LinearLayout>
Get Root attribute always be <Relative> and with in that write your remaining XML code.
e.g.
<?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"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/black">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
</LinearLayout>
</LinearLayout>
//Replace above XML code by...
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/black">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/white">
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Categories

Resources