dynamically selecting radiobutton view inside activity - java

I am working on an application where my activity has 2 radio buttons.
4 channel
9 channel
Now:
default selection of the screen is on 4channel radio button.
if the user wants to click the (9channel) button, it will open a dialog box prompting user
" if you move, all saved items will be deleted "
In the dialog box i have "Yes" and "No" button. If you click "Yes" button it will select the 9 channel radio button.
The issues that i want to fix are :
when the user clicks on "No" button on the dialog box, i quit the dialog. But selection of radio button still moves to the (9channel) radio button. How to keep the old selection if user click "No" on the dialog box?
Default selection on the activity is on (4channel) radio button. If the user clicks on the same selected button, i dont want the dialog box to open.
The dialog box should open only if i click on the other radio button .
How can i achieve the above 2 scenarios? I don't know how to get the radionbutton view and show to user.
my Code :
rg = (RadioGroup) findViewById(R.id.radioGroup1);
public void addListenerOnButton()
{
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
RadioButton rb = (RadioButton) v;
selection = (String) rb.getText();
mFlag = true;
showDialog(SELECTION_ALERT_ID);
}
};
RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb2.setOnClickListener(listener);
}

Make a code for "Negative" option in your code:
new AlertDialog.Builder(this)
.setTitle("Hello!")
.setMessage("Choose your RadioButton:")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
rb1.setChecked(true);
rb2.setChecked(false);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
rb1.setChecked(false);
rb2.setChecked(true);
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();

for 1, in your dialog button listener, do:
first, move your radio button to global variable and add following listener to your dialog popup.
then, update the listener for negative button :
.setNegativeButton(getString(R.string.alert_dialog_negative_text),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//not checked, but according to api, should be this method
rb1.setSelected(true);
}
});
for 2, you can do a check in your listener code and if the clicked radio button is checked, do not showDialog.
But personally I would recommend to use the OnCheckedChangeListener for the radio button, refer to this question: radio button listener

Related

How to use a dialogue box within a popup window?

I have a popup window that gets executed when a button is pressed. When the user clicks "home" within the popup window I want a dialogue message to pop up asking the user if they are sure they want to quit.
Here is how I declare the popup window:
LayoutInflater inflater = (LayoutInflater) endlessflags.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.success_menu, (ViewGroup) findViewById(R.id.myPop5));
pwindo4 = new PopupWindow(layout, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
pwindo4.setAnimationStyle(R.style.AnimationPopup);
pwindo4.showAtLocation(layout, Gravity.TOP | Gravity.CENTER, 0, 0);
And then I have a onTouchListener for the button that are also called in the popup window. I try to call the dialogue function I created but when I do nothing happens and I get no errors or crashes:
AlertDialog.Builder myAlert = new AlertDialog.Builder(mainActivity.this);
myAlert.setMessage("Would you like to quit?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveState = true;
saveState();
//finish();
dialog.cancel();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveState = false;
dialog.cancel();
}
});
My question is why is it that when I call the dialogue event function nothing seems to happen and how can I fix this so the dialogue box shows up when I click a button within my popup window?
You need to show the dialog:
AlertDialog dialog = myAlert.create();
dialog.show();

how can i make small popup type thing appear on a button click that contains two sub-buttons which on clicking goes to their corresponding activities?

I want to open a small popup or something like that on a button click such that the popup contains two buttons which on clicking goes to their corresponding activities.
Please provide the code as i am totally new to android?
Yes you can do this with Dialogs
use Below Code to make dialog:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Uncomment the below code to Set the message and title from the strings.xml file
//builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
//Setting message manually and performing action on button click
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Action for 'NO' Button
dialog.cancel();
}
});
//Creating dialog box
AlertDialog alert = builder.create();
//Setting the title manually
alert.setTitle("AlertDialogExample");
alert.show();
A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.
find details of dialogs in below link:
Dialogs
You can use material alert dialog for this
Check this!

java Android - two dialogs, prevent first dialog from closing after second one exits

Initially in my app I am creating an AlertDialog which has three buttons, in which the middle button opens up another AlertDialog. The problem is that when the second AlertDialog closes after a button is pressed, the first one closes with it. I think both AlertDialogs get closed after I press a button on the second AlertDialog.
What I want is for the first AlertDialog to open another AlertDialog that has its own buttons, and when second AlertDialog presses a button, it only closes itself and goes back to the first one. Is there any way to achieve this?
Here is the code for the button used to open the AlertDialog:
final ImageButton fabgroup = (ImageButton) findViewById(R.id.groupButton);
Here's the code for a button that opens an AlertDialog that contains another button that opens another AlertDialog using the middle button (create button) on itself, but closes them both when a button on the second one is pressed (either the yes or no button, which is not what I want as I only want the second one to close itself and go back to the first AlertDialog, and yea this sounds pretty confusing in theory so I can try to clarify if needed):
fabgroup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(CreateNote.this);
helpBuilder.setTitle("Select a group");
helpBuilder.setMessage("Add to group?");
final TextView input = new TextView(mainactiv.this);
input.setSingleLine();
input.setText("");
helpBuilder.setView(input);
helpBuilder.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
Toast.makeText(CreateNote.this, "Page has been added to group", Toast.LENGTH_SHORT).show();
}
});
helpBuilder.setNeutralButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//open another alertbox
AlertDialog.Builder helpBuilder2 = new AlertDialog.Builder(CreateNote.this);
helpBuilder2.setTitle("Assign a new group");
helpBuilder2.setMessage("Create group?");
final EditText input = new EditText(CreateNote.this);
input.setSingleLine();
input.setText("");
helpBuilder2.setView(input);
helpBuilder2.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Create Group
Toast.makeText(CreateNote.this, "Group has been created", Toast.LENGTH_SHORT).show();
}
});
helpBuilder2.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog2 = helpBuilder2.create();
helpDialog2.show();
}
});
helpBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
});
Help would be greatly appreciated.
I eventually managed to solve this problem by creating two separate functions to generate each dialog box, and when one closes it calls the function to create the other one, kinda like recycling (or maybe closer to looping functions). Although, I'm not entirely sure how performance heavy this is, but it seems to do the job without any issues from what I'm testing. If anyone would like to chime in on how this could be an issue, then I'm open to hearing what others have to say about the negative points of using alert dialog boxes this way.
You can show an activity as dialog. Put this in your manifest file.
<activity android:theme="#android:style/Theme.Dialog" android:excludeFromRecents="true"/>
From this answer: Android Activity as a dialog

Make an action when button clicked only if checkbox is marked

I have a button and a checkbox. How can I do if the user check the box, it make one action (like a toast) and if the box is not checked another toast.
I mean:
If the box is checked and I click the button:
'The box was checked'
If the box is not checked and I click the button
'The box was not checked'
Inside button listener place if statement to check if checkbox is checked. If checked do something else don't do.
A simple algorithm would be:
if(checkbox is checked){
display toast saying is checked
}else{
display toast saying is not checked
}
And put this inside your onClickListener of button
From the Android documentation:
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
if (checkBox.isChecked()) {
// display toast1
}
else {
// display toast2
}
}
});
Is that what you're looking for?
https://developer.android.com/reference/android/widget/CheckBox.html

how to add the text which the user enters in the edit text box into the listview?

I have created an layout which contains the listview. I have added a button to it, which on clicked gives an alert dialogue box with an edit text box and two options OK and CANCEL.
I want to add the item which the user enters in the edit text box into the listview. Can any one help me out how to add it?
this is the code which shows me the Alert dialogue box, the value has to be shown as the items of the listview.
if(v.getId() == addbtn.getId())
{
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Add new location");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
You should use an ArrayAdapter and bind it to your ListView. When the user enters a new value, add it to the ArrayAdapter and call mListView.notifyDataSetChanged().
There is a good example of setting up a ListView and ArrayAdapter here http://anujarosha.wordpress.com/2011/11/17/how-to-create-a-listview-using-arrayadapter-in-android/

Categories