Number picker data is not showing correctly android - android

Hello guys I am facing a problem with number picker. I have some string that needs to be displayed as
but on select second option 'menu' with select button and reopen the dialog containing number picker the data is being shown something like this
Here it can be seen that menu 1 is being shown in both of the Number picker option menu the upper one and the middle one. But on scrolling the correct data is being displayed. The code is as follows
MainActivity
package com.example.anakumar6.numberpickerexample;
import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.NumberPicker;
public class MainActivity extends AppCompatActivity {
int index =0;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get the widgets reference from XML layout
tv = (TextView) findViewById(R.id.tv);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDialog(MainActivity.this);
}
});
}
public void showDialog(Activity activity){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
final NumberPicker np = (NumberPicker) dialog.findViewById(R.id.np);
//Set TextView text color
tv.setTextColor(Color.parseColor("#FF2C834F"));
//Initializing a new string array with elements
final String[] values= {"Menu 1", "Menu", "Menu 2"};
// modifyDataForNumberPicker(values);
//Populate NumberPicker values from String array values
//Set the minimum value of NumberPicker
np.setMinValue(0); //from array first value
//Specify the maximum value/number of NumberPicker
np.setMaxValue(values.length-1); //to array last value
//Specify the NumberPicker data source as array elements
np.setDisplayedValues(values);
np.setValue(index);
//Gets whether the selector wheel wraps when reaching the min/max value.
np.setWrapSelectorWheel(true);
//Set a value change listener for NumberPicker
np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal){
//Display the newly selected value from picker
tv.setText("Selected value : " + values[newVal]);
index = newVal;
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.btnClick);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a number..."
android:textSize="25dp" />
</RelativeLayout>
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
>
<NumberPicker
android:id="#+id/np"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_below="#id/tv"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnClick"
android:text="select"/>
</LinearLayout>
I think it is android number picker native issue. Please help me to fix this issue.

I have tried many a workarounds to fix it like extending class, in which there is a function getSelectedPos which returns the position on the base of start with function rather than equals or equalignorecase. But due to being private method of NumberPicker class it can't be done. I also tried other work around that did not work.
Finally I got a simple work around for this issue. I created my own function to modify the data to be set on NumberPicker before set on NumberPicker.
modifyDataForNumberPicker(values);
np.setDisplayedValues(values);
private void modifyDataForNumberPicker(String[] dataList){
int i=0;
for(String data : dataList){
int pos = i++;
dataList[pos] = data+" ";
}
}
This function prevents any string to be substring of any string from the given array to be set on NumberPicker.

I have faced the same issue, and I have resolved by determine fixed width for NumberPicker.
<NumberPicker
android:id="#+id/np"
android:layout_width="50dp" // <=== replace "wrap_content" with fixed width
android:layout_height="0dp"
android:layout_below="#id/tv"
android:layout_weight="1"/>

Related

How to know which button user clicked and its text and id in Android Studio?

Through out the whole android application I want to capture button click, radio button clicks, link click etc... basically a user interaction in whole android application. Is there any common method to detect which element user click and its values.?
Try using onCickListener() on the buttons.
In Kotlin:
Add id to button in .xml files with android:id="#+id/nameOfButton". Every button needs an unique name.
In .kt file, use setOnClickListener with the id to set up the action when user click the button.
If there are several buttons, just follow step 1 and 2.
Example:
step 1
<Button
android:id="#+id/buttonSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
step 2
buttonSave.setOnclickListenter {
//TODO: your code goes here
}
Its very simple you just need to get the text and id of button from the onclick method. Here is java and xml code for it:
XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:onclick="getid"
android:text="Save" />
JAVA:
public void getid(View v){
int id = v.getId();
String text = v.getText();
}
As #Muthukumar Lenin asked here is listview in xml and java
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:textColor="#5f65ff"
android:padding="5dp"
android:text="Choose is the best football player?"/>
<ListView
android:id="#+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textview" />
</RelativeLayout>
JAVA:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listview);
final TextView textView = (TextView) findViewById(R.id.textview);
String[] players = new String[] {"CR7", "Messi", "Hazard", "Neymar"};
List<String> Players_list = new ArrayList<String>(Arrays.asList(players));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Players_list);
listView.setAdapter(arrayAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = (String) parent.getItemAtPosition(position);
textView.setText("The best football player is : " + selectedItem);
}
});
}
}
Some of these code are from tutorialspoint and some are edited by me.

Listview items text is not showing complete

I'm trying to create an activity for Admin to approve some Posts(by user) to publish, so I'm using Listview where all posts will be checked/unchecked. Now the issue is that some posts have large text and this big text is not showing properly in listview row.
I'm new to software development I searched all solutions, one is to use custom listview but I'm not getting it.
here are my files:
approvepost.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:background="#android:color/transparent"
tools:context=".ApprovePost"
android:layout_marginTop="25dp">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="20dp"
android:paddingBottom="10dp"
/>
</RelativeLayout>
This is the main class...
ApprovePost.java
package com.ahmed.signup;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
public class ApprovePost extends AppCompatActivity {
public static final String TAG = "ListView";
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.approvepost);
listView = (ListView)findViewById(R.id.listView);
// CHOICE_MODE_NONE: (Default)
// (listView.setItemChecked(..) doest not work with CHOICE_MODE_NONE).
// CHOICE_MODE_SINGLE:
// CHOICE_MODE_MULTIPLE:
// CHOICE_MODE_MULTIPLE_MODAL:
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemClick: " +position);
CheckedTextView v = (CheckedTextView) view;
boolean currentCheck = v.isChecked();
ApprovePostDto user = (ApprovePostDto) listView.getItemAtPosition(position);
user.setActive(!currentCheck);
}
});
this.initListViewData();
}
private void initListViewData() {
ApprovePostDto user1 = new ApprovePostDto("Ali", "I would like to set up a meeting to give an overview of Foxit and demo the features important to your workflows.");
ApprovePostDto user2 = new ApprovePostDto("Haider", "See the data in your Google Account and choose what activity is saved to personalize your Google experience");
ApprovePostDto user3 = new ApprovePostDto("Usman", "Your account storage is shared across Google services, like Gmail and Photos");
ApprovePostDto[] users = new ApprovePostDto[]{user1,user2, user3};
ArrayAdapter<ApprovePostDto> arrayAdapter
= new ArrayAdapter<ApprovePostDto>(this, android.R.layout.simple_list_item_checked , users);
listView.setAdapter(arrayAdapter);
ProgressDialog progress;
progress = new ProgressDialog(this);
progress.setTitle("Please Wait!!");
progress.setMessage("Wait!!");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
for(int i=0;i< users.length; i++ ) {
listView.setItemChecked(i,users[i].isActive());
progress.show();
}
}
}
```
If the layout of your list items (android.R.layout.simple_list_item_checked) does not work very well for large texts you can always create your own layout.
TextView and CheckedTextView have some attributes that will help:
android:lines="2" // use more lines
android:ellipsize="marquee" // scroll your text horizontally
app:autoSizeTextType="uniform" // makes your text smaller/larger depending on available space
app:autoSizeMinTextSize="11sp"
app:autoSizeMaxTextSize="14sp"
PS: Set your ListView width and height to match_parent
Use this in initListViewData method
ArrayAdapter<ApprovePostDto> arrayAdapter
= new ArrayAdapter<ApprovePostDto>(this, android.R.layout.simple_list_item_1 , users);
instead
ArrayAdapter<ApprovePostDto> arrayAdapter
= new ArrayAdapter<ApprovePostDto>(this, android.R.layout.simple_list_item_checked , users);
2nd Suggestion
Create new layout have the below code in it. Refer this layout in your adapter that would work for you.
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:checkMark="?android:attr/textCheckMark"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"/>

Show String from EditText

I want to show TextView editable like the app "Google Keep" but
EditText text = (EditText)findViewById(R.id.edittext);
String value = text.getText().toString();
didn't work
Hello I've made a example that I think you can use:
Layout:
<LinearLayout 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:orientation="vertical"
>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#FF0000"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/getInfoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GET INFO"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
MainActivity
package com.example.testedittext;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView info;
private EditText input;
private Button getInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = (TextView) findViewById(R.id.textView);
input = (EditText)findViewById(R.id.editText);
getInfo = (Button)findViewById(R.id.getInfoButton);
getInfo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String inputText = input.getText().toString();
info.setText(inputText);
}
});
}
}
Here is the output:
Hope this helps,
Cheers
That is the correct syntax for getting the text in string format of an edittext.
The value of String "value" is actually "" right now because you called
text.getText().toString();
IMMEDIATELY after EditText text was instantiated. As you can imagine, the moment it was created, there was no text inside it, so that's why "value" has an empty string.
If you want to retrieve the value of the edittext at a specific call, I'd recommend adding a button in your xml layout, and in your code, add this:
Button button = (Button) findViewById(R.id.somebutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.getText().toString();
}
});
This will get the current String value of the edittext when you click on the button.
You have to use the value String somewhere else it will tell you that its not used.
Then you have to use that piece of code at a point of time where you have had a change to set the value of that ediettext.

Multiple TextViews Added in Loop onClick Unexpected Behavior

I have two RelativeLayouts that I am programmatically adding TextViews to from an ArrayList with the intent that, based on the TextView clicked, I can index back into the original ArrayList. For my trivial code example, I'm arbitrarily splitting the items into the two RelativeLayouts.
The code works as expected when the first TextView is clicked. The dialog shows up displaying the correct word and index. However, after closing the dialog, if a different TextView is clicked, the word and index (testWord and testID in my code example) are not updated and only the first TextView's information is displayed. It seems that onClick is only being called on the first click.
Here is an example Java class (I apologize for any formatting errors, it's my first time posting here):
package com.test.test;
import java.util.ArrayList;
import java.util.Scanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class AndTestActivity extends Activity {
private final int DIALOG_CASE_ITEM_SELECT=0,CURR_ID=128,P_ID=256,P_HR=512,C_HR=1024;
private final String TEST="This is a test. Only a test.";
private int numPast,testID;
private String testWord;
private ArrayList<String> test;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Scanner s=new Scanner(TEST);
test=new ArrayList<String>();
while(s.hasNext()){
test.add(s.next());
}
int currID=CURR_ID,pID=P_ID,pHrID=P_HR,cHrID=C_HR,tempLen=3;
numPast=0;
RelativeLayout rlP=(RelativeLayout)findViewById(R.id.caseItemListPastLayout);
RelativeLayout rlC=(RelativeLayout)findViewById(R.id.caseItemListCurrLayout);
for(int x=0;x<test.size();x++){
if(x>tempLen){
addToRL(test.get(x),true,rlC,currID,cHrID);
currID++;
cHrID++;
}else{
numPast++;
addToRL(test.get(x),false,rlP,pID,pHrID);
pID++;
pHrID++;
}
}
}
private void addToRL(String sb, boolean current,RelativeLayout rl,int id,int hrID){
TextView citv=new TextView(this);
citv.setText((CharSequence)sb);
citv.setMovementMethod(new ScrollingMovementMethod());
citv.setTextSize(15);
citv.setTextColor(Color.BLACK);
citv.setGravity(Gravity.CENTER_HORIZONTAL);
if(current){
citv.setBackgroundColor(Color.RED);
}else{
citv.setBackgroundColor(Color.WHITE);
}
citv.setPadding(20, 10, 20, 10);
citv.setWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 360, getResources().getDisplayMetrics()));
citv.setId(id);
RelativeLayout.LayoutParams cilp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(current&&id!=CURR_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
else{
if(id!=P_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
}
citv.setLayoutParams(cilp);
citv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TextView tv0=(TextView)v;
int tvID=tv0.getId()-P_ID;
if(tvID<0){
tvID=(tv0.getId()-CURR_ID)+numPast;
}
testID=tvID;
testWord=test.get(testID);
showDialog(DIALOG_CASE_ITEM_SELECT);
}
});
rl.addView(citv);
View hr=new View(this);
hr.setBackgroundColor(getResources().getColor(R.color.background));
RelativeLayout.LayoutParams hrlp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 5);
hrlp.addRule(RelativeLayout.BELOW,id);
hr.setLayoutParams(hrlp);
hr.setId(hrID);
rl.addView(hr);
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
switch(id){
case DIALOG_CASE_ITEM_SELECT:
builder = new AlertDialog.Builder(this);
builder.setMessage("word: "+testWord+" index: "+testID)
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
Here is the associated XML for the layout (nothing special, but I thought I'd include it so there's a fully working example):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical"
android:background="#color/background"
android:screenOrientation="portrait">
<ScrollView
android:id="#+id/caseItemCurrScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListCurrLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
<View
android:id="#+id/caseItemScrollSpacer"
android:layout_width="fill_parent"
android:layout_below="#id/caseItemCurrScroll"
android:layout_height="5dp"
android:background="#color/background"/>
<ScrollView
android:id="#+id/caseItemPastScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_below="#id/caseItemScrollSpacer"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListPastLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
That should be a fully "working" example, with the addition of
<color name="background">#3E3E3E</color>
in the strings.xml
onCreateDialog() is only called once, while onPrepareDialog() is called each time the Dialog opens.
Override onPrepareDialog() and call ((AlertDialog) dialog).setMessage() here.

EditText.getText() returns text from different view in onPause after rotating device

I have a strange problem here with an EditText view. In onPause() after an orientation change, the EditText view returns text via getText() that was never assigned to it.
In fact, the EditText object in question is only assigned an empty string.
In the error situation, it returns part(!) of a text that was assigned to a different TextView.
This does however not happen if onPause is triggered by pressing the "back" key.
Can anybody give me a clue? I do not understand what is happening here:
My activity:
package com.example.rotationtest;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private final String LOG_TAG = "Test";
private EditText mEditText;
private TextView mTextView;
private EditOk mEditOk;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.title);
mEditText = (EditText) findViewById(R.id.editText);
mEditText.setText("");
mEditOk = new EditOk() {
#Override
public void ok(String result) {
mTextView.setText(result);
}
};
editTextDialog(R.string.dialog_title, (CharSequence)getString(R.string.dialog_title), mTextView.getText().toString(), mEditOk);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
abstract class EditOk {
abstract public void ok(String result);
}
void editTextDialog(int titleId, CharSequence message, String text,
final EditOk ok) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.edittext_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView messageView = (TextView) layout.findViewById(R.id.text);
messageView.setText(message);
final EditText input = (EditText) layout.findViewById(R.id.editTextDialog);
Log.d(LOG_TAG, "input = " + input);
input.setTransformationMethod(android.text.method.SingleLineTransformationMethod.getInstance());
input.setText(text);
new AlertDialog.Builder(this)
.setTitle(titleId)
.setView(layout)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String newName = input.getText().toString();
if (newName.length() != 0) {
ok.ok(newName);
}
}})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setResult(RESULT_CANCELED);
finish();
}
})
.create().show();
}
#Override
protected void onPause() {
super.onPause();
Log.d(LOG_TAG, "onPause: editText is " + mEditText.getText());
}
}
and layout:
<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" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="textMultiLine" />
</RelativeLayout>
There is an AlertDialog involved which's layout is this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:paddingLeft="20dp" android:paddingRight="20dp" android:paddingBottom="20dp" android:paddingTop="15dp" android:layout_gravity="top">
<TextView android:id="#+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#FFF"
android:gravity="top" android:textSize="16sp" android:paddingBottom="20dp"/>
<EditText
android:id="#+id/editTextDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
Now the steps are these:
Start activity in portrait orientation. A dialog pops up containing an EditText, prefilled with "Hello World!"
Append "zzz" to "Hello world!"
Press OK. "Hello world!zzz" is now assigned to the TextView on the Activity.
Now rotate the device to landscape. In onPause, mEditText.getText() now returns "zzz" although mEditText was not touched at all.
Any ideas? My expectation is that mEditText.getText() always returns "". If you repeat these steps but trigger onPause() by pressing back instead of rotating the device, getText() indeed does return "" as expected. Why not when rotating the device?
Additional note: I noticed that the soft keyboard seems to be necessary for the issue to appear. On an emulator with "Keyboard support = yes", the issue doesn't show up.
I had the same kind of issue with EditText. Looking in forums, I found that setting android:inputType="textNoSuggestions" fixes the issue, I don't know exactly how it works but, it worked fine on the Motorola Et1 tablet(Android 2.3).

Categories

Resources