I’ve one simple project (API10) with one Edittext and one button in the “FragmentActivity”.
The main goal is: when I click in the button “Open Dialog” a custom dialog will popup and if I click in “Positive” button, the dialog dismiss and the Edittext (in Fragmentactivity) will show “Positive”… (the same behavior for Negative button).
Everything works fine except when I click to open de Dialog and before I click in the pos/neg button if I rotate the screen, the dialog remains, but, after this rotation, if I click in the Pos/Neg button, nothing happens to the Edittext?!
I’ve created the “applytext” method so that I can find the actual Edittext, but with no success.
can you please help me to understand whats is going on?
(Note: this is a simple example that I made to demonstrate my problem, because I’m using dialogFragment, but I have the same problem behavior).
this Is my code (MainActivity.java):
public class MainActivity extends FragmentActivity {
private Button bt;
private EditText et;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.bt_openDialog);
// et = (EditText) findViewById(R.id.editText_result);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog d = new Dialog(MainActivity.this, R.style.AppThemeModificado);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.signinlayout);
Button c = (Button) d.findViewById(R.id.bt_cancelar);
c.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// et.setText("Negative");
applytext("Negative");
d.dismiss();
}
});
Button e = (Button) d.findViewById(R.id.bt_entrar);
e.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// et.setText("Positive");
applytext("Positive");
d.dismiss();
}
});
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(d.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.FILL_PARENT;
//lp.height = WindowManager.LayoutParams.FILL_PARENT;
d.show();
d.getWindow().setAttributes(lp);
}
});
}
private void applytext(String text) {
Log.d("HugoXp", "----in");
et = (EditText) findViewById(R.id.editText_result);
if (et == null) {
Log.d("HugoXp", "et = null");
}else{
Log.d("HugoXp", "et <> null");
Log.d("HugoXp", "Text that was in the et: " + et.getText().toString());
Log.d("HugoXp", "actual 'String text': " + text);
}
et.setText(text);
Log.d("HugoXp", "----out");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
(the file: signinlayout.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/outrashape"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name"
android:scaleType="center"
android:src="@drawable/logoheader" />
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="16dp"
android:hint="@string/username"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:hint="@string/password"
android:inputType="textPassword"
android:typeface="serif" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Applied theme"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/bt_entrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="@drawable/estilobotaored"
android:text="Positive" />
<Button
android:id="@+id/bt_cancelar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="@drawable/estilobotaored"
android:text="Negative" />
</LinearLayout>
</LinearLayout>
You might consider using Android AlertDialog Fragment. It’s slightly less flexible in some respects, but seems to do exactly what you need to here. Although, this is not where your problem lies.
Either way, you will create your dialog by doing something like this(either in a seperate file or in the same activity, doesnt matter)
Then, from the calling activity, you can use these callback methods:
So in your buttons onClick(), you will simply call showDialog(), and the rest should work as expected