android width of linearlayout the same as image - android

I have my layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView
android:id="#+id/id_new_big_list_item_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:background="#drawable/round_corners_new_bottom" >
<TextView
android:id="#+id/id_new_big_list_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/new_restaurant_name"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="0dp" />
</LinearLayout>
</LinearLayout>
I need to show image at the top (center horizontal) and below linearlayout with textview. Image can have different width, but i want to set the width of the linearlayout the same as image has. But if text is too long, the width becomes more. How can i do that? if text's width more than image width is, make 2 lines of text.
Is it possible??
here snap of my layout :

set ImageView's layout_width="wrap_content" and remove the 2nd LinearLayout enclosing the TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/id_new_big_list_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/id_new_big_list_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/new_restaurant_name"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="0dp" />
</LinearLayout>

Your Image should be wrap_contant and depends on it size. TextView will place in all parent.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/id_new_big_list_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:background="#drawable/round_corners_new_bottom"
android:id="#+id/id_new_big_list_item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:textColor="#color/new_restaurant_name" />
</LinearLayout>
Good luck!

Here instead of taking a LinearLayout use a RelativeLayout.
And set the Bounds of your EditText to be Left & right aligned to the ImageView Width.So that when the length of the text is exceeding the height of the EditText is incremented in Vertical than that of Horizontal.

Related

Center ImageView and TextView Side by side

I want to center all contents layout to center but this is problem I am having:
As you can see, I want to center image and text in their parent but want to have image to left of text.
Here is layout code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/app_light_green" >
<ImageView
android:id="#+id/toastImage"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_centerInParent="true"
android:paddingTop="1dp"
android:src="#drawable/exclamation" />
<TextView
android:id="#+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="3dp"
android:paddingTop="2dp"
android:textColor="#color/app_darker_green"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
You can use a horizontal Linearlayout and add your TextView and ImageView inside it and then add the Linearlayout in Relativelayout and make it center.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/toastImage"
android:layout_width="24dp"
android:src="#drawable/exclamation"
android:layout_height="24dp"
android:paddingTop="1dp" />
<TextView
android:id="#+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:paddingRight="3dp"
android:paddingTop="2dp"
android:textColor="#color/app_darker_green"
android:textSize="#dimen/sixteen_sp"/>
</LinearLayout>
</RelativeLayout>
Try using framelayout.
In framelayout, add imageview and textview, assign gravity to be left for imageview and gravity to be center for textview. And atlast give some left margin to imageview in order to align it with textview.
Alternate soln with relativelayout is,
in your imageview tag use property
align_toLeftOf=“#+id/R.id.Textview1"
And remove center in parent from imageview.
Your problem will be solved.
Please ignore the syntax I have typed this answer from phone. :)
i am also bringing minel..just for simplicity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/app_light_green" >
<TextView
android:id="#+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:paddingBottom="3dp"
android:paddingLeft="5dp"
android:drawableLeft="#drawable/exclamation"
android:paddingRight="3dp"
android:paddingTop="2dp"
android:textColor="#color/app_darker_green"
android:textSize="#dimen/sixteen_sp" />
</RelativeLayout>
with mine you do not have to worry about imageview, you just position your textview in the center that's all..

Layout Weight Parameter Removed After setImageBitmap

I have a weird situation. I have a relatively simple layout with some rows and an image.
My layout looks like this (high level drawing):
The red square is an ImageView, the others are LinearLayouts and TextViews. My problem is that if i use Java code (setImageBitmap) to update the red ImageView's bitmap image then the Text 3's LinearLayout's weight seems disappear and its height will equal to the Text 3's TextView's height.
Like on this mockup (the green square is the new image):
The Text 3's LinearLayout's Weight is set to 1 so its height will force the Text 4's LinearLayout to be pushed to the bottom of the screen. So the Text 4 wont be on the bottom anymore, it jumps right after the Text 3's LinearLayout's bottom.
Here is my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text 1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/text2text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 2" />
</LinearLayout>
<ImageView
android:id="#+id/Pic2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/timage" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/Text3Container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 3" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" >
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text 4" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
It looks like im using some LinearLayouts that i should not but it contains more elements in the real layout, i just stripped it a little bit to be more simple to analyse here. But generally this is my xml.
Do you guys have any suggestion why is this happening? I also tried to put the image into a FrameLayout, same...
Thanks!

How to set width of TextView for fill HorizontalScrollView?

I need a TextView that the user can scroll horizontally with his finger. I create HorizontalScrollView and add TextView. But I can not set the width of TextView, so that it fills completely HorizontalTextView. How can this be achieved?
Thanks!
layout .xml 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" >
<ImageButton
android:id="#+id/btnBackspace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/buttonface_backspace" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnBackspace"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/btnBackspace" >
<TextView
android:id="#+id/calculatedField"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="75sp" />
</HorizontalScrollView>
</RelativeLayout>

Center list item vertically on ListView

I'm developing an Android application.
On one activity I have a List with list items. Those list items will have a TextView (I'm using my own layout because probably I will need to add more widgets to it).
This is ListActivity xml:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="469dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="40dp">
</ListView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/orderErrorMsg"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16dp" >
</TextView>
</RelativeLayout>
</LinearLayout>
And this is ListItem xml:
<?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="100dp"
android:orientation="vertical">
<TextView
android:id="#+id/orderToFillName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dp" />
</LinearLayout>
And, finally, this is the result:
Why TextView is not centered vertically?
You have "wrap_content" on the layout_height of the TextView = there is no space to center vertically on since the TextView is wrapped tightly.
Try changing the layout_height to "match_parent" or place gravity="center_vertical" attribute in the LinearLayout instead.
<TextView
android:id="#+id/orderToFillName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:textSize="20dp" />
the answer is really simple: you have to add this to your TextView's XML:
android:gravity="center"
Currently you're not setting the text's position in the TextView but just the View's position.
Best wishes,
Tim
Try this way:
<TextView
android:id="#+id/orderToFillName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity= "center_vertical|center_horizontal"
android:textSize="20dp" />
or
android:gravity="center"

Android: Layout List header

I have a header for a list that contains two image buttons. I want one of those image buttons to be on the left, and the other to be on the right. This is what my xml file looks like right now but it does not work, it just puts the two image buttons on the left together. I have also tried without the additional LinearLayouts but no luck.
<?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:gravity="fill_horizontal"
android:background="#drawable/list_header_background">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="left"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_refresh"
android:background="#drawable/list_header_background"
/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="right"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_battle"
android:background="#drawable/list_header_background"
/>
</LinearLayout>
</LinearLayout>
As kabuko wrote, the RelativeLayout width is equal to the parent width. When we have a child view inside that RelativeLayout, we can use the android:layout_alignParentXXXXXX="true" parameter in order to align it (the child) accordingly to the parent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#drawable/list_header_background">
<ImageButton
android:id="#+id/refreshBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_refresh"
android:layout_alignParentLeft="true"
android:background="#drawable/list_header_background"
/>
<ImageButton
android:id="#+id/battleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/ic_battle"
android:layout_alignParentRight="true"
android:background="#drawable/list_header_background"
/>
</RelativeLayout>
You probably want to use a RelativeLayout with layout_alignParentLeft and layout_alignParentRight on the buttons.

Categories

Resources