I have a butoon, on clicking of this button i want to open multiple buttons on a single AlertDialog like this :
Give Me a help :
I was using this.... to add multiple buttons
alertDialog.setButton(delete, "Delete", new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
but I found..., change setButton() to setButton2().. something like..... wt xcan i do for this....
A simple solution without xml:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setItems(new CharSequence[]
{"button 1", "button 2", "button 3", "button 4"},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
Toast.makeText(context, "clicked 1", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(context, "clicked 2", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(context, "clicked 3", Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(context, "clicked 4", Toast.LENGTH_SHORT).show();
break;
}
}
});
builder.create().show();
I would inflate the AlertDialog with my own custom view (my_alert_dialog.xml).
AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..)
View view = inflater.inflate(R.layout.my_alert_dialog, (ViewGroup) findViewById(R.id.root));
Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
alert.setView(view);
alert.show();
You can only create a Alertdialog with 3 buttons if you dont make the view by yourself.
You can either make your own custom view in xml.
but i'd suggest you just make a List.
Check
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
"Adding a list"
Dialog dialog = new Dialog(context);
RelativeLayout featureLayout = (RelativeLayout) View.inflate(this, R.layout.yourview, null);
dialog.setContentView(featureLayout);
dialog.show();
int item = 0;
ArrayList<String> list = new ArrayList<String>();
ArrayList<Integer> intList = new ArrayList<Integer>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
item = -1;
for (int i = 0; i < list.size(); i++) {
item++;
intList.add(i);
}
showRatingBarAlertDialog(intList);
private void showRatingBarAlertDialog(ArrayList<Integer> Id) {
if (item != -1) {
RatingFragment alertDialog = RatingFragment.instance(BaseActivity.this, Id.get(item), (ratingValue, comments) -> {
CXLog.d(TAG, "select the rating::" + ratingValue);
CXLog.d(TAG, "comment the feednback " + comments);
item--;
showRatingBarAlertDialog(requestId);
});
alertDialog.show(CXBaseActivity.this.getFragmentManager(), "alertDialog");
}
}
Related
I have a class that extends baseadapter to assign values to two textviews, I then call this class in the main activity and initialise it with the values for the two textviews. I then set the adapter class to a listview.
What I want to do when the user clicks on the list a AlertDialog with a edittext pops up and the user can enter a new value and append that back to the listview. I'm having trouble implementing this, please help.
EDIT
This method shows the alertdialog
public void editInfo(final String item, final List<Info> list, final int position, final String itemTitle, final InfoAdapter adapter)
{
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Edit "+item+"?");
//Create a Layout where all the views will be appened
LinearLayout myLayout = new LinearLayout(this);
myLayout.setOrientation(LinearLayout.VERTICAL);
// Set an EditText view to get user input
final EditText inputEdit = new EditText(this);
//Declare all the properties for the email inputBox
inputEdit.setHint(item);
inputEdit.setSingleLine(true);
myLayout.addView(inputEdit);
// add all the EditTexts into the alertdialog view
alert.setView(myLayout);
//Declare the variable that will be returned
alert.setPositiveButton("Save", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
//Get all the values from the input boxes
editItem = inputEdit.getText().toString();
//Validate all the data before doing anything with it
if(editItem.equals(""))
{
showEnterValuesToast();
editInfo(item,list,position,itemTitle,adapter);
}else
{
list.set(position, new Info(itemTitle, item));
adapter.notifyDataSetChanged();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}
This is the listView's setOnItemClickListener
final InfoAdapter adapter = new InfoAdapter(this, listOfInfo);
listView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View view, int position, long index)
{
switch(position)
{
case 0:
String itemToEditName = listOfInfo.get(position).getItem();
editInfo(itemToEditName,listOfInfo,position,"Name",adapter);
break;
case 1:
String itemToEditSurname = listOfInfo.get(position).getItem();
editInfo(itemToEditSurname,listOfInfo,position,"Surname",adapter);
break;
case 2:
String itemToEditEmail = listOfInfo.get(position).getItem();
editInfo(itemToEditEmail, listOfInfo, position,"Email",adapter);
break;
case 3:
String itemToEditDate = listOfInfo.get(position).getItem();
editInfo(itemToEditDate,listOfInfo,position,"Date Of Birth",adapter);
break;
case 4:
String itemToEditAddress = listOfInfo.get(position).getItem();
editInfo(itemToEditAddress,listOfInfo,position,"Physical Address",adapter);
break;
case 5:
String itemToEditPostal = listOfInfo.get(position).getItem();
editInfo(itemToEditPostal,listOfInfo,position,"Postal Code",adapter);
break;
case 6:
String itemToEditNumber = listOfInfo.get(position).getItem();
editInfo(itemToEditNumber,listOfInfo,position,"Phone Number",adapter);
break;
}
}
});
listView.setAdapter(adapter);
I have a dialog box that works perfectly well when i use just the spinner, but returns a null pointer when i try to fetch a value from an edit Text to be used for calculation in the OK button.
below is my deliberations for your consideration
//the alert box
public boolean dialogValuessP1() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
Arena.this);
alertDialogBuilder.setTitle("Connecting Options");
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View dialogView = li.inflate(R.layout.option, null);
EditText connectionNumber = (EditText)findViewById(R.id.txtConnectedBy);
connectedV = Integer.parseInt(connectionNumber.getText().toString());
spinnercategory = (Spinner) dialogView.findViewById(R.id.viewSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ConnectBY = spinnercategory.getItemAtPosition(arg2).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setPositiveButton("Connect",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (ConnectBY == "Single Score") {
scoreV = 1 * connectedV;
} else if (ConnectBY == "Double Score") {
scoreV = 2 * connectedV;
} else if (ConnectBY == "Tripple Score") {
scoreV = 3 * connectedV;
}
alert = true;
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getApplicationContext(),
"Please choose Connection Pattern",
Toast.LENGTH_LONG).show();
// Arena.this.finish(); used to close the game
alert = false;
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return alert;
}
//the option.xml is the Xml file used
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dip" >
<Spinner
android:id="#+id/viewSpin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#array/category" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/connectwith" />
<EditText
android:id="#+id/txtConnectedBy"
android:layout_width="76dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
</LinearLayout>
Since the edittext is inside the alert dialog
try using
EditText connectionNumber =(EditText) dialogView .findViewById(R.id.txtConnectedBy);
and to retrive the value use
String edittextValue=connectionNumber.getText().toString()
I think you can achieve this functionality with a custom dialog functionality
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
EdittText text = (EdittText ) dialog.findViewById(R.id.textDialog);
Spinner spinimage = (Spinner) dialog.findViewById(R.id.spinner);
//spinner details here
dialog.show();
Button ok= (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
String text = text.getText().toString();
//do something with the edittext value
}
});
please make a try and share your view
Thanks for the suggestions, i made the following changes to the code and was able to use both an edittext and a spinner in the same alertdialog
public boolean dialogValuessP1() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
alertDialogBuilder.setTitle("Connecting Options");
LayoutInflater li = LayoutInflater.from(context);
final View dialogView = li.inflate(R.layout.option, null);
final EditText input = (EditText) dialogView
.findViewById(R.id.txtConnectedBy);
spinnercategory = (Spinner) dialogView.findViewById(R.id.viewSpin);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.category, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnercategory.setAdapter(adapter);
spinnercategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
position = arg2 + 1;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setPositiveButton("Connect",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
edittextValue = input.getText().toString();
connectedV = Integer.parseInt(edittextValue);
scoreV = position * connectedV;
playNext();
alert = true;
}
});
alertDialogBuilder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getBaseContext(),
"It is not your turn to Play",
Toast.LENGTH_LONG).show();
alert = false;
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
return true;
}
I'm new to Android Sqlite DB Dev't so I need your help guys. I was able to do the basic CRUD operations. Now I want to know how to refresh the listview to display the newly added data. I've looked into adapter.notifyDataSetChanged but I wonder why it's not working for me. Here's what I've tried so far
private void initControls() {
SearchAuto = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
String[] SearchItems = getResources().getStringArray(R.array.DogSearch);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.searchitems,SearchItems);
SearchAuto.setAdapter(adapter);
SearchAuto.setThreshold(0);
search = (Button) findViewById (R.id.handle);
search.setOnClickListener(this);
lv = (ListView) findViewById (R.id.lv);
values = dbHelper.getAllAnimals(ANIMALTYPE);
for (int i = 0; i < values.length; i++) {
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
}
// TODO onItemClick
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String strDog = parent.getItemAtPosition(position).toString();
Intent i = new Intent(DogClass.this, Dogs.class);
i.putExtra("dog_name", strDog);
startActivity(i);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.addanimal, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// get prompt_addprayer.xml view
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.prompt_addanimal, null);
AlertDialog.Builder alertDialogBuilder =
new AlertDialog.Builder(this);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText etBreed = (EditText) promptsView
.findViewById(R.id.etBreed);
final EditText etDescription = (EditText) promptsView
.findViewById(R.id.etDescription);
final EditText etDiet = (EditText) promptsView
.findViewById(R.id.etDiet);
final EditText etShelter = (EditText) promptsView
.findViewById(R.id.etShelter);
final EditText etHygiene = (EditText) promptsView
.findViewById(R.id.etHygiene);
final EditText etMedication = (EditText) promptsView
.findViewById(R.id.etMedication);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
String strAnimalType = ANIMALTYPE;
String strBreed = etBreed.getText().toString();
String strDesc = etDescription.getText().toString();
String strDiet = etDiet.getText().toString();
String strShelter = etShelter.getText().toString();
String strHygiene = etHygiene.getText().toString();
String strMedication = etMedication.getText().toString();
dbHelper.addAnimalInfo(strAnimalType, strDesc,
strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();
lvAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
return true;
}
Any ideas? I really need to get this working. Thanks
dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();
lvAdapter.notifyDataSetChanged();
This is where you lacked. What you did is, you just inserted data into database, still you are remaining to bring data from database and bind it with adapter, and after that you may call notifyDataSetChanged();, and you'll achieve what you want...
Try this:
dbHelper.addAnimalInfo(strAnimalType, strDesc, strDiet, strShelter, strHygiene, strMedication, strBreed);
Toast.makeText(DogClass.this, "Data has been added successfully!",
Toast.LENGTH_SHORT).show();
values = dbHelper.getAllAnimals(ANIMALTYPE);
lvAdapter.clear();
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lvAdapter.notifyDataSetChanged();
Note: you should not write this way. Your code:
for (int i = 0; i < values.length; i++) {
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
}
If your getAllAnimals() method is returning an arrayList then you simply need to:
lvAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
lv.setAdapter(lvAdapter);
The last argument of your ArrayAdapter constructor needs List<Item> to be passed. And then just call setAdapter(adapter) of ListView.
For more clarification, refer this SO link: notifyDataSetChanged example
http://androidadapternotifiydatasetchanged.blogspot.in
I am new to Android. I am stuck, need help.
I have a List as my Activity. OnItemLongClickListener for any element of a List, I am displaying a Dialog with Custom List (Red, Green, Blue) & on the selection of any of the items (Red, Green, Blue) I need to change the back color of the selected item (on which the event raised the Dialog) of a List (main activity).
The dialog box pops, but I am stuck how to get the selected item (of Dialog). Below is my code.
public class SimpleList extends ListActivity
{
String[] contactNames = {"Name 1", "Name 2", "Name 3", "Name 4", "Name 5", "Name 6"};
ArrayAdapter<String> contactAdpater;
String itemSelected;
String choosenColor;
private final Context context = this;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ArrayList<String> myContactList = new ArrayList<String>(Arrays.asList(contactNames));
OnItemLongClickListener itemChangeColorListener = new OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View arg1, int position, long arg3) {
itemSelected = parent.getItemAtPosition(position).toString();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final String[] colorNames = {"Red","Green","Blue"};
builder.setTitle("Pick a Colour!")
.setItems(colorNames, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
choosenColor = colorNames[which];
//Toast.makeText(getApplicationContext(), colorNames[which], Toast.LENGTH_SHORT).show(); <<-- its working fine here
//I am not able to access parent here... I want to perform,
**//parent[position].setBackgroundColor(Color.RED); in case Red is selected from Dialog**
}
});
builder.show();
return false;
}
};
contactAdpater = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
myContactList);
setListAdapter(contactAdpater);
getListView().setOnItemLongClickListener(itemChangeColorListener);
}
}
Yes got your problem
You have to use view's setBackgroundColor(int color); for changing the color if selected item of first ListView with the color selelcted from Dialog.
So In onClick you must use :
choosenColor = colorNames[which];
if(choosenColor.equals("Red"))
{
view.setBackgroundColor(Color.RED);
}
else if(choosenColor.equals("Blue"))
{
view.setBackgroundColor(Color.BLUE);
}
else if(choosenColor.equals("Green"))
{
view.setBackgroundColor(Color.GREEN);
}
I'm using a custom ListView with a Title and a Subtitle where you can read a brief explanation of the item.
For each item on the list, I'm displaying an AlertDialog to select an option (different for each case). When the option is selected, i want to change the Subtitle for the option selected by the user.
This is what I've tried:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
case 1:
final CharSequence[] alertText2 = {"1", "2", "3", "5", "10", "20", "60"};
ventana.setTitle("Max. duration");
ventana.setItems(alertText2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText2[item]);
}
});
ventana.show();
break;
case 2:
final CharSequence[] alertText3 = {"3", "5", "10", "20", "30", "60"};
ventana.setTitle("Time between events");
ventana.setItems(alertText3, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
TextView subTitulo = (TextView) findViewById(R.id.subTitulo);
subTitulo.setText(alertText3[item]);
}
});
ventana.show();
break;
For the first item on the list it works fine, when I select an option, the subtitle get replaced by that option, but when I make a selection in the AlertDialogs of the other 2 items, the option selected replaces the subtitle of the first item!
Any idea of how can I fix that?
Since nobody answered the question and i find a solution, im going to publish it here to help other people who eventually can be facing the same problem or a similar one :D
I just remove the TextView subTitulo = (TextView) findViewById(R.id.subTitulo); from each case and added it before the switch starts but "taking" the view argument on the onClick function (the type final, is because Eclipse warned me about it :P) : final TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
The code looks like this:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView subTitulo = (TextView) view.findViewById(R.id.subTitulo);
switch(position){
case 0: final CharSequence[] alertText1 = {"Area 1", "Area 2", "Area 3"};
ventana.setTitle("Choose an Area");
ventana.setItems(alertText1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
subTitulo.setText(alertText1[item]);
}
});
ventana.show();
break;
[...]