How do I select which editText is used first? - android

I have two edit text fields on my phone:
'Name:' and 'Phone:'
Whenever I launch my program, my cursor is already in the 'phone' field with the number keyboard pulled up. However, I'd like for it to center on the first editText, 'name', so that the regular keyboard pulls up.
Does anybody know why this is working this way, and how to correct it?

(EditText)txtName = findViewById(R.id.NAME_ID);
txtName.requestFocus();
Do this in your onCreate() after you've inflated your layout

See requestFocus tag on your layout

You can do this in code with a requestFocus() method on the view:
https://developer.android.com/reference/android/view/View.html#requestFocus()
or with an tag inside the view tag in the layout:
https://developer.android.com/guide/topics/resources/layout-resource.html#requestfocus-element

Related

How to give the hint of edittextbox of dialog which is created by code in android

I am creating a dialog box where there is a edit box I want to set the hint of editbox how can I do it.
please help me
thank you
Every EditText is also TextView and the TextView defines a method setHint():
final void setHint(int resid)
Doesn't this work?
EDIT Btw by edit box you mean EditText right?
I am not sure whether this works but might help you,
You could have initialized your Dialog with something like this,
Dialog dialog = null;
And now to Initialize your EditText you should use something like this,
edittext = (EditText) dialog. findViewById(R.id.editbox);
Now use the below method. It should do the trick,
edittext.setHint("Hint");
The same you can provide in the XML file also by adding :
android:hint="Text For Hint"
tag.
For dynamically adding it (in your code and not in XML), Boris & Androi have aldready mentioned it, so wont repeat it.
If you wont be changing once set, then I guess adding in XML is preferred rather than in code.

How to give focus to a button from an EditText?

In my form I have different EditText areas and a Button on the end.
If I use android:nextFocusForward and android:nextFocusDown between EditText in my XML file everything is ok, but if I use these in the last EditText in order to give focus to the Button I get:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{org.test/org.test.myActivity}: java.lang.ClassCastException:android.widget.TextView
I even tried:
android:focusableInTouchMode="true"
Of course they are in the same GroupView.
Can you try if this works, of course this is a code solution not an xml one:
button.requestFocus()

Access text of Multiline TextView in Android

I want to access text of particular line in a multi-line TextView. Any suggestions on how I could do this?
Fetch the Layout (android.text.Layout) of the TextView by calling mTextView.getLayout(); There you can for instance use the getLineStart or getLineEnd methods to get the offset of the text. That combined with getText and used using normal String operations should probably be enough for u!
By default, text view is multi line. Set the text with new line characters for ex. "First line \nSecond line".
Another option is listed here : https://stackoverflow.com/questions/2610280/how-to-get-multi-line-text-on-a-button-in-android

Put constrains to an EditText

I have an edit text and I'd like to put the following constrains (in the XML code if possible):
Disable all the capital Letters (the inverse of android:capitalize or the same fonction than toLowerCase())
Block to EditText to 1 line max. (for instance avoid that when I press enter the editText
get bigger to create a new line)
In fact , my editText is a Search Field (but in my case I don't want to use the special Search Widget).
Thanks
To make the edittext single line add this to its tag:
android:singleLine="true"
Also android:capitalize is depreciated and the following should be used instead
android:inputType="textCapCharacters"
However there seems to be no matching lowercase inputType, so you may have to implement it manually using a setOnKeyListener.
use android:lines="1" in the android xml page and then see the layout.

Using inputType on a TextView in a list item layout

I've made a ListActivity. I have an xml file that defines the layout for this Activity. I also have an xml file that defines the layout for an item in that list. This item layout has 3 TextViews inside of it. If I add the inputType="text" property to one of these TextViews, the onListItemClick handler no longer executes when I run my application in the emulator.
I noticed that singleLine="true" was deprecated, which is why I switched it out for inputType="text". Does anyone know why this is happening?
Note: I'm developing against 2.1
Thanks for your help
Setting an inputType probably causes the TextView to be focusable , and when you have a focusable element in a ListView row, the row is no longer clickable.
InputType has to do with users entering text. The equivalent to singleLine="true" would be lines="1", not inputType="text"
I had the same problem. I had resolved this issue by making textview's property focusable, false.
android:focusable="false"
Hope this would be useful to someone, as singleLine property is deprecated and inputType text is preferred.

Categories

Resources