Issue
I am creating a gym app in Android Studio and the first feature I'm trying to implement is to have the user create a workout by clicking on an option in the action bar to add it. Clicking this button brings up an alert dialog with an EditText field to type in the workout name. Later, I will use the input to create a list view with the different workouts added, but for now I am just concerned about capturing the input from this EditText field.
Here is what should happen.. on this screen I click the + button and it brings up an alert dialog box with an EditText field. I want to capture this input in the java main activity file.
Here is the java MainActivity File. I want the input from the EditText field to be stored in the m_Text variable.
public class MainActivity extends AppCompatActivity {
private String m_Text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
//Clicking add workout button in the action bar
//stackoverflow.com/questions/13143006/alert-dialog-from-within-onooptionsitemselected-android
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_add_workout:
//final EditText mAddWorkout = (EditText)R.layout.userinput;
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter the workout name");
//Create the user input xml file into a java object; capturing the user input from the dialog box
//inflate means "fill"
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
final EditText mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);
builder.setView(R.layout.userinput);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
m_Text = mAddWorkout.getText().toString();
boolean brkpt = true;
}
}); //Second parameter pass in which event listener should trigger when the button is clicked
builder.setNegativeButton("Cancel",null);
builder.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is the xml for my actionbar menu item for adding the workout name (main_menu.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_add_workout"
android:icon="@drawable/ic_add"
android:title="@string/add_workout"
app:showAsAction="always"/>
</menu>
Last, the xml for the EditText (userinput.xml)
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/workout_name_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="name..">
</EditText>
When I debug my code, the m_Text variable is always empty if I enter a workout name in the EditText field. I have been stuck on this for days now and I have combed youtube and SO for an answer and haven't found much relating to my issue. Any insight is greatly appreciated. Thank you.
EDIT: Updated code for MainActivity. I can get control pass to the custom clicker but the input is still not saved. Thanks
public class MainActivity extends AppCompatActivity {
private EditText mAddWorkout;
public class CustomClickListener implements View.OnClickListener {
private final Dialog dialog;
CustomClickListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
String editTextValue= mAddWorkout.getText().toString();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
//Clicking add workout button in the action bar
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.action_add_workout:
//final EditText mAddWorkout = (EditText)R.layout.userinput;
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter the workout name");
//Create the user input xml file into a java object; capturing the user input from the dialog box
//inflate means "fill"
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);
builder.setView(R.layout.userinput);
builder.setPositiveButton("OK",null);
builder.setNegativeButton("Cancel",null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
Button saveWorkout = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
saveWorkout.setOnClickListener(new CustomClickListener(alertDialog));
builder.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
******FINAL EDIT******* I've since finished the entire app and thought I would post my alert dialog code in case it helps someone else out. It turns out that this issue with alert dialogs was the only major issue I had, once I got used to android studio and java things really took off. Anyways my gymapp is a nice little app that uses SQLlite to track workouts, exercises, and sets.. I've actually used it in the gym :)
@Override
//Clicking add workout button in the action bar
public boolean onOptionsItemSelected(MenuItem item) {
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
builder.setTitle("Enter the workout name").setView(input).setView(input);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Workout workout = new Workout(input.getText().toString());
long workout_key = myDb.createWorkout(workout);
populateWorkouts();
}
});
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return super.onOptionsItemSelected(item);
}
In my case, I use the input to create a workout object and insert it to my database. And obviously you would need a switch statement if you had more than one option in your action bar menu.
Thanks again to the 2 guys that tried to help me.
Solution
Here is how you should do this:
Create a customer click listener
private class CustomClickListener implements View.OnClickListener {
private final Dialog dialog;
CustomClickListener(Dialog dialog) {
this.dialog = dialog;
}
@Override
public void onClick(View v) {
String editTextValue= mAddWorkout.getText().toString();
}
}
You should make mAddWorkout
value class-level so you can access it easily!
Then :
Set this click listener to your dialog like this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter the workout name");
//Create the user input xml file into a java object; capturing the user input from the dialog box
//inflate means "fill"
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
final EditText mAddWorkout = (EditText)view.findViewById(R.id.workout_name_input);
builder.setView(R.layout.userinput);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean brkpt = true;
}
}); //Second parameter pass in which event listener should trigger when the button is clicked
AlertDialog alertDialog = builder.create();
alertDialog .show();
Button saveWorkout = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
saveWorkout .setOnClickListener(new CustomClickListener(alertDialog));
This is the solution that I use for my own code and works fine; you can do validation inside the onClick method of the click listener and alert the user accordingly!
Good luck!
Answered By - Eenvincible
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.