I am running into an issue and don't know the best way to use a set of drawables inside an AlertDialog. The AlertDialog is presented when a user presses a button and is prompted to choose from a list of drawables.
Currently it shows Buttons I have placed in a dialog_view layout when the dialog is created. Ideally I would like to be able to use some type of listview, but if that is not possible I would like to be able to handle the selection/pressing of an item when inside the AlertDialog.
What should I do to handle any actions from within the AlertDialog?
DIALOGFRAGMENT
public class PicturePickerFragment extends DialogFragment {
ArrayList<Integer> imageList = new ArrayList<Integer>();
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// fill an array with selected images
String title = "Picture";
imageList.add(R.drawable.barbershop);
imageList.add(R.drawable.wedding);
imageList.add(R.drawable.meeting);
imageList.add(R.drawable.barbershop);
// return alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_view, null))
.setTitle(R.string.event_type)
.setPositiveButton(R.string.select_picture,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// call the method on the parent activity when
// user click the positive button
}
});
return builder.create();
}
}
DIALOG VIEW
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn_baby"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/baby"
android:text="Baby Shower" />
<Button
android:id="#+id/btn_baking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/baking"
android:text="Baking" />
<Button
android:id="#+id/btn_barber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/barbershop"
android:text="Barbershop" />
</LinearLayout>
You can use some ListAdapter such as an ArrayAdapter where you present a drawable for every item in the list and then use the adapter when you build the AlertDialog:
builder.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//which is the position of the item clicked
}
})
Edit: This is an example that would work with your imageList:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setAdapter(new ArrayAdapter<Integer>(getActivity(), R.layout.dialog_image, imageList) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.dialog_image, parent, false);
} else {
view = convertView;
}
ImageView imageView = (ImageView) view.findViewById(R.id.image);
int resId = getItem(position);
imageView.setImageResource(resId);
return view;
}
}, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Dialog", "selected position " + which);
}
});
return builder.create();
where R.layout.dialog_image contains an ImageView with id android:id="#+id/image" as root element. For example:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:minHeight="?attr/listPreferredItemHeight" >
</ImageView>
Related
I have created a Custom listview inside the AlertDialog and set data by parsing a list. I need to get the clicked value of the individual object of that clicked row. But listView.setOnItemClickListener is not working. I have tried to solve this problem, but couldn't find a way. Please help me to solve this.
Thanks..
here is my code
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select A Customer");
//insert array to constructor
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogLayout = inflater.inflate(R.layout.product_list_pop_up, null);
final CustomerPopupAdapter testAdapter = new CustomerPopupAdapter(getContext(), customers_data);
ListView listView = dialogLayout.findViewById(R.id.product_list_view);
TextView cancel_btn = dialogLayout.findViewById(R.id.cancel_btn);
TextView done_btn = dialogLayout.findViewById(R.id.done_btn);
listView.setAdapter(testAdapter);
builder.setView(dialogLayout);
final AlertDialog alert = builder.create();
alert.show();
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("selected Item", "ListView ");
// customerName = testAdapter.getItem(position);
// customer_name.setText((CharSequence) testAdapter.getItem(position));
alert.dismiss();
}
});
alert.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
cancel_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
done_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
here is my adapter
public class CustomerPopupAdapter extends ArrayAdapter<Customer> {
private List<Customer> list;
public CustomerPopupAdapter(Context context, List<Customer> test) {
super(context, R.layout.discount_popup_row, test);
this.list = test;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
final Customer customer = list.get(position);
View customRow = inflater.inflate(R.layout.customer_popup_row, parent, false);
TextView customer_name = customRow.findViewById(R.id.customer_name);
TextView customer_id = customRow.findViewById(R.id.customer_id);
customer_id.setText(customer.getId());
customer_name.setText(customer.getName());
return customRow;
}
}
here is the custom_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/match_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/activity_margin_10dp"
android:weightSum="3">
<TextView
android:id="#+id/customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Username"
android:inputType="text"
android:layout_weight="1"
android:textColor="#color/colorBlack"
android:textSize="#dimen/text_size_18sp" />
<TextView
android:id="#+id/customer_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="id"
android:inputType="text"
android:layout_weight="2"
android:textColor="#color/colorBlack"
android:textSize="#dimen/text_size_18sp" />
</LinearLayout>
<View
android:id="#+id/bottom_border"
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginTop="#dimen/activity_margin_5dp"
android:background="#color/colorGrey" />
</LinearLayout>
Use your customers_data list to get the clicked value.
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("selected Item", customers_data.get(position));
alert.dismiss();
}
});
You i am not sure why your code is not working if you can post the project that would be very really helpful or at least the whole activity ,so this is how you can easily display list in alert box .
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Choose an animal");
// add a list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
builder.setItems(animals, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // horse
case 1: // cow
case 2: // camel
case 3: // sheep
case 4: // goat
}
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
Check the full reference https://stackoverflow.com/a/43532478/9638167
Don't use ListView, read this: Should we use RecyclerView to replace ListView? and other materials on google, RecyclerView is the way to go
I think what you need is:
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
//element selected is yourList.get(position);
}
});
Actually, this is an answer for this question. I have added android: inputType="text" to the code. It was the problem. I removed it and then it works perfectly.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/match_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/activity_margin_10dp"
android:weightSum="3">
<TextView
android:id="#+id/customer_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Username"
android:layout_weight="1"
android:textColor="#color/colorBlack"
android:textSize="#dimen/text_size_18sp" />
<TextView
android:id="#+id/customer_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="id"
android:layout_weight="2"
android:textColor="#color/colorBlack"
android:textSize="#dimen/text_size_18sp" />
</LinearLayout>
<View
android:id="#+id/bottom_border"
android:layout_width="match_parent"
android:layout_height="0.8dp"
android:layout_marginTop="#dimen/activity_margin_5dp"
android:background="#color/colorGrey" />
</LinearLayout>
The answer was given by the "I_A_Mok", All the credits should goes to him.
Please remove the android:inputType="text" from your xml. this will solve your problem. Thanks.
I want to display an AlertDialog with an image. But the image may change depending on some circumstances.
This is the code I'm using for the AlertDialog:
AlertDialog.Builder alertadd = new AlertDialog.Builder(wheel.this);
LayoutInflater factory = LayoutInflater.from(wheel.this);
final View view = factory.inflate(R.layout.alert, null);
alertadd.setView(view);
alertadd.setTitle("Alert Dialog Title");
alertadd.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
And this is the alert.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/dialog_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
</LinearLayout>
Is there any way to programmatically access the imageView within the XML file so I can change the image? Or is there a better way to do that?
You can get a reference of ImageView by calling findViewById on your view and then call setImageResource on it :
LayoutInflater factory = LayoutInflater.from(wheel.this);
final View view = factory.inflate(R.layout.alert, null);
// change the ImageView image source
final ImageView dialogImageView = (ImageView) view.findViewById(R.id.dialog_imageview);
dialogImageView.setImageResource(R.drawable.your_image);
alertadd.setView(view);
alertadd.setTitle("Alert Dialog Title");
alertadd.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int sumthin) {
}
});
alertadd.show();
I have created a custom Alert Dialog with a ExpandableListView inside it. The ExpandableListView childs are Checkboxes.
I am trying to set the event listener whenever the user clicks on one of the checkboxes but it does not work. I can select and unselect the checkboxes in the custom alert dialog but the event handler does not work. Can anyone help me?
Code to create the alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select the route or segments");
LinearLayout layout = (LinearLayout) findViewById(R.id.custom_alert_dialog);
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_alert_dialog_routes, null);
ExpandableListView listView = (ExpandableListView) dialoglayout.findViewById(R.id.routesListView);
ExpandableListAdapter listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
listView.setAdapter(listAdapter);
listView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Toast.makeText(getApplicationContext(), "Clicked the checkbox", Toast.LENGTH_LONG).show();
Log.d("Click", "I have clicked a checkbox.");
return false;
}
});
builder.setView(dialoglayout);
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/expandedListItem"
android:textColor="#000000"
android:focusable="false"
android:layout_marginLeft="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"/>
</LinearLayout>
I have tried with the android:focusable and without it and it did not work.
Try using setOnItemClickListener instead of setOnChildClickListener.
I am trying to implement a custom DialogFragment, following this tutorial. My problem is that I fail to handle my custom's view's button.setOnClickListener event. The strangest part is that I have no problem in geting the .getText() of my button, I just can't find a way to handle the click event. Bellow is my code:
SettingsDialogFragment.java
public class SettingsDialogFragment extends DialogFragment
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_settings, null);
final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
String s = colorButton.getText().toString();
System.out.println("its working "+s);
//NOT working
colorButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
System.out.println("OnClick");
}
});
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
// Add action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id)
{
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
`
My custom view code (dialog_settings.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:scaleType="center"
android:background="#00CCCC"
android:contentDescription="#string/app_name"
android:text="#string/dialog_settings_title"
android:id="#+id/editText"/>
<Button
android:id="#+id/colorButton_dialogSettings"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/color_picker_title"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stroke"
android:layout_marginLeft="55dp"
android:id="#+id/radioButtonStroke"
android:checked="false"
android:layout_below="#+id/colorButton_dialogSettings"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fill"
android:id="#+id/radioButton_fill"
android:checked="false"
android:layout_below="#+id/colorButton_dialogSettings"
android:layout_toRightOf="#+id/radioButtonStroke"
android:layout_toEndOf="#+id/radioButtonStroke"
android:layout_marginLeft="10dp"
/>
I am just showing you the important part.. i hope you find their respective lines in your code
final View view = inflater.inflate(R.layout.dialog_settings, null);
// inflating your view..for drawback, this line is [A]
your colorButton has a reference to view.findViewById(R.id.colorButton_dialogSettings) which is from the viewgroup view..which you reference an onclick listener for it..
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
this code right here sets the content view for your dialog. it inflates a the layout and does it's work.. so in the end your builder is not referencing it's content view to view but a new inflated R.layout.dialog_settings layout..
so to solve it just do this
builder.setView(view) // hope you know the view parameter
the view is what you instantiated at line [A] ..
Hope i made sense, and lucid enough for you..let me know if it helps
New Answer
Change your onCreateDialog to this:
import android.view.View.OnClickListener;
public class SettingsDialogFragment extends DialogFragment implements onClickListener
{
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_settings, null);
final Button colorButton =(Button) view.findViewById(R.id.colorButton_dialogSettings);
String s = colorButton.getText().toString();
System.out.println("its working "+s);
colorButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.colorButton_dialogSettings
System.out.println("OnClick");
break;
default:
break;
}
});
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
// Add action buttons
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id)
{
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
In your activity:
private Button colorButton = (Button) findViewById(R.id.colorButton_dialogSettings);
**Old answer that requires you to write your own showDialog method**
Try deleting your button code in the onCreateDialog and add this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
Button colorButton =(Button) v.findViewById(R.id.colorButton_dialogSettings);
public void onClick(View v) {
// When button is clicked, call up to owning activity.
((FragmentDialog)getActivity()).showDialog();
System.out.println("OnClick");
}
});
return v;
I'm trying to show a NumberPicker in a dialog in order to let the user select a value between 0 and 10. It's my second day spent trying to make it work.
This is what I have:
fragment_number_picker (layout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/numberPickerFragmentLinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<NumberPicker
android:id="#+id/numberPickerInFragment"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:orientation="horizontal" />
</LinearLayout>
DialogFragment definition:
public class NumberPickerCustomDialog extends DialogFragment {
Context context;
public NumberPickerCustomDialog() {}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
context = getActivity().getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater li = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflate and set the layout for the dialog
View view = li.inflate(R.layout.fragment_number_picker, null);
builder
// Set view:
.setView(view)
// Add action buttons
.setPositiveButton(R.string.accept, new DialogInterface.OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int id) {
// Accept number
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
NumberPicker np = (NumberPicker) view.findViewById(R.id.numberPickerInFragment);
np.setMaxValue(200);
np.setMinValue(0);
np.setFocusable(true);
np.setFocusableInTouchMode(true);
return builder.create();
}
}
Call from the main Activity:
public void openNumberPicker(){
FragmentManager fm = getSupportFragmentManager();
NumberPickerCustomDialog npc = new NumberPickerCustomDialog();
npc.show(fm, "fragment_number_picker");
}
I'm getting an InvocationTargetException and I can't make it work.
Any ideas? Thanks!
Try changing the context you're using:
context = getActivity().getApplicationContext();
Instead, you need the activity's own context:
context = getActivity();