Add space between each item layout in inflate layout android - android

I do inflate layout. I want give spacing between each item in layout. Like the image below
This is my main code in xml. The parent layout
<LinearLayout
android:id="#+id/mLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#F2F2F2"
android:orientation="vertical"
android:padding="2dp"/>
This is my inflate 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="wrap_content"
android:orientation="horizontal"
android:padding="3dp"
android:layout_marginTop="5dp"
android:background="#color/white_pure">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:textStyle="bold"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="wrap_content"
android:textSize="9sp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="right"
android:layout_marginRight="5dp"
android:textColor="#color/blueTransfer"/>
</LinearLayout>
How can i do to get like image (left side). Have spacing between each item in inflate layout. Hope anyone can help me.

One soluion is change you item layout like this
<?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="wrap_content"
android:orientation="horizontal"
android:padding="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#fff"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="aaaaaaaaaaaa"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="bbbbbbbbbb"
android:textColor="#000"
android:textSize="9sp"
/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="3"
android:gravity="right|center"
android:text="Details"
android:textColor="#00f"
/>
</LinearLayout>
</LinearLayout>
Another solution is
<?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="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:background="#fff"
android:orientation="horizontal"
android:padding="3dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="aaaaaaaaaaaa"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="bbbbbbbbbb"
android:textColor="#000"
android:textSize="9sp"
/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="3"
android:gravity="right|center"
android:text="Details"
android:textColor="#00f"
/>
</LinearLayout>
Then if you use ListView, you can increase spacing between ListView items by using android:divider https://stackoverflow.com/a/5309871/5381331
Or for RecyclerView: How to add dividers and spaces between items in RecyclerView?

Try this,
<?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"
android:layout_marginTop="5dp"
android:background="#color/white_pure"
android:orientation="horizontal"
android:padding="3dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toStartOf="#+id/tv_details"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_title"
android:layout_marginTop="5dp"
android:textSize="9sp" />
</RelativeLayout>
<TextView
android:id="#+id/tv_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_weight="3"
android:gravity="right"
android:padding="10dp"
android:layout_centerVertical="true"
android:textColor="#color/blueTransfer" />
</RelativeLayout>

You can also set the space (Margin) between two inflated layouts by java code like below, It works for me.
View v =LayoutInflater.from(this).inflate(R.layout.item,null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0,10,0,0);
v.setLayoutParams(layoutParams);
Hope This will helps you

<?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="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:background="#color/white_pure">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical">
<TextView
android:textStyle="bold"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order Attempt 1"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="wrap_content"
android:textSize="9sp"
android:layout_height="wrap_content"
android:text="aa"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="right"
android:layout_marginRight="5dp"
android:text="DETAILS"
android:textColor="#color/blueTransfer"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#F2F2F2" />
I also just think this trick .. put view .. it also worked .. Thank you all for helped me .. :-)

just use pading instead of marging in item's top group.and listview use margingLeft and margingRight.item code like this.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:background="#color/white_pure">

Related

How to align this layout. I'm using layout weight

XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/fragone"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="8"
android:background="#4caad4"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center"/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="62dp"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="13sp"
/>
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"/>
</Linearlayout>
PREVIEW :
It works perfectly, but I want to align text view. How do they do it? In this size perfect for horizontal view. In portrait view it align looks like this picture. How to set both horizontal & portrait view on same?
where do you want to align your textview,
to right, left or center or any predefined position
I am exactly not clear about your ques, please specify it and tell what you want exactly.
Here I have fixed some lines of your layout, as you are not very specific to your question
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#4caad4"
android:orientation="vertical"
android:weightSum="8">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center"
android:weightSum="4"
android:orientation="vertical">
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_centerHorizontal="true"
android:layout_gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:textColor="#fff"
android:text="text"
android:textSize="20sp"
android:layout_weight="2"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="text"
android:gravity="center"
android:textColor="#fff"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="text"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="text"
android:textAlignment="center"
android:textColor="#ffffff"
android:textSize="13sp" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="text"
android:gravity="center"
android:textColor="#fff"
android:layout_weight="1" />
</LinearLayout>
it is working fine in both mode.

How to remove Irregularity in layout of items in listview in android?

This is how the cart screen looks like. The layout changes as per the length of item name.
I want to keep a fixed ratio for all columns in the listview and at the same time, keep the size of button fixed.
How to achieve this ?
Below is the Screenshoot and layout code:
]1
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:background="#ffffff">
<TableRow>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/cartlist_layout"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t1"
android:textStyle="normal|bold"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/t2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t2"
android:padding="5dip"
android:textStyle="normal|bold"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/t2"
android:id="#+id/t3"
android:textStyle="normal|bold" />
<Button
android:layout_width="5dp"
android:layout_height="50dp"
android:id="#+id/button6"
android:background="#drawable/delete1" />
</TableRow>>
</TableLayout>
Edit1: The Listview layout code:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/fragment_mycart">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView1" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="16dp"
android:clickable="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#color/colorPrimary"
app:backgroundTint="#android:color/holo_orange_light"
app:borderWidth="0dp"
app:elevation="6dp"
app:fabSize="normal"
app:srcCompat="#drawable/cart" />
</RelativeLayout>
You can use a LinearLayout as root view of the item instead of TableLayout. You can add weights to the views inside the LinearLayout.
<?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="horizontal"
android:background="#ffffff">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:id="#+id/t1"
android:textStyle="normal|bold"
android:padding="5dip"
android:maxLines="1"
android:ellipsize="end" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#+id/t2"
android:padding="5dip"
android:textStyle="normal|bold" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:id="#+id/t3"
android:textStyle="normal|bold" />
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button6"
android:background="#drawable/delete1" />
</LinearLayout>
</LinearLayout>
Note : You may want to adjust the weights a bit.
Use LinearLayout instead
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:text="text1"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:text="text2"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/t3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dip"
android:text="text3"
android:textStyle="normal|bold" />
<Button
android:id="#+id/button6"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:drawable/ic_input_add" />
</LinearLayout>

Android XML code : how to place an image left of Textview?

In my Android Application i have 2 textviews and 1 image to e displayed on the listview. While i used my imageview it is going to the new line..
I want one textview and imageview on the same line. Every time i place the imageview the whole alignment is changing ..
my XML code is
<?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="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/album1" />
<TextView
android:layout_toRightOf="#id/icon"
android:id="#+id/textViewSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="22dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_weight="1"
android:textColor="#android:color/white"
android:textSize="15dp" />
<TextView
android:id="#+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginLeft="10dp"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
use drawableleft in your xml of textview
android:drawableLeft="#drawable/settingmenuright"
you can use this layout i have set weightSum property.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3">
<ImageView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="0.7">
<TextView
android:layout_toRightOf="#id/icon"
android:id="#+id/textViewSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="iconTextview"
android:textColor="#android:color/background_dark"
android:textSize="22dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/textViewArtist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="textViewArtist"
android:textColor="#android:color/background_dark"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5">
<TextView
android:id="#+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/background_dark"
android:layout_marginLeft="10dp"
android:text="textViewDuration"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
The layout messes up because you have the ImageView and the TextView in a vertical LinearLayout. Do this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/album1" />
<TextView
android:layout_toRightOf="#id/icon"
android:id="#+id/textViewSongName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/white"
android:textSize="22dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textViewArtist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_weight="1"
android:textColor="#android:color/white"
android:textSize="15dp" />
<TextView
android:id="#+id/textViewDuration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginLeft="10dp"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
I have moved the ImageView and TextView in another horizontal LinearLayout

Android Layout for calendar control

I am looking to build a layout as above using linear/relative layouts with textbox.
Any help is appreciated.
Add background style to root layout for rounded corner and done
<?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:background="#999999"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#FFFFFF"
android:text="25"
android:padding="5dp"
android:textColor="#000000"
android:textSize="60sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Sept \n2016"
android:padding="5dp"
android:textSize="25sp" />
</LinearLayout>
</LinearLayout>
<?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:background="#999999"
android:layout_margin="20dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#999999"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#FFFFFF"
android:padding="10dp"
android:text="25"
android:textColor="#000000"
android:textSize="60sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Sept \n2016"
android:textColor="#android:color/white"
android:padding="10dp"
android:textStyle="bold"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>

Custom GridView Item

How to create a custom GridView item like below ?
Layout written by me -
<?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:orientation="vertical"
android:background="#drawable/round_corner_drawable">
<ImageView
android:id="#+id/market_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/default_image"/>
<TextView
android:id="#+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Text 1"/>
<TextView
android:id="#+id/priceTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Text 2"/>
</LinearLayout>
To make it square, I added these lines in the adapters getView() method -
GridView grid = (GridView)parent;
int size = grid.getColumnWidth();
view.setLayoutParams(new GridView.LayoutParams(size, size));
After running it on the device, i am getting below result.
The texts below imageview are cropped and not visible. Also i want the imageview to go above the layout as shown in the image above. Please help
Image which i am using -
Try to give weight=1 to image layout:
<?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:orientation="vertical"
android:background="#drawable/round_corner_drawable">
<ImageView
android:id="#+id/market_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/default_image"/>
<TextView
android:id="#+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Text 1"/>
<TextView
android:id="#+id/priceTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Text 2"/>
</LinearLayout>
As per above xml.
try using following layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="#+id/anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/market_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/nameTV"
android:layout_below="#+id/anchor"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/nameTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_above="#+id/priceTV"
android:gravity="center_horizontal"
android:text="Text 1" />
<TextView
android:id="#+id/priceTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="5dp"
android:gravity="center_horizontal"
android:text="Text 2" />
</RelativeLayout>
or use following 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:weightSum="100"
android:orientation="vertical">
<ImageView
android:id="#+id/market_image"
android:layout_width="wrap_content"
android:layout_height=match_parent
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="10"
android:src="#drawable/ic_launcher"/>
<TextView
android:id="#+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="45"
android:text="Text 1"/>
<TextView
android:id="#+id/priceTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_weight="45"
android:text="Text 2"/>
</LinearLayout>
Finally i am able to solve it.
I have written below layout to get the desired output
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="210dp"
android:layout_height="240dp"
android:background="#drawable/round_corner_drawable"
android:layout_marginTop="15dp">
<TextView
android:id="#+id/nameTV"
android:layout_above="#+id/priceTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Latin American Republic"/>
<TextView
android:id="#+id/priceTV"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="65$"/>
</RelativeLayout>
<ImageView
android:id="#+id/market_image"
android:layout_width="190dp"
android:layout_height="200dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scaleType="fitXY"
android:src="#drawable/default_marketplace"/>
</RelativeLayout>

Categories

Resources