So, I've got this code for an AlertDialog in an Android activity, and although it works and pops up at the correct moment, when I press the "OK" buttons it's supposed to save the 2 variables which I put in, into 2 Strings which after the activity should be able to use those strings.
private void showDialog(){
AlertDialog.Builder alertdg = new AlertDialog.Builder(this);
alertdg.setTitle("Choose page");
alertdg.setMessage("Choose episode/page");
final EditText page = new EditText(this);
final EditText episode = new EditText(this);
page.setWidth(210);
episode.setWidth(210);
LinearLayout layout = new LinearLayout(this);
layout.addView(episode);
layout.addView(page);
alertdg.setView(layout);
alertdg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
episodeString = episode.getText().toString();
pageString = page.getText().toString();
}
});
alertdg.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertdg.show();
}
And yes if you're wondering I already declared the 2 strings "episodeString" and "pageString" somewhere in the start of the activity, and I know I declared them correctly. Now what I'm wondering is, why can't I return those values so that the rest of the activity can use them? I've tried many times but the Dialog just won't return/save values... What am I doing wrong?
You code absolutely works
package pete.android.study;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity{
private String mEpisode = "";
private String mPage = "";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showDialog();
}
private void showDialog(){
AlertDialog.Builder alertdg = new AlertDialog.Builder(this);
alertdg.setTitle("Choose page");
alertdg.setMessage("Choose episode/page");
final EditText page = new EditText(this);
final EditText episode = new EditText(this);
page.setWidth(210);
episode.setWidth(210);
LinearLayout layout = new LinearLayout(this);
layout.addView(episode);
layout.addView(page);
alertdg.setView(layout);
alertdg.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mEpisode = episode.getText().toString();
mPage = page.getText().toString();
Toast.makeText(MainActivity.this, mEpisode + " | " + mPage, Toast.LENGTH_SHORT).show();
}
});
alertdg.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
alertdg.show();
}
}
Related
I searched but the questions I could see dealt with just copy, or copying to clipboard, or just pasting. Specifically what I want (in 1 button click, the PositiveButton in AlertDialog) is to copy the text entered by user in the EditText of my alertdialog to the EditText of my Activity.
Can you tell me how to do this please? Here is the code I am using and trying to fix:
//when user touches on "commentname" edittext we want the alertdialog to open
commentname.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//we want to copy the text entered in "input", in the alertdialog,
//and paste it in commentname
commentname.setText(alertdialog_edittext.getText().toString());
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
alertdialog_edittext = (EditText) dialog.findViewById(R.id.input);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
return true;
}
return false;
}
});
i wrote this simple code example for you to do it.
just add method to setText on Your edittext in your Activity:
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
when user click in dialog get text from edittext dialog and pass using this method:
setTextFromDialog(YouEditTextValueX);
here code example:
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText myEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ShowDialog = findViewById(R.id.showdialog_id);
myEditText = findViewById(R.id.editText_id);
ShowDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
final EditText edittext = new EditText(MainActivity.this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setView(edittext);
alert.setPositiveButton("Set text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String YouEditTextValueX = edittext.getText().toString();
if(YouEditTextValueX.length() > 0){
//this line for call method and pass the text
setTextFromDialog(YouEditTextValueX);
}
}
});
alert.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
alert.show();
}
});
}
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
}
hope this help you
I am learning Android from some book and keep getting error on .setMultichoiceItem block : cannot resolve method .setMultichoiceItems .
I check it over multiple times and my code is all case sensitive, and no misspelled words.
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends ActionBarActivity {
CharSequence[] items = {"Google","Safari","Yahoo"};
Boolean[] itemChecked = new Boolean[items.length];
Button btn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(0);
}
});
}
protected Dialog onCreateDialog(int i) {
switch (i) {
case 0:
return new AlertDialog.Builder(this)
.setTitle("Test of Dialog")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplication(), "OK Clicked !", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "Cancel Clicked !", Toast.LENGTH_LONG).show();
}
})
.setMultiChoiceItems(items, itemChecked,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), items[which] + (isChecked ? " checked!" : " unchecked!"), Toast.LENGTH_SHORT).show();
}
}).create();
}
return null;
}
}
Logcat Error : Cannot resolve method 'setMultiChoiceItems(java.lang.CharSequence[], java.lang.Boolean[], android.content.DialogInterface.OnMultiChoiceClickListener)'
Any help would be awesome .
Thanks
try this one,
Change this line
Boolean[] itemChecked = new Boolean[items.length];
to
boolean[] itemChecked = new boolean[items.length];
because its second parameter accepts boolean[], not Boolean[] objects
setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)
import android.content.DialogInterface.OnMultiChoiceClickListener;
I found the better solution:
public void alertMultipleChoiceItems(){
final CharSequence[] dialogList = Symbollist.toArray(new CharSequence[Symbollist.size()]);
HashSet<String> uniqueValues = new HashSet<>(Symbollist);
Symbollist.clear();
for (String value : uniqueValues) {
//... //Do something
Symbollist.add(value);
}
AlertDialog.Builder builder = new AlertDialog.Builder(AddPackageStep3.this);
selectedItems = new ArrayList<Integer>();
// set the dialog title
boolean[] itemChecked = new boolean[selectedItems.size()];
builder.setMultiChoiceItems(dialogList,null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
// if the user checked the item, add it to the selected items
selectedItems.add(which);
}
else if (selectedItems.contains(which)) {
// else if the item is already in the array, remove it
selectedItems.remove(Integer.valueOf(which));
}
// you can also add other codes here,
// for example a tool tip that gives user an idea of what he is selecting
// showToast("Just an example description.");
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// user clicked OK, so save the mSelectedItems results somewhere
// here we are trying to retrieve the selected items indices
String selectedIndex = "";
for(Integer i : selectedItems){
selectedIndex += i + ", ";
}
//showToast("Selected index: " + selectedIndex);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// removes the AlertDialog in the screen
}
})
.show();
}
You can convert to boolean Array this way by using the Guava:
boolean[] listBoolean = Booleans.toArray(checkedList);
I have a following snippet of codes. What I want to do is that when I click button1 just show the text. But eclipse suggest me to add onClick(DialogInterface dialog, int which) but then at btnOk.setOnClickListener(oclBtnOk); it gives me this error:
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)
Here is my code:
TextView tvOut;
Button btnOk;
Button btnCancel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
tvOut = (TextView) findViewById(R.id.textView1);
btnOk = (Button) findViewById(R.id.button1);
btnCancel = (Button) findViewById(R.id.button2);
// create click listener
OnClickListener oclBtnOk = new OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
};
// assign click listener to the OK button (btnOK)
btnOk.setOnClickListener(oclBtnOk);
}
You are importing the wrong OnClickListener class.
View.setOnClickListener() Takes a View.OnClickListener, not a DialogInterface.OnClickListener, which is what you have imported.
If you don't use the DialogInterface.OnClickListener elsewhere in this class, simply change your import statement to import android.view.View.OnClickListener.
If you do also use the DialogInterface.OnClickListener interface in your class, you will need to further qualify the class name here, like so:
View.OnClickListener oclBtnOk = new View.OnClickListener() {
public void onClick(View v) {
// change text of the TextView (tvOut)
tvOut.setText("Button OK clicked");
}
}
You should also remove the onClick(DialogInterface dialog, int which) method, as that is only defined for DialogInterface.OnClickListener.
Same problem :
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)
calButton.setOnClickListener(addClick);
Source:
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {
LinearLayout layout1;
EditText no1Text,no2Text;
Button calButton;
TextView answerText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout1=new LinearLayout(this);
no1Text=new EditText(this);
no2Text=new EditText(this);
calButton=new Button(this);
answerText=new TextView(this);
answerText.setText("0");
calButton.setText("Calculate");
layout1.setOrientation(LinearLayout.VERTICAL);
calButton.setOnClickListener(addClick);
layout1.addView(no1Text);
layout1.addView(no2Text);
layout1.addView(calButton);
layout1.addView(answerText);
setContentView(layout1);
}
private OnClickListener addClick=new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
String firstStr=no1Text.getText().toString();
String secondStr=no2Text.getText().toString();
double firstNo=Double.parseDouble(firstStr);
double secondNo=Double.parseDouble(secondStr);
double sumNo=firstNo+secondNo;
String sumStr=String.valueOf(sumNo);
answerText.setText(sumStr);
}
};
}
I made an application that checks for root using RootTools. Now, I added an option to check for BusyBox availability. When I click on "Check For BusyBox", nothing happens, though the "Check For Root" is working perfectly. I don't understand why is it happening. Please help!
package com.maverick.checkforroot;
import com.stericson.RootTools.RootTools;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Manufacturer = (TextView) findViewById(R.id.Manufacturer);
String Manu = android.os.Build.MANUFACTURER;;
Manufacturer.setText(Manu);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String Model = android.os.Build.MODEL;
tv1.setText(Model);
TextView Product = (TextView) findViewById(R.id.Product);
String Pro = android.os.Build.PRODUCT;;
Product.setText(Pro);
Button Root = (Button) findViewById(R.id.Root);
Root.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isAccessGiven()) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Congratulations!");
builder.setMessage("You Have Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Oops!");
builder.setMessage("No Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
Button BusyBox = (Button) findViewById(R.id.BusyBox);
BusyBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isBusyboxAvailable()) {
Toast.makeText(MainActivity.this,"No BusyBox!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"BusyBox Is Available!", Toast.LENGTH_LONG).show();
}
}
});
}
});
}}
All the code concerning the BusyBox button (inclusive the definition of its OnClickListener) is written inside the Root buttons OnClickListener code. Move the code and it should work for you.
When i press the Button the alert dialog does not appear here is the code:
Please could you tell me whats wrong with it, and what i can do to fix it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_item);
Button bDR = (Button) findViewById(R.id.bDR);
bDR.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(DeleteRenameList.this);
builder.setCancelable(true);
builder.setMessage("Would you like to delete or rename the list?");
builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("Rename", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
Plese Help
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_item);
Button bDR = (Button) findViewById(R.id.bDR);
bDR.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(DeleteRenameList.this).create();
builder.setCancelable(true);
builder.setMessage("Would you like to delete or rename the list?");
builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("Rename", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog.show();
}
});
Try the following format:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Context context = this;
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonAlert);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Would you like to delete or rename the list?")
.setCancelable(false)
.setPositiveButton("Delete",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
})
.setNegativeButton("Rename",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
Source