Set maximum column height in gridLayout to avoid overlapping - android

in my app I am encountering this problem:
As you can see i added the option to let the use specify a name through an edit text in an alertDialog. Hypotetically the user could enter an extremely long name, ruining the entire layout as the following rows will overlap.
How can I enter a specified height and width for each row?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:longClickable="true"
/>
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<TextView
android:id="#+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="test text"
/>
</LinearLayout>

Give some height to your row item parent or textview as below:
<?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="100dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<TextView
android:id="#+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="test text"
/>
</LinearLayout>
Or setheight of your textview or give it maxlines
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<TextView
android:id="#+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="6"
android:minLines="6"
tools:text="test text"
/>
</LinearLayout>

Related

LinearLayout under GridView does not wrap content

I am working on Xamarin app but I believe this concept also applies for android.
I would like to show my LinearLayout wrapped items in GridView as below. But is does not render as I expected.
Is there anything I am missing here?
Code:
Main.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<GridView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridViewHomeItems"
android:horizontalSpacing="10px"
android:verticalSpacing="10px"
/>
HomeItem.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:padding="25px">
<ImageView
android:src="#android:drawable/ic_menu_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/homeItemImage" />
<TextView
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/homeItemTitle" />
</LinearLayout>
Remove the px from properties. Use sp or dp instead. Same thing apply to the Grid View layout also.
Like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:minWidth="25dp"
android:orientation="vertical"
android:padding="25dp">
<ImageView
android:id="#+id/homeItemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/homeItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Grid View like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="25dp"
android:minWidth="25dp"
android:orientation="vertical">
<GridView
android:id="#+id/gridViewHomeItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="96dp"
android:horizontalSpacing="10dp"
android:minHeight="25dp"
android:minWidth="25dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp" />
</LinearLayout>
Try adding this atributes. Main.axml#GridView:
android:columnWidth="96dp"
android:numColumns="auto_fit"
HomeItem.axml (#ImageView and #TextView):
android:layout_centerHorizontal="true"
Try the below code in your layout.
<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"
>
<GridView
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/gridViewHomeItems"
/>
</LinearLayout>
Change your custom layout as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:minWidth="25dp"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="#+id/homeItemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_gallery"/>
<TextView
android:id="#+id/homeItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
Also make sure you use dp instead of px as per android standards.

LinearLayout gravity inside RecyclerView

Hey I'm making a simple chat application. I'm having these two layouts to pass for the inflater to add to the RecyclerView:
received_msg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00">
<TextView
android:text="Hello"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
sent_msg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:layout_gravity="right">
<TextView
android:text="Text"
android:textColor="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
RecyclerView:
<android.support.v7.widget.RecyclerView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
The layout_gravity property seem to have no effect on the sent_msg.xml's LinearLayout. What to do?
You can make the two item XML's full width (match_parent) and move the layout_gravity and background attributes to the child TextView:
sent_msg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" (* Changed)
android:layout_height="wrap_content">
<TextView
android:text="Text"
android:background="#fff" (* Changed)
android:textColor="#f00"
android:layout_gravity="right" (* Changed)
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Unwanted Gridview spacing

My grid design is as such. But i want to remove the spaces on both sides of the grid elements. (Black marked)
I want the image to be expanded somewhat like
My XML layouts for the gridview
<?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"
tools:context=".MainActivity" >
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="2"
android:stretchMode="spacingWidthUniform" />
</LinearLayout>
My grid element 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:orientation="vertical" >
<ImageView
android:id="#+id/grid_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:contentDescription="#string/im" >
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="23"
android:gravity="center"
android:layout_marginTop="10sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="12sp" >
</TextView>
</LinearLayout>
Try Changing
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:numColumns="2"
android:background="#abcdef"
android:stretchMode="columnWidth" />
You can replace GridView with GridLayout and use weights to make the images fill the screen. Here's the reference: http://developer.android.com/reference/android/widget/GridLayout.html, (See the section: Excess space distribution)

listview items not filled the whole area(Expandable list view)

i am referring this to implement 3 level expandable list. the code works fine. but my problem is , the items in the child elements not filled the whole area. because of that i cant expand the next child element by clicking on it.(i have to click the exact area which is child element text appeared)
can someone point me, where is the issue?
i have edit my xml view also as android:layout_width="fill_parent"
but didn't worked..
please help me on this.
thank you.
EDIT
HERE THE ALL LAYOUT XML CODES.
ROOT
<?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="fill_parent"
android:layout_height="wrap_content"
android:background="#ff5b5b"
android:orientation="vertical" >
<TextView
android:id="#+id/itemRootTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
</LinearLayout>
PARENT
<?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="fill_parent"
android:layout_height="wrap_content"
android:background="#40e0d0"
android:orientation="vertical" >
<TextView
android:id="#+id/itemParentTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
</LinearLayout>
CHILD
<?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="fill_parent"
android:layout_height="wrap_content"
android:background="#839096"
android:orientation="vertical" >
<TextView
android:id="#+id/itemChildTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
</LinearLayout>
MAIN XML LAYOUT
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_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=".MainActivity" >
<ExpandableListView
android:id="#+id/expList"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
>
</ExpandableListView>
</RelativeLayout>
I also encountered this problem with a spinner
I solved it like this
child 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="fill_parent"
android:layout_height="wrap_content"
android:background="#839096"
android:orientation="vertical" >
<TextView
android:id="#+id/itemChildTitle"
android:layout_width="match_parent" <== and this
android:minWidth="500dp" <=== add this
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
</LinearLayout>

GridView filling parent despite wrap_content

I have a gridlayout like this in the main layout xml of my activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/parentLayout"
android:layout_gravity="right">
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AAAAAA"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="none"
/>
</LinearLayout>
I populate it with 6 icons by inflating the following:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/searchIcon"/>
What I want is that the gridlayout is right aligned in the linear layout and that it just wraps its content. I am testing this by giving it a different coloured background. The layout_height="wrap_content" works fine, but the width seems to have no effect. The result is this:
Any ideas?
Thanks in advance,
Stephen
Insert android:gravity="right" in GridView
<GridView
......
.....
android:gravity="right" />
use android:gravity="right" in LinearLayout instead-of LinearLayout ..............
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/parentLayout"
android:gravity="right"> //<-----------------------
android:gravity sets the gravity of the content of the View its used
on.
android:layout_gravity sets the gravity of the View or Layout in its
parent.
for test try
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:id="#+id/parentLayout"
android:gravity="right">
<TextView android:id="#+id/gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#AAAAAA"
android:text="ewewqe" />
</LinearLayout>
also see this not the answer but see
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/parentLayout"
android:layout_gravity="right"
android:gravity="right" android:weightSum="1">
<GridView
android:id="#+id/gridview"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:background="#AAAAAA"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="none"
android:layout_weight=".5" />
</LinearLayout>

Categories

Resources