I want EditText to temporarily not gain foucs.
Scenario is : I have two EditTexts. Whenever any EditText's text is changed, I want another EditText to not to respond to user clicks until another predefined event occurs.
To achieve that I tried this :
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume");
registerBroadcastReceivers();
}
// Register BroadcastReceivers
private void registerBroadcastReceivers() {
Log.d(TAG, "Registering broadcast receivers.");
// Google response receiver
googleResponseReceiver = new GoogleAPIResponseReceiver();
IntentFilter googleResponseIntentFilter = new IntentFilter(
RS_GoogleApiIntentService.ACTION_RECEIVED_GOOGLE_RESPONSE);
googleResponseIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
LocalBroadcastManager.getInstance(this).registerReceiver(
googleResponseReceiver, googleResponseIntentFilter);
}
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// Set EditText non-focusable
MyActivity.this.editText2.setFocusableInTouchMode(false);
}
});
.
.
.
// Somewhere in one function I call below function to start an IntentService.
.
.
.
// Call Google API with given URL
private void CallGoogleApiWithUrl(String url, int requestId) {
Intent intent = new Intent(this, RS_GoogleApiIntentService.class);
intent.putExtra(RS_GoogleApiIntentService.EXTRA_URL_STRING, url);
intent.putExtra(RS_GoogleApiIntentService.EXTRA_GOOGLE_API_REQUEST_ID,
requestId);
startService(intent);
}
// Broadcast receiver for Google API response
public class GoogleAPIResponseReceiver extends BroadcastReceiver {
// Enabling EditText works up to this point.
#Override
public void onReceive(Context context, Intent intent) {
// Stops working now onwards.
}
}
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Set EditText focusable
MyActivity.this.editText2.setFocusableInTouchMode(true);
}
Layout :
<LinearLayout
style="#style/create_trip_activity_components_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<EditText
android:id="#+id/from_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:ems="10"
android:hint="#string/from_location_hint"
android:imeOptions="actionDone"
android:imeActionLabel="Done"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/use_current_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="useCurrentLocation"
android:text="#string/use_current"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:contentDescription="#string/swap_button_description"
android:onClick="swapLocations"
android:scaleType="fitStart"
android:src="#drawable/swap" />
<EditText
android:id="#+id/to_location"
style="#style/create_trip_activity_components_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:gravity="left"
android:hint="#string/to_location_hint"
android:imeOptions="actionDone"
android:imeActionLabel="Done"
android:inputType="text" >
</EditText>
<LinearLayout
style="#style/create_trip_activity_components_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center_vertical"
android:orientation="horizontal" >
<EditText
android:id="#+id/departuretime_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:hint="#string/departure_date_hint"
android:inputType="datetime" >
</EditText>
<EditText
android:id="#+id/departuretime_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center"
android:hint="#string/departure_time_hint"
android:inputType="datetime" >
</EditText>
</LinearLayout>
<Button
android:id="#+id/pick_match_create_trip"
style="#style/big_centered_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:onClick="pickMatchesButtonClicked"
android:text="#string/pick_your_matches" />
<TextView
style="#style/create_trip_activity_components_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="75dp"
android:text="#string/current_location_textlabel"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/current_location_text"
style="#style/create_trip_activity_components_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginTop="5dp"
android:text=""
android:textAppearance="?android:attr/textAppearanceSmall" />
<ListView
android:id="#+id/places_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:cacheColorHint="#00FFFF"
android:background="#color/white" >
</ListView>
EditText never gains focus again. Disabling focus works but enabling it again does not. I also tried using editText2.setEnabled(); method. It did not work, too.
Its really Simple my friend.
You can try something like below:
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// Set EditText non-focusable
editText2.setEnable(false);
editText2.setClickable(false);
}
});
.
. // some work
.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Set EditText focusable
editText2.setEnable(true);
editText2.setClickable(true);
}
Just update your code something like above and try. that will surly works. if not then let me know, i will help you.
Enjoy Coding... :)
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
editText2.setEnable(false);
editText2.setClickable(false);
}
}, 20);
try this..
editText2.clearFocus();
You set editText2.requestFocus(); when ever you want to focus on EditText.
write it in manifest file of activity, It defocus the edittext by creation activity at first time. and re gain the focus by touching on Edit text
android:windowSoftInputMode="adjustPan|stateHidden"
you may use the following code and manipulate accordingly by java.
`<EditText ...
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false">
</EditText>'
Related
I'm on a working on a project on Android Studio. Then I would like to implement a button that generate a view above him. And store it's text data in an ArrayList.
This is the java code:
addClassBtn.setOnClickListener((v)->{
View cricketerView = getLayoutInflater().inflate(R.layout.check_box,categoryList,false);
EditText checkBox_title = (EditText)cricketerView.findViewById(R.id.checkBox_title);
switch (appPreference.getInt("lang",0)){
case 0:checkBox_title.setHint(getResources().getString(R.string.en_en_editBox_Text));break;
case 1:checkBox_title.setHint(getResources().getString(R.string.ch_tw_editBox_Text));break;
default:checkBox_title.setHint("Error");
}
temporaryClass.add(checkBox_title.getText().toString());
checkBox_title.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
temporaryClass.remove(checkBox_title.getText().toString());
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
temporaryClass.add(checkBox_title.getText().toString());
}
});
CheckBox checkBox_check = (CheckBox)cricketerView.findViewById(R.id.checkBox_check);
checkBox_check.setOnClickListener((w)->{
});
Button checkBox_delete = (Button)cricketerView.findViewById(R.id.checkBox_delete);
checkBox_delete.setOnClickListener((w)->{
categoryList.removeView(w);
});
categoryList.addView(cricketerView);
});
And this is the XML Layout code of the view that I would like to add above the button:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/checkBox_title"
android:layout_width="295dp"
android:layout_height="40dp"
android:layout_marginStart="13dp"
android:layout_marginTop="7dp"
android:layout_marginEnd="5dp"
android:autofillHints="#string/ch_tw_editBox_Text"
android:background="#drawable/rounded_edittext"
android:hint="#string/ch_tw_editBox_Text"
android:inputType="text"
android:paddingStart="13dp"
android:paddingEnd="13dp"
android:selectAllOnFocus="false"
android:textSize="22sp" />
<CheckBox
android:id="#+id/checkBox_check"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:text=""
android:textColorLink="#000000" />
<Button
android:id="#+id/checkBox_delete"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/ic_delete"
android:layout_marginStart="5dp"
android:layout_marginEnd="13dp"
android:layout_marginTop="5dp"/>
</LinearLayout>
Then when I try it, it only generate the view one time and I can't generate more view. I can't even delete the view that was generated.
TestCase:
I already used the code above on an other activity, and I work really good.
Thank you
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.
I have an android application when clicked on an option from a side bar it goes to a fragment, and then into another fragment which has clickable radio buttons. When clicked on these it will create a popup window with some text fields in it.
Basically this is how the flow goes,
Activity --> Fragment 1 --> Fragment 2 --> PopupWindow
When i try to access the resources inside this popupWindow, in example:
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
in the line damageComponenetAutoCompleteTextview.requestFocus(); it gives me an error,
Attempt to invoke virtual method on a null object reference
I believe it's due to a wrong view i'm referring when calling on the Resources. Hope someone could point me to what i'm doing wrong. Thanks in Advance.
This is the method i'm using to create the popupWindow.
public void showDamagedItemEntryPopup(RadioButton radioButton, View view){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewG = ((ViewGroup)view.findViewById(R.id.damaged_comp_popup));
//I tried passing the root view to inflate() method instead of passing it null. Didn't work.
//View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
View popupView = layoutInflater.inflate(R.layout.component_selection_popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setAnimationStyle(R.style.popupAnimation);
//I tried setting the content view manually but didnt work
//popupWindow.setContentView(view.findViewById(R.id.damaged_comp_popup));
Button buttonClose = (Button)popupView.findViewById(R.id.close_add_component_btn);
// Close button damaged item popop window
buttonClose.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
originalAmount = (EditText)this.findViewById(R.id.popup_add_component_original_amount);
customerContribution = (EditText)this.findViewById(R.id.popup_percentage);
quantity = (EditText)this.findViewById(R.id.popup_quantity);
finalAmount = (EditText)this.findViewById(R.id.popup_add_component_final_amount);
remarks = (EditText)this.findViewById(R.id.popup_add_component_remarks);
// Item Spinner
itemSpinnerArray = new ArrayList<String>();
itemSpinnerArray.add("Select Item");
// Status Spinner
ArrayList<String> statusSpinnerArray = new ArrayList<String>();
statusSpinnerArray.add("MR");
statusSpinnerArray.add("DR");
statusSpinnerArray.add("SP");
// Error comes at this point initially. When these Resource access line codes are commented, the popup works fine.
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
damageComponenetAutoCompleteTextview.requestFocus();
ArrayAdapter<String> itemSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, itemSpinnerArray);
damageComponenetAutoCompleteTextview.setThreshold(1);
damageComponenetAutoCompleteTextview.setAdapter(itemSpinnerArrayAdapter);
damageComponenetAutoCompleteTextview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//int index = cityNames.indexOf(actvCity.getText().toString());
itemSpinnerValue = (String) parent.getItemAtPosition(position);
Log.d("SK-->", "----------------------------------------------------------");
Log.d("SK-->","itemSpinnerValue: " + itemSpinnerValue);
}
});
statusSpinner = (Spinner)this.findViewById(R.id.popup_status_spinner);
ArrayAdapter<String> statusSpinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, statusSpinnerArray);
statusSpinner.setAdapter(statusSpinnerArrayAdapter);
//Creating a text Watcher
TextWatcher textWatcher = 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) {
//here, after we introduced something in the EditText we get the string from it
//String answerString = originalAmount.getText().toString();
if (originalAmount.getText().toString().trim().equals("") || customerContribution.getText().toString().trim().equals("")
|| quantity.getText().toString().trim().equals("")) {
// Error , one or more editText are empty
}
else
{
calculateFinalAmount();
}
//and now we make a Toast
//modify "yourActivity.this" with your activity name .this
//Toast.makeText(yourActivity.this,"The string from EditText is: "+answerString,0).show();
}
};
// Adding Text Watcher to our text boxes
originalAmount.addTextChangedListener(textWatcher);
customerContribution.addTextChangedListener(textWatcher);
quantity.addTextChangedListener(textWatcher);
// Show the popup
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
}
This is the XML i'm using for the popup.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/damaged_comp_popup"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/popup_wire">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="2dp"
android:background="#color/popup_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/popup_damage_component_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:hint="#string/damaged_componenet_item_string"/>
<Spinner
android:id="#+id/popup_status_spinner"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_damage_component_item"
android:layout_marginLeft="10dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp" />
<EditText
android:id="#+id/popup_add_component_original_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_damage_component_item"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:layout_toRightOf="#+id/popup_status_spinner"
android:ems="10"
android:hint="#string/original_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_percentage"
android:layout_width="52dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/popup_status_spinner"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="2dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/percentage_string"
android:inputType="number" />
<TextView
android:id="#+id/popup_percentageMark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_toRightOf="#+id/popup_percentage"
android:text="#string/percentage_string"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="1dp"
android:layout_marginRight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<EditText
android:id="#+id/popup_quantity"
android:layout_width="46dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_percentage"
android:layout_alignBottom="#+id/popup_percentage"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="6dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_percentageMark"
android:ems="10"
android:hint="#string/quantity_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_final_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/popup_quantity"
android:layout_alignBottom="#+id/popup_quantity"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="#+id/popup_quantity"
android:ems="10"
android:hint="#string/final_amount_string"
android:inputType="number" />
<EditText
android:id="#+id/popup_add_component_remarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/popup_percentage"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="#string/remarks_string"
android:inputType="text|textMultiLine" />
<Button
android:id="#+id/add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_below="#+id/popup_add_component_remarks"
android:background="#drawable/correct"
android:layout_marginBottom="15dp"
android:onClick="onSaveItem"/>
<Button
android:id="#+id/close_add_component_btn"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="#+id/popup_add_component_remarks"
android:layout_toRightOf="#+id/add_component_btn"
android:layout_marginLeft="10dp"
android:background="#drawable/cancel"/>
</RelativeLayout>
</LinearLayout>
It seems your damageComponenetAutoCompleteTextview belongs to the popupWindow which you inflated at top.
So try changing
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) findViewById(R.id.popup_damage_component_item);
To
damageComponenetAutoCompleteTextview = (AutoCompleteTextView) popupView.findViewById(R.id.popup_damage_component_item);
And other relevant elements as well.
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);
...
}
When I first launch my app everything displays and works correctly but when I close and reopen it a spinner and button that sit next to each other at the top of my relative layout are only about half of their usual height and the spinner is unusable. If I click it nothing drops down (but the button works as usual). Other elements below the spinner and but display as usual.
I spent half the day googling and playing with the app code and layout XML and have made little to no progress.
Anyone got a clue as to what could be causing this? It only happens on subsequent runs of the app.
Here is the layout XML:
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="#+id/namespinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/namespinner"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/namespinner"
android:onClick="onEditButtonPressed"
android:text="#string/dob_edit" />
<TextView
android:id="#+id/secondsDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="36dp"
android:text="#string/seconds"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/minutesDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/secondsDisplay"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:text="#string/minutes"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/hoursDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#+id/minutesDisplay"
android:layout_marginBottom="27dp"
android:text="#string/hours"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/hourstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/hoursDisplay"
android:layout_alignBottom="#+id/hoursDisplay"
android:layout_alignLeft="#+id/daystext"
android:text="#string/hours_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/dayDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/hoursDisplay"
android:layout_centerHorizontal="true"
android:layout_marginBottom="22dp"
android:text="#string/days"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="48sp" />
<TextView
android:id="#+id/daystext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dayDisplay"
android:layout_toRightOf="#+id/namespinner"
android:text="#string/days_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/minutesText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/minutesDisplay"
android:layout_alignBottom="#+id/minutesDisplay"
android:layout_alignLeft="#+id/hourstext"
android:text="#string/minutes_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/secondstext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/secondsDisplay"
android:layout_alignBottom="#+id/secondsDisplay"
android:layout_alignLeft="#+id/minutesText"
android:text="#string/seconds_lable"
android:textAppearance="?android:attr/textAppearanceSmall" />
This should be the relevant parts of the main activity that call it:
public class MainActivity extends Activity {
static Spinner nameSpinner;
ArrayAdapter nameAdapter;
static ArrayList nameList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onResume() {
super.onResume();
// get spinner and init array if needed
if (nameList == null) {
nameSpinner = (Spinner) findViewById(R.id.namespinner);
nameList = new ArrayList();
nameList.add("( Add )");
}
// populate spinner
nameAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, nameList);
nameSpinner.setAdapter(nameAdapter);
// listen for events/selections on the spinner
nameSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView parentView,
View selectedItemView, int position, long id) {
// read state
}
}
#Override
public void onNothingSelected(AdapterView parentView) {
// do I want to do anything here?
}
});
}
}
Edited to clean up the code display and add the class def. and class variables.
You have to insert the code wrote in the onresume method in oncreate method!
Setting the spinner in an if statement that didn't really have anything to do with setting the spinner was the cause of the problem. By changing this:
if (nameList == null) {
nameSpinner = (Spinner) findViewById(R.id.namespinner);
nameList = new ArrayList();
nameList.add("( Add )");
}
to
nameSpinner = (Spinner) findViewById(R.id.namespinner);
if (nameList == null) {
nameList = new ArrayList();
nameList.add("( Add )");
}
got it sorted.
A good nights sleep and fresh eyes where all that were needed. :P