EditText not returning content on getText() - android

The code snippet below displays a Dialog with a simple login-form. The problem is that when the user hits the login-button, the text that was entered into the EditTexts are not returned on the getText()-call. However, if I set android:setText="foo" on the EditTexts in the xml-layout "foo" is returned on getText(). Any idea why the text entered at runtime won't stick?
private void showLoginDisplay() {
loginDialog = new Dialog(GOFdroid.this);
loginDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
loginDialog.setTitle("Logga in");
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = li.inflate(R.layout.login_dialog, null);
loginDialog.setContentView(dialogView);
loginDialog.show();
Button loginButton = (Button) dialogView.findViewById(R.id.login_button);
Button cancelButton = (Button) dialogView.findViewById(R.id.cancel_button);
EditText unameView = (EditText) dialogView.findViewById(R.id.uname_id);
if (unameView != null)
Log.d(TAG, "unameView != null");
Log.d(TAG, "uname.getText(): " + unameView.getText().toString());
uname = unameView.getText().toString();
EditText pwdView = (EditText) dialogView.findViewById(R.id.pwd_id);
if (pwdView != null)
pwd = pwdView.getText().toString();
Log.d(TAG, "uname = " + uname + ", pwd = " + pwd);
loginButton.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
Log.d(TAG, "Clicked <logga in>");
loginDialog.dismiss();
viewEvents = new Runnable() {
//#Override
public void run() {
getEvents(uname, pwd);
}
};
Thread thread = new Thread(null, viewEvents, "MagentoBackground");
thread.start();
pDialog = ProgressDialog.show(GOFdroid.this,
"Uppdaterar...", "Hämtar registrerade körningar...", true);
}
});
}
and the XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/uname_label"/>
<EditText
android:id="#+id/uname_id"
android:layout_width="220dip"
android:layout_height="wrap_content"
android:text="foo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/pwd_label"
android:paddingTop="10dip"/>
<EditText
android:id="#+id/pwd_id"
android:layout_width="220dip"
android:layout_height="wrap_content"
android:text=""
android:inputType="textPassword" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="15dip"
>
<Button
android:id="#+id/login_button"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:text="Logga in" />
<Button
android:id="#+id/cancel_button"
android:layout_width="100dip"
android:layout_height="fill_parent"
android:text="Avbryt" />
</LinearLayout>
</LinearLayout>

The problem is that you are not updating uname and pwd within the anonymous inner class. You only update uname and pwd when the showLoginDisplay() method is called. When the user changes the text, and hits the login button the onClick() method is called.
Within the onClick() method of the inner class you want to do:
uname = unameView.getText();
pwd = pwdView.getText();
You will have to make unameView and pwdView global/field variables, so they are accessable by the anonymous inner class.
The reason you have to do this is because when the login button is pressed, only the code with the anonymous inner class's onClick is run.
I hope that fixes the problem.

Related

how to add buttons that appear dynamically in a dialog using android studio

I have a dialog that shows an error message,
i know i can take that error message and just make it invisible, and visible in the code,
but then the Dialog would still save room in it and it will just show as a white space.
I want the error message to be added dynamical so if no error message will be shown the dialog will be sized accordingly.
how do i do that ?
here is my Dialog >
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:id="#+id/textContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:counterEnabled="true"
app:counterMaxLength="14">
<EditText
android:id="#+id/barcode_activity_input_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:ems="10"
android:hint="Enter Serial Number"
android:inputType="number"
android:maxLength="14" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/barcode_activity_button_cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/barcode_activity_manual_input_check_box"
android:text="Cancel" />
<Button
android:id="#+id/barcode_activity_button_ok"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/barcode_activity_manual_input_check_box"
android:layout_toEndOf="#+id/barcode_activity_button_cancel"
android:text="Ok" />
<TextView
android:id="#+id/barcode_activity_text_view_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textContainer"
android:layout_toRightOf="#+id/barcode_activity_image_view_warning"
android:paddingTop="5dp"
android:text="Serial doesn't match Workorder"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#f00000"
android:textSize="13dp" />
<ImageView
android:id="#+id/barcode_activity_image_view_warning"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#+id/textContainer"
android:background="#drawable/warning" />
<CheckBox
android:id="#+id/barcode_activity_manual_input_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/barcode_activity_image_view_warning"
android:text="Allow Serial In Workorder"
android:textSize="13dp" />
</RelativeLayout>
and here is my main file >
inputFieldDialog = new Dialog(this); // CASTING
//inputFieldDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
inputFieldDialog.setContentView(R.layout.aligment_manager_manual_input_layout); // INFLATING VIEW
Button cancelInputButton, confirmInputButton; //declaring BOXES
final EditText inputField;
TextView warningTextView;
ImageView warningImageView;
CheckBox warningCheckBox;
cancelInputButton = (Button) inputFieldDialog.findViewById(R.id.barcode_activity_button_cancel); //casting
confirmInputButton = (Button) inputFieldDialog.findViewById(R.id.barcode_activity_button_ok); //casting
inputField = (EditText) inputFieldDialog.findViewById(R.id.barcode_activity_input_editText); //casting
warningTextView = (TextView) inputFieldDialog.findViewById(R.id.barcode_activity_text_view_warning); //casting
warningImageView = (ImageView) inputFieldDialog.findViewById(R.id.barcode_activity_image_view_warning); //casting
warningCheckBox = (CheckBox) inputFieldDialog.findViewById(R.id.barcode_activity_manual_input_check_box); //casting
warningTextView.setVisibility(TextView.INVISIBLE); //sets warnings invisible
warningImageView.setVisibility(ImageView.INVISIBLE); //sets warnings invisible
warningCheckBox.setVisibility(CheckBox.INVISIBLE); //sets warnings invisible
cancelInputButton.setOnClickListener(new View.OnClickListener() { //listeners for the buttons
#Override
public void onClick(View v) {
inputFieldDialog.hide();
}
});
confirmInputButton.setOnClickListener(new View.OnClickListener() { //listeners for the buttons
#Override
public void onClick(View v) {
scanResults = String.valueOf(inputField.getText());
scanBarcodeText.setText(inputField.getText());
editor.putString("boxID", String.valueOf(inputField.getText())); //saves the data as antenaNamein shared prefrences
editor.commit();
if (checkIfIdMatched(String.valueOf(inputField.getText())) == true) { //checkks if id is maching the one in the workorder
aligmentManagerClass.scanFromBarcodeApproved(); //ID MACHED
if (mainDialog != null) {
mainDialog.show();
}
loaderScreenMainText.setText("initlizing WiFi");//shows the user on screen message
wifiWrapper myWifiWrapper = new wifiWrapper(); //- INIZILIZING WIFI
myWifiWrapper.checkWifiState(getApplication(), callbackFunctionForisWifiOn);
} else {
loaderScreenMainText.setText("Scan Doesn't Match Data In Workoder"); // notify onScreen User
if (mainDialog != null) { // hides the loading dialog
mainDialog.hide();
}
AlertDialog wrongNumberDialog = getAlertDialogForWrongId(); //shows to user alert dialog for Wrong Number
wrongNumberDialog.show(); // show it
}
}
For remove white space...
Use "View.GONE" property of setVisibility() method rather than "TextView.INVISIBLE".
Set below code for hide views
warningTextView.setVisibility(View.GONE);
warningImageView.setVisibility(View.GONE);
warningCheckBox.setVisibility(View.GONE);
And for visible view just user
warningTextView.setVisibility(View.VISIBLE);
warningImageView.setVisibility(View.VISIBLE);
warningCheckBox.setVisibility(View.VISIBLE);
You can add buttons dynamically by creating buttons in Java file
Add this XML to dialog like dialog.addView(...)
Then Create Dynamically Buttons by according to values like
for(){
Create Buttons and add to above layout
layout.addView(button);
}
Hope it will help.

Run time not able to fire the button click event for dynamic buttons

I have generated button run time(in app). but I am not able to handle the fire the event of the button run time. my scenario I have the Two Text box 1 for "Name" and another for "Phone No." and 1 Button "Add". when click on Add button, I dynamically generated the One Text box with two button "Call" and "Remove".
but when click on 1st or 2nd call button its make a call on always lastly added number.
Code is below :
public class MainActivity : Activity
{
int count = 1;
EditText textIn, txtPhoneNo;
Button buttonAdd;
LinearLayout container;
EditText textOut;
System.Collections.ArrayList arrList = new System.Collections.ArrayList();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
textIn = (EditText)FindViewById(Resource.Id.textin);
txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo);
buttonAdd = (Button)FindViewById(Resource.Id.add);
container = (LinearLayout)FindViewById(Resource.Id.container);
}
private void buttonAdd_Click(object sender, EventArgs e)
{
LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View addView = layoutInflater.Inflate(Resource.Layout.row, null);
textOut = (EditText)addView.FindViewById(Resource.Id.textout);
arrList.Add(txtPhoneNo.Text);
if (textIn.Text != "" && txtPhoneNo.Text != "")
{
textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal);
container.AddView(addView);
Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall);
btnCall.Click += BtnCall_Click;
Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove);
buttonRemove.Click += ButtonRemove_Click;
}
else
{
Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show();
}
}
private void BtnCall_Click(object sender, EventArgs e)
{
var callDialog = new AlertDialog.Builder(this);
string strNo = After(textOut.Text,":");
callDialog.SetMessage("Call " + strNo + "?");
callDialog.SetNeutralButton("Call", delegate
{
var callIntent = new Intent(Intent.ActionCall);
callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo));
StartActivity(callIntent);
});
callDialog.SetNegativeButton("Cancel", delegate { });
// Show the alert dialog to the user and wait for response.
callDialog.Show();
}
}
}
Main.axml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/textin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="name" />
<EditText
android:id="#+id/txtPhoneNo"
android:layout_width="345.0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="Phone No." />
<Button
android:id="#+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Add" />
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</LinearLayout>
row.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="wrap_content">
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Remove"/>
<Button
android:id="#+id/btnCall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/remove"
android:text="Call"/>
<EditText
android:id="#+id/textout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/remove"/>
</RelativeLayout>
My problem is that how I can make call to particular no. but in this case I am able to call only lastly added number.
You are overwriting textOut everytime buttonAdd_Click is invoked. So when a new layout is added textOut will always be the last added EditText. This is basically a logical error. As per your logic you should having a List of EditTexts rather than a single instance.
List<EditText> textOuts; // instead of EditText textOut;
int layoutCount=0;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
textIn = (EditText)FindViewById(Resource.Id.textin);
txtPhoneNo = (EditText)FindViewById(Resource.Id.txtPhoneNo);
buttonAdd = (Button)FindViewById(Resource.Id.add);
container = (LinearLayout)FindViewById(Resource.Id.container);
textOuts= new List<EditText>();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
int i=layoutCount++;
LayoutInflater layoutInflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View addView = layoutInflater.Inflate(Resource.Layout.row, null);
textOuts.Add((EditText)addView.FindViewById(Resource.Id.textout));
arrList.Add(txtPhoneNo.Text);
if (textIn.Text != "" && txtPhoneNo.Text != "")
{
textOut.SetText(textIn.Text + " : " + txtPhoneNo.Text, TextView.BufferType.Normal);
container.AddView(addView);
Button btnCall = (Button)addView.FindViewById(Resource.Id.btnCall);
btnCall.Click +=delegate(object sender, EventArgs e) {
var callDialog = new AlertDialog.Builder(this);
string strNo = After(textOuts[i].Text,":");
callDialog.SetMessage("Call " + strNo + "?");
callDialog.SetNeutralButton("Call", delegate
{
var callIntent = new Intent(Intent.ActionCall);
callIntent.SetData(Android.Net.Uri.Parse("tel:" + strNo));
StartActivity(callIntent);
});
callDialog.SetNegativeButton("Cancel", delegate { });
// Show the alert dialog to the user and wait for response.
callDialog.Show();
};
Button buttonRemove = (Button)addView.FindViewById(Resource.Id.remove);
buttonRemove.Click += ButtonRemove_Click;
}
else
{
Toast.MakeText(this, "Field can not be blank.", ToastLength.Short).Show();
}
}
Changing the code like above might fix it. I haven't tested the code myself.

how to get the value of edittext

i have a feedback activity and it has an EditText and a button :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="230dp"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:gravity="center"
android:layout_height="wrap_content"
android:text="#string/bugtitle"
android:textSize="35sp"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textColor="#000000"
android:id="#+id/BUG_title"
android:fontFamily="sans-serif-thin"/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/BUG_title"
android:layout_marginTop="15dp"
android:layout_marginRight="30dp"
android:id="#+id/et_bug"
android:layout_marginLeft="30dp"
android:fontFamily="sans-serif-light"
android:hint="#string/bug_et"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:text="SEND"
android:fontFamily="sans-serif-light"
android:id="#+id/send_bug"/>
</RelativeLayout>
so user write his feedback and when he clicked on SEND button it should send to Parse host with this code:
et1 = (EditText)findViewById(R.id.et_bug);
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", et1.getText().toString());
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
and problem is here when users sends his feedback a null text will be send to Parse host
also i have tried this one:
et1 = (EditText)findViewById(R.id.et_bug);
String BUG_SEND = et1.getText().toString();
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", BUG_SEND);
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
but same as the first one
and then i have tried SharedPreferences and saving the value to SP and send SP value to Parse but its null :|
this is the feedback dialog:
public void onBug(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.bug_dialog);
Button dialogButton = (Button) dialog.findViewById(R.id.send_bug);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String PHONE_MODEL = android.os.Build.MODEL;
final String OS_VERSION = Build.VERSION.RELEASE;
et1 = (EditText)findViewById(R.id.et_bug);
String BUG_SEND = et1.getText().toString();
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", BUG_SEND);
bugsend.put("OS", OS_VERSION);
bugsend.put("MODEL", PHONE_MODEL);
bugsend.saveInBackground();
Toast.makeText(getApplication(),"Thank You We Will Check This Report",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
anyone knows how i should fix this?
EDIT: I've already changed a bit your code:
public void onBug(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(true);
dialog.setContentView(R.layout.bug_dialog);
Button dialogButton = (Button) dialog.findViewById(R.id.send_bug);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseObject bugsend = new ParseObject("BUGS");
bugsend.put("BUG_REPORT", et1.getText().toString()).commit();
bugsend.put("OS", Build.VERSION.RELEASE;).commit();
bugsend.put("MODEL", android.os.Build.MODEL).commit();
bugsend.saveInBackground(); //THIS GUY
Toast.makeText(getApplication(),"Thank You We Will Check This Report",Toast.LENGTH_LONG).show();
dialog.dismiss();
}
});
dialog.show();
}
If you're using SharedPreferences you need after putting variable, also commit it like in code above. I don't know what is doing your saveInBackground() method, but i think that adding .commit() after every argument should also work.
Please check it and let me know if it won't work

Programmatically adding EditText box

I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<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="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());

myEditText.setText not refreshing on page

I have a simple activity which needs to accept a 4-digit PIN number using a custom pin-pad (buttons on the screen). The digits are stored in four EditTexts.
When I click a button, the text in the button is stored in a char[] (myEditText.getText() shows that this is happening), the focus is moved to the next EditText through an onFocusListener() and the Log output shows that this is all happening correctly.
11-20 10:19:56.969: I/DebugA(17742): Pin1 updated to: 1 // Pin1 is the ID
11-20 10:19:58.289: I/DebugA(17742): Pin2 updated to: 2 // '2' is Pin2.getText()
11-20 10:19:58.849: I/DebugA(17742): Pin3 updated to: 3
11-20 10:19:59.659: I/DebugA(17742): Pin4 updated to: 4
The screen itself is simply not being updated. The EditTexts all appear empty, even though the code executes perfectly.
I have looked through a whole heap of answers on SO and tried many of the suggestions with absolutely no luck, so I am posting this question which I hope someone can shed some light on! Has anyone else had this happen?
Here's some of the layout file:
<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" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp" >
<EditText
android:id="#+id/Pin1"
android:tag="Pin1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="2"
android:gravity="center_vertical|center_horizontal"
android:nextFocusDown="#+id/Pin2"
android:maxLength="1"
android:inputType="textPassword" />
...
...
...
</RelativeLayout>
...and the buttons are:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:layout_below="#+id/layout1" >
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:text="1" />
<Button
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="2" />
<Button
android:id="#+id/Button03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnPinClick"
android:layout_marginLeft="21dp"
android:text="3" />
</LinearLayout>
The activity is a standard activity (MyActivity extends Activity).
Any thoughts?
UPDATE
Here's some of the java as requested:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layout = (RelativeLayout)inflater.inflate(R.layout.activity_login, null);
EditText Pin1 = (EditText) layout.findViewById(R.id.Pin1);
Pin1.setOnFocusChangeListener(focusListener);
Pin1.setText("1"); // <--THIS HAS NO EFFECT
EditText Pin2 = (EditText) layout.findViewById(R.id.Pin2);
Pin2.setOnFocusChangeListener(focusListener);
EditText Pin3 = (EditText) layout.findViewById(R.id.Pin3);
Pin3.setOnFocusChangeListener(focusListener);
EditText Pin4 = (EditText) layout.findViewById(R.id.Pin4);
Pin4.setOnFocusChangeListener(focusListener);
...
...
...
}
The Onclick method is:
public void btnPinClick(View btnPin) {
String PinNumber = ((Button)btnPin).getText().toString();
RelativeLayout layout = (RelativeLayout)getLayoutInflater().inflate(R.layout.activity_login, null);
ArrayList<View> views = (ArrayList<View>) layout.getFocusables(View.FOCUS_DOWN);
EditText nextItem = null;
for (int i = 0; i < views.size(); i++) {
// THIS IS THE CORRECT EditText FOR EACH OF THESE
EditText textBox = (EditText) views.get(i);
if (textBox.getTag().toString().equals(currentText)) {
textBox.setText(PinNumber);
pin[i] = PinNumber.toCharArray()[0];
Log.i("DebugA", textBox.getTag().toString() + " updated to: " + textBox.getText().toString());
textBox.invalidate();
textBox.refreshDrawableState();
if(i+1 < views.size()) {
nextItem = (EditText) views.get(i+1);
}
else {
doCheckPin();
}
break;
}
}
if(nextItem != null){
nextItem.requestFocus();
currentText = nextItem.getTag().toString();
}
}
UPDATE 2
Here is the code for the OnFocusChangeListener as requested:
protected OnFocusChangeListener focusListener = new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
//THIS SIMPLY SETS A VARIABLE STRING TO SAY WHICH
//EditText HAS FOCUS.
currentText = v.getTag().toString();
}
}
};
It looks like you're using a layout inflater in addition to setContentView in your activity. And affecting the edittexts referenced by the inflater. Try removing use of the inflater and rely solely on setContentView for this part of the activity. You might be performing in an incorrect view hierarchy.

Categories

Resources