EditText value is reversed - android

I've got an odd one. I'm trying to format some text while the user types into an EditText field. I am using the TextWatcher and responding to afterTextChanged event (code below). What is odd is after the first time this logic runs the text starts to become reversed. It seems that the Editable object contains the string backwards. Does anyone have any idea how to fix this?
_textWatcher = new TextWatcher()
{
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
}
#Override
public void afterTextChanged(Editable editable)
{
Editable e = textView.getText();
String text = e.toString();
logger.d("[FormListAdapter][addTextChangedListener] Format Text: " + text);
Number numVal = FormFieldBusiness.ConvertStringToNumber(text, formField.GetFormFieldType());
String fText = FormFieldBusiness.GetFormattedValue(numVal, formField.GetFormFieldType());
editText.removeTextChangedListener(_textWatcher);
textView.setText(text);
editText.addTextChangedListener(_textWatcher);
}
};
editText.addTextChangedListener(_textWatcher);
* UPDATE *
In an attempt to help anyone out there who's looking at this here is my XML layout file.
As I mentioned before the text is correct until after the first the time setText method is called. After that the Editable object is reversed.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="120dp">
<!-- Top Layout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="10"
android:id="#+id/TopLayout">
<TextView
android:id="#+id/TitleTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16dp"
style="#style/KelFormListItemLabel" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:weightSum="2"
android:background="#color/formListItemValueBackgroundColor">
<ImageView
android:id="#+id/CurrencyIconView"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_weight="0"
android:src="#drawable/icon_currency_pound"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp" />
<EditText
android:id="#+id/ValueTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="right|center_vertical"
android:text="20,000"
android:textSize="16dp"
android:inputType="number"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
style="#style/KelFormListItemValue"/>
</LinearLayout>
</LinearLayout>
<!-- Bottom Layout -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:weightSum="10"
android:id="#+id/BottomLayout"
android:layout_below="#+id/TopLayout">
<TextView
android:id="#+id/BottomTitleTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center_vertical"
android:text="Headling Rent (pa)"
android:textSize="16dp"
style="#style/KelFormListItemLabel" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:weightSum="2"
android:background="#color/formListItemValueBackgroundColor">
<ImageView
android:id="#+id/BottomCurrencyIconView"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_weight="0"
android:src="#drawable/icon_currency_pound"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp" />
<EditText
android:id="#+id/BottomValueTextView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="right|center_vertical"
android:text="20,000"
android:textSize="16dp"
android:inputType="number"
android:selectAllOnFocus="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:textIsSelectable="true"
style="#style/KelFormListItemValue"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>

The issue is you are setting the text in the EditText field (even though you have removed the listener) you are listening for. this is causing the cursor to move to the beginning of the line, therefore making it type backwards
try changing from a TextWatcher to TextView.OnEditorActionListener and listen for the action key set on the EditText so when they hit the action key, it does your desired functionality.
add
android:imeOptions="actionDone"
to your button xml and assign it with
TextView.OnEditorActionListener editingActionListener = new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) || actionId == EditorInfo.IME_ACTION_DONE) {
//do stuff where you set the text here
}
return true;
}
};
editText.setOnEditorActionListener(editingActionListener);
alternatively, you can use OnFocusChangeListener
View.OnFocusChangeListener focusChangeListener = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (v.getClass().equals(EditText.class)) {
if (!hasFocus) {
//do your stuff here
}
}
}
};
editText.setOnFocusChangeListener(focusChangeListener);

Simply move the cursor to the correct position in your TextWatcher after setting the EditText content:
#Override
public void afterTextChanged(Editable editable)
{
...
int position = editText.getSelectionStart();
editText.setText(text);
editText.setSelection(position);
...
}

Related

Show button when all EditTexts are filled out by user

I have this login activity, when the user clicks on either Login or Register Button, different EditTexts appear. My question is how can i check if all EditTexts are filled out so the continueButton will be enabled?
(When Login is clicked, email and password must be filled, and when register is clicked, email, password and username must be filled)
ACTIVITY_LOGIN.XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/yellow"
android:orientation="vertical"
tools:context=".LoginActivity">
<TableRow
android:id="#+id/welcomeTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8pt">
<TextView
android:id="#+id/welcomeTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/welcome_to"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="normal|bold" />
</TableRow>
<TableRow
android:id="#+id/logoTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/welcomeLogoImageView"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_span="2"
android:contentDescription="#string/todo"
android:scaleType="fitCenter"
app:srcCompat="#drawable/logo_medium" />
</TableRow>
<TableRow
android:id="#+id/loginInfoTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="12pt">
<TextView
android:id="#+id/infoEditText"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:padding="6pt"
android:text="#string/ask_to_register_or_login"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="18sp" />
</TableRow>
<TableRow
android:id="#+id/loginOrRegisterTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="4pt"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="12dp">
<Button
style="#style/Widget.AppCompat.Button.Borderless"
android:id="#+id/chooseToLoginButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="5dp"
android:layout_marginStart="10dp"
android:layout_weight="1"
android:background="#drawable/login_screen_button_unclicked"
android:onClick="chosenEvent"
android:text="#string/login" />
<Button
style="#style/Widget.AppCompat.Button.Borderless"
android:id="#+id/chooseToRegisterButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="5dp"
android:layout_weight="1"
android:background="#drawable/login_screen_button_unclicked"
android:onClick="chosenEvent"
android:text="#string/register" />
</TableRow>
<TableRow
android:id="#+id/nameTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:background="#android:color/white"
android:padding="4pt"
android:visibility="visible"
android:weightSum="1">
<TextView
android:id="#+id/nameTextView"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.3"
android:paddingEnd="2pt"
android:paddingStart="2pt"
android:text="#string/name"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="#+id/nameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:ems="10"
android:hint="#string/name"
android:inputType="textPersonName" />
</TableRow>
<TableRow
android:id="#+id/emailTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:background="#android:color/white"
android:padding="4pt"
android:visibility="visible"
android:weightSum="1">
<TextView
android:id="#+id/emailTextView"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.3"
android:paddingEnd="2pt"
android:paddingStart="2pt"
android:text="#string/email"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="#+id/emailEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:ems="10"
android:hint="#string/email"
android:inputType="textEmailAddress" />
</TableRow>
<TableRow
android:id="#+id/passwordTableRow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:background="#color/white"
android:padding="4pt"
android:visibility="visible"
android:weightSum="1">
<TextView
android:id="#+id/passwordTextView"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="0.3"
android:paddingEnd="2pt"
android:paddingStart="2pt"
android:text="#string/password"
android:textAlignment="textEnd"
android:textColor="#color/black"
android:textSize="18sp"
android:textStyle="bold" />
<EditText
android:id="#+id/passwordEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword" />
</TableRow>
<TableRow
android:id="#+id/continueButtonTableRow"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="8pt"
android:visibility="visible">
<Button
android:id="#+id/continueButton"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="80dp"
android:layout_marginStart="80dp"
android:background="#drawable/login_screen_button_unclicked"
android:enabled="false"
android:text="#string/continue_button"
android:visibility="visible" />
</TableRow>
</TableLayout>
LOGIN ACTIVITY:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import kotlinx.android.synthetic.main.activity_login.*
LoginActivity:
class LoginActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
}
private fun unclickAllChooseButtons() {
chooseToLoginButton.setBackgroundResource(R.drawable.login_screen_button_unclicked)
chooseToRegisterButton.setBackgroundResource(R.drawable.login_screen_button_unclicked)
}
private fun showEmailRow() {
emailTableRow.visibility = View.VISIBLE
}
private fun showNameRow() {
nameTableRow.visibility = View.VISIBLE
}
private fun showPasswordRow() {
passwordTableRow.visibility = View.VISIBLE
}
private fun hideAllRows() {
emailTableRow.visibility = View.GONE
passwordTableRow.visibility = View.GONE
nameTableRow.visibility = View.GONE
}
fun chosenEvent(view: View) {
when (view.id) {
R.id.chooseToLoginButton -> {
unclickAllChooseButtons()
chooseToLoginButton.setBackgroundResource(R.drawable.login_screen_button_clicked)
hideAllRows()
showEmailRow()
showPasswordRow()
}
R.id.chooseToRegisterButton -> {
unclickAllChooseButtons()
chooseToRegisterButton.setBackgroundResource(R.drawable.login_screen_button_clicked)
hideAllRows()
showNameRow()
showEmailRow()
showPasswordRow()
}
else -> {
unclickAllChooseButtons()
}
}
}
}
This is a hard one because what you need to do is to enable the button in real-time. So you have to listen each EditText and once a condition is met on the written text then you should set the button as enabled. There is another complexity here, which it is: what happened if the user goes back and then try to delete some text. There are 2 ways to solve this last issue, the first is to save a state of each EditText and the second is to also validate the data to be sent.
//You have to set initial state as false representing conditione haven'met
//Otherwise when you try to get the tag is gonna be null
boolean initialValidity = false;
emailEt.setTag(initialValidity);
passEt.setTag(initialValidity);
emailEt.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) {
}
#Override
public void afterTextChanged(Editable editable) {
String email = emailEt.getText().toString();
//You can improve this condition
if (email.trim().length() > 0 && email.contains("#") && email.contains(".")) {
emailEt.setTag(true);
if ((boolean) passEt.getTag()) {
sendButton.setEnabled(true);
}
} else {
emailEt.setTag(false);
sendButton.setEnabled(false);
}
}
});
passEt.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) {
}
#Override
public void afterTextChanged(Editable editable) {
String password = passEt.getText().toString();
//You can improve this condition
if (!password.contains(" ") && password.length() > 6) {
passEt.setTag(true);
if ((boolean) emailEt.getTag()) {
sendButton.setEnabled(true);
}
} else {
passEt.setTag(false);
sendButton.setEnabled(false);
}
}
});
sendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String email = emailEt.getText().toString();
String password = passEt.getText().toString();
//TODO add validation to send the data
//TODO send the data
}
});
What we are doing here is listening to both EditText and while the user is writting we check if the conditions are met. Both EditText while checking their own condition and also the other condition. This crossed checking allow us to enable or disable the Button if any EditText breaks the clauses in the conditions. Each EditText can know if the other has met the needed conditions by checking the tag which is a boolean representing if the conditions are met or not. I'm using enabled as an example, you could hide using setVisibility()
This is all the help you can get, cause you didn't include any code of yours. Also, my code is in Java, you should get the idea.
if(!edittext1.equals("") && !edittext2.equals("")){
// enable button
}
You must have different methods to validate that your forms are full, and finish with these validations:
public boolean validatePassword(String password){if(!password.equals("")){
if(password.length() == 6){ //Valid if the password is of certain sizes
this.password = password;
return true;
}
else {
Toast.makeText(this, "Length invalid", Toast.LENGTH_SHORT).show();
return false;
}
}
else {
Toast.makeText(this, "Please enter your password", Toast.LENGTH_SHORT).show();
return false;
}
}}
Method for each form.
You get the form data in the following way:
String pass = youreditText.getText().toString();
finally:
if(validatePassword(password)){ if(validateOtherForm(text){
//What you want to do
}}

Make character count visible during input

I have an EditText used for a description.
Below it, I have a TextView showing the number of characters inputted in the EditText.
Example:
The user should be able to see the live character count during the input, but at this moment the characted counter is hidden by the keyboard:
What can I do to correct that ?
Here is my xml code:
<EditText
android:id="#+id/MyDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:maxLength="500"
android:hint="Description" />
<TextView
android:id="#+id/MyDescriptionCharCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0/500"
android:layout_below="#id/MyDescription"
android:layout_marginTop="5dp"
android:layout_marginRight="3dp"
android:layout_marginEnd="3dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
The parent is a RelativeLayout.
You have to extend the class with TextWatcher and override afterTextChanged(),beforeTextChanged(), onTextChanged().
EditText MyDescription = (EditText)findViewById(R.id. MyDescription);
MyDescription.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// s.toString().trim().length();
}
});
You can try the following layout.
<RelativeLayout
android:id="#+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginLeft="16dp"
android:background="#android:color/transparent"
android:inputType="textNoSuggestions|textMultiLine"
android:maxLength="200"
android:paddingBottom="8dp"
android:paddingRight="5dp"
android:paddingTop="8dp"
android:textColor="#202020"
android:textColorHint="#979797"
android:textSize="14sp"/>
<TextView
android:id="#+id/charecter_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/add_comment_edit_text"
android:layout_gravity="bottom"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:gravity="bottom"
android:text="0/200"
android:textColor="#979797"
android:textSize="14sp"/>
</RelativeLayout>
To see Textview of character counter, add below line in activity tag of manifest file.
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
This will automatically move your activity layout up to fix in available height after adding soft input keyboard.For more detail about this check here.

Drawable gets changed if clicked twice on edit text

I have two edit text views on which I have added two drawables. One is edit_text_bottom_line and another is edit_drawable.
Now I want to show edit_text_bottom_line drawable onClick of an edit text input.So I can see the layout like this :
This worked. Now Again I want to show another drawable when the we again click on edit text input and floating hint appears that time it should look like this:
This also works. But now if I again click on edit text view then it changes the layout edit_text_bottom_line drawable appears like this:
I want to show blue drawable when user starts typing and till he is on that edit text view and after if edit text is focused.
How can I find out if edit text is focused? I tried from onFocusChangeListener if the edit text is focused show the blue drawable but it gets dispappear on next click.
Layout:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kiranacustomerapp.Activities.SearchActivity"
tools:showIn="#layout/activity_search">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="380dp"
android:background="#android:color/white"
android:orientation="vertical"
android:id="#+id/linearLayoutContainer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linearp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content"
android:id="#+id/linear1"
android:layout_marginTop="10dp">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg"
android:id="#+id/linearLayoutSpinner"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:id="#+id/recyclerview"
android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/editTextItemName"
android:layout_gravity="center_horizontal|center_vertical"
android:hint="#string/item_name"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="07dp"
android:textSize="14sp"
android:imeOptions="actionDone"
android:background="#drawable/edit_text_bottom_line"
android:textStyle="bold"
android:inputType="text"
android:textColor="#color/lighttextcolor">
</android.support.design.widget.TextInputEditText>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear2"
android:layout_below="#+id/linear1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg"
android:id="#+id/linearLayoutUnits"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:id="#+id/recyclerviewUnits"
android:layout_margin="08dp"></android.support.v7.widget.RecyclerView>
</LinearLayout>
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:focusable="false"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="07dp"
android:id="#+id/editTextItemUnit"
android:layout_gravity="center_horizontal"
android:hint="#string/unit"
android:textSize="14sp"
android:textStyle="bold"
android:imeOptions="actionDone"
android:background="#drawable/edit_text_bottom_line"
android:inputType="text"
android:textColor="#color/lighttextcolor" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear3"
android:layout_below="#+id/linear2">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="02dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="05dp"
android:id="#+id/editTextItemQuantity"
android:layout_gravity="center_horizontal"
android:hint="#string/quantity"
android:textSize="14sp"
android:imeOptions="actionDone"
android:textColorHint="#color/grey"
android:textStyle="bold"
android:background="#drawable/edit_text_bottom_line"
android:inputType="number"
android:textColor="#color/lighttextcolor" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true"
android:layout_below="#+id/linearp"
android:layout_marginTop="130dp">
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Select"
style="?android:attr/borderlessButtonStyle"
android:id="#+id/buttonSelect"
android:background="#drawable/btn_hlf_blue"
android:textColor="#android:color/white"
android:textSize="12sp"
android:layout_alignParentBottom="false"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
code:
containerLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_Name.setFocusable(false);
edt_Item_Unit.setFocusable(false);
edt_Item_quantity.setFocusable(false);
linearLayoutRecycleView.setVisibility(View.GONE);
linearLayoutUnits.setVisibility(View.GONE);
textInput_Item_quantity.setVisibility(View.VISIBLE);
textInput_Item_Unit.setVisibility(View.VISIBLE);
textInput_Item_Unit.setBackgroundResource(0);
textInput_Item_name.setBackgroundResource(0);
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Name.setText("");
edt_Item_quantity.setText("");
edt_Item_Unit.setText("");
}
});
edt_Item_Name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
}
}
});
edt_Item_Unit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
}
}
});
edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_drawable));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
}
});
edt_Item_Name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_Name.setFocusableInTouchMode(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
mRecyclerView.setAdapter(mAdapter);
edt_Item_Name.setText("");
edt_Item_Name.setBackgroundResource(R.drawable.edit_text_bottom_line);
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Name.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));
linearLayoutRecycleView.setVisibility(View.VISIBLE);
linearLayoutUnits.setVisibility(View.GONE);
textInput_Item_quantity.setVisibility(View.GONE);
textInput_Item_Unit.setVisibility(View.GONE);
textInput_Item_name.setBackgroundResource(R.drawable.purple_bg);
textInput_Item_Unit.setBackgroundResource(0);
}
});
edt_Item_Name.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchActivity.this.mAdapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
edt_Item_Unit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_Unit.setFocusable(true);
edt_Item_Name.setFocusable(false);
edt_Item_Unit.setFocusableInTouchMode(true);
edt_Item_Name.setBackgroundResource(0);
mUnitsRecyclerView.setLayoutManager(new LinearLayoutManager(SearchActivity.this));
mUnitsRecyclerView.setAdapter(mUnitsAdapter);
edt_Item_Unit.setText("");
edt_Item_Unit.setBackgroundResource(R.drawable.edit_text_bottom_line);
edt_Item_Unit.setHintTextColor(ContextCompat.getColor(SearchActivity.this,R.color.grey));
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
linearLayoutUnits.setVisibility(View.VISIBLE);
linearLayoutRecycleView.setVisibility(View.GONE);
textInput_Item_quantity.setVisibility(View.GONE);
textInput_Item_Unit.setBackgroundResource(R.drawable.purple_bg);
}
});
edt_Item_Unit.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchActivity.this.mUnitsAdapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
edt_Item_quantity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
edt_Item_quantity.setFocusableInTouchMode(true);
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this,R.drawable.edit_text_bottom_line));
}
});
edt_Item_quantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b) {
edt_Item_Name.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_Unit.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_text_bottom_line));
edt_Item_quantity.setBackgroundDrawable(ContextCompat.getDrawable(SearchActivity.this, R.drawable.edit_drawable));
}
else {
}
}
});
Or rather applying two layouts, I have set the accent color as blue in the theme so by default the edit text takes blue color if focused that's fine but when I change the drawable of grey line after that how can I go to default blue line of an edit text?
Please help, thank you..
Just do one thing make your input text layout like this:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="#android:style/TextAppearance.Small">
<EditText
android:id="#+id/user_name"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="#dimen/_10sdp"
android:hint="#string/full_name"
android:inputType="text"
android:background="#android:color/transparent"
/>
</android.support.design.widget.TextInputLayout>
And it will solve your problem.
Change your xml TextInputLayout and TextInputEditText with this:
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="02dp"
android:textColorHint="#color/grey">
<android.support.design.widget.TextInputEditText
android:id="#+id/editTextItemQuantity"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="05dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:hint="#string/quantity"
android:imeOptions="actionDone"
android:inputType="number"
android:textColor="#color/lighttextcolor"
android:textSize="14sp"
android:textStyle="bold"
android:theme="#style/style_edittext_color_control"/>
</android.support.design.widget.TextInputLayout>
In your res/values/styles.xml add this:
<style name="style_edittext_color_control" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/grey</item>
<item name="colorControlActivated">#color/blue</item>
</style>
Hope this will give you proper result.

Custom Keyboard is hiding the Edit box in Android

I have designed a custom key board with only numeric keys. I have followed the below link:
example
Now when i am touching the edit box, key board is appearing. But if i have 10 editboxes, and i am touching the 10th edit box, key board is appeared and hiding the edit box.How can i make the edit box will scroll up automatically so that it will be not hidden.
I have written the below layout code for xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext0"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext2"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
</RelativeLayout>
</ScrollView>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
Also here is my custom keyboard java class:
class CustomKeyboard {
/** A link to the KeyboardView that is used to render this CustomKeyboard. */
private KeyboardView mKeyboardView;
/** A link to the activity that hosts the {#link #mKeyboardView}. */
private Activity mHostActivity;
/** The key (code) handler. */
private OnKeyboardActionListener mOnKeyboardActionListener = new OnKeyboardActionListener() {
public final static int CodeDelete = -5; // Keyboard.KEYCODE_DELETE
public final static int CodePrev = 55000;
public final static int CodeNext = 55001;
public final static int CodeDone = 55002;
#Override
public void onKey(int primaryCode, int[] keyCodes) {
// NOTE We can say '<Key android:codes="49,50" ... >' in the xml
// file; all codes come in keyCodes, the first in this list in
// primaryCode
// Get the EditText and its Editable
View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
if (focusCurrent == null
|| focusCurrent.getClass() != EditText.class)
return;
EditText edittext = (EditText) focusCurrent;
Editable editable = edittext.getText();
int start = edittext.getSelectionStart();
// Apply the key to the edittext
if (primaryCode == CodeDone) {
hideCustomKeyboard();
}
else if (primaryCode == CodeDelete)
{
if (editable != null && start > 0)
editable.delete(start - 1, start);
}
else if (primaryCode == CodePrev) {
View focusNew = edittext.focusSearch(View.FOCUS_BACKWARD);
if (focusNew != null)
focusNew.requestFocus();
}
else if (primaryCode == CodeNext) {
View focusNew = edittext.focusSearch(View.FOCUS_FORWARD);
if (focusNew != null)
focusNew.requestFocus();
}
else { // insert character
editable.insert(start, Character.toString((char) primaryCode));
}
}
#Override
public void onPress(int arg0) {
}
#Override
public void onRelease(int primaryCode) {
}
#Override
public void onText(CharSequence text) {
}
#Override
public void swipeDown() {
}
#Override
public void swipeLeft() {
}
#Override
public void swipeRight() {
}
#Override
public void swipeUp() {
}
};
/**
* Create a custom keyboard, that uses the KeyboardView (with resource id
* <var>viewid</var>) of the <var>host</var> activity, and load the keyboard
* layout from xml file <var>layoutid</var> (see {#link Keyboard} for
* description). Note that the <var>host</var> activity must have a
* <var>KeyboardView</var> in its layout (typically aligned with the bottom
* of the activity). Note that the keyboard layout xml file may include key
* codes for navigation; see the constants in this class for their values.
* Note that to enable EditText's to use this custom keyboard, call the
* {#link #registerEditText(int)}.
*
* #param host
* The hosting activity.
* #param viewid
* The id of the KeyboardView.
* #param layoutid
* The id of the xml file containing the keyboard layout.
*/
public CustomKeyboard(Activity host, int viewid, int layoutid) {
mHostActivity = host;
mKeyboardView = (KeyboardView) mHostActivity.findViewById(viewid);
mKeyboardView.setKeyboard(new Keyboard(mHostActivity, layoutid));
mKeyboardView.setPreviewEnabled(false); // NOTE Do not show the preview
// balloons
mKeyboardView.setOnKeyboardActionListener(mOnKeyboardActionListener);
// Hide the standard keyboard initially
mHostActivity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
/** Returns whether the CustomKeyboard is visible. */
public boolean isCustomKeyboardVisible() {
return mKeyboardView.getVisibility() == View.VISIBLE;
}
/**
* Make the CustomKeyboard visible, and hide the system keyboard for view v.
*/
public void showCustomKeyboard(View v) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if (v != null)
((InputMethodManager) mHostActivity
.getSystemService(Activity.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
/** Make the CustomKeyboard invisible. */
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
}
/**
* Register <var>EditText<var> with resource id <var>resid</var> (on the
* hosting activity) for using this custom keyboard.
*
* #param resid
* The resource id of the EditText that registers to the custom
* keyboard.
*/
public void registerEditText(int resid) {
// Find the EditText 'resid'
EditText edittext = (EditText) mHostActivity.findViewById(resid);
// Make the custom keyboard appear
edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
// NOTE By setting the on focus listener, we can show the custom
// keyboard when the edit box gets focus, but also hide it when the
// edit box loses focus
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
showCustomKeyboard(v);
else
hideCustomKeyboard();
}
});
edittext.setOnClickListener(new OnClickListener() {
// NOTE By setting the on click listener, we can show the custom
// keyboard again, by tapping on an edit box that already had focus
// (but that had the keyboard hidden).
#Override
public void onClick(View v) {
showCustomKeyboard(v);
}
});
// Disable standard keyboard hard way
// NOTE There is also an easy way:
// 'edittext.setInputType(InputType.TYPE_NULL)' (but you will not have a
// cursor, and no 'edittext.setCursorVisible(true)' doesn't work )
edittext.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
EditText edittext = (EditText) v;
int inType = edittext.getInputType(); // Backup the input type
edittext.setInputType(InputType.TYPE_NULL); // Disable standard
// keyboard
edittext.onTouchEvent(event); // Call native handler
edittext.setInputType(inType); // Restore input type
return true; // Consume touch event
}
});
// Disable spell check (hex strings look like words to Android)
edittext.setInputType(edittext.getInputType()
| InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}
}
Help me please!!
Thanks,
Arindam
I had the exact same problem and after a full days research i have finally solved it.
Here is how:
I have nearly identical xml layout as yours but i have multiple KeyboardViews because i need different keyboards for different EditTexts.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="HardcodedText,TextFields" >
<ScrollView
android:id="#+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:paddingBottom="16dp">
<TextView
android:id="#+id/DecLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Decimal" />
<EditText
android:id="#+id/DecText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/HexLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Hexadecimal" />
<EditText
android:id="#+id/HexText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/BinaryLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Binary" />
<EditText
android:id="#+id/BinaryText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/OctalLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Octal" />
<EditText
android:id="#+id/OctalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_gravity="fill" />
</LinearLayout>
</ScrollView>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_hex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_dec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_oct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview_bin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:keyBackground="#drawable/btn_keyboard_key_ics"
android:visibility="gone" />
It is not enough. I also needed to set ScrollView's android:layout_above property when the KeyboardView is shown and i did this with an interface:
public interface OnKeyboardStateChangedListener
{
public void OnDisplay(View currentview, KeyboardView currentKeyboard);
public void OnHide(KeyboardView currentKeyboard);
}
And implemented this interface to my MainActivity to edit ScrollView's properties:
public class MainActivity extends Activity BaseKeyboard.OnKeyboardStateChangedListener
Also i passed MainActivity as a parameter and saved as OnKeyboardStateChangedListener:
private OnKeyboardStateChangedListener mStateListener;
public BaseKeyboard(Activity host, int viewid, int layoutid, OnKeyboardStateChangedListener listener) {
..
..
mStateListener = listener;
}
And invoked it when displaying/hiding the keyboard:
public void showCustomKeyboard( View v ) {
mKeyboardView.setVisibility(View.VISIBLE);
mKeyboardView.setEnabled(true);
if( v!=null ) {
mStateListener.OnDisplay(v, mKeyboardView);
((InputMethodManager)mHostActivity.getSystemService(Activity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
public void hideCustomKeyboard() {
mKeyboardView.setVisibility(View.GONE);
mKeyboardView.setEnabled(false);
mStateListener.OnHide(mKeyboardView);
}
And finally i edited the parameters in the MainActivity:
#Override
public void OnDisplay(View currentview, KeyboardView currentKeyboard) {
ScrollView mScroll = (ScrollView) findViewById(R.id.scroll_content);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, currentKeyboard.getId());
mScroll.setLayoutParams(params);
mScroll.scrollTo(0, currentview.getBaseline()); //Scrolls to focused EditText
}
#Override
public void OnHide(KeyboardView currentKeyboard) {
ScrollView mScroll = (ScrollView) findViewById(R.id.scroll_content);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mScroll.setLayoutParams(params);
}
Here are the screenshots:
How it should look without keyboard
With a keyboard displayed
Notes:
I have LinearLayout instead of RelativeLayout inside ScrollView because when my keyboard is shown, ScrollView works as needed but it cuts the most bottom View in half and padding or margin doesn't work with RelativeLayout in this situation but adding a paddingBottom to LinearLayout pushes it back and all of the View is visible. I couldn't find another way to fix this.
You should try different size of paddings to satisfy your needs.
I can provide full source code and additional info if you need.
Did check it and working fine.
Your main layout is a RelativeLayout which gives whole a relative layout and so everything gets change relatively in view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext0"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext0"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext2"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
<EditText
android:id="#+id/edittext4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edittext3"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:inputType="text" />
</RelativeLayout>
</ScrollView>
<android.inputmethodservice.KeyboardView
android:id="#+id/keyboardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="gone" />
</RelativeLayout>
Check this output I just tried
Img1 = http://i44.tinypic.com/2412r0n.png
Img2 = enter link description here

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;
}

Categories

Resources