I am using Kotlin. I have used gravity right so that input number start from right. But EditText cursor keep stay always left of the input text in EditText. I want to keep gravity right & text right to left.Here is the image
Here is my EditText xml code..
<EditText
android:id="#+id/et_amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="numberDecimal"
android:ellipsize="end"
android:gravity="right|center_horizontal|center_vertical"
android:textStyle="bold"
android:maxLength="6" >
<requestFocus />
</EditText>
Please help me so that input text start from left to right & cursor show always right.
Try to add this to your edittext:
android:textDirection="rtl"
try this:
EditText text = new EditText(context);
text.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
String text1 = text.getText().toString();
text.setText(text1);
text.setSelection(text1.length());
}
});
it will be work;
hope to help you
Related
I want to display the text that the user enters in an EditText in a TextView.
As the user starts typing, the value of the TextView should change.
eg: If the user types a the value of the TextView should be changed to a and then he types b after that, the value of the TextView should be changed to ab...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"/>
This is the xml I'm using.
So is there anything like a edittext.setOnTypeListener so that I can add the textView's text directly
You can use a text change listener for this purpose.
Do it like this.
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
yourTextView.setText(s.toString());
});
You may use it according to your convenience in the before,after or onTextChanged method.
I want to use an EditText for the input from Barcode Scanners.
The Barcode Scanner acts like a physical Keyboard.
I scan a Barcode
It types the Barcode like an external Keyboard with a enter at the end
I have an EditText which should always be focused but never show the
software keyboard. I don't want to deactivate the Keyboard completely because it's needed in the same Class on a different EditText.
<EditText
android:id="#+id/barcodeText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:ems="10"
android:singleLine="true"
android:cursorVisible="false"
android:gravity="center"
android:background="#00000000"
android:textColor="#color/secondary_text"
>
then use TextChange Listener for your Edittext. Like this
editText.setInputType(InputType.TYPE_NULL);
editText.requestFocus();
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
// Your action
}
});
Please try with this property
android:inputType="none"
I have an EditText set up for user input. As soon as the user types any character I want that to trigger a floating label to rise above the EditText line. What is best method to see when user has typed in the first character on the EditText line? TextWatcher? Others?
partial Activity file:
...
private ListenerEditText cListenerEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
cListenerEditText = (ListenerEditText) findViewById(R.id.CEditText);
cTextInputLayout = (TextInputLayout) findViewById(R.id.ToDo_text_input_layout);
cListenerEditText.requestFocus();
cListenerEditText.setHint("To Do");
partial layout xml file:
...
<android.support.design.widget.TextInputLayout
android:id="#+id/ToDo_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.example.jdw.fourthscreen.ListenerEditText
android:id="#+id/CEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textCapSentences|textNoSuggestions"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textColorHighlight="#30D5C8"
android:singleLine="true"
android:maxLength="51"
android:imeOptions="actionNext|flagNoExtractUi"
android:layout_marginBottom="3dp"
android:nextFocusDown="#+id/DEditText" >
</com.example.jdw.fourthscreen.ListenerEditText>
</android.support.design.widget.TextInputLayout>
To monitor and/or control text input, we usually use TextWatcher or InputFilter.
For your case, I believe TextWatcher is the right way to do.
To monitor user typing event in EditText you can use TextWatcher.
You can use afterTextChanged() method to check every typed character.
Below is code for adding TextWatcher to EditText.
cListenerEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) { }
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void afterTextChanged(Editable s) {
Log.e("TextWatcherTest", "afterTextChanged:\t" +s.toString());
}
});
How to make EditText Hint right to left -sided
I write Arabic word hint ,, and its showing on the left side .
Try this one:
Use android:gravity="right"
try to use attributes
android:textDirection="locale"
android:textAlignment="viewStart"
It works for me
You can use below code to resolve your problem:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (myEditText.getText().toString().length() > 0 & myEditText.getGravity() != Gravity.LEFT) {
myEditText.setGravity(Gravity.LEFT);
} else if (myEditText.getText().toString().length() == 0 & myEditText.getGravity() != Gravity.RIGHT) {
myEditText.setGravity(Gravity.RIGHT);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
You can use android:gravity to change the position of your text in side the EditText container. Here you can find more about Gravity.
For example
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Your hint here"
android:gravity="end"
android:id="#+id/editText" />
I am using EditText and providing some Hint there. I am putting it like this:
android:hint="#string/user_name_hint"
android:textColorHint="#ffffff"
android:gravity="left"
But here hint is coming in the left side. But i want the hint is in the middle of the box and when i am clicking the text should start from the left.
How it is possible??
Use this EditText
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="user_name_hint"
android:textColorHint="#fffff"
android:gravity="center_horizontal"
android:id="#+id/editText1"/>
Then you need to add addTextChangedListener to solve your problem.
final EditText hintText = (EditText) findViewById(R.id.editText1);
hintText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
hintText.setGravity(Gravity.CENTER);
} else {
hintText.setGravity(Gravity.LEFT);
}
}
});
set Gravity to center.. android:gravity="center_horizontal"
Not sure you can do it in a "straight forward way". Probably some workaround - either suggested by nagesh or by placing a button on top of the edit text and switching to edit text when it gets clicked or focused