I had created AlertDialog by inflating the following XML:
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.activity_list_logs, null);
ListView list = (ListView) dialogView.findViewById(R.id.listDates);
The full code for showing alertDialog is:
private void showLogs(final List<Absentees> abs) {
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.activity_list_logs, null);
ListView list = (ListView) dialogView.findViewById(R.id.listDates);
list.setAdapter(new CustomAdapterTagAbsentees(this,abs));
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view,int position, long id) {
Absentees a =(Absentees)adapter.getItemAtPosition(position);
try
{
showLogDetails(a);
}
catch(Exception e)
{
show_popup(e+"");
}
}
});
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(dialogView);
alertDialogBuilder.setTitle("Attendance Details:");
alertDialogBuilder.setMessage("Day : ");
alertDialogBuilder
.setCancelable(true)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
The Layout Used :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
>
<ListView
android:id="#+id/listDates"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="20sp"
/>
</LinearLayout>
About My Problem:
if the List is small enough to fit the screen I can see "Dismiss" button(The positiveButton).
Else if the List is too large, I can't see the "Dismiss" Button
Kindly Help me!!, I tried to add a Button to Bottom of the within the layout itself, Its showing correctly, But i wonder why this default way of showing a positiveButton is not displaying at all
Use a RelativeLayout instead of a Linear. Put the buttons to layout_alignParentBottom="true". Put the listview to layout_above="id of button". This will force the buttons to the bottom of the screen and reserve space for them before the listview is drawn. Otherwise the listview is greedy and will suck up all the space needed.
Here is the final Solution for my problem, Thanks for #blackbelt for the suggestion
private void showLogs(final List<Absentees> abs) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
final ListAdapter adapter = new CustomAdapterTagAbsentees(this,abs);
alertDialogBuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
showLogs(abs);
Absentees a =(Absentees) adapter.getItem(item);
showLogDetails(a);
}
});
alertDialogBuilder.setTitle("Attendance Details:");
//alertDialogBuilder.setMessage("Day : XXXX");
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
I have to comment/remove
alertDialogBuilder.setMessage("Day : XXXX ");
for succesfull implementation, Dont know why!!
Related
I have successfully created a working AlertDialog for my Android application:
public class MyClass extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
ArrayList selectedItems = new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_toppings)
builder.setMultiChoiceItems(R.array.my_array, null, new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (isChecked) {
selectedItems.add(which);
} else if (selectedItems.contains(which)) {
selectedItems.remove(Integer.valueOf(which));
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
return builder.create();
}
}
This MultiChoiceItems list is backed by an array in /res/values/array.xml
<resources>
<array name="my_array">
<item>item 01</item>
<item>item 02</item>
<item>item 03</item>
<item>item 04</item>
<item>item 05</item>
</array>
</resources>
From my Activity, I call the AlertDialog this way:
MyClass myClass = new MyClass();
myClass.show(getSupportFragmentManager(), "My Dialog");
What I want to do now is use a custom layout with the AlertDialog so that I can do things like alternate-row shading, custom buttons, and add an EditText so I can have an "other" option with the ability to fill in the "other".
After doing some googling, it looks like I need to create a new layout, and set the view of the AlertDialog to this layout. So, I created a layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="other"
android:textSize="18sp"/>
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Then I added this to my DialogFragment class:
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
View view = layoutInflater.inflate(R.layout.my_new_layout, null);
then
builder.setView(view);
As you can guess, this did not work. The new CheckBox and EditText was inserted after my other checkboxes that were populated from my array, but it looks terrible, and I don't appear to have any control over the appearance of the checkboxes created from the array.
Like I said, I would like the ability to add this new CheckBox/EditText combination, as well as have the ability to customize the look of the entire AlertDialog.
I really want to use the array from /res/values/array.xml so that I do not have to hard code a new option if I want to add new items to the list.
Is what I am wanting to do possible? If so, some advice would be great.
Thanks
This is what I would like my AlertDialog to look/act like:
Ok, I finally figured this out on my own. Here is my resolution:
New Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right">
</LinearLayout>
New Class:
public class MyClass extends DialogFragment{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String[] theOptions = getResources().getStringArray(R.array.options);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_toppings)
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
LinearLayout view = (LinearLayout) layoutInflater.inflate(R.layout.my_layout, null);
for(String option : theOptions){
CheckBox checkbox = new CheckBox(getContext());
checkbox.setText(option);
view.addView(checkbox);
}
LinearLayout otherLinearLayout = new LinearLayout(getContext());
otherLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
otherLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
otherLinearLayout.setId(R.id.otherLinearLayout);
CheckBox otherCheckBox = new CheckBox(getContext());
otherCheckBox.setText("other");
otherCheckBox.setId(R.id.otherCheckBox);
EditText otherEditText = new EditText(getContext());
otherEditText.setId(R.id.otherEditText);
otherLinearLayout.addView(otherCheckBox);
otherLinearLayout.addView(otherEditText);
view.addView(otherLinearLayout);
builder.setView(view);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// do stuff here ...
}
});
return builder.create();
}
}
You can do something like this, get the array using
String[] myarray = getResources().getStringArray(R.array.testArray);
and the create new checkbox objects using each array item and set it to the inflated view
LinearLayout layout2 = new LinearLayout(context); layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
and loop through your array list
layout2.addView(new Checkbox(context));
and finally add the parent linear layout to the inflated view inflatedView.addView(layout2);
Create Custom Dialog Fragment and add all items and checkboxes inside
the new layout defined by programmatically i.e dynamically.Write the
code for checkItemSelectedListener in your code.
Good evening out there,
i am trying to use an alert dialog in an Fragment (Cause of the TabNavigation). It is nessesary that i use the layout "privacy".
But eclipse gave me an error at the "AlertDialog.Builder": (The constructor AlertDialog.Builder(AboutActivity2) is undefined)
and at the ".from" after the inflate: (The method from(Context) in the type LayoutInflater is not applicable for the arguments (AboutActivity2))
Thanks for help,
greetings
View rootView = inflater.inflate(R.layout.activity_about2, container, false);
rootView.findViewById(R.id.privacybutton).setOnClickListener(this);
return rootView;
}
final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.privacybutton:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
LayoutInflater factory = LayoutInflater.from(getActivity());
final View view = factory.inflate(R.layout.privacy, null);
alertDialog.setView(view);
alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
alertDialog.show();
break;
}
}
AlertDialog.Builder receives a context as parameter. And not a fragment.
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#AlertDialog.Builder(android.content.Context)
Use getActivity() instead :
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(getActivity());
LayoutInflater factory3 = LayoutInflater.from(getActivity());
You also need to add the listener to your button. You can do it like this:
View rootView = inflater.inflate(R.layout.activity_about2, container, false);
rootView.findViewByID(R.id.privacybutton).setOnClickListener(this);
return rootView;
FINAL CODE
View rootView = inflater.inflate(R.layout.activity_about2, container, false);
rootView.findViewById(R.id.privacybutton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
LayoutInflater factory = LayoutInflater.from(v.getContext());
final View view = factory.inflate(R.layout.privacy, null);
alertDialog.setView(view);
alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
return rootView;
your_dialouge_fragment dppr = new your_dialouge_fragment();
dppr.show(getActivity().getSupportFragmentManager(),"mmtag");
This worked for me.
You can use normal dialog for showing alert. Alert dialog has also derived from dialog, You need to just write an custom XML for view and set the view to dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
In my opinion, the problem is about Context and it's related to fragment.
Try getting the context of the application instead of the context of the class (AboutActivity2.this)
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(getActivity());
or
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(v.getContext());
Please try and tell if it works.
Better you go for the DialogFragment instead of AlertDialog or Dialog becasue DialogFragment is easy to implement and it has it own lifecycle methods which can be useful to handle other events or data handling.
See this Google link -
https://developer.android.com/guide/fragments/dialogs
I have created a Custom AlertDialog and I'm trying to set text for EditTexts. I have tried following
public class CheckListDialog extends DialogFragment {
private View view;
public CheckListDialog(View view) {
this.view = view;
}
#Override
public AlertDialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.dialog_checklist, null);
EditText etCost = (EditText) dialogLayout.findViewById(R.id.etCost);
EditText etOdoReading = (EditText) dialogLayout.findViewById(R.id.etOdometer);
etOdoReading.setText("bla");
etCost.setText("tada");
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(Reminders.this, android.R.color.transparent));
builder.setView(inflater.inflate(R.layout.dialog_checklist, null))
.setTitle("jaja")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
return builder.create();
}
}
The EditTexts are empty and does not contain the values. How can i fix this?
Replace
builder.setView(inflater.inflate(R.layout.dialog_checklist, null))
with
builder.setView(dialogLayout)
You're modifying the edittexts in one layout and then inflating a new layout for the dialog.
For such operations you have to use a custom layout to inflate into the CheckListDialog class of yours. DialogFragment will provide you complete freedom to customize your dialog as per your requirement.
You can refer to this tutorial
I am a self-taught beginner and appreciate patience! Thanks!
In Eclipse, I made a custom alertdialog with its own xml file ("custom_dialog") and it is called "usernamealert".
I want an alert to pop-up if the user hasn't entered a username yet (ie, username.length == 0).
Inside this layout I have a textView ("What is your name?"), editText and button ("usernameButton").
Before putting in the onclicklistener for the button, everything worked. This was my (relevant) Java:
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);
AlertDialog usernamealert = usernamebuilder.create();
When I put the onclicklistener in, it broke! Where should I have put it?
(the following is what I had tried... all in my OnCreate)
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus());
AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
usernamebuilder.setView(dialoglayout);
Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton);
usernameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//store username in sharedprefs
usernamealert.dismiss();
}
});
After the code I said:
if (username.length() == 0) {
usernamealert.show();
}
Again, it worked before I started messing with the button!!
It is needed to specify where will the code search the button, if its only "findViewById" it would search in the xml of the host, it should be
LayoutInflater inflater =getLayoutInflater();
View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(myview);
Button addS = (Button) myview.findViewById (R.id.bAddS);
This is part of my class, Hireteachernegotiate.class, which has a layout of hireteachernegotiate.xml
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater =getLayoutInflater();
View myview = inflater.inflate(R.layout.dialoghireteachernegotiate, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(myview);
Button addS = (Button) myview.findViewById (R.id.bAddS);
addS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do some stuff
}
});
Button minusS = (Button) myview.findViewById (R.id.bMinusS);
addS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do other stuff
}
});
// Add action buttons
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
This is the dialog layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/bAddS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/bMinusS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Try this.
usernamebuilder.setCancelable(false)
usernamebuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do what you want.
}
});
See if that works, or if it helps in some way.
I am trying to create a AlertDialog with multiple choice option. I have tried with the setMultiChoiceItems but what i have is a ArrayList<Category> and not a CharSequence so i tried with the adapter.
The problem with setAdapter is that when i select one item it closes the dialog window. And what i want is to select the items and then hit the OK button to see what items where selected.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
ArrayAdapter<Category> catsAdapter = new ArrayAdapter<Category>(this, android.R.layout.select_dialog_multichoice,this.categories);
builder.setAdapter(catsAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
}
});
builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//do something
}
});;
AlertDialog alert = builder.create();
alert.show();
Unfortunately, there doesn't seem to be an easy way to toggle on AlertDialog's multichoicemode without calling setMultiChoiceItems().
However, you can set an adapter, then turn on multichoice mode in the contained ListView itself.
final AlertDialog dialog = new AlertDialog.Builder(getActivity())
.setTitle("Title")
.setAdapter(yourAdapter, null)
.setPositiveButton(getResources().getString(R.string.positive), null)
.setNegativeButton(getResources().getString(android.R.string.cancel), null)
.create();
dialog.getListView().setItemsCanFocus(false);
dialog.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
dialog.getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// Manage selected items here
System.out.println("clicked" + position);
CheckedTextView textView = (CheckedTextView) view;
if(textView.isChecked()) {
} else {
}
}
});
dialog.show();
this will stop ur dialog from disappearing after one selection.
AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
}
});
To get which items are selected , you need to plan your adapter accordingly.
see below code it may help you. i used this in my app.
public static ArrayList<String> Party_list_new = new ArrayList<String>();
ArrayList<String> party_multi_cheked = new ArrayList<String>();
public void show_alert() {
final Dialog dia = new Dialog(this);
dia.setContentView(R.layout.alert_);
dia.setTitle("Select File to import");
dia.setCancelable(true);
final ListView list_alert = (ListView) dia
.findViewById(R.id.alert_list);
list_alert.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_multiple_choice,
Party_list_new));
list_alert.setItemsCanFocus(false);
list_alert.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list_alert.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
}
});
Button btn = (Button) dia.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SparseBooleanArray positions = list_alert
.getCheckedItemPositions();
int j = 0;
for (int k = 0; k < Party_list_new.size(); k++) {
if (positions.get(k)) {
party_multi_cheked.add("" + k);
}
}
dia.dismiss();
}
});
dia.show();
}
alert_list.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select Party" />
<ListView
android:id="#+id/alert_list"
android:layout_width="match_parent" android:padding="5dp"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
make it right if it is correct.