Hint and Text in editText at same time - android

I just need an editText in which i can set Text and hint at the same time.
e.g i want user to enter in editText string like user#1234
in this string user# should be text after that numbers will vary so that 1234 should be show as hint when user try to enter cursor will go after # and 1234 hint will gone when user type a character.
Please guide me how to do this.
Thanks

I managed to achieved this with bit of tricks and playing with margin and padding
This is what I did
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp" >
<TextView
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="right"
android:text="user#" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1234"
android:paddingLeft="40dp"
android:textSize="14sp" />
</RelativeLayout>
Hope this will help you out

This is not possible in the Android's default EditText but if want to achieve this any how, you will have to work a bit.
You can use this library called Masked-EditText. Read the documentation and understand how you can use it to solve your purpose. Hope that helps.
[EDIT]
I've got one more trick that you can definitely use. Implement an onChangeListener on the edit text. Every time it is called, run a function. The function should get the text in the editText and get the substring (first 5 char). If the substring matches "user#" then do not do anything. Else replace the text in editText with "user#". Easy hah!

You can do this in 2 ways
#1
Place an Image with user# as drawableLeft to your EditText
#2
Use a RelativeLayout to place a TextView and EditText one above the other so that it shows text in TextView as hint in EditText as below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="user#"
android:textColor="#aaaaaaaa"
android:textSize="30dip" />
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_toRightOf="#+id/textView1"
android:background="#00000000"
android:textSize="30dip">
</EditText>

Related

Android cannot deselect TextView

I'm making a simple test app to wrap my head around reading text from TextViews in android.
My problem is: I have 2 TextViews, one for a username and one for a password field, and a button. The button calls a method that gets text from both TextViews. However, my username TextView is automatically selected when the app runs and I can type text into it, and I cannot deselect it and select the password field to write into, but I can click the button. Does anybody know what the issue might be?
Android 7.0 used for testing
<?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"
android:orientation="horizontal">
<TextView
android:id="#+id/userField"
android:layout_width="#dimen/textView_width"
android:layout_height="#dimen/textView_height"
android:layout_above="#id/passField"
android:layout_marginHorizontal="#dimen/activity_horizontal_margin"
android:layout_marginVertical="#dimen/activity_vertical_margin"
android:background="#color/LightCoral"
android:gravity="center_vertical"
android:hint="#string/user"
android:inputType="text"
android:textAlignment="gravity"
android:textSize="10pt"
android:visibility="visible" />
<TextView
android:id="#+id/passField"
android:layout_width="#dimen/textView_width"
android:layout_height="#dimen/textView_height"
android:layout_centerInParent="true"
android:layout_marginHorizontal="#dimen/activity_horizontal_margin"
android:layout_marginVertical="#dimen/activity_vertical_margin"
android:background="#color/LightCoral"
android:gravity="center_vertical"
android:hint="#string/pass"
android:inputType="textPassword"
android:textAlignment="gravity"
android:textSize="10pt"
android:visibility="visible" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/passField"
android:layout_centerHorizontal="true"
android:width="#dimen/button_width"
android:height="#dimen/button_height"
android:background="#color/FireBrick"
android:onClick="uradi"
android:text="#string/button_tag"
android:textColor="#color/Black" />
</RelativeLayout>
EditText is better widget over TextView for handling user input. Replace your TextView with EditText
You should choose the controls that are suitable for the use of the sense.
The input of the username and password is generally used for EditText

how to fix the size of multiline edit text

I want to make a multi line edit text but the problem is that whenever i enter new line height of edit text increases. how to fix its height. My code is
<EditText
android:id="#+id/editAdditionalInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editPhoneCutomerDetails"
android:layout_alignParentBottom="true"
android:layout_marginBottom="60dp"
android:layout_below="#+id/editPhoneCutomerDetails"
android:layout_marginTop="10dp"
android:background="#drawable/message_additional_box1x"
android:ems="10"
android:padding="10dp"
android:gravity="top">
<requestFocus />
</EditText>
To fix the size of editText you can use
android:singleLine="true"
but it can limit your editText line to 1 .
if you want more lines in editText then use the following property.
Use android:lines="3" .
Use android:lines="2" for fixing multilines and for fixing single line use android:singleLine="true"
for example use like this as shown for multilines
<EditText
android:id="#+id/.."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Name"
android:maxLength="20"
android:lines="2"// fix the multiline limit
android:textColor="#000000" />
for example use like this as shown for single line
<EditText
android:id="#+id/.."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/editbox_background_normal"
android:ems="10"
android:hint="Name"
android:maxLength="20"
android:singleLine="true"// fix the single line limit
android:textColor="#000000" />
try this;
android:inputType="textMultiLine"
<EditText
android:inputType="textMultiLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
As singleline is now deprecated use inputType:multiline and set the number of lines you want with lines tag
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="4"/>
I had a similar requirement but ended up not changing the size of the box. i.e it grows as text is typed into it but stops short of blocking the other widgets in view. I read this post during my quest and had found a solution. Decided to post it back here. Restricting size to a value may possibly have different effects on different screens. So I think it is better to let it be.
The thing is to put the edit text inside a linearlayout with the rest of the widgets. The code and snapshot are here
Multi line edit box with ok cancel buttons

Strange error of dimensionally on the EditText after disabling the standard Android keyboard

in my app I have disabled the standard Android keyboard for each EditText with
myEditText.setInputType(0);
but now when I use the instruction:
myEditText.setText("string");
the sizes of my EditText does not fit to the size of the text you entered; so the text is not displaying in whole.
How can I fix this?
This is my xml:
<LinearLayout
android:layout_width="305dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="DECIMAL:"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25sp"
android:textStyle="bold" android:textColor="#000000"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
>
</LinearLayout>
If you want to disable editText then I recommend to use editText.setEnabled(false). Hiding keyboard is not the right solution until you really need it. But if you have disabled edittext then keyboard will not appear even if you focus on it.
I don't think problem is because of keyboard. One thing I observed in your code is, you are setting ems for editText. I think because of ems, editText size is constant always. Check this link for more information.

Android: how to align two text views based on length of text

I have a layout in which two TextViews are to be displayed on the same line such that
If TextView1 is a short text, TextView2 should be immediately right to TextView1(IMAGE1) and if the TextView1 is a long text, TextView2 should be at right corner of the Layout on the same line(IMAGE2)
How can I achieve this ?
i use simple horizontal LinearLayout with android:layout_weight attribute and it worked like you want.
Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="teeeeeeeext1"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text2"/>
</LinearLayout>
Use a layout like this..
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="38dp"
android:layout_toRightOf="#+id/editText1" />
</RelativeLayout>
edittext 1 will push edittext2 to its right...depending on text in it..
You can set android:maxWidth property for first text view. So your layout would look like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="200dp"
android:text="sdfksdofsdfsdfsdfsadfgwagrswargweqrgeqrgqwergeqrgeqrg"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:text="text2"
/>
Edit:
I apparently misread (or did not read fully) your question. Just don't mind my answer - I vote for mxy's :)
The last time I had the same problem, I wasn't able to find or hack away a straightforward solution. Counting pixels was not an option (at least not for me). But I found a workaround that eventually led to the same display concept, and that was to use SpannableStringBuilder.
As I'm assuming you want two different TextViews because you want them to have different colors (or size, or style, or typeface).
The idea is to use a single TextView for the whole thing, and prepare a SpannableString before doing setText on your view.
By using combinations of ForegroundColorSpan, TextAppearanceSpan, etc, you can make your single TextView look like different TextViews with different styles, sitting side by side, wrapping to the next line as necessary.
Links:
Setting font color at runtime
TextAppearanceSpan sample

How to make EditText smaller than default?

I need to show a large number of EditText controls on a screen, each of which will only allow entering 0-2 digits. The default EditText size is too wide for me to show enough EditText controls on a screen, but I have been unable to figure out how to make them narrower. I have tried the following attributes in
XML:
android:maxLength="2"
android:layout_width="20dip"
android:maxWidth="20px"
android:ems="2"
android:maxEms="2"
So the question is: how can EditText be made smaller than default?
Try this way instead, shows the difference. Use the layout_width with the specified width.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:width="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<EditText
android:layout_width="40dip"
android:layout_height="wrap_content"
android:text="#string/hello"
android:maxLength="2"
/>
</LinearLayout>
Hey once you try this,it may work.......
android:textAppearance="?android:attr/textAppearanceSmall"
In case you end up on this page to search how to achieve the same in Java code..
txtSample.setFilters(new InputFilter[]{new InputFilter.LengthFilter(2)});
When in a table, as you describe, android:layout_gravity="left" will collapse the EditText to to the stated size of the EditText

Categories

Resources