I've created a custom Linear layout in order to have "square" layout fitting the width of its parent but I'm having trouble positioning it with another LinearLayout.
I want the LinearLayout to be just below the SquareLinearLayout and I got this (both layout are top aligned):
I've tried android:parentAlignBottom="true" and android:gravity="bottom" but nothing works for me ...
Here is my layout xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.dekajoo.rpg.MainActivity" >
<com.dekajoo.rpg.custom_layouts.SquareLinearLayout
android:id="#+id/game_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:background="#f00" />
<android:LinearLayout
android:id="#+id/control_bar"
android:layout_width="match_parent"
android:layout_height="100dip"
android:background="#0f0"
android:gravity="center"
android:orientation="horizontal" />
</RelativeLayout>
And here is my SquareLinearLayout class :
package com.dekajoo.rpg.custom_layouts;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLinearLayout extends LinearLayout {
public SquareLinearLayout(Context context) {
super(context);
}
public SquareLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
/*public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}*/
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
}
Thanks,
Add this to your LinearLayout, android:layout_below="#id/game_layout"
Related
In my app I use a GridView but my values for each cells are not same size, when I put my values in GridView, it automatically set a size for all cells(I think it consider first cell height for all cells), can you tell me how can I fix that?
GridView Adapter:
You should try to use custom view your row Item in Gridview
public class SquareRelativeLayout extends RelativeLayout {
public SquareRelativeLayout(Context context) {
super(context);
}
public SquareRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (widthMeasureSpec < heightMeasureSpec)
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
else
super.onMeasure(heightMeasureSpec, heightMeasureSpec);
}
}
In GridView row XML
<SquareRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
</SquareRelativeLayout>
Hope this helps you.
I suggest please use RecyclerView with StaggeredGridLayoutManager, Throw this you can get layout same which you draw upside,
This is the link which will help you:
https://android--code.blogspot.in/2015/12/android-recyclerview.html
i am trying to add Layout+View pager +list view,Layout+View pager working fine but View pager inside recycler view not scrolling.
it is possible...guide me
-Advance thanks
requirement Screenshot
How Can i set viewpager an recyclear view height dynamically
my xml code ...
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView3"
android:background="#drawable/profile"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabTextColor="#color/your_unselected_text_color"
app:tabSelectedTextColor="#color/your_selected_text_color"
android:layout_below="#+id/imageView3"
android:layout_alignParentStart="true" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/tabs"
android:layout_alignParentStart="true">
</android.support.v4.view.ViewPager>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/android.support.v7.widget.RecyclerView"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
First of all copy this class file in your project
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
public class ExpandableHeightListView extends ListView {
boolean expanded = false;
public ExpandableHeightListView(Context context) {
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
}
public boolean isExpanded() {
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (isExpanded()) {
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
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 use the ExpandableHeightlistview in scrollview like this
<ScrollView ...>
<RelativeLayout ...>
<viewpager></viewpager>
<com.example.ExpandableHeightListView ... />
<other view items />
</RelativeLayout>
Then setadpater of listview
and lastly and very important listview.setExpandable(true)
I dont know to add more listview inside one page layout (attach picture)
I want Each ListView is not scrolling. Page layout is scrolling only.
Help me!!
Build same home layout app: https://play.google.com/store/apps/details?id=com.google.android.youtube
Answer
I had resolve this problem
Step1: Make new file java:
public class ExpandableHeightListview extends ListView {
boolean expanded = false;
public ExpandableHeightListview(Context context)
{
super(context);
}
public ExpandableHeightListview(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 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;
}
}
Step2: Java: Replace all ListView ->ExpandableHeightListview, and set listview.setExpanded(true);
Step3: layout xml: Replace
<yourpackage.ExpandableHeightListview
android:id="#+id/etResponse"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="300dp"
android:scrollbars="none"
android:orientation="vertical"
android:fadingEdge="none">
</yourpackage.ExpandableHeightListview>
Step4: Done!
You can calculate height of listview belong to number item in this. Here is example :
public class ExpandedListView extends ListView {
private android.view.ViewGroup.LayoutParams params;
private int old_count = 0;
public ExpandedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
if (getCount() != old_count) {
old_count = getCount();
params = getLayoutParams();
if (getChildAt(0)!= null){
params.height = getCount() * (old_count > 0 ? (getChildAt(0).getHeight()) : 0) ;
}else
params.height = 0;
setLayoutParams(params);
requestLayout();
}
super.onDraw(canvas);
}
}
And use it in xml like this :
.......
<your package.ExpandedListView
android:id="#+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
<your package.ExpandedListView
android:id="#+id/lv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" />
....
Note This solution will fix the height of listView.
You need to set the height of each ListView. Lets say 30dp also give layout_weight="1" Here is example LinearLayout nested inside ScrollView
<ScrollView
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:orientation="vertical">
<ListView
android:id="#+id/lastest"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp" />
<ListView
android:id="#+id/featured"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp" />
<ListView
android:id="#+id/startup"
android:layout_marginTop="10sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="30dp" />
</LinearLayout>
</ScrollView>
Hope it helps
Please use custom Listview:
public class ExpandableHeightListview extends ListView
{
boolean expanded = false;
public ExpandableHeightListview(Context context)
{
super(context);
}
public ExpandableHeightListview(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightListview(Context context, AttributeSet attrs,int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 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;
}
}
in .xml file:
<yourpackagename.ExpandableHeightListview
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:scrollbars="none"
android:orientation="vertical"
android:fadingEdge="none">
</cyourpackagename.ExpandableHeightListview>
Android, when GridView nested in ScrollView or ListView, it's width will fill_parent even it's child is just one, i want it's width is wrap_content, any sulotion ? thanks.
the listview item xml like below :
<?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" >
<TextView
android:id="#+id/my_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp" >
</TextView>
<com.umeng.example.communitydemo.MGridView
android:id="#+id/my_gv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#aaaaaa"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:padding="10dp"
android:verticalSpacing="5dp" />
</LinearLayout>
the MGridView is :
public class MGridView extends GridView {
public boolean hasScrollBar = true;
/**
* #param context
*/
public MGridView(Context context) {
this(context, null);
}
public MGridView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public MGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = heightMeasureSpec;
if (hasScrollBar) {
expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
two screen shot : http://pan.baidu.com/s/1c06Gfny。
I'm using GridView in my app:
<GridView
android:id="#+id/main_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="4" >
</GridView>
Every cell in this GridView is ImageView:
<?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" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/img"
android:scaleType="centerCrop" />
</RelativeLayout>
All cells in my GridView must be square (height must equals width) and image must fit cell. But it always looks like rectangle... How I can to implements square cells in GridView?
Make sure your cell is square.
For this, you can set the width and height of the RelativeLayout programmatically with the value screen_width/4.
This can do the trick
#Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
As mentioned in the answer here - Gridview with two columns and auto resized images
You can make a custom ImageView for this
public class SquareImageView extends ImageView {
public SquareImageView(Context context) {
super(context);
}
public SquareImageView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
public SquareImageView(Context context, AttributeSet attributeSet, int defStyle) {
super(context, attributeSet, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());
} else {
setMeasuredDimension(getMeasuredHeight(), getMeasuredHeight());
}
}
}
and then use this in your grid view cell layout as
<package_containing_SquareImageView.SquareImageView
android:layout_width="match_parent"
android:layout_height="match_parent" />