Gridview elements not getting centered in relativeLayout - android

I am using a gridview in my application in which I want to make my view centered if there only one element in my adapter and maximum of three elements in one row.But for some reason one element is not being centered.
Please help me in this.
this is my parent layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
>
<RelativeLayout
android:id="#+id/cplist_title_layout"
android:layout_width="wrap_content"
android:layout_height="74dp"
android:gravity="center_vertical" >
<TextView
android:id="#+id/cplist_title"
android:layout_width="match_parent"
android:layout_height="74dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:gravity="left|center_vertical"
android:textColor="#color/RGB_0_176_241"
android:textSize="19dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/cplist_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cplist_title_layout"
android:paddingTop="8dp" >
<GridView
android:id="#+id/cp_gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="21dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="8dp"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
>
</GridView>
</RelativeLayout>
</RelativeLayout>
this is my grid view item layout
<RelativeLayout
android:id="#+id/detail_cplist_item_logo"
android:layout_width="match_parent"
android:layout_height="108dp"
android:gravity="center_vertical"
android:layout_marginLeft="10dp" >
<ImageView
android:id="#+id/detail_cplist_item_cplogo_bg"
android:layout_width="225dp"
android:layout_height="104dp"
android:layout_centerVertical="true"
android:background="#color/RGB_23_27_33"
android:gravity="center" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/detail_cplist_item_details"
android:layout_width="match_parent"
android:layout_height="59dp"
android:layout_below="#id/detail_cplist_item_logo"
android:gravity="center_vertical"
android:layout_marginLeft="10dp" >
<LinearLayout
android:id="#+id/detail_cplist_item_price"
android:layout_width="match_parent"
android:layout_height="31dp"
>
<TextView
android:id="#+id/detail_cp_price_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginTop="4dp"
android:gravity="left"
android:text="Free"
android:textColor="#color/RGB_100_217_217_217"
android:textSize="18dp" />
</LinearLayout>
<RelativeLayout
android:id="#+id/detail_cplist_item_quality_section"
android:layout_width="37dp"
android:layout_height="19dp"
android:layout_below="#id/detail_cplist_item_price"
android:layout_centerVertical="true"
android:layout_marginBottom="9dp"
>
<ImageView
android:id="#+id/detail_cplist_item_quality"
android:layout_width="37dp"
android:layout_height="19dp"
android:gravity="center"
android:src="#drawable/common_selector_icon_hd" />
</RelativeLayout>
<ImageView
android:id="#+id/detail_cplist_item_tag_tv"
android:layout_width="21dp"
android:layout_height="wrap_content"
android:layout_below="#id/detail_cplist_item_price"
android:layout_centerVertical="true"
android:layout_marginLeft="9dp"
android:layout_toRightOf="#id/detail_cplist_item_quality_section"
android:src="#drawable/tag_tv"
android:visibility="invisible" />
<ImageView
android:id="#+id/detail_cplist_item_tag_mobile"
android:layout_width="21dp"
android:layout_height="wrap_content"
android:layout_below="#id/detail_cplist_item_price"
android:layout_centerVertical="true"
android:layout_marginLeft="3dp"
android:layout_toRightOf="#id/detail_cplist_item_tag_tv"
android:src="#drawable/tag_mobile" />
<ImageView
android:id="#+id/detail_cplist_item_live_icon"
android:layout_width="37dp"
android:layout_height="19dp"
android:layout_below="#id/detail_cplist_item_price"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/detail_cplist_item_quality_section"
android:src="#drawable/tag_live"
android:visibility="invisible" />
</RelativeLayout>

Instead of gravity, set this:
android:layout_centerInParent="true"

Had the same problem... I solved it with overridden onMeasure()
public class GridViewEx extends GridView {
private int mRequestedNumColumns = 0;
public GridViewEx(Context context) {
super(context);
}
public GridViewEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridViewEx(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void setNumColumns(int numColumns) {
super.setNumColumns(numColumns);
if (numColumns != mRequestedNumColumns) {
mRequestedNumColumns = numColumns;
}
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mRequestedNumColumns > 0) {
int width = (mRequestedNumColumns * getColumnWidth())
+ ((mRequestedNumColumns-1) * getHorizontalSpacing())
+ getListPaddingLeft() + getListPaddingRight();
setMeasuredDimension(width, getMeasuredHeight());
}
}
}

I was using
android:layout_below="#+id/msg"
works fine after i removed it and used
android:layout_centerInParent="true"

Related

List View inside Scroll View . I am not able to scroll list and it displays only 3 database files

Please help me to scroll the database list. Its displaying only 3 database files and I am not able scroll that database list. Please help me to fix this issue. I am beginner for android.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:gravity="center|top"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="information"
android:textSize="22dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#color/DarkGray"
android:gravity="center|top"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="information"
android:textColor="#color/DarkMagenta"
android:textSize="20dp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/Black" />
<TextView
android:id="#+id/backupView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select storage location"
android:textSize="18dp" />
<RadioGroup
android:id="#+id/radioGroupBackup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/phoneBackupRb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Memory" />
<RadioButton
android:id="#+id/externalBackupRb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="External Memory" />
</RadioGroup>
<Button
android:id="#+id/backupBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="onClickBackup"
android:text="Back Up" />
<TextView
android:id="#+id/backupSuccessView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:text=""
android:textSize="15dp" />
<Button
android:id="#+id/AutobackupBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="onClickAutoBackup"
android:text="Auto BackUp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:background="#color/DarkGray"
android:gravity="center|top"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="information"
android:textColor="#color/DarkMagenta"
android:textSize="20dp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/Black" />
<TextView
android:id="#+id/restoreView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select storage location"
android:textSize="18dp" />
<RadioGroup
android:id="#+id/radioGroupRestore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/phoneRestoreRb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Memory" />
<RadioButton
android:id="#+id/externalRestoreRb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="External Memory" />
</RadioGroup>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="#color/Black" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"
android:text="Select database file to restore"
android:textSize="20dp" />
<TextView
android:id="#+id/restoreResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text=""
android:textSize="15dp" />
</LinearLayout>
<ListView
android:id="#+id/listDatabase"
android:layout_width="fill_parent"
android:layout_height="150dp" />
</LinearLayout>
Maybe it's better to move your ListView in other Activity, which will extend ListActivity. Then it will definitely be scrollable.
Hi By default listview can't add inside a scrollview.For doing this you need to either calculate the size of list view based on item or you need to override the onMeasure of listview.
Added below a custom Listview with overrid onMeasure method use this listview instead of the default one.Also make sure in xml file the heigh should be wrap_content.
Also make scrollview's fillviewport property to true inorder to use full height.
public class MyListView extends ListView {
public MyListView(Context context) {
super(context);
}
public MyListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
// The two leftmost bits in the height measure spec have
// a special meaning, hence we can't use them to describe height.
heightSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >>2, MeasureSpec.AT_MOST);
}
else {
// Any other height should be respected as is.
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
public void showListView() {
dbList = (ListView) findViewById(R.id.listDatabase);
ListAdapter listAdapter = dbList.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(
dbList.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, dbList);
listItem.measure(desiredWidth,
MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = dbList.getLayoutParams();
params.height = totalHeight
+ (dbList.getDividerHeight() * (listAdapter
.getCount() - 1)) + 30;
dbList.setLayoutParams(params);
dbList.requestLayout();
}

Refresh LinearLayout size

I want to have a square imageview in my app and I made a custom ImageView :
namespace Blood_Pressure_Notebook.Widget
{
public class SquareImageView : ImageView
{
public int SquareMode { get; set; } = 0;
public SquareImageView(Context context) : base(context) { }
public SquareImageView(Context context, IAttributeSet attrs) : base(context, attrs)
{
Android.Content.Res.TypedArray ta = Context.ObtainStyledAttributes(attrs, Resource.Styleable.SquareImageView);
this.SquareMode = ta.GetInt(Resource.Styleable.SquareImageView_SquareMode, 0);
ta.Recycle();
}
public SquareImageView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer) { }
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
if (SquareMode == 0)
{
SetMeasuredDimension(MeasuredWidthAndState, MeasuredWidthAndState);
}
else if (SquareMode == 1)
{
SetMeasuredDimension(MeasuredHeight, MeasuredHeight);
}
}
}
}
and I use it here:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardElevation="2dp"
app:cardCornerRadius="1dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true"
app:contentPadding="0dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
**<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layoutDirection="rtl">
**<blood_pressure_notebook.widget.SquareImageView
android:src="#android:drawable/ic_menu_gallery"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/imgImage"
android:scaleType="centerInside"
android:layout_weight="0.2"
android:adjustViewBounds="true"
app:SquareMode="0"/>**
<TextView
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/txtName"
android:layout_gravity="start|center_vertical"
android:layout_weight="0.8"
android:textSize="#dimen/textSize" />
</LinearLayout>**
<TextView
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/txtDescription" />
<RelativeLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout1">
<ImageView
android:src="#drawable/ic_account_box_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgLogin"
android:layout_alignParentEnd="true" />
<ImageView
android:src="#drawable/ic_mode_edit_black_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgEdit"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
The imageview size changes without any problem but height of its parent (LinearLayout) doesn't change and stays large!
My question is how to refresh or update the LinearLayout?
I've tried Invalidate() and setting LayoutParameter but nothing changed.

GridView does not expand

I am kinda new in android and got such a problem, I am trying to use ExpandableHeightGridView for gridview inside scrollview, but layout doesn't render, some advice please.
It always gives me this error:
Exception raised during rendering: ScrollView can host only one direct child
ExpandableHeightGridView.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)
{
// HACK! TAKE THAT ANDROID!
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;
} }
layout.xml:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fed70d"
android:orientation="horizontal">
<ImageView
android:id="#+id/iconBack"
android:layout_width="45dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="16dp"
android:src="#drawable/icon_back_white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="Wishlist"
android:textColor="#ac920d"
android:textSize="24sp" />
<LinearLayout
android:id="#+id/myCart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|right"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/addedInCart"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center|right"
android:src="#drawable/icon_menu_cart" />
<TextView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginTop="5dp"
android:background="#drawable/icon_mycart_count"
android:gravity="center_horizontal|center"
android:text="2"
android:textColor="#ffffff"
android:textSize="10dp" />
</LinearLayout>
<TextView
android:id="#+id/myMoneyInMyPocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|right"
android:layout_marginRight="10dp"
android:text="2000$"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ad8c22" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#fed70d"
android:orientation="horizontal"
android:weightSum="5.0">
<LinearLayout
android:id="#+id/menuItemStores"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconStores"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_logo" />
<TextView
android:id="#+id/menuTextStores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Stores"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menuItemInfo"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconInfo"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_info" />
<TextView
android:id="#+id/menuTextInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Info"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:background="#drawable/selected_menu_background"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconWishlist"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_wishlist" />
<TextView
android:id="#+id/menuTextWishlist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Wishlist"
android:textColor="#ffffff"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menuItemAccount"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconAccount"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_account" />
<TextView
android:id="#+id/menuTextAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Account"
android:textSize="14sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/menuItemCart"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:paddingTop="8dp">
<ImageView
android:id="#+id/menuIconCart"
android:layout_width="27dp"
android:layout_height="27dp"
android:layout_gravity="center"
android:src="#drawable/icon_menu_cart" />
<TextView
android:id="#+id/menuTextCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Cart"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/dropShadow"
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#drawable/drop_shadow"
android:cacheColorHint="#f1e7dd"
android:paddingTop="8dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<com.example.utils.ExpandableHeightGridView
android:id="#+id/wishListGridView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="2"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
This problem is discussed here.
Try adding one more LinearLayout inside your ScrollView as a direct child and put all other layouts/controls inside of the new LinearLayout.

GridView Expandable Height

I'm using ExpandableHeightGridView but I don't manage to calculate the correct height of the gridview, I use custom rows made ​​by an external layout, but it cuts several rows at the bottom,
here's the code:
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) {
if (isExpanded()) {
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;
}
}
Custom Row
<?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="#null"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewBioAnno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="#color/oro"
android:textSize="20sp" />
<TextView
android:id="#+id/textViewBioTesto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TextView"
android:textSize="15sp" />
</LinearLayout>
My Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/sfondo"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:scaleType="fitCenter"/>
<ImageButton
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="12dp"
android:background="#null"
android:scaleType="fitXY" />
</RelativeLayout>
<TextView
android:id="#+id/textViewBio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/bio"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/oro" />
<******.ExpandableHeightGridView
android:id="#+id/mebersListGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:src="#drawable/fondo_pagina" />
<ImageView
android:id="#+id/ImageView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"/>
<ImageView
android:id="#+id/ImageView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"" />
</LinearLayout>
</ScrollView>
</LinearLayout>
My activity:
...
g = (ExpandableHeightGridView) findViewById(R.id.mebersListGridView);
arrayAdapter = new StandardArrayAdapter(this,
R.layout.activity_bio, arrayList);
g.setExpanded(true);
g.setAdapter(arrayAdapter);
...

ImageView doesn't show when layout is on ListView

I have the following layout file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/second_grey">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="#+id/categoryName"
android:text="#string/sport"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="12dp"
android:padding="2dp"
android:id="#+id/button"
android:text="#string/see_more"
android:textSize="12sp"
android:background="#drawable/blue"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="0.99">
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img1"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/football"
android:padding="8dp"
android:id="#+id/tv1"
android:layout_below="#+id/imgContainer1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img2"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/basketball"
android:padding="8dp"
android:id="#+id/tv2"
android:layout_below="#+id/imgContainer2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer3">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img3"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tennis"
android:padding="8dp"
android:id="#+id/tv3"
android:layout_below="#+id/imgContainer3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
And I put it in a ListView in this layout:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/categories"></ListView>
then, with a simple Adapter, I just set the layout of each element to the layout file.
It looks like this:
But if I just do instead of the ListView, it looks like this
EDIT: Notice the change of Sport for Fútbol, that's just my mistake, it has nothing to do with the actual layout.
EDIT2:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Category category = mList.get(position);
if (convertView == null)
convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.category, parent, false);
TextView categoryName = (TextView) convertView.findViewById(R.id.categoryName);
categoryName.setText(category.name);
categoryName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Italic.ttf"));
return convertView;
}
EDIT3:
package com.favega.groups;
import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
#TargetApi(11)
public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
}
int size = width > height ? height : width;
setMeasuredDimension(size, size);
Clearly you are setting Measured Dimension to the smallest of width and height!
Since you gave width and height as match_parent, we can assume that one of the value got as zero from parent. As your intention is to make a square image, I prefer you relate this SquareLayout to the child(image) itself rather that the parent. By using wrap_content.
Set each tag of view in converView.setTag(viewName); before returning covertView.
Try May help.Thanks!!

Categories

Resources