new line when user press enter in edit text in android - android

I am trying to create edit text in which when user presses enter it goes to new line and allow user to enter more text.
here is my design part
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/discussion"
android:layout_width="match_parent"
android:layout_height="100dp"
android:inputType="textMultiLine"
android:maxLines="5"
android:hint="Discussion outcome"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "/>
</android.support.design.widget.TextInputLayout>
Here is my code, but the problem is cursor position is not going to the next line. i tried discussion.append("\n") too but it didnt work
discussion= (EditText) findViewById(R.id.discussion);
discussion.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
Log.e(TAG , "Key Action "+keyEvent.getAction());
if(keyEvent.getAction() == KeyEvent.ACTION_UP && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER){
String data =discussion.getText().toString();
discussion.setText(data+"\n");
Log.e(TAG , "Key "+keyEvent.getKeyCode());
}
return false;
}
});

You should include the newline character \n in the allowed digits in your xml:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/discussion"
android:layout_width="match_parent"
android:layout_height="100dp"
android:inputType="textMultiLine"
android:maxLines="5"
android:hint="Discussion outcome"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890\n "/>
</android.support.design.widget.TextInputLayout>
Next, you don't need any key listeners if you do that.

try this
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/edt_address"
android:hint="Shipping Address" />
hope it will help you

Try this solution: https://stackoverflow.com/a/20475011/13179254
Basically what you need to do:
Kotlin:
isSingleLine = false
Java:
setSingleLine(false)
Xml:
android:singleLine="false"

Related

onFocus function not working properly in Android ?

Hi I am newbie in android, I have 3 edit box and I have set 3 setOnFocusChangeListener based on edit box wise, When I focus mobile no edit box ,I am getting sim no id instead mobile no Id. onFocus function getting called with wrong id's. If I did any any mistake please let us know or else solve the solution.
Source code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_temp);
objMobileNo = (EditText) findViewById(R.id.mobile_no);
objSimNo = (EditText) findViewById(R.id.sim_no);
objImsiNo = (EditText) findViewById(R.id.imsi_no);
View.OnFocusChangeListener a = new View.OnFocusChangeListener() {
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
//this if condition is true when edittext lost focus...
//check here for number is larger than 10 or not
// focusText = "MOBILE";
// Log.d("BKS", "onFocusChange: " + focusText);
Log.d("BKS", "onFocusChange: " + view.getId());
Log.d("BKS", "onFocusChange: " + R.id.mobile_no);
}
}
};
objMobileNo.setOnFocusChangeListener(a);
objSimNo.setOnFocusChangeListener(a);
objImsiNo.setOnFocusChangeListener(a);
XML Code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_marginTop="100dp"
android:id="#+id/mobile_no"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_mob_no"
android:inputType="number"
android:maxLength="10" />
<EditText
android:id="#+id/sim_no"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_sim_no"
android:inputType="number"
android:maxLength="20" />
<EditText
android:id="#+id/imsi_no"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_imsi_no"
android:inputType="number"
android:maxLength="15" />
<EditText
android:id="#+id/status"
style="#style/Material_Action"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:visibility="gone" />
<EditText
android:id="#+id/scanning_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:visibility="gone" />
<LinearLayout
android:id="#+id/mLlayoutBottomButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<Button
android:id="#+id/verify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_green_light"
android:text="Verify"
android:textColor="#android:color/white">
</Button>
</LinearLayout>
Thanks in Advance,
Kalanidhi.
You are setting same OnFocusChangeListener for all EditText. so when you focus a new EditText, the current one loses its focus. So for both the events onFocusChange() will be called. But your if condition filters only the view that loses the focus and not the one which got the focus.
So replace
if (!hasFocus) {
}
with this to make it work as expected
if (hasFocus) {
}

Android EditText with icon/button

How can I obtain an EditText as those bellow?
The x button removes the text inserted and the eye button displays the clear password as long as it's pressed. Note that I refer to them as buttons because I don't really know what they actually are nor if they are part of the EditText itself or if they are independent views.
You can just add a button at the right of your EditText
<FrameLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ScreenLoginContainer">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/edit_text_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:hint="#string/enter_password_hint"
android:paddingRight="30dp"
style="#style/ScreenPasswordEditText"/>
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+screen_card_ean_enter/show_password_button"
style="#style/ShowHidePasswordButton"/>
</FrameLayout>
You can use android:drawableRight
Set a Drawable to the right of the text, in EditText, in XML .
android:drawableRight="#drawable/Your_drawable"
You can use this :
android:drawableRight="#drawable/your_icon"
Also for click this answer
may help
This is the prefect way to create an EditText with cross button at the end:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp">
<EditText
android:id="#+id/calc_txt_Prise"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:layout_marginTop="20dp"
android:textSize="25dp"
android:textColor="#color/gray"
android:textStyle="bold"
android:hint="#string/calc_txt_Prise"
android:singleLine="true" />
<Button
android:id="#+id/calc_clear_txt_Prise"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_gravity="right|center_vertical"
android:background="#drawable/delete" />
And now to clear the EditText upon click you can use
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditTextID.setText("");
}
});
And in order to make the password visible till the button is pressed, you can implement it this way:
yourButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch ( event.getAction() ) {
case MotionEvent.ACTION_DOWN:
editText.setInputType(InputType.TYPE_CLASS_TEXT);
break;
case MotionEvent.ACTION_UP:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
break;
}
return true;
}
});
The design support library has a setPasswordVisibilityToggleEnabled method: https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html#setPasswordVisibilityToggleEnabled(boolean)
This would be good for your password edittext, but not the username one.

Move to next Edit text

i have customEditText
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/five_margin"
android:focusableInTouchMode="true"
android:orientation="horizontal"
app:hintTextAppearance="#style/TextAppearence.App.TextInputLayout"
>
<EditText
android:id="#+id/edt_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="#dimen/sub_heading"
android:textColorHint="#color/hind_text_col0r"
android:inputType="textCapSentences|textAutoCorrect|textMultiLine"
android:textColor="#color/active_text_col0r"
/>
</android.support.design.widget.TextInputLayout>
and using it As follow
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.paisa.main.base.ui.widget.CustomEditText
android:id="#+id/custom_edt_investor_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
/>
<com.paisa.main.base.ui.widget.CustomEditText
android:id="#+id/custom_edt_nominee_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
how can i move investor name to nominee name edit text via soft input keyboard
i had use imeoption next bt it didn't work its hide the keyboard.
This will helps you :- make this for all TextView ...
custom_edt_investor_name.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
custom_edt_investor_name.clearFocus();
custom_edt_nominee_name.requestFocus();
return true;
}
return false;
}
});
Use the android:nextFocusDown param to specify which field you'd like to get focus when the user hits the enter key.
Example :
android:inputType="text"
android:nextFocusDown="#+id/..."

Android: EditText NextFocusDown not trigger Spinner

In my application I have one registration screen, There are lot of EditTexts and Spinners.
I want to go through the Registrations field one by one.
So I applied android:imeOptions="actionNext" . But It ignores all the Spinners. It will give focus only to EditText. I have also tried setNextFocusDownId(). This is also ignore the spinners.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportentry11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/numbers"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="phone"
>
<requestFocus/>
</EditText>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportentry12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txt_exp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="EXP:"
android:textColor="#000000"
android:textSize="12dp" />
<Spinner
android:id="#+id/spin_date"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusDown="#+id/spin_year"
android:text="date"
android:textSize="12dp" />
<Spinner
android:id="#+id/spin_year"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginRight="5dp"
android:nextFocusDown="#+id/cvc"
android:text="year"
android:textSize="12dp" />
</LinearLayout>
<EditText
android:id="#+id/cvc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/reg_label_cvc"
android:imeOptions="actionNext"
android:inputType="phone" />
<EditText
android:id="#+id/fname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/reg_label_fname"
android:imeOptions="actionNext" />
<EditText
android:id="#+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/reg_label_address"
android:imeOptions="actionNext" />
<EditText
android:id="#+id/city"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/reg_label_city"
android:imeOptions="actionNext"
android:nextFocusDown="#+id/pr_spin" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/reportentry13"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/txt_pr"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="PROV:"
android:textColor="#000000"
android:textSize="12dp" />
<Spinner
android:id="#+id/pr_spin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="date"
android:imeOptions="actionNext"
android:textSize="14dp" />
<EditText
android:id="#+id/pcode"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hint="#string/reg_label_pcode"
android:imeOptions="actionDone" />
</LinearLayout>
<Button
android:id="#+id/register_register_button"
android:layout_width="wrap_content"
android:background="#drawable/green_button_bg"
android:onClick="completeClicked"
android:text="#string/reg_label_complete"
android:textSize="28dp"
android:textStyle="bold" />
</LinearLayout>
Please provide me the best way to trigger the spinners.
On your edit texts, override onEditorAction and give focus (or do whatever like open your spinner)...
yourEditTXT.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionID, KeyEvent event) {
if (actionID == EditorInfo.IME_ACTION_NEXT) {
//do your stuff here...
return true;
}
return false;
}
});
Edit 12/4:
I see you were still struggling with this as of last night, if you didn't find a solution (and didn't post one) or to help others who may read this, this may help to get from spinner to edit text.
mySpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parentview, View v, int position, long id) {
// your spinner proces code
// then when you are done,
yourEditText.setFocusableInTouchMode(true); //if this is not already set
yourEditText.requestFocus(); //to move the cursor
final InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT); // this line and the one above will open the soft keyboard if it doesn't already open
}
public void onNothingSelected(AdapterView<?> arg0) { }
};
do the following in your xml file:
<Spinnner
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusDown="#+id/youedittextid"
/>
Apart from the solutions above to focus on Spinners, here are further troubles with ime options:
If the spinners are between the EditTexts, then the EditTexts that are below the spinners ignore imeOption="actionDone" and lines="1" attributes. In both the cases they accept enter keys and move to next line, when, it should not allow the enter key event.
I found that though the listener for ime was still intact, the ime option set to whatever (next or done), it changed to 0.
So my answer is for second part while I am still trying to make spinners focusable:
xml:
<EditText
android:id="#+id/et_1"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_40"
android:layout_marginLeft="#dimen/dp_8"
android:layout_marginStart="#dimen/dp_8"
android:gravity="start|bottom"
android:hint="#string/et_1"
android:imeOptions="actionNext"
android:inputType="textCapWords" />
<!--This ime option works fine-->
<!--lots of spinners in between these Edit texts-->
<EditText
android:id="#+id/et_2"
android:layout_gravity="start|bottom"
android:layout_marginLeft="#dimen/dp_8"
android:layout_marginStart="#dimen/dp_8"
android:gravity="start|bottom"
android:hint="#string/et_2"
android:imeOptions="actionDone" />
<!--this actionDone is ignored and set to 0-->
[value of actionNext = 5 while that of actionDone = 0, but for edit text et2 reports as 0 in the listener]
set listener:
...
EditText et_1 = view.findViewById(R.id.et_1);
fet_1.setOnEditorActionListener(this);
//==
EditText et_2 = view.findViewById(R.id.et_2);
et_2.setOnEditorActionListener(this);
...
The handling of listeners where the ime actions are ignored too:
#Override
public boolean onEditorAction(TextView textView, int actionID, KeyEvent keyEvent) {
FontEditText et_1 = getView().findViewById(R.id.et_1);
//==
EditText et_2 = getView().findViewById(R.id.et_2);
final int id = textView.getId();
switch (id) {
case R.id.et_1: {
if (actionID == EditorInfo.IME_ACTION_NEXT) {
// works actionID = 5.
//inherited method
activity.hideKeyboard(textView);
fet_bus_entity.clearFocus();
et_business_add.requestFocus();
et_business_add.performClick();
return true;
}
}
break;
case R.id.et_2: {
//following line doesn't work hence commented, value of actionID = 0 here.
// if (actionID == EditorInfo.IME_ACTION_DONE) {
et_business_add.clearFocus();
//inherited method
activity.hideKeyboard(textView);
return true;
// }
}
// break;
}
return false;
}

Android: how to make keyboard enter button say "Search" and handle its click?

I can't figure this out. Some apps have an EditText (textbox) with the help of which, when you touch it and it brings up the on-screen keyboard, the keyboard has a "Search" button instead of an enter key.
I want to implement this. How can I implement that Search button and detect the press of the Search button?
Edit: found how to implement the Search button; in XML, android:imeOptions="actionSearch" or in Java, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);. But how do I handle the user pressing that Search button? Does it have something to do with android:imeActionId?
In the layout set your input method options to search.
<EditText
android:imeOptions="actionSearch"
android:inputType="text" />
In the java add the editor action listener.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
In kotlin use below:
editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch()
}
true
}
Hide keyboard when user clicks search. Addition to Robby Pond answer
private void performSearch() {
editText.clearFocus();
InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(editText.getWindowToken(), 0);
//...perform search
}
In xml file, put imeOptions="actionSearch" and inputType="text", maxLines="1":
<EditText
android:id="#+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1" />
In Kotlin
evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
doTheLoginWork()
}
true
}
Partial Xml Code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<EditText
android:id="#+id/evLoginUserEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:textColor="#color/black_54_percent" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<EditText
android:id="#+id/evLoginPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/password"
android:inputType="textPassword"
android:imeOptions="actionDone"
android:textColor="#color/black_54_percent" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
by XML:
<EditText
android:id="#+id/search_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search"
android:imeOptions="actionSearch"
android:inputType="text" />
By Java:
editText.clearFocus();
InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
This answer is for TextInputEditText :
In the layout XML file set your input method options to your required type. for example done.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionGo"/>
</com.google.android.material.textfield.TextInputLayout>
Similarly, you can also set imeOptions to actionSubmit, actionSearch, etc
In the java add the editor action listener.
TextInputLayout textInputLayout = findViewById(R.id.textInputLayout);
textInputLayout.getEditText().setOnEditorActionListener(new
TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_GO) {
performYourAction();
return true;
}
return false;
}
});
If you're using kotlin :
textInputLayout.editText.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_GO) {
performYourAction()
}
true
}

Categories

Resources