Android Design Issues on Phone and Tablet - android

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.

Related

Android XML: How to place widget below included layout

I am trying to place a chart widget (com.android.AnyChartView, #+id/piechart) from the AnyChart library (https://github.com/AnyChart/AnyChart-Android) below an included layout (#layout/content_main) that contains a TabHost. I tried giving the include an id and setting the layout_below property of the chart widget, but that has not worked. The app graph fills the entire screen, covering the Tab view.
Below is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"/>
<include layout="#layout/content_main"
/>
<com.anychart.AnyChartView
android:id="#+id/piechart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:background="#ffffff"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<TabHost
android:id="#+id/tab_host"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#ffffff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</FrameLayout>
</LinearLayout>
</TabHost>
</androidx.constraintlayout.widget.ConstraintLayout>
Below is a screenshot of the app:
You need to remove your pie chart from main_activity and put it under content_main like this:
<?xml version="1.0" encoding="utf-8"?>
<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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:padding="5dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<TabHost
android:id="#+id/tab_host"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ffffff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
<com.anychart.AnyChartView
android:id="#+id/piechart"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#+id/tab_host" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
I have added the scrollview but you can remove it if it's not in your requirements.
There are a number of possible solutions
Solution 1
On the include tag indicate android:layout_width="match_parent" and android_height="wrap_content"
<include layout="#layout/content_main"
android:layout_width="match_parent"
android_height="wrap_content"
/>
Solution 2
Using LinearLayout as the parent ViewGroup provide android:layout_weight="1" and android:layout_height="0dp" on the include tag, this will allow the content_main.xml to occupy the remaining space and yet fit on the screen.
<include layout="#layout/content_main"
android:layout_width="match_parent"
android_height="0dp"
android_layout_weight="1"
/>

layout_weight is not working inside ScrollView

I can't figure out , why layout_weight is not working inside the ScrollView , when i am running this xml , i am getting imageview capturing the whole screen and below that i can see my tabs and the viewpager got hidden .
When i am giving the height in dp to all the linear layout below the ScrollView , i am getting my view perfectly ,why it's happening . i want my imageview to take 30% screen and tab/viewpager to take 70% of screen.
<?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:orientation="vertical">
<com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/gift_vouchr_header_img"
android:layout_weight="3"
android:adjustViewBounds="true"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageview_gift"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash"/>
</LinearLayout>
<LinearLayout
android:id="#+id/viewpager_ll"
android:layout_width="match_parent"
android:background="#color/lvb_divider_color"
android:layout_height="0dp"
android:orientation="vertical"
android:tag="sticky"
android:layout_weight="7"
android:adjustViewBounds="true"
android:layout_gravity="center"
>
<android.support.design.widget.TabLayout
android:id="#+id/common_tablayout"
style="#style/VoucherTabLayoutStyle"
android:layout_width="match_parent"
app:tabGravity="fill"
android:layout_height="#dimen/margin_48">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/common_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>
When i remove weightsum and hardcoding the height then the view is working perfectly : below is my code :
<?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:orientation="vertical">
<com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#color/gift_vouchr_header_img"
android:adjustViewBounds="true"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageview_gift"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash"/>
</LinearLayout>
<LinearLayout
android:id="#+id/viewpager_ll"
android:layout_width="match_parent"
android:background="#color/lvb_divider_color"
android:layout_height="500dp"
android:orientation="vertical"
android:tag="sticky"
android:adjustViewBounds="true"
android:layout_gravity="center">
<android.support.design.widget.TabLayout
android:id="#+id/common_tablayout"
style="#style/VoucherTabLayoutStyle"
android:layout_width="match_parent"
app:tabGravity="fill"
android:layout_height="#dimen/margin_48">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/common_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>
MyTab1 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_15"
android:layout_marginRight="#dimen/margin_15"
android:layout_marginTop="#dimen/margin_15"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_amount_footer_txt"
android:text="#string/validity"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/voucher_common_layout"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_title_txt"
android:layout_marginTop="#dimen/margin_26"
android:text="#string/receiver_detail"/>
<EditText
android:id="#+id/receiver_name"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_48"
android:backgroundTint="#color/QuaternaryTextColor"
android:hint="#string/receiver_name"
android:paddingLeft="#dimen/margin_2"
android:layout_marginTop="#dimen/margin_23"/>
<EditText
android:id="#+id/receiver_email"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_48"
android:hint="#string/receiver_email"
android:inputType="textEmailAddress"
android:backgroundTint="#color/QuaternaryTextColor"
style="#style/giftEditetxtStyle"
android:paddingLeft="#dimen/margin_2"
android:layout_marginTop="#dimen/margin_30"/>
<EditText
android:id="#+id/receiver_mobile"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_48"
android:backgroundTint="#color/QuaternaryTextColor"
android:inputType="phone"
android:hint="#string/receiver_mobile"
android:paddingLeft="#dimen/margin_2"
android:layout_marginTop="#dimen/margin_30"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_40"
android:layout_marginBottom="#dimen/margin_30"
android:layout_marginRight="#dimen/margin_12"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="#+id/send_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_term_condition"
android:layout_marginLeft="#dimen/margin_5"
android:text="#string/gift_terms_condition"/>
</LinearLayout>
<Button
android:id="#+id/btn_send_gift"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/gift_submit_btn"
android:layout_marginBottom="#dimen/margin_10"
android:layout_marginTop="#dimen/margin_20"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
MyTab2 xml :
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="#dimen/margin_15"
android:layout_marginLeft="#dimen/margin_15"
android:layout_marginRight="#dimen/margin_15">
<include layout="#layout/voucher_common_layout"/>
<LinearLayout
android:id="#+id/terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_40"
android:layout_marginRight="#dimen/margin_12"
android:orientation="horizontal"
android:gravity="center">
<CheckBox
android:id="#+id/buy_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_term_condition"
android:layout_marginLeft="#dimen/margin_5"
android:text="#string/gift_terms_condition"/>
</LinearLayout>
<Button
android:id="#+id/gift_buy_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/gift_submit_btn"
android:layout_marginTop="#dimen/margin_30"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
None of the above solution worked for me , i found the solution myself like this : hope it will help someone : No changes in the tab code . In the main xml , i have done like 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:orientation="vertical">
<com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/childImg_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gift_vouchr_header_img"
android:adjustViewBounds="true"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageview_gift"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/splash"/>
</LinearLayout>
<LinearLayout
android:id="#+id/viewpager_ll"
android:layout_width="match_parent"
android:background="#color/lvb_divider_color"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="sticky"
android:adjustViewBounds="true"
android:layout_gravity="center">
<android.support.design.widget.TabLayout
android:id="#+id/common_tablayout"
style="#style/VoucherTabLayoutStyle"
android:layout_width="match_parent"
app:tabGravity="fill"
app:tabMode="fixed"
android:layout_height="#dimen/margin_48">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/common_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>
Here comes the important part : Pragmatically set the height of the child layout :D
private void setViewPagerandImageViewHeight(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int heightOfScreen = displayMetrics.heightPixels;
child_img_ll.getLayoutParams().height= (int) (heightOfScreen*.30);// set imageview linearlayout to capture 30% of screen
viewpager_ll.getLayoutParams().height=heightOfScreen;//while scrolling set the viewpager height to maximum ,intially it capture 70% of screen
}

Linear Layout does not fill the whole screen

Why the linear layout does not fill the whole screen?
As you can see I am using match parent in the linear layout.
If I delete the Scrollbar nothing changes. The button is not at the bottom of the screen.
main_activity.xml:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context="com.sarahp.demo.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout ----------> HERE
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="32dp"
android:layout_marginTop="32dp"
android:text="#string/demo"
android:textSize="42dp"/>
<RelativeLayout
android:id="#+id/fragment_container_a"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="#+id/fragment_container_b"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/transparent"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
F A is the fragment part.
Give android:fillViewport="true" to your ScrollView.
Your xml will looks like below. Main_Activity.xml
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Test Demo"
android:textStyle="bold"
android:gravity="center"/>
<RelativeLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#drawable/border_image">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="Test text Fragement1" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="Test text Fragement2" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Remove the relative layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="32dp"
android:layout_marginTop="32dp"
android:text="#string/demo"
android:textSize="42dp"/>
<RelativeLayout
android:id="#+id/fragment_container_a"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="#+id/fragment_container_b"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/transparent"/>
</LinearLayout>
</ScrollView>

android using layout height 0dp in both child and parent

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>

android tabwidgets doesnot getting in device

<?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:background="#808285"
android:orientation="vertical">
<LinearLayout android:id="#+id/homeHeaderLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="34dp"
android:background="#drawable/header_bg">
<ImageView android:id="#+id/searchImageView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#drawable/header_bg_states"
android:layout_gravity="center|center_vertical"
android:layout_weight="25"
android:scaleType="center"
android:src="#drawable/ic_search"/>
<View android:layout_height="match_parent"
android:layout_width="1.2dp"
android:background="#drawable/header_divider"/>
<ImageView android:id="#+id/headerLogo"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/header_bg_states"
android:layout_gravity="center|center_vertical"
android:layout_weight="50"
android:scaleType="center"
android:src="#drawable/madisons_logo"/>
<View android:layout_height="match_parent"
android:layout_width="1.2dp"
android:background="#drawable/header_divider"/>
<ImageView android:id="#+id/shoppingCartImageView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:background="#drawable/header_bg_states"
android:layout_gravity="center|center_vertical"
android:layout_weight="25"
android:scaleType="center"
android:src="#drawable/ic_cart"/>
</LinearLayout>
<TabHost android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TabWidget android:id="#android:id/tabs"
android:layout_height="35dp"
android:layout_width="match_parent"
android:background="#drawable/tabs_bg"
android:gravity="center"
android:layout_gravity="center"
android:layout_marginTop="1dp"
android:orientation="vertical"
android:tabStripEnabled="true"
style="#style/TabText"/>
<View android:layout_height="0.5dp"
android:layout_width="match_parent"
android:background="#FFFFFF"/>
<FrameLayout android:id="#android:id/tabcontent"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#FFFFFF"/>
</LinearLayout>
</TabHost>
Tabs are not coming in my device... but in emulator they are showing & working.
my requirement is is work in Google API2.2 version level_8
please anyone can tell me the solution for this

Categories

Resources