I have ViewPager and grid view in my activity.i want when I scroll anywhere on screen both ViewPager and grid view should scroll at a same time.here is my ss :image.now the only few images are displayed in grid view I have 63 images but shows only 9 in grid..2)ss main
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
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="fill_parent"
android:background="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="#+id/SliderDots"
android:layout_below="#+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="15dp"/>
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="554dp"
android:background="#ffffff"
android:columnWidth="172dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Use this code to resolve your problem
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="#+id/SliderDots"
android:layout_below="#+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="15dp"/>
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:columnWidth="172dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
You must be use CUSTOM viewpager to achieve vertical scrolling of viewpager because default viewpager doesn't support vertical scrolling with other components.
Check my below code.
public class CustomViewPager extends ViewPager {
private View mCurrentView;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mCurrentView == null) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
int height = 0;
mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = mCurrentView.getMeasuredHeight();
if (h > height) height = h;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void measureCurrentView(View currentView) {
mCurrentView = currentView;
requestLayout();
}
public int measureFragment(View view) {
if (view == null)
return 0;
view.measure(0, 0);
return view.getMeasuredHeight();
}
}
Apply this Class instead of default ViewPager into your fragment. THAT'S IT!!
Related
I have the following GridView:
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
app:layout_constraintTop_toTopOf="parent"
android:numColumns="2"
android:gravity="center" />
and the following Item:
<?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:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:id="#+id/planlayout_teams_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Tambora 0 - 0 Balagath"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center"/>
<TextView
android:id="#+id/planlayout_status_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="noch unentschieden"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="#+id/planlayout_menu_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text=".\n.\n."
android:textSize="12sp" />
</LinearLayout>
And everything is working fine except for the last item(s). Whether there is one or two items left, the whole row is not displayed. I googled a lot but nothing could help me.
Edit: I have the same "problem" as here: Custom GridView's Last row items not showing fully but those links don't help me.
I think the problem is about your layout. Your adapter load your all items but your layout not let you see last item because of your top margin.
You can edit your grid layout like this. Can you try this way.
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="120dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:numColumns="2"
android:gravity="center" />
just try this -->
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:columnWidth="172dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"/>
Or try with custom gridview
your gridview code on layout something like this :--
<com.yourpackagename.MyGridView
android:id="#+id/mGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:adjustViewBounds="true"
android:columnWidth="90dp"
android:verticalSpacing="-2dp"
android:horizontalSpacing="-10dp"
android:stretchMode="columnWidth"
android:scrollbars="vertical"
android:fastScrollEnabled="true"
/>
MyGridview activity:-
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.MATCH_PARENT) {
heightSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
}
else {
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
In your mainactivity (activity where you want to declare the gridview)
private MyGridView mGridView;
//also add your adapter of gridview
.
.
oncreate(){
mGridView = getView().findViewById(R.id.mGridView);
//then attach your adapter
adapter = new GridViewAdapter(getContext()...whatever parameters you have in your constructor);
mGridView.setAdapter(adapter);
try this custom gridview...still it doesnt worked let me know
Im having problem where the android scrollview starts hiding a pair of textviews (Top ~5-6 lines doesnt show). I use padding, margin, fillviewport, wrap in linearlayout, original scrollview etc. many variations but it didnt work. I check this problem both api 16 and 23.
Im working many hours but doesnt find solution.
dialog_base.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/base"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/wallpaper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/panelAlt"
android:clickable="false"
android:focusable="false"
android:visibility="gone"/>
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/topInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:layout_below="#id/toolbar"/>
<RelativeLayout
android:id="#+id/innerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/topInfo">
<!--Inner view-->
</RelativeLayout>
<LinearLayout
android:id="#+id/lyBottom"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:layout_below="#+id/innerView"
android:animateLayoutChanges="true">
<android.support.v7.widget.AppCompatButton
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
dialog_checkbox.xml (this view inflate programmatically and adding in id: innerView layout from dialog_base.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="wrap_content">
<include
android:id="#+id/scroll"
layout="#layout/scroll_and_text"/>
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/checkbox"
android:layout_below="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"/>
</RelativeLayout>
scroll_and_text.xml
<?xml version="1.0" encoding="utf-8"?>
<app.CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadeScrollbars="true"
android:scrollbarStyle="outsideInset"
android:scrollbarFadeDuration="3000"
android:fillViewport="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="16dp"/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</app.CustomScrollView>
CustomScrollView.java
public class CustomScrollView extends ScrollView {
private final int maxHeight = Utils.dptoPx(250);
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (maxHeight != -1 && heightSize > maxHeight) {
heightSize = maxHeight;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
getLayoutParams().height = heightSize;
} catch (Exception e) {
Log.print(e);
} finally {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
Thanks in advance!
I am adding a fragment with:getSupportFragmentManager().beginTransaction().replace(R.id.fragment_host, TutorialFragment_.builder().build(), "tutorial").commit();
the FrameLayout is inside a RelativeLayout using:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="0dp"
android:visibility="visible">
<ImageView
android:id="#+id/phone_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="-100dp"
android:adjustViewBounds="true"
android:src="#drawable/phone_tutorial"
android:visibility="invisible" />
<ImageView
android:id="#+id/tuto_image_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginTop="60dp"
android:visibility="invisible"
android:src="#drawable/a_capacity" />
<ImageView
android:id="#+id/tuto_image_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp"
android:src="#drawable/a_loan_simulation"
android:visibility="invisible" />
<ImageView
android:id="#+id/tuto_image_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginTop="100dp"
android:src="#drawable/a_checklist"
android:visibility="invisible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:src="#drawable/add_btn_circle_center"
android:visibility="gone" />
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/mButtons"
android:layout_marginBottom="20dp" />
<LinearLayout
android:id="#+id/mButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_margin="15dp"
android:orientation="vertical"
android:padding="15dp"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.bnpf.androidnative.core.view.FontButton
android:id="#+id/next_tutorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="right"
android:text="#string/Generic_Button_Next"
android:textColor="#color/processStepCategoryTextColor" />
<com.bnpf.androidnative.core.view.FontButton
android:id="#+id/skip_tutorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="left"
android:text="#string/Generic_Button_Cancel"
android:textColor="#color/processStepCategoryTextColor" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
The TutorialFragment object uses this layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/tutorial_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
No matter what I try, this FrameLayout is always aligned on top of the screen, and I need it to be at the bottom of the screen.
Try by change your layout like this:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="0dp"
android:visibility="visible" >
.....
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/mButtons"
android:layout_below="#+id/relLayout"
android:layout_marginBottom="20dp" />
Now view pager will come below 'relLayout' and above 'mButtons'
maybe your RelativeLayout is not full screen,so the FrameLayout will never aligned the bottom of the screen
You can try this,
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_alignParentBottom="true"
android:id="#+id/tutorial_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
you can use like this also:
public class MyViewPager extends ViewPager {
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// find the first child view
View view = getChildAt(0);
if (view != null) {
// measure the first child view with the specified measure spec
view.measure(widthMeasureSpec, heightMeasureSpec);
}
setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
}
private int measureHeight(int measureSpec, View view) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
// set the height from the base view if available
if (view != null) {
result = view.getMeasuredHeight();
}
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
in xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/mButtons"
android:layout_below="#+id/relLayout"
android:background="#0000ff" >
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp" />
</RelativeLayout>
sorry for my bad English ,I want to display 2 Grid views and one text view and image view in android ,Here below my XML code any one help me How to achieve that
I want to display 2 Grid views and one text view and image view in 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="100sp"
android:src="#drawable/ic_launcher"
/>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/image"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:id="#+id/text"
android:layout_below="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Treding"
/>
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/text"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
</ScrollView>
What you should do is the following:
Expandable height gridview:
This means the more you add items to the GridView, the more it will expand. (This is what you need).
Now how to do it is simple:
First you need to create a java class:
public class ExpandableHeightGridView extends GridView {
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// This is a definite hack!!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = View.MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
View.MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
} }
Then in xml layout:
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="100sp"
android:src="#drawable/ic_launcher"
/>
<custom.ExpandableHeightGridView
android:id="#+id/grid1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="5dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<TextView
android:id="#+id/text"
android:layout_below="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Treding"
/>
<custom.ExpandableHeightGridView
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="5dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</RelativeLayout>
</ScrollView>
Then in your activity:
grid = (ExpandableHeightGridView) findViewById(R.id.grid);
grid.setExpanded(true);
grid.setAdapter(your adapter);
**Let me know if you need further assistance **
My screen is divided into two layouts.
Left side(clients_list_layout) is OK, but I have one problem with right side(detail_layout). It consists of two TextViews. I want first TV to wrap it's content and to take not more then 30 percent of parent layout(detail_layout).
For now I have next xml:
<?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:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="100" >
<LinearLayout
android:id="#+id/clients_list_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:weightSum="100" >
.......
</LinearLayout>
<LinearLayout
android:id="#+id/detail_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="vertical"
android:weightSum="100" >
<TextView
android:id="#+id/client_comments"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:padding="5dp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:text=""
android:textSize="20sp"
tools:ignore="NestedWeights" />
<TextView
android:id="#+id/client_debt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="20sp"
android:text="" />
</LinearLayout>
What should I do to make "client_comments" take from 0 to 30 percent of parent layout, not 30 percent everytime.
I have similar issue and finally end up with custom layout, based on LinearLayout. I override onMeasure method for this layout to set it's child views width either to wrap content or width proportional to weight value:
package com.snaprix.androidfrequentlyused.views;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
/**
* Created by vladimirryabchikov on 8/26/14.
*/
public class EvenLayout extends LinearLayout {
private static boolean DEBUG = false;
private static final String TAG = "EvenLayout";
public static void setDebug(boolean debug){
DEBUG = debug;
}
public EvenLayout(Context context) {
super(context);
}
public EvenLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EvenLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
final int width = MeasureSpec.getSize(widthMeasureSpec);
final int height = MeasureSpec.getSize(heightMeasureSpec);
final int count = getChildCount();
float totalWeight = 0;
for (int i = 0; i < count; ++i) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams)
child.getLayoutParams();
totalWeight += lp.weight;
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
}
int accWidth = 0;
for (int i = 0; i < count; ++i) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
if (i < count - 1) {
final LayoutParams lp = (LayoutParams)
child.getLayoutParams();
if (lp.weight > 0) {
int maxChildWidth = (int) (lp.weight / totalWeight * width);
if (maxChildWidth < child.getMeasuredWidth()) {
child.measure(
MeasureSpec.makeMeasureSpec(maxChildWidth, MeasureSpec.EXACTLY),
heightMeasureSpec);
}
}
accWidth += child.getMeasuredWidth();
} else {
int remainingWidth = width - accWidth;
child.measure(
MeasureSpec.makeMeasureSpec(remainingWidth, MeasureSpec.EXACTLY),
heightMeasureSpec);
}
}
}
}
Sample activity with this layout:
<FrameLayout 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">
<com.snaprix.androidfrequentlyused.views.EvenLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Framer Studio 1.6 with Revamped Sketch Support"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="30"
android:singleLine="true"/>
<TextView
android:text="In close collaboration with the Bohemian Coding team, we’re happy to announce much improved Sketch support in Framer Studio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="70"
android:singleLine="true"/>
</com.snaprix.androidfrequentlyused.views.EvenLayout>
</FrameLayout>
try this
<LinearLayout
android:id="#+id/detail_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="vertical"
android:weightSum="100" >
<TextView
android:id="#+id/client_comments"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="70"
android:padding="5dp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:text=""
android:textSize="20sp"
tools:ignore="NestedWeights" />
<TextView
android:id="#+id/client_debt"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="30"
android:padding="2dp"
android:textSize="20sp"
android:text="" />
</LinearLayout>
Try this..
<LinearLayout
android:id="#+id/detail_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="vertical"
android:weightSum="100" >
<TextView
android:id="#+id/client_comments"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:padding="5dp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="vertical"
android:text=""
android:textSize="20sp"
tools:ignore="NestedWeights" />
<TextView
android:id="#+id/client_debt"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="70"
android:padding="2dp"
android:textSize="20sp"
android:text="" />
</LinearLayout>