Android cannot deselect TextView - android

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

Related

TextView selection after EditText has been focused

I have a TextView which has text selectable.
<TextView
android:textIsSelectable="true"
android:layout_marginLeft="#dimen/reading_left_margin"
android:layout_marginRight="#dimen/reading_right_margin"
android:layout_marginTop="#dimen/reading_content_title_margin"
android:textColor="#color/colorText"
android:textSize="#dimen/reading_text_size"
android:lineSpacingMultiplier="1.8"
android:id="#+id/text_content"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
In the same fragment i have an EditText.
Once I focus into the editText, I am not able to select text on the TextView any more. However, if I minimize the app and reopen, I am able to again select the text on the TextView.
<EditText
android:imeOptions="actionDone"
android:layout_margin="5dp"
android:hint="#string/type_here"
android:textColorHighlight="#00000000"
android:id="#+id/input_comment"
android:textColor="#color/colorCommentsText"
android:padding="5dp"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I have tried textview.invalidate(), textview.requestFocus() neither seems to work.
EDIT: Forgot to mention I have done edittext.clearFocus() before trying to select text from textView.

Hint and Text in editText at same time

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>

Android: Making EditText similar to Chrome's URL box

I'm making chatting application for my school's assignment.
In my opinion, the default EditText in Android does not looks good. I want to make it looks like the URL box in Android's Chrome:
I have seen this design used in other applications such as Catch Notes (which looks beautiful).
So, is there any built-in option to change the EditText or we must draw it from scratch? Can anyone give link? I tried googling it, but I don't know the correct term
Thanks
you can achieving this by doing edittext background transparent.
so put your image in background of your layout and take a edittext inside this. and make edittext transpernt..
or
try this
<EditText
android:id="#+id/tracknumbertxt"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_toLeftOf="#+id/info"
android:background="#drawable/enter_tracking_no"
android:ems="10"
android:hint="Enter Tracking No."
android:paddingLeft="30dp"
android:inputType="text"
android:imeOptions="actionGo"
android:singleLine="true" >
</EditText>
here i have assign padding left..
so as per your design you have to set paddingRight property instead of paddingLeft..
Blindly use below layout style..
You will have your layout ready. Just replace the button backgrounds and your are done.
<?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:background="#android:color/darker_gray"
android:padding="5dip"
android:gravity="center">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:background="#android:color/white"
android:gravity="center"
android:padding="5dip">
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Type URL"
android:layout_weight="1"
android:background="#android:color/transparent"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Refresh"/>
</LinearLayout>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn2"/>
</LinearLayout>
There is nothing native to the SDK that can make it look like that. (maybe if you restrict use to only a certain version of the OS, then you can use that version's default styling, but isn't really a viable solution)
You will need to create an image to use as a background for the EditText to make it look how you want. The best options for text boxes and things like that are to either use a 9-patch image, or to create a layer-list XML file and describe your design that way. Either way, the file would go in the res/drawable folder in the project and you would set it as the background like so:
<EditText android:id="#+id/et_whatever"
android:layout_width="match_parent"
android:layout_height="wrap_content"
background="#drawable/YOUR_FILE_NAME" />

EditText goes out of the screen sometimes when virtual keyboard shows up

I am developing an application for Android.
I have a simple layout. A CheckBox, a Spinner and a Button at the top, a two line EditText at the Bottom which is displayed when the CheckBox is checked and another EditText which covers the rest of the space in the middle.
The problem is that when I am writing on the big EditText, sometimes the layout goes out of the screen on top and as a result I can't see what I am writing.
As a solution, I can set my layout to resize when the keyboard shows (using android:windowSoftInputMode="adjustResize" on the manifest.xml) but that won't be good since I do not want the 2 EditTexts to share the space left on the screen.
My layout code follows:
<?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">
<Button android:id="#+id/button"
android:text="Push"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
<CheckBox android:id="#+id/check_box"
android:text="Check"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<Spinner android:id="#+id/spinner"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText android:id="#+id/text_2"
android:hint="#string/hint_2"
android:inputType="textMultiLine"
android:maxLines="2"
android:gravity="top"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:visibility="gone" />
<EditText android:id="#+id/text_1"
android:hint="#string/hint_1"
android:inputType="textMultiLine"
android:gravity="top"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_below="#id/spinner"
android:layout_above="#id/text_2"
android:layout_alignWithParentIfMissing="true" />
</RelativeLayout>
P.S. This thing happens only on my Wildfire and very rare on the Emulator. A friend's Desire HD didn't have any problem at all. I don;t know if this makes any difference...
Thanks in advance for your help.
Antonis
I have the exact same problem. I've tested and this only happens with EditText in multiline mode and when SoftInputMode is set to adjustPan. This is most probably an Android bug.
The only solution I've found is to remove adjustPan.

Inflating a view multiple times in the same parent when a button is clicked

My app is about creating surveys. In order to do so, the user should be able to create new fields dynamically.
So, when creating a new survey, an Activity is shown with only 2 buttons (add and delete). When a user clicks the add button, a row appears containing a EditText(so he can write the question), a Spinner(that enables user to select the type of field: text, RadioButton, CheckBox, etc..) and another EditText (in which the different available answer choices are written, if applicable).
To accomplish this I created a XML file containing these Widgets and each time the user clicks in the add button, the XML is inflated. It works the first time I press the button, but after that... nothing happens.
I read somewhere that you cannot inflate the same child 2 times in a parent unless it is done in a loop.
So, my questions are:
-> How can I get around this and obtain the desired effect?
-> Why doesn't this apply in a Loop?
Any help or hint is appreciated. Once again, thank you very much for all the support the community has given me.
NOTE: In the android documentation they speak about cloning an inflator but the information there is scarce and I couldn't find any other info about it.
SOurce code:
XML - Main cointainer:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/mds_new_addfield_button"
android:tag="new"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Field"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/mds_new_deletefield_button"
android:tag="delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete Field"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
XML -Inflated:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow>
<TextView
android:id="#+id/mdsnew_field_tv"
style="#style/dsn_field"
android:text="Field Name "
/>
<TextView
android:id="#+id/mdsnew_type_tv"
style="#style/dsn_field"
android:text="Type of Field "
/>
<TextView
android:id="#+id/mdsnew_choices_tv"
style="#style/dsn_field"
android:text="Choices"
/>
</TableRow>
<TableRow>
<EditText
android:id="#+id/mdsnew_fieldname_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Field Name"
android:textSize="18sp"
/>
<Spinner
android:id="#+id/mdsnew_type_sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<EditText
android:id="#+id/mdsnew_choices_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choices "
android:textSize="18sp"
/>
</TableRow>
</TableLayout>
Add Button:
LinearLayout myRoot = (LinearLayout) findViewById(R.id.wrapper);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.mds_new_row, myRoot);
Your parent is a LinearLayout, and it looks like you intended a vertical orientation, but you haven't specified this. Try setting the orientation and see if that helps.

Categories

Resources