EditText keeps writing in single line Android - android

I know this question has been asked before and I tried several answers but none of them worked. I have an EditText where the user gives caption of a picture. The editText keeps writing the text in a single line. It doesn't go to the next line. I want the text to go to next line automatically when it reaches the screen's border/end. Here's my code. Please let me know if I have made any mistakes in my code.
<EditText
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:gravity="start"
android:inputType="textMultiLine|textPersonName"
android:hint="Write some thing about the photo"
android:maxLength="160"
android:paddingLeft="8dp"
android:textSize="14sp"
android:layout_below="#+id/privacyHolder"
android:lines="3"
android:layout_alignParentEnd="true"
android:ems="20"
android:paddingEnd="4dp"
android:maxLines="3"
android:layout_marginEnd="4dp"
android:layout_marginStart="4dp"
android:paddingTop="4dp"
android:background="#drawable/roundedbutton"
android:id="#+id/description"
/>

In the Resource, design your edit text as follow:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textMultiLine|textCapSentences"
android:gravity="left"
/>
In the activity you can use TextWatcher to listen when text exceeds few character and automatically move down.
Here is the snippet :
private boolean isReach = false;
// put this in onCreate method
edittext.addTextChangedListener(new TextWatcher(){
#Override
public void afterTextChanged(Editable s) {
// if edittext has 20 character reached add a new line, you can use either 20 or more here
if(yourtextEd.getText().length() == 20 && !isReach) {
yourtextEd.append("\n");
isReach = true;
}
// if edittext has less than 20 characters and isReach has set , change
if(yourtextEd.getText().length() < 20 && isReach) isReach = false;
}
});
Hope this will help you.

Related

how to remove ` frequently used email ` toast on my edit text?

as you can see from the image above, there is something like a toast with text "Frequently used email" . whenever I complete typing an email then that toast will appear, I don't think I make it programmatically. It seems it appears only on Android 10, in my Android Lollipop that toast doesn't appear. I use Xiaomi Redmi Note 7 for my Android 10.
I want to remove that toast from my edittext. how to do that ?
here is the XML for the edittext
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/email_login_textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:theme="#style/TextInputLayoutAppearance"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView_back_button">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/email_textView_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
and in Kotlin code, I just have this listener
emailTextView.setOnFocusChangeListener { v, hasFocus ->
val emailText = emailTextView.text.toString()
if (!emailText.isEmpty()) {
if (!hasFocus && (!emailText.contains("#") || (!emailText.contains(".")))) {
emailTextInputLayout.isErrorEnabled = true
emailTextInputLayout.error = "wrong email format"
} else if (!hasFocus) {
emailTextInputLayout.isErrorEnabled = false
emailTextInputLayout.error = ""
}
}
}
I have tried to change/remove android:inputType="textEmailAddress" , but that toast is still appear
I was also facing this problem and have solved by adding the following -
android:inputType="textNoSuggestions|textEmailAddress"
textNoSuggestions will prevent the 'frequently used email' toast and since the EditText is for email address so you need to add textEmailAddress as well

For setting limit in multiline edittext 's each line

I want to set limits in edittext each line and then click enter key cursor move to the next line in android studio, please answer how can I do this..my codes are
<EditText
android:inputType="textMultiLine|number"
android:singleLine="false"
android:digits= "0,1,2,3,4,5,6,7,8,9"
android:id="#+id/number"
android:hint="Enter Number Here..."
android:paddingLeft="10dp"
android:textSize="15sp"
android:fontFamily="sans-serif"
android:layout_margin="5dp"
android:gravity="top|left"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="130dp" />
when I click enter key it did not work and when I remove inputType from XML code enter key works please solve
Follow steps below:
Step 1: add these lines in your XML
android:maxLines="1"
android:imeOptions="actionNext"
Step 2(optional): use following code in your Activity
If you want to do some action when user clicks next
editText.setOnEditorActionListener((v, actionId, event) -> {
if (actionId == EditorInfo.IME_ACTION_NEXT) {
//your code here
return true;
}
return false;
});
I m using AndroidX with Java 1_8 enabled
Hope this will help!
May be you need to use android:minEms or android:maxEms. Read more in following link:
https://developer.android.com/reference/android/widget/EditText

Android ellipsize does not work properly

I'm building a new InfoWindow (google maps api v2) and I'm trying to get my layout to be perfect.
A part of that layout is this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtInfoWindowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#ff000000"
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/txtInfoWindowObstacles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLength="32"
android:lines="1"
android:singleLine="true"
android:textColor="#ff7f7f7f"
android:textSize="14dp"/>
Now, the problem is with the android:ellipsize="end".
It should draw the three dots at the end but it doesn't do that.
Now I'm getting something like this:
TextTextTextTextTe
Instead of this:
TextTextTextTextTe...
I'm thinking it has something to do with the fact that I'm using layout_width="wrap_content" . But I need that because I'm using a LinearLayout
I tried your code and they work fine, except the second TextView which has the attribute android:maxLength="32", which in this case the text cannot be ellipsized because of 32 characters limit. But if you remove it, it works as expected.
There is another option - you can format your string from code like this:
private static final int STR_MAX_CHAR_COUNT = 32;
private String formatString(String stringToFormat) {
if(stringToFormat.length() > STR_MAX_CHAR_COUNT){
stringToFormat = stringToFormat.substring(0, STR_MAX_CHAR_COUNT - 1) + "...";
}
return stringToFormat;
}
And then just set string like this:
String newText = "some long-long text";
mTextView.setText(formatString(newText))

EditText is not taking numeric input

I have an EditText view in my android application. My EditText is not taking integer values as an input however it is taking all the other ones. the following is my EditText View.
<EditText
android:id="#+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true">
</EditText>
On the possible issue that i faced once and suspects is: The EditText might be contained in an XML layout file and the layout file will be used for a Dialog instance. The problem occurs when the onKeyListener of the Dialog instance is returning some invalid default value i.e. 'return true' for irrelevant keys. In such case your EditText will be behaving strangely. The sample code for the scenario is
Dialog d=new Dialog(context);
d.setContentView(R.layout.mylayout);
d.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
Log.i("onkey", keyCode + " == Onkey");
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return true; // !!! this is wrong it should be return false;
}
});
}
Try this?
android:inputType="number"
android:digits="0123456789"
If it works, you can add letters in with the numbers in digits that you want to allow. Here's a handy list of all the inputTypes you could try. They mostly change what appears on the soft keyboard.
You need to add android:inputType="number" in your xml just like follows,
<EditText
android:id="#+id/txtSearchItems"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:visibility="visible"
android:hint="Search"
android:textSize="14dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:inputType="number"> <------------- this line added
</EditText>

Android RequestFocus() Ineffective

I want the EditText in my application to have the cursor by default when the application starts.
I tried using
<EditText
android:id="#+id/idInput"
android:layout_width="480dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:ems="10" >
<requestFocus />
</EditText>
and also
userIdInput = (EditText)findViewById(R.id.idInput);
userIdInput.setFocusable(true);
userIdInput.setFocusableInTouchMode(true);
userIdInput.requestFocus();
But none of it seems to have any effect. The cursor is nowhere visible when the application starts and I have to manually click the EditText to make the cursor appear on it.
Where could I be possibly go wrong ?
By the way, I am developing for Android 4.0.3 tablets.
Try this
userIdInput = (EditText)findViewById(R.id.idInput);
userIdInput.post(new Runnable()
{
public void run()
{
userIdInput .requestFocus();
}
});

Categories

Resources