How to programmatically set edit text hint in android? - java

Is there a way to set the text hint displayed inside of an android edittext with java? I would like for my app to be able to display different things in the edittext at different times.

Did you try the setHint?
myTextView.setHint("My conditional hint");
Hope it helps..

If you are using simple Editext without TextInputLayout :
EditText etUsername;
etUsername = (EditText) findViewById(R.id.etUsername);
etUsername.setHint("Your Hint");
If you are using Editext within TextInputLayout then you need to change property of TextInputLayout not Edittext:
TextInputLayout layoutUser;
layoutUser = (TextInputLayout) findViewById(R.id.inputLayoutUname);
layoutUser.setHint(getResources().getString(R.string.hintFaculty));

Call setHint() on the EditText.

((TextView) findViewById(R.id.yourEditTextId)).setHint("hint text");

Kotlin
editText.hint = "Your hint"

if you are using actionbarsherlock searchview, this is the best way to set string dynamically
AutoCompleteTextView searchTextView =
(AutoCompleteTextView) mSearchView.findViewById(R.id.abs__search_src_text);
searchTextView.setHint(getResources().getString(R.string.search));

In Kotlin
val layoutMobile: TextInputLayout = findViewById<View>(R.id.inputLayout_MobileNumber) as TextInputLayout
layoutMobile.hint = "Your Hint"

for editText.hint you must delete name of your edit.Text from activity_main.xml then set in the MainActivity in java or activity_main.xml because text is preferred to hint.

<EditText
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine|textLongMessage"
android:hint="\n"/>
To be honest I don´t understand why.. but including "\n" as hint in the xml did the trick!
Now you can call setHint from Java code:
searchEditText.setHint(getString(R.string.search_hint));
and remove the \n from strings.xml if you want to allow the text split automatically according to the room
<string name="search_hint">keyword, location, max price (e.g. 1K, 1M)</string>

in Java
yourEditTex.setText("");//First
yourEditTex.hit("Your text");
this for alls cases.

You can try following,
in Java
yourEditText.setHint("hint");
in Kotlin
yourEditText.hint = "Your hint"

Related

How to get EditText by xml id Java Android SDK

I am new to the android sdk. I have this weird gui thing and I made a Button and an EditText inside of it. I think I properly set up the onClick stuff for the button, but how to I get the text inside of the EditText with the id of editText1 from what it says inside the xml file? Here is my EditText xml element inside of the xml file:
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/button1"
android:ems="10"
android:hint="#string/test"
android:inputType="textMultiLine|none" >
<requestFocus />
Thanks! Sorry that I am so new to Android development. None of the things I have searched make an sense to me. I keep seeing stuff about the "R" class and I have no idea what that means.
Starting learning from https://developer.android.com/training/
In onCreate() of your source code say MainActivity.java
Try this!
final EditText et=(EditText)findViewById(R.id.editText1);
in onClick() listener of your button, try String ss=et.getText().toString();
Hope it helps!
To get the text inside the EditText do that:
EditText editText = (EditText) findViewById(R.id.editText1);
String yourText = editText.getText().toString();
To get the text entered inside of your EditText, you would do the following inside of your onClick:
EditText et = (EditText)findViewById(R.id.editText1);
String words = et.getText().toString();
Then do whatever you want with the text with the variable.

How to display the numeric keyboard directly in the Android [duplicate]

I just basically want to switch to the number pad mode as soon a certain EditText has the focus.
You can configure an inputType for your EditText:
<EditText android:inputType="number" ... />
To do it in a Java file:
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
I found this implementation useful, shows a better keyboard and limits input chars.
<EditText
android:inputType="phone"
android:digits="1234567890"
...
/>
Additionally, you could use android:maxLength to limit the max amount of numbers.
To do this programmatically:
editText.setInputType(InputType.TYPE_CLASS_PHONE);
KeyListener keyListener = DigitsKeyListener.getInstance("1234567890");
editText.setKeyListener(keyListener);
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
android:inputType="number|phone"/>
will show the large number pad as dialer.
You can do it 2 ways
Runtime set
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
Using XML
<EditText
...
android:inputType="number" />
If you need fractional number, then this is the answer for you:
android:inputType="numberDecimal"
If you are using your EditText in Dialog or creating dynamically and You can't set it from xml then this example will help you in setting that type of key board while you are using dynamic Edit Text etc
myEditTxt.setInputType(InputType.TYPE_CLASS_NUMBER);
where myEditTxt is the dynamic EDIT TEXT object(name)
editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);
Use the below code in java file
editText.setRawInputType(Configuration.KEYBOARD_QWERTY);
Below code will only allow numbers "0123456789”, even if you accidentally type other than "0123456789”, edit text will not accept.
EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_CLASS_PHONE);
number1.setKeyListener(DigitsKeyListener.getInstance("0123456789”));
More input types and details found here on google website
Define this in your xml code
android:inputType="number"
EditText number1 = (EditText) layout.findViewById(R.id.edittext);
number1.setInputType(InputType.TYPE_CLASS_NUMBER);

How to use an EditText as an indicator

I want to use an EditText as an indicator to show values, which i receive from a remote device. So the text in the EditText must not be editable. How to do that ?.
Use TextView isntead. That is what TextView is for. If you read its documentation it says: It displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.
Hope this helps.
For what it sounds like you're trying to use it for, a TextView would be more appropriate, as others have suggested.
However, if you're set on using an EditText for a particular reason, you can achieve what you're looking for by disabling the EditText. This greys it out and prevents the user from editing the text. You can do this either statically (in XML) or dynamically (in Java).
XML:
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="This is disabled." />
Java:
EditText editText1 = (EditText) findViewById(R.id.editText1);
editText1.setEnabled(false);
What you are looking for is TextView. Set any text you want. It is un-editable.
TextView myTextView;
#Override
public void onCreate()
{
....
myTextView = (TextView) findViewById(R.id.textView);
myTextView.setText("Hello world");
}
For further info LINK.

How to add a scrollbar in a EditText dynamically/programmatically in Android?

I know how to adda scrollbar through the xml properties such that there is a bar in the right side of the edittext field once we write stuff in it by making setSingleline(false).
My question is :-
is there a way to do the same through the activity , that is, dynamically?
for eg:-
Code is :-
public class abc extends Activity
{
EditText edit = (EditText)findViewById(R.id.EditText1);
edit.setVerticalScrollBarEnabled(true);
edit.isVerticalScrollBarEnabled();
}
These two does not work. When i add these , i still am not able to see the scrollbar on the rightside of the Edittext when i scroll..
Through XML, when we add :- android:scrollbars="vertical" , then we are able to see the scrollbar..
I am asking for a WORKING way to do it through the activity dynamically or in other words programatically
Please its urgent. I would sincerely be thankful.
In XML
android:fadeScrollbars="false"
Dynamically/programatically:
edit.setScrollbarFadingEnabled(false);

paste option for edittext

I have an edittext and I would like to paste some text in it. I can copy the text from some web page but I am not able to paste the text in my edittext control.How can I enable my edittext to paste some text.Here is my main.xml for edittext ;
enter code here
<EditText
android:id="#+id/enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight= "2"
android:scrollbars="vertical"
android:textColor="#color/black"
/>
Thanks
This is on Android 4.4.2 Samsung S4;
Documentation for TextView says that:
To allow users to copy some or all of the TextView's value and paste
it somewhere else, set the XML attribute android:textIsSelectable to
"true" or call setTextIsSelectable(true). The textIsSelectable flag
allows users to make selection gestures in the TextView, which in turn
triggers the system's built-in copy/paste controls.
There is also another Textview attribure called android:cursorVisible which determines if the system should be invoked about the copy/paste callbacks.
By default I believe both of these are true and selection/copy/paste mechanics are already enabled. I could not change that behaviour by using android:textIsSelectable="false" but if I set android:cursorVisible="false" initially you can't paste anything inside the EditText. Only after you type something in, cursor and selection behaviour becomes enabled again. Maybe this should be handled inside the code rather than in the layout xmls, or it might be related to android:inputType which also did not make a difference for me.
So try setting android:cursorVisible="true" in your EditText's layout xml if paste is not enabled by default.
According to your problem if you copied some data any where in your system and you want to paste it in some specific variable, like Edit TextBox, Textview etc, then this code will surely help you.
ClipboardManager clipMan = (ClipboardManager)getSystemService(v.getContext().CLIPBOARD_SERVICE);
myEdtTxt.setText(clipMan.getText());
Note:- here the clipMan object will store the data whenever copied process take place and we will return that data from that object and will set it,
To enable the standard copy/paste for TextView, U can choose one of the following:
Change in layout file:
Either add below property to your TextView
android:textIsSelectable="true"
and In your Java class write this line to set it programmatically.
myTextView.setTextIsSelectable(true);
if fragment try with
mContext.myTextView.setTextIsSelectable(true);
And long press on the TextView you can see copy/paste action bar.
Try setting the inputType="text" for the EditText field

Categories

Resources