I need to use a dialogfragment to create buttons each time the Start Goal button is pressed. The Dialogfragment contains 2 editTexts for a goal name and goal amount. How can I get the value of the 2 editTexts and display them on the button?
I used 2 text fields in the main activity to test if the dialog box can send the values of the 2 edit texts to the text fields on the main activity.
This is the xml for the activity_main
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eugene.myapplication.MainActivity">
<TextView
android:id="@+id/textview_GoalName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:gravity="center_horizontal"
android:text="Here will be your Goal Name"
android:textSize="30sp" />
<TextView
android:id="@+id/textview_GoalAmount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/textview_GoalName"
android:gravity="center_horizontal"
android:text="Here will be your Goal Amount"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview_GoalAmount"
android:layout_centerHorizontal="true"
android:text="Create a Goal!" />
</RelativeLayout>
This is the xml for the dialogfragment
<?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="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/edit_goalName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Goal Name"/>
<EditText
android:id="@+id/edit_goalAmt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/edit_goalName"
android:hint="Goal Amount"
android:inputType="number" />
This is the java code for the MainActivity
public class MainActivity extends AppCompatActivity implements
ExampleDialog.ExampleDialogListener {
private TextView textViewGoalName;
private TextView textViewGoalAmt;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewGoalName = findViewById(R.id.textview_GoalName);
textViewGoalAmt = findViewById(R.id.textview_GoalAmount);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog(){
ExampleDialog exampleDialog = new ExampleDialog();
exampleDialog.show(getSupportFragmentManager(), "example dialog");
}
@Override
public void applyTexts(String username, String password) {
textViewGoalName.setText(username);
textViewGoalAmt.setText(password);
}
}
Here is the java code for the dialogframgent
public class ExampleDialog extends AppCompatDialogFragment {
private EditText editTextGoalName;
private EditText editTextGoalAmt;
private ExampleDialogListener listener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_dialog,null);
builder.setView(view)
.setTitle("Create Goal")
.setNegativeButton("Maybe next time..", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i){
}
})
.setPositiveButton("Start!", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i){
String goalname = editTextGoalName.getText().toString();
String goalamt = editTextGoalAmt.getText().toString();
String message = "Fields Required";
if (goalname == "" && goalamt == ""){
Toast.makeText(getActivity(),message,
Toast.LENGTH_SHORT).show();
}
listener.applyTexts(goalname, goalamt);
}
});
editTextGoalName = view.findViewById(R.id.edit_goalName);
editTextGoalAmt = view.findViewById(R.id.edit_goalAmt);
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
listener = (ExampleDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString() + "Must implement
ExampleDialogListener");
}
}
public interface ExampleDialogListener{
void applyTexts(String username, String password);
}
}