onItemContextMenu and button created programatically problem - java

I would like if anybody could help to solve the problem, that I'm trying to fix in my code. I'm really despearte!
I would like to know if it's possible get from the contextmenu, all the information of the button that I created, and use the setText function later.
Ok, first of all I create a tablerow with some buttons (like a soundboard application)
for (int j = 0 ; index > 0 && j < 2 ; j++) {
final CustomToggleButton tagB = new CustomToggleButton(this);
tagB.setId(index);
...
...
registerForContextMenu(tagB);
tagB.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
....
}
...
}
}
Secondly, I let every button an "edit" option for changing the text in it:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
menu.setHeaderTitle("Opciones de la etiqueta");
menu.add(0, v.getId(), 0, "Edit");
}
Finally I use onContextItemSelected for creating an EditText Alert and let the possibility of introduce the new text.
#Override
public boolean onContextItemSelected(final MenuItem item) {
if (item.getTitle() == "Edit") {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
alert.setView(input);
alert.setTitle("Nombre del tag");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString().trim();
Log.v(null, "nombre del tag nuevo: "+value);
Toast.makeText(getApplicationContext(), value,Toast.LENGTH_SHORT).show();
int button_id = item.getItemId(); // BUTTON ID?
//CustomToggleButton tagB = (CustomToggleButton) findViewById(R.id.button_id);//DOESN'T WORK!!
//CustomToggleButton tagB = (CustomToggleButton) findViewById(button_id); // NEITHER DOESN'T WORK!!
}
});
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
alert.show();
return true;
}
else return super.onContextItemSelected(item);
}
The problem is that I can't use its id from R.java, because I have created the button programatically...
Is there any solution for this problem??

You don't need the ID of the button. You need to reference the Button object you created via code.
Edit: Just define the button object with the scope you require. You probably need class scope. You could use an array or list if you would like. Depending on how many buttons you have, that may be the way to go.

Related

Capturing user input from an actionbar menu item

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.
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!
private android.app.AlertDialog mAlert;
private EditText mAddWorkout;
//Creating the dialog box for entering the workout name
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View view = LayoutInflater.from(this).inflate(R.layout.userinput,null);
builder.setTitle("Enter the workout name");
mAlert = builder.create();
mAlert.setCancelable(true);
mAlert.setView(view, 10, 10, 10, 10);
if (mAlert != null && !mAlert.isShowing()) {
mAlert.show();
}
mAddWorkout = (EditText) mAlert.findViewById(R.id.workout_name_input);
m_Text = mAddWorkout.getText().toString();
Hope this solves your problem
A bit late, but I had run into the same problem, and found yet another answer. The solution that works for me is...
binding.myEditText.setOnEditorActionListener { _, action, key ->
if (actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_DONE ||
key == null ||
key.keyCode == KeyEvent.KEYCODE_ENTER) {
// Put your actions here
true
} else {
false
}
}
I have only been writing kotlin for a month or so, so I cannot promise this follows best practice, but maybe someone else can improve this.
This task is hard because we had only one control, so it is harder to trap focus changes. If we had a page full of buttons, we might have a [Reset] and a [Save] button. We have one function that loads all the parameters into the EditTexts (used on entry and Reset); and one function that updates all the parameters with the EditText text (used on Save). Then all is easy. But I was going to get one parameter going before adding th fancy stuff, which is why I fell into this hole.

How to make checkbox in alert dialog align left?

It is possible to modified the checkbox align left in alert dialog?
this is activity.java file
AlertDialog dialog;
final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
// arraylist to keep the selected items
final ArrayList seletedItems=new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Difficulty Level");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
// indexSelected contains the index of item (of which checkbox checked)
#Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
// write your code when user checked the checkbox
seletedItems.add(indexSelected);
} else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the array, remove it
// write your code when user Uchecked the checkbox
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on OK
// You can write the code to save the selected item here
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on Cancel
}
});
dialog = builder.create();//AlertDialog dialog; create like this outside onClick
dialog.show();
}
.........................................................................
http://i.stack.imgur.com/rTpYb.jpg">
but... I want the checkbox displayed at the left side...anyone help me.
You can put custom view in your alertdialog. In that view you can easily align your checkbox where you want. For more read this tutorial. http://www.pcsalt.com/android/create-alertdialog-with-custom-layout/#sthash.yN5edpAX.FKWQjxBj.dpbs
Try this way,hope this will help you to solve your problem.
dialog = builder.create();
dialog.getWindow().setGravity(Gravity.LEFT);
dialog.show();

Alertdialog.Builder's setItems() onClick returning null dialog

I am making a custom Alertdialog.Builder and took the liberty of extending the work done by danoz73, found here https://github.com/danoz73/QustomDialog.
I have implemented my own setItems() method in the class QustomDialogBuilder.java (a subclass of AlertDialog.Builder). Since the items I pass are not the standard, the method is NOT marked as Override.
public QustomDialogBuilder setItems(ArrayList<KMColor> items, final DialogInterface.OnClickListener listener) {
LinearLayout itemList = (LinearLayout) mDialogView.findViewById(R.id.items_list);//INFLATE THE LINEARLAYOUT INSIDE THE SCROLLVIEW
Log.i("TAG","1 setItems items dialog is "+mDialog);//<--THIS IS NULL
//HIDE UNUSED VIEWS SO THERE ISN'T A SPACE
mDialogView.findViewById(R.id.customPanel).setVisibility(View.GONE);
mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);
for (int i = 0; i < items.size(); i++) {
KMColor item = items.get(i);
View listItem = inflateItem(item.name,item.color);
listItem.setId(item.color);//VIEWS IDS CAN'T BE 0
itemList.addView(listItem);
Log.i("TAG","2 setItems items dialog is "+mDialog);//<--THIS IS NULL
if (listener != null) {
/*itemList <-- doesn't tell you what item was clicked*/
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("TAG","3 setItems items dialog is "+mDialog);//<--STILL NULL
listener.onClick(mDialog, v.getId());
}
});
}
}
return this;
}
This all works fine I get the custom dialog I want to display. BUT when I select an item I get the id however my dialog retuned from the onClick is null so I cannot dismiss the dialog!
alert.setItems(items,new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("TAG", "setItems dialog is " + dialog);//<--null, so I crash if I try to dismiss()
}
});
Back in QustomDialogBuilder.java here is the show override I use to grab the dialog.
#Override
public AlertDialog show() {
if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
if (mMessage.getText().equals("")) mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);
mDialog = super.show();//<--GET THE DIALOG TO PASS IN THE ONCLICK
Log.i("TAG","AlertDialog show() dialog is "+ mDialog);
return mDialog;
}
I'm sure I could grab reference to the dialog in a different manner however I'd really like to understand why this isn't quit working the way I expect it to. I would appreciate any help I can get. Thanks!
EDIT Here is how I build the dialog
public void showColorPicker(){
QustomDialogBuilder alert = new QustomDialogBuilder(context);
alert.setTitle("Pick Color");
alert.setTitleColorWithResource(getResources().getColor(R.color.white));
alert.setDividerColorWithResource(getResources().getColor(R.color.white));
ArrayList<KMColor> items = new ArrayList<KMColor>();
items.add(new KMColor("Black",Color.BLACK));
items.add(new KMColor("Blue",Color.BLUE));
items.add(new KMColor("Cyan",Color.CYAN));
items.add(new KMColor("Green",Color.GREEN));
items.add(new KMColor("Dark Grey",Color.DKGRAY));
items.add(new KMColor("Light Grey",Color.LTGRAY));
items.add(new KMColor("Magenta",Color.MAGENTA));
items.add(new KMColor("Red",Color.RED));
items.add(new KMColor("White",Color.WHITE));
items.add(new KMColor("Yellow",Color.YELLOW));
alert.setItems(items,new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("TAG", "setItems dialog is " + dialog);//<--NULL
}
});
alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();//<--WORKS FINE
}
});
// create alert dialog
AlertDialog alertDialog = alert.create();
// show it
alertDialog.show();
}
Embarrassed to say I found the answer. My problem was here
// create alert dialog
AlertDialog alertDialog = alert.create();
// show it
alertDialog.show();
I was calling create() on my Alertdialog.Builder however I was not Overrideing the create() in QustomDialogBuilder.java, I was only Overrideing the show() therefore I never got a handle on the dialog! simple fix! The above code is now just
alert.show();
I now also Override the create() in QustomDialogBuilder.java. Same thing as the show() but now I won't accidentally chase my tail!
#Override
public AlertDialog create() {
if (mTitle.getText().equals("")) mDialogView.findViewById(R.id.topPanel).setVisibility(View.GONE);
if (mMessage.getText().equals("")) mDialogView.findViewById(R.id.contentPanel).setVisibility(View.GONE);
mDialog = super.create();
return mDialog;
}

How to change text size of a checkbox list within an alert dialog

I'm working on an alert dialog that has multiple checkboxes. How do I change the text size of the elements of the checkbox list? (I can change the size of the title and the buttons just fine)
boolean[] allBoolean = {true, true, true, true, true, true, true, true};
AlertDialog.Builder prompt = new AlertDialog.Builder(Class.this);
prompt.setTitle("Title");
prompt.setMultiChoiceItems(all, allBoolean, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
allBoolean[which] = isChecked;
}
});
prompt.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do nothing
}
});
prompt.setPositiveButton("Select", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//do something
}
});
alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);
You can actually get access to the message's TextView pretty easily, and then change it's size. I tested with a bigger size, but you could use whatever size you want. The text will scroll nicely as it already does. The view's id is android.R.id.message :
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(40);
This is probably a cleaner solution, though I'm not sure if there's a risk that the TextView could be null or not.

onButtonClick vs. onMenuItemSelected ----- Not the same result

I have a method deleteDilaog (it displays dialog with yes and no option. when clicked yes it does something, when clicked no it cancel dialog) and it is called either buy taping a button or on selecting a item in a option menu. Problem is, result is not the same? It works fine when selected from menu but when clicking a button it just displays dialog and no matter what i click,nothing happens?
Button:
private void RemoveAll(){
Button button=(Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
deleteDialog();
}
});
}
Menu Item:
#Override
public boolean onMenuItemSelected(int id, MenuItem item) {
mDeleteId=item.getItemId();
switch(item.getItemId()) {
case INSERT_ID:
addItem();
return true;
case DELETE_ALL_ID:
deleteDialog();
break;
}
return super.onMenuItemSelected(id, item);
}
deleteDialog method:
private void deleteDialog() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage
(CONFIRM_DIALOG_STRING).setCancelable(false).setPositiveButton
(POSITIVE, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int
which) {
switch (mDeleteId) {
case DELETE_ALL_ID:
mDbHelper.removeAllLists();
fillData();
break;
case DELETE_ID:
Cursor c = (Cursor)
getListView().getAdapter().getItem(which);
mDbHelper.removeList
(mItemId);
c.requery();
break;
}
}
}).setNegativeButton(NEGATIVE, new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int
which) {
dialog.cancel();
}
});
AlertDialog alertDialog = dialog.create();
alertDialog.show();
}
A dialog firstly should never be called as you have coded.
Make us of Activity.onCreateDialog to initialise and maintain you dialog lifecycle
Activities provide a facility to manage the creation, saving and restoring of dialogs. Also See onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int), and dismissDialog(int).
It looks like the problem is with mDeleteID. It is set in OnMenuItemSelected, but not in button2's onClick listener.
I'm guessing that the switch(mDeleteID) falls through when the button is clicked.
In case of button you're not setting a value to mDeleteId.

Categories