I found some similar questions here, but all of their were either really confusing for such a newbie like me or didn't help to solve my problem: I have a TextView tv. By click on it I want to display a custom AlertDialog with EditText + Cancel- and Update-Buttons. By click on Update-Button of my Dialog I want to replace the text of the same TextView (tv) with the value of EditText from Dialog. Here is one of my attepmts:
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv̶̶C̶̶l̶̶i̶̶c̶̶k̶̶e̶̶d̶ = (TextView) findViewById(R.id.tv_id);
tv.setText("Initial value");
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
UpdateDialogFragment updDiag = new UpdateDialogFragment();
updDiag.show(getFragmentManager(), "dialog");
tv.setText(updDiag.value); // I try to get the value of EditText like this, but it doesn't work
}
});
}
public class UpdateDialogFragment extends DialogFragment {
String value; // I try to get the value of EditText like this, but it doesn't work
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.edit_tv, null))
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText) UpdateDialogFragment.this.getDialog().findViewById(R.id.et_tv);
value = et.getText().toString(); // I try to get the value of EditText like this, but it doesn't work
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
UpdateDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
}
And my edit_tv.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="match_parent">
<EditText
android:id="@+id/et_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:layout_marginTop="40dp"
android:hint="Enter text ..."
android:layout_gravity="center_horizontal">
<requestFocus />
</EditText>
</LinearLayout>
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: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="com.yev.tabletasting.MainActivity">
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
In my case the Text of tv will be setted to "" (probably null) after click on "Update". I would be thankful for every advice, thanking you in anticipation!