I have an edittext for which I want to set below background
but when I set this as background it stretches itself to fit in whole EditText like below image.
and below is my code for xml file for editext
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="visible" >
<EditText
android:id="#+id/comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:hint="#string/add_your_comment"
android:imeOptions="actionDone"
android:padding="5dp"
android:background="#drawable/bg_search_field"
android:scrollHorizontally="true"
android:singleLine="true" />
<ImageView
android:id="#+id/post_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:layout_marginRight="#dimen/fifteen_dp"
android:src="#drawable/tick_selection" />
</LinearLayout>
How can I set the background correctly?
Ok So issue resolved simply by using nine patch image i have got from actionBarsherlock library which is using it for it's search view.
This line.-
android:layout_weight="1"
makes your TextView fill the empty width in the LinearLayout. Just try and remove it. You should also replace
android:padding="5dp"
for
android:layout_margin="5dp"
// try this
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:visibility="visible" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical">
<EditText
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="add_your_comment"
android:imeOptions="actionDone"
android:padding="5dp"
android:background="#drawable/bg_search_field"
android:scrollHorizontally="true"
android:singleLine="true" />
</LinearLayout>
<ImageView
android:id="#+id/post_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="5dp"
android:layout_marginRight="#dimen/fifteen_dp"
android:src="#drawable/tick_selection" />
</LinearLayout>
// also use my uploaded image which is editext background i'm apply 9-patch so now it not stretched if editext width is large.
Related
I am making the list view in which I have a image view and to its left I have to align the TextView which should have text in the center of it not the bottom of it. I have set the gravity but its only looking good in the design but when I run my app and data is fetched in list view then the text gets set in the bottom .
I will show you what I mean by this.But first let me show you how I am doing to align this
<RelativeLayout
android:id="#+id/block_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pin" />
<TextView
android:id="#+id/row_address"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBaseline="#+id/iv_address"
android:layout_alignBottom="#+id/iv_address"
android:layout_marginLeft="3dp"
android:layout_toRightOf="#+id/iv_address"
android:gravity="center|center_vertical"
android:text="Address Goes here"
android:textColor="#AAAAAA" />
</RelativeLayout>
and this is how I am getting this
And this is what I want it to be
as you can see in the second picture I want this type of adjustment. and in design preview it is showing me design just like it is expected. But When I run the app it just showing me text like i showed you in picture one.
Please tell me what i am doing wrong and How to constrict this not to get out of allignment.
Replace whole code with :
<TextView
android:id="#+id/row_address"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:android:drawableLeft="#drawable/pin"
android:drawablePadding="10dp"
android:gravity="center|center_vertical"
android:text="Address Goes here"
android:textColor="#AAAAAA" />
You can add a drawable icon
<TextView
android:drawableLeft="#drawable/..."/>
Replace your TextView with this:
<TextView
android:id="#+id/row_address"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="#+id/iv_address"
android:layout_alignBottom="#+id/iv_address"
android:layout_marginLeft="3dp"
android:layout_toRightOf="#+id/iv_address"
android:gravity="center|center_vertical"
android:text="Address Goes here"
android:textColor="#AAAAAA" />
Use LinearLayout Instead of RelativeLayout as acheiveing something like this is pretty easy in LinearLayout
<LinearLayout android:layout_width="match_parent"
android:layout_height="150dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/iv_address"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:src="#drawable/andro"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:text="Hello World"
android:gravity="center"/>
</LinearLayout>
Try LinearLayout instead of RelativeLayout. because its easy to align.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/block_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/iv_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logout" />
<TextView
android:id="#+id/row_address"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="3dp"
android:gravity="center|center_vertical"
android:text="Address Goes here"
android:textColor="#AAAAAA" />
</LinearLayout>
I'm trying to make an image fit into the EditText itself using drawableLeft, without any padding or "whitespaces" around it.
This is what the image looks like for now
This is how it should look like
I tried increasing the size of the image itself but it made the EditText expand even more.
Here's the XML file:
<EditText
android:layout_marginTop="6dp"
android:id="#+id/login_password"
android:layout_width="315dp"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#8d8e8f"
android:hint="Password"
android:drawableLeft="#drawable/sample"
android:singleLine="true"
android:inputType="textPassword"
android:layout_below="#+id/login_name"
android:layout_centerHorizontal="true" />
How can the image fit inside the EditText? Does it something to do about the image itself?
You can try the LinearLayout for it. Means in the LinearLayout , you should put the Editext's drawable image as the background , and than divide it in to three parts,1st part is for the password_image , 2nd one for your EditText and and 3rd last one for is your INVISIBLE View, which prevent , your EditText value to touch at end
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/EDITEXT_DRAWABLE"
android:gravity="center_vertical"
android:padding="5dp"
android:weightSum="2">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="left|center"
android:src="#drawable/sample" />
<EditText
android:id="#+id/password_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1.5"
android:background="#android:color/transparent"
android:ems="10"
android:gravity="center"
android:hint="********"
android:inputType="textPassword"
android:paddingLeft="-10dp"
android:singleLine="true"
android:imeOptions="actionDone"
android:textColor="#android:color/white"
android:textColorHint="#android:color/white" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:layout_weight="0.1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:src="#drawable/sample"
android:visibility="invisible" /> ///PUT THE VIEW INVIIBLE
</LinearLayout>
Just use a Fram layout it works well use a frame layout and inside it edit text and an image view is created set the background of image view to transperent and gravity to the right or to the left
<FrameLayout
android:layout_width="match_parent"
android:layout_height="25dp">
<EditText
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/border_round"
android:drawableLeft="#color/place_autocomplete_search_text"
android:drawableStart="#drawable/places_ic_search"
android:drawableRight="#drawable/arrowxml">
</EditText>
<ImageView
android:layout_width="25dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
android:layout_gravity="right"
android:src="#drawable/searcharrow"
android:layout_height="25dp" />
use this...........
<EditText
android:id="#+id/user_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/editback"
android:drawableLeft="#drawable/useremail"
android:drawablePadding="10dp"
android:hint="#string/user_name"
android:padding="7dp"
android:singleLine="true"
android:textColor="#9291A1"
android:textSize="15sp" />
I am using the EditText with drawableLeft property and set ImageView in Right side for ClearText. My Question is how to put gap between text and Imageview in EditText?
Please help me.
Actually I have Like this,My Screenshot is,
Actually I want to Like this,
Create Gap Between Text and ImageView. How it is Possible?.
My xml file is,
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/edtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="5dip"
android:drawableLeft="#drawable/search"
android:drawablePadding="10dip"
android:hint="Find"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true" >
</EditText>
<ImageView
android:id="#+id/imgSearchClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/edtSearch"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:contentDescription="#string/app_name"
android:src="#drawable/clear" />
</RelativeLayout>
Okay i got your Question now...you have to use Search view instead of edittext
Here is link:- Check it out
Only this is a solution of your problem
i am using LinearLayout for this
<LinearLayout
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:orientation="horizontal" >
<EditText
android:id="#+id/chatText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textMultiLine"
android:padding="8dp" />
<ImageView
android:id="#+id/buttonSend"
android:layout_width="50sp"
android:layout_height="50sp"
android:padding="5dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Had the same problem a while ago fixed it with drawablePadding
Hello
Use Linear layout with weight instead of Relative as m showing below:-
<LinearLayout
width
hight
orientation="horizontal">
<EditText
android:id="#+id/edtSearch"
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:drawableLeft="#drawable/search"
android:drawablePadding="10dip"
android:hint="Find"
android:imeOptions="actionNext"
android:inputType="text"
android:singleLine="true" >
</EditText>
<ImageView
android:id="#+id/imgSearchClear"
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:contentDescription="#string/app_name"
android:src="#drawable/clear" />
</LinearLayout>
if you found any issue regarding this please let me know..i love to help you
or in relative layou you can use drawable padding
Spartacus thanks :)
I can try to implement your code but It Like this,
and I want to Like this,
Set the right padding of EditText to the width of Clear button. Set all the dimension in dimen folder so that it will be compatible with all device sizes.
I have a layout with one row:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingRight="#dimen/total_sales_page_summary_text_distance"
android:text="test2"
android:textSize="#dimen/total_sales_page_info_font_size" />
<TextView
android:id="#+id/year_on_year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/total_sales_info_text_color"
android:textSize="#dimen/total_sales_page_info_font_size" />
<ImageView
android:id="#+id/tradeGreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical"
android:contentDescription=""/>
</LinearLayout>
In source code I just set different number and visibility (GONE/VISIBLE) of imageview (clicking on different buttons).
as result:
image is not visible (button1 was clicked)
2.image and some long number are vivible (button2 was clicked)
3.image and other little shorter number are visible (button3)
You can compare how it looks like. Text becomes visible but image is on the previous place. Why? Why image is not moved to left.
By the way. When screen is rotated - image becomes visible on correct place.
Try to use weight.. it will align based on screen
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="#dimen/total_sales_page_summary_text_distance"
android:text="test2"
android:textSize="#dimen/total_sales_page_info_font_size" />
<TextView
android:id="#+id/year_on_year"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#color/total_sales_info_text_color"
android:textSize="#dimen/total_sales_page_info_font_size" />
<ImageView
android:id="#+id/tradeGreen"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical"
android:contentDescription=""/>
As Deepchand rightly said, add that Image as a drawable to the TextView directly. This will take care of the Image position and will remove an extra ImageView from the layout.
So to your TextView add this,
<TextView
android:id="#+id/year_on_year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/total_sales_info_text_color"
android:drawableRight="your image" //this will add an image to its right
android:drawablePadding="5dp" // you can also add padding as per you need
android:textSize="#dimen/total_sales_page_info_font_size" />
Hope this works the way you want. :)
This will be solved by using Weight.
Here your UI is horizontally so you need to change in your xml file. Like,
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="#dimen/total_sales_page_summary_text_distance"
android:text="test2"
android:textSize="#dimen/total_sales_page_info_font_size" />
<TextView
android:id="#+id/year_on_year"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textColor="#color/total_sales_info_text_color"
android:textSize="#dimen/total_sales_page_info_font_size" />
<ImageView
android:id="#+id/tradeGreen"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical"
android:contentDescription=""/>
</LinearLayout>
I have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/aussie_bg_big" >
<ImageView
android:id="#+id/titleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="#null"
android:src="#drawable/aussie_title_small" />
<ImageView
android:id="#+id/clearButtonOutput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/titleImage"
android:layout_alignParentRight="true"
android:contentDescription="#null"
android:src="#drawable/aussie_clear" />
<EditText
android:id="#+id/OutputText"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_above="#+id/clearButtonOutput"
android:layout_alignParentLeft="true"
android:background="#drawable/textbox"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:textColor="#ffffff" />
<ImageView
android:id="#+id/translateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/OutputText"
android:layout_centerHorizontal="true"
android:clickable="true"
android:contentDescription="#null"
android:src="#drawable/translatebuttonselector" />
<ImageView
android:id="#+id/ClearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/translateButton"
android:layout_alignParentRight="true"
android:contentDescription="#null"
android:src="#drawable/aussie_clear" />
<EditText
android:id="#+id/InputText"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_above="#+id/ClearButton"
android:layout_alignParentLeft="true"
android:background="#drawable/textbox"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:singleLine="false"
android:textColor="#ffffff" />
What I'd like to do is:
-> Keep the "imageBottom" imageview on the BOTTOM of the activity at all times, centered horizontally.
-> Keep the "imageCenter" imageview on the center of the activity at all times, centered horizontally.
-> Keep "imageBelowInputText" imageview below the InputText edittext which is located ABOVE imageCenter, aligned to the right.
-> Keep "imageBelowOutputText" imageview below the OutputText edittext which is located BETWEEN imageCenter and imageBottom, aligned to the right.
It works fine so far but I'd also like for the edittext views to occupy ALL of the remaining space EQUALLY. Providing different versions of the layout for small, normal, large, x-large screens worked fine, considering I hardcoded each edittext's size in dpi. However, on some layouts, it still looks a bit wrong and misplaced (misaligned). That's why I'm looking for a solution to place the ImageViews accordingly first, and then let the two EditTexts take all the remaining space EQUALLY (important).
So far, I couldn't think of a 100% correct solution. Can you help me out? Thanks!
Notice the two edittexts have their layout_height set to "0dp" and layout_weight set to "1"
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="15dp"
android:orientation="vertical" >
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Clear" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:text="Bear" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Clear" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="center"
android:text="Speaking Aussie" />
</LinearLayout>
Try giving each of the EditText views a height of 0dp, and a layout_weight value of 1. Give it a shot and let me know if that works.