I have one EditText witch is of type numberDecimal. I would like to have the unit symbol (for instance m, km, kg, g) at the end of the value. I have found many posts here at stackoverflow that tells to use TextWatcher or InputFilter, but what I would really love to is to restrict this unit symbol to not being editable for the user.
So when you edit the value in the textbox, you can't move the courser to manipulate/delete the symbol. It is basically not a editable part of the value.
I'm sure this is possible to achieve, but Im not sure how much custom code I need to write to make it work. Are there any SDK support for this?
Ismar
<LinearLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.70"
android:gravity="center_horizontal"
android:padding="4dip" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/browseurl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dip"
android:imeOptions="actionGo"
android:textColor="#BCBCBC"
android:textSize="14dip"
android:typeface="serif" />
<ImageButton
android:id="#+id/cross"
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="#android:drawable/ic_menu_close_clear_cancel"
android:clickable="true"
android:focusable="false" />
</RelativeLayout>
</LinearLayout>
Replace the ImageButton with either a textbox or a image button for units.
Thanks for the hints. Here is my solution, I have one linearlayout with 20dip margin to the left and right. Inside of the layout I have one EditText and one TextViw. I set weight to 1 on the EditText so it fills any remaining space in the parent view.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:orientation="horizontal" >
<EditText
android:id="#+id/value_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/value_unit_symbol"
android:layout_width="wrap_content"
android:layout_height="25dip"
android:text="kg"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Related
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.
In the following UI, I hope the two buttons both btnAddress and btnDelete take minimum space,
and editnumber take max space, how can I do? Need I use RelativeLayout layout? Thanks!
BTW, the following code, the btnDelete button only display a part.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:orientation="horizontal" >
<EditText
android:id="#+id/editnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:ems="10" >
</EditText>
<Button
android:id="#+id/btnAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnAddressMin" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnDeleteMin" />
</LinearLayout>
This should do it. Giving editnumber a weight of 1. Leve the rest as it is.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:orientation="horizontal" >
<EditText
android:id="#+id/editnumber"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:phoneNumber="true"
android:ems="10" >
</EditText>
<Button
android:id="#+id/btnAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnAddressMin" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnDeleteMin" />
</LinearLayout>
Instead of fixing the length of EditText with ems, use layout_weight to fill the remaining space.
<EditText
android:id="#+id/editnumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:phoneNumber="true" />
Edit: I'm not sure if android:phoneNumber="true" is still recommended or not, but I would change it to android:inputType="phone" instead.
Give the android:layout_weight="1 to edit text .No need to change it to relative layout
<EditText
android:id="#+id/editnumber"
android:layout_width="0dp"//no need to specifing the width if parent layout is horizontal
android:layout_height="wrap_content"
android:phoneNumber="true"
android:layout_weight="1";>
</EditText>
Doing this Edittext will take maximum space and remaing space will be left for other View.Other then this no need to change anything in your xml
I want to put in the same row a TextView, and Edittext and a button but I am having the problem that the button is not aligned properly to left and in small screens edittext fills entire with.
Small screen:
Big Screen:
My codification is as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right" >
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</RelativeLayout>
</LinearLayout>
Apply a weight to your EditText so it will take up as much room as it can while letting the other two elements do the normal wrap_content. To do this, remove the relative layout container and then change the EditText width to "0dp" and give it a layout_weight of "1" as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</LinearLayout>
First, many people will tell you that hint is Android's solution for not needing the label. I don't care if you use the label or not but it does save you space, especially on smaller screens. That was just an FYI.
Now, your RelativeLayout that only has a Button appears to be useless...I would remove that. You can use layout_weight so that each View takes up the appropriate amount of space. Make sure to make the layout_width="0dp". So it may look something like
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:text="Go" />
</LinearLayout>
Here I used 2,3,1 for the weights of your TextView, EditText, and Button respectively. You may need to change those to get exactly what you want but that should give you a start.
Layout weigth is ideal for designing layouts that adjust to screen size. However, make sure to set layout_width to 0dp, or it won't work properly.
Use like this:
android:layout_width="0dp"
android:layout_weight="1"
I'm going to assume you mean the button is not properly aligned to the right.
It's because your RelativeLayout's android:width="wrap_content", but it should be android:width="match_parent".
Also, you'd be better off setting your EditText's android:width="0dp" and adding android:weight="1" so that it expands/contracts between screen sizes.
I am working on a calculator where on the "screen" the left part shows the symbol + - * / and the right shows the digits. The size of the symbol cannot be too small, but when the user presses for the symbol and the symbol shows e.g. +, the figures on the right will shift rightwards a bit in order to accomodate the symbol.
The figure on the right keep shifting rightwards when have symbol while back to its place when no symbols. Also, the symbol cannot be shown properly. This is frustrating.
Yet I would like to maintain that the symbol and the figures be maintained within 1 row such that it really looks like calculator. How could this be handled? The layout code is as follows. Thanks in advance!!!!
<TableRow
android:id="#+id/tableRow3"
android:layout_weight="0.1"
android:background="#android:color/white"
android:paddingLeft="0dp"
android:paddingRight="0dp" >
<EditText
android:id="#+id/symEditText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_span="1"
android:background="#android:color/transparent"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:longClickable="false"
android:maxLength="3"
android:paddingRight="0dp"
android:text=""
android:textColor="#003366"
android:textSize="14dp"
android:textStyle="bold" >
</EditText>
<EditText
android:id="#+id/ansEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_span="9"
android:background="#android:color/transparent"
android:clickable="false"
android:ems="4"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:longClickable="false"
android:maxLength="23"
android:paddingRight="20dp"
android:textColor="#003366"
android:textSize="#dimen/display_text_size"
android:textStyle="bold" >
<requestFocus />
</EditText>
</TableRow>
You should only use TableRow inside a TableLayout. I think you can just use a horizontal LinearLayout instead. If you want to always maintain a space to show a symbol, give that view a fixed width instead of using wrap_content, otherwise it will grow/shrink when the content changes. (make sure to specify your width in dp units)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/symbol"
android:layout_width="20dp" <!-- fixed size -->
android:layout_height="wrap_content"
<!-- more attributes -->
</TextView>
<TextView
android:id="#+id/answer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" <!-- fill remaining space -->
android:gravity="right"
<!-- more attributes -->
</TextView>
</LinearLayout>
I created a layout for dialog box. I want same width for zip code EditText and Email EditText Edit boxes and I want them to align left.
So it should look like:
Email: _______
ZipCode: _______
Correction: Actually above is not showing correctly on StackOverflow. The lines above should be exactly even and left justified. So I have exactly the same display problem on this forum. I put spaces after Email: but it still does not align.
Instead I am getting email always wider than Zip code. I even changed both EditText to same input type but no luck. Only if the TextView has exactly the same text do they align like if they both say Zip Code:. If I pad Email text edit with spaces they layout manager seems to know this and expand the email EditText larger than for the zip code. Very frustrating!
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="#+id/messageMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/text_21px"
android:textColor="#color/white"
android:text="lorum ipsum afasdf lajsdfasldfjald:"
android:padding="10dip"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/emailDescriptor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/text_21px"
android:textColor="#color/white"
android:padding="10dip"
android:text="Email: "/>
<EditText android:id="#+id/emailId"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_margin="10dip"
android:textSize="#dimen/text_21px" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/zipCodeDescript"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="#dimen/text_21px"
android:text="ZIP Code: "
android:textColor="#color/white"/>
<EditText android:id="#+id/zCode"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_margin="10dip"
android:textSize="#dimen/text_21px"/>
</LinearLayout>
As #AedonEtLIRA said, you can hardcode the position. You could increase the margin attribute for the left side to make them align.
Alternatively, you could use the attribute,
android:layout_alignLeft="#id/your_other_edittext"
A third idea, make the text boxes for your labels the same "width", then the following edittexts should align.
Finally, you could use weighted nests layouts. But that is a bad idea. Poor performance & excessively confusing. =)
If I understand you correctly, try this
<?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:orientation="vertical" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="7dp"
android:text="ZipCode" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</TableRow>
</TableLayout>
Stumbled upon a useful attribute today and remembered this post.
Check out Textview.setEms(int). Edittext inherits that function as well.