How to make EditText Hint right to left -sided? - android

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" />

Related

android gravity right cursor shows left of the input text EditText

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

Android EditText with hardware keyboard only

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"

Android Space Bar Is Not Working

In my Android project I have restricted Edittext to allow only Alphanumeric characters. I have using below code snippet to achieve this
<EditText
android:id="#+id/edt_text"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
android:ems="10"
android:imeOptions="actionNext"
android:maxLength="8"
android:padding="5dp"
android:singleLine="true" />
But while using this code if I tab the Space Bar showing in soft keypad it is acting as a BackSpacebutton and removing the characters in EditText. Anyone please help me to solve this issue.
You can also handle this Programetically.
mEditText1.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 (s.equals(" ")) {
mEditText1.getText().toString().trim();
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
I hope it may help you. :)
I know it's very too late, but the problem is in the input digit restriction you have set.
How about allowing the spacebar to create a space, and just remove it programmatically?
use this RegEx it will return true if it is alphanumeric else it will return false.
public boolean isAlphaNumeric(String s){
String pattern= "^[a-zA-Z0-9]*$";
if(s.matches(pattern)){
return true;
}
return false;
}

TextView takes up space with no text

I have a TextView that is still taking up space when there is no text to display. This happens only on OS 1.6.
When my app runs on 2.2, the height of the TextView collapses so that it doesn't take up any space.
Is there some property I can set in TextView to because it to collapse or disappear when no text is set? Here is my XML code.
<?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"
android:gravity="center"
android:layout_marginTop="3dp"
android:layout_marginRight="3dp"
android:background="#ff737373"
android:padding="3dp"
android:minWidth="64dp" >
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:tag="tabImage"></ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:tag="tabCaption"
android:textColor="#ffd9d9d9" />
</LinearLayout>
If you want to make a textview disappear if its text is null then use android:visibility or programatically use textview.setVisibility(View.INVISIBLE); or textview.setVisibility(View.GONE);.
for what you asking, you have to use TextWatcher, and then when the text is empty just make the TextView disappear:
your_text_view.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(your_text_view.getText().toString().equals("")){
your_text_view.setVisibility(View.GONE);
}
}
});
and any time you want to change the Text in it, just use your_text_view.setVisibility(View.VISIBLE); just before you set the new text to it, and if the new text will be empty it will not be shown as it will disappear before you even notice, and in the case it is already VISIBLE, this line won't make any harm or affection.
just make sure to declare your TextView as a general variable in order to be able to use it within the TextWatcher.
textview.setVisibility(View.INVISIBLE);
textview.setHeight(0);
Just to give a more complete Answer. You can give the TextView AutoCollapse capabilities by checking what text is being displayed whenever it is changed via TextChangedListener
I would simply use
.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.length()>0)
textView.setVisibility(View.VISIBLE);
else textView.setVisibility(View.INVISIBLE);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
and it now has the functionality that you are looking for.
I haven't encountered many things in android UI that don't have a viable change listener, this is applicable for most issues of adding functionality to views.

How to set the hint properly in a text Box means EditText in android

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

Categories

Resources