I'd need your help to do something in Android, in the following the use case.
I have created a custom dialog in Android, whose layout is:
dialog_threshold.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp">
<ImageView
android:src="#drawable/ic_media_route_on_03_dark"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/set_target" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner_threshold" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout_threshold">
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/targetSelected"
android:layout_weight="1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/threshold_operator_spinner"
android:layout_weight="1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/threshold_val"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
And in the Activity, when I click on a specific button, I have something like this, to create the custom dialog:
setThreshold_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder mBuilder = new AlertDialog.Builder(DiseaseActivity.this);
//Inflate the custom layout
View mView = getLayoutInflater().inflate(R.layout.dialog_threshold, null);
//Set title for dialog
mBuilder.setTitle("Set thresholds for your target");
//Define the spinner inside your custom layout
final Spinner mSpinner = (Spinner) mView.findViewById(R.id.spinner_threshold); //because it doesn't exist in the main layout, but only in the custom layout
//Define the ArrayAdapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(DiseaseActivity.this,
android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.threshold_choices));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(adapter);
if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
String ThresholdSelection = mSpinner.getSelectedItem().toString();
}
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if ((dialog2 != null) && dialog2.isShowing() && !mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
} else {
dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
//Set the positive and negative button for the custom dialog
mBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
disease.setThreshold(mSpinner.getSelectedItem().toString());
if (!mSpinner.getSelectedItem().toString().equalsIgnoreCase("Choose a threshold option")) {
Toast.makeText(DiseaseActivity.this,
mSpinner.getSelectedItem().toString(),
Toast.LENGTH_SHORT)
.show();
setInterval_button.setEnabled(true);
}
}
});
mBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
mBuilder.setView(mView);
dialog2 = mBuilder.create();
dialog2.show();
dialog2.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
}
});
What I want to do now is to add dynamically other components (like EditText or TextView) to the above Dialog before is it shown.
How can I do it?
Thanks in advance!
just create your custom view >> find the parent(in which viewgroup you want to add) >>> and add it. eg:-
mView.addView(new TextView(ActivityContext),LayoutParamss);
mBuilder.setView(mView);
Related
Are there any possibilities to check what view inside ListView Item was clicked?
In other words, when you click on different Views inside ListView item app should perform a different action.
In details, I have a simple Book.java class that contains some book description.
Then I create ListView<Book> using BooksAdapter.class:
public class BooksAdapter extends ArrayAdapter<Book> {
public BooksAdapter (Context context, List<Book> books) {
super(context,0,books);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
// ConstraintLayout constraintLayout = new ConstraintLayout();
// constraintLayout.setVisibility();
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.books_list_item, parent, false);
}
Book currentBook = getItem(position);
ImageView coverView = (ImageView) listItemView.findViewById(R.id.preview_image_view);
if (currentBook.getImage() == "No cover") {
coverView.setImageResource(R.drawable.no_book_cover);
} else {
Picasso.get().load(currentBook.getImage()).into(coverView);
}
TextView authorTextView = (TextView)listItemView.findViewById(R.id.autor_text);
authorTextView.setText(formatAuthor(currentBook.getAuthor(),currentBook.getDate()));
TextView titleTextView = (TextView)listItemView.findViewById(R.id.title_text);
titleTextView.setText(currentBook.getTitle());
//TextView descrTextView = (TextView)listItemView.findViewById(R.id.description_text);
//descrTextView.setText(currentBook.getDescription());
return listItemView;
}
private String formatAuthor (String name,String date ) {
name = name.substring(2,name.length()-2);
date = date.substring(0,4);
String fullString = name + ", " + date;
return(fullString);
}
}
books_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:paddingEnd="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingStart="16dp"
android:paddingBottom="16dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/preview_image_view"
android:layout_width="80dp"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/autor_text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:maxLines="1"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:textColor="#color/textColorPrimary"
android:textSize="16sp" />
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="0dp"
android:textColor="#color/textColorLight"
android:textSize="16sp" />
</LinearLayout>
<ImageView
android:layout_width="40dp"
android:layout_height="match_parent"
android:src="#drawable/ic_info_black_24dp"
android:scaleType="center"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#android:color/darker_gray"/>
</LinearLayout>
In MainActivity.java create mAdapter and override setOnItemClickListener:
ListView booksListView = (ListView) findViewById(R.id.list);
mAdapter = new BooksAdapter(this, new ArrayList<Book>());
booksListView.setAdapter(mAdapter);
booksListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Book book = mAdapter.getItem(position);
ad = new AlertDialog.Builder(BooksListActivity.this);
ad.setTitle("Book description");
ad.setMessage(book.getDescription());
AlertDialog alert = ad.create();
alert.show();
//ad.create();
}
});
And here are some general question can we set onClickListeners on different Views inside Item (#+id/autor_text and #+id/title_text for example) and perform different actions in these cases?
I'm reading about this several places but doesn't find any helpful things.Thanks for any help.
Inside your getView() method set onClickListener to all your different Views and perform respective action.
By implementing this code:-
authorTextView.setOnClickListener(this);
titleTextView.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.authorTextView:
//code to be written to handle the click event
break;
case R.id.titleTextView:
//code to be written to handle the click event
break;
}
}
};
Inside your getView
authorTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});
titleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your action
}
});
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 know they are similar questions but I could not make them work. I have a list fragment which includes the item in my array list and a button next to it for each element in my array list. My ultimate goal is to make program respond only when the user clicks to button but I could not even manage to detect the clicks on the screen. I also tried setting button's focusable to false(suggested from other questions) but that also did not work. Here is my code.
public class ResultListFragment extends ListFragment {
private List<String> listValues, keyValues;
private String email, username;
private ArrayAdapter<String> myAdapter;
private ListView myListView;
private TextView title;
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_resultlist, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View v = getView();
myListView = getListView();
listValues = new ArrayList<String>();
myAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),
R.layout.fragment_rowlayout, R.id.myListText, CameraActivity.resultList);
setListAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
#Override
public void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Log.d("blabla", "onListItemClick: clicked to : "+position);
final String delete=CameraActivity.resultList.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle("DELETION");
builder.setMessage(delete + " delete it.");
builder.setPositiveButton("Onayla",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity().getApplicationContext(), delete+ "has been deleted", Toast.LENGTH_SHORT).show();
CameraActivity.resultList.remove(position);
myAdapter.notifyDataSetChanged();
for(String st:CameraActivity.resultList){
Log.d("TAG", "onClick: eleman: " +st);
}
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
Here are my xml files
fragment result list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:background="#drawable/cembutton"
android:text="Yükle"
android:id="#+id/load"
android:layout_alignRight="#+id/results"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentBottom="true"
android:textColor="#ffffff"
android:textStyle="bold"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>
</RelativeLayout>
and fragment_rowlayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
>
<TextView
android:id="#+id/myListText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:textStyle="bold"
android:textColor="#3700ff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawable="#drawable/cembutton"
android:layout_alignParentRight="true"
android:text="Çıkart"
android:layout_marginRight="50dp"/>
</RelativeLayout>
You should provide custom adapter for your listview.
Then, in getView() method you can find your button by id and set onClickListener to it.
I'm trying to create a custom dialog for the settings of my application, the problem is, that the custom buttons, EditText & the spinner don't respond to user activity. I hope someone can help.
Here is my Code:
public class SettingsDialog extends DialogFragment {
private EditText editText;
private Spinner ipSpinner;
private MainActivity mainActivity;
private Settings settings;
private int selectedIP;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_settings, null);
mainActivity = (MainActivity) getActivity();
settings = mainActivity.getSettings();
selectedIP = settings.getSelectedIP();
editText = (EditText) dialogView.findViewById(R.id.edit_text_ip);
ipSpinner = (Spinner) dialogView.findViewById(R.id.spinner_ip);
ImageButton imageButton = (ImageButton) dialogView.findViewById(R.id.image_button_save_ip);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
});
ipSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
selectedIP = position;
editText.setText(settings.getIP(selectedIP));
mainActivity.showToast(Integer.toString(position));
settings.settingsSave();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
return;
}
});
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
String IP = editText.getText().toString();
settings.setIP(selectedIP, IP);
settings.settingsSave();
mainActivity.showToast("IP at " + Integer.toString(selectedIP) + " with value" + IP);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SettingsDialog.this.getDialog().cancel();
}
});
return builder.create();
}
}
and my xml for the dialog:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="IP-address"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="#+id/spinner_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="#array/spinner_ip_visual"
android:entryValues="#array/spinner_ip_values" />
<EditText
android:id="#+id/edit_text_ip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="8" />
<ImageButton
android:id="#+id/image_button_save_ip"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:src="#drawable/ic_done_black_24dp"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unit"
android:textAllCaps="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/button_unit_celsius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Celsius"
android:textAllCaps="true" />
<RadioButton
android:id="#+id/button_unit_fahrenheit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenheit"
android:textAllCaps="true" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickGenerateRandomData"
android:text="Generate random data"
android:textAllCaps="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onButtonClickDropDatabase"
android:text="delete weatherdata"
android:textAllCaps="true" />
Thanks in advice
You are inflating dialogView and then you set listeners for the views that are in dialogView, however when you set the view for dialog builder, you inflate the new view.
This is wrong.
builder.setView(inflater.inflate(R.layout.dialog_settings, null))
This is correct
builder.setView(dialogView)
I am trying to alert multi selection dropdown check box in alert dialog like spinner but with check boxes in my android application ,can any one help here?
It may not work properly with respect to touch events. I guess that you will have to clone it and develop something like MultiSelectSpinner. You might wanna consult this answer for further detail.
Use customlayout in AlertDialog class using api, dialog.setconteView() method. You can add whatever widgets you wish in your custom layout
Use below code:-
AlertDialog.Builder builder = new AlertDialog.Builder(
context);
builder.setTitle("Title");
builder.setMultiChoiceItems(list, null,
new DialogInterface.OnMultiChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog,
int indexSelected, boolean isChecked)
{
}
})
.setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog,
int id)
{
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int id)
{
}
});
dialog = builder.create();
dialog.show();
As your view, Cant get the click events in AlertDialog.
Best solution for your Question. Just include the custom layout and get the Alertdialog Click events in your activity.
create a XML layout which you want to design
Include that XML layout in your activity through SetContentView()
Get the click events through your activity.
Please check this link for reference http://www.mkyong.com/android/android-custom-dialog-example/
Happy Coding :)
for Custom checkbox dropdown Dialogbox first we need to create MultiCheckAdaptar
public class MultiCheckAdaptar extends RecyclerView.Adapter<MultiCheckAdaptar.MyViewHolder> {
protected Context context;
private LayoutInflater inflater;
private ArrayList<ModelSpacialization> joblist ;
private String TYPE = "";
private int count = 0 ;
MultiCheckAdaptar.OnItemClickListener listener;
public interface OnItemClickListener {
void onClick(ModelSpacialization jobs, int pos , boolean type);
}
public MultiCheckAdaptar(Context context, ArrayList<ModelSpacialization> list, MultiCheckAdaptar.OnItemClickListener listener) {
this.context = context;
this.listener = listener;
if (context != null) {
inflater = LayoutInflater.from(context);
this.joblist= new ArrayList<>();
this.joblist.addAll(list);
}
}
public MultiCheckAdaptar.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.layout_ckeck_item , parent, false);
MultiCheckAdaptar.MyViewHolder holder = new MultiCheckAdaptar.MyViewHolder(view);
return holder;
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public void onBindViewHolder(#NonNull final MultiCheckAdaptar.MyViewHolder holder, final int position) {
ModelSpacialization item = joblist.get(position);
holder.txt_item.setText(item.getName());
if(item.isCheck())
holder.txt_item.setChecked(true) ;
else
holder.txt_item.setChecked(false) ;
holder.txt_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
listener.onClick(item, position, false);
}
});
}
#Override
public int getItemCount() {
return joblist.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
CheckBox txt_item ;
public MyViewHolder(View itemView) {
super(itemView);
txt_item = itemView.findViewById(R.id.txt_item);
}
}
}
layout_check_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/txt_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/size_10"
android:text="text"
android:textSize="#dimen/size_16" />
</RelativeLayout>
dialog_dropdwon_recyle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:text="TItle"
android:id="#+id/tv_dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="#color/primary"
android:fontFamily="#font/poppins_medium"
android:textSize="#dimen/size_16"
tools:ignore="SpUsage" />
<ImageButton
android:id="#+id/btn_dialog_close"
android:layout_width="#dimen/size_25"
android:layout_height="#dimen/size_25"
android:layout_alignParentEnd="true"
android:layout_gravity="right"
android:background="#drawable/btn_grey_transparent"
android:elevation="1dp"
android:padding="#dimen/size_5"
android:src="#drawable/ic_close" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_dialog_close"
android:paddingHorizontal="#dimen/size_5"
android:orientation="vertical">
<EditText
android:id="#+id/edit_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search..."
android:visibility="gone" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/dialog_list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/size_10"
android:scrollbars="none" />
<Button
android:id="#+id/btn_save"
android:layout_width="match_parent"
android:layout_height="#dimen/size_40"
android:layout_marginHorizontal="#dimen/size_10"
android:layout_marginTop="#dimen/size_10"
android:layout_marginBottom="#dimen/size_10"
android:background="#drawable/button_primary"
android:fontFamily="#font/poppins_medium"
android:text="Save"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="#dimen/size_15" />
</LinearLayout>
</RelativeLayout>
in Activity
Dialog dg_industries = new Dialog(context);
selected_spaci = new HashSet<>();
industresList.addAll(industries2);
selected_spaci = new HashSet<>();
dg_industries.setContentView(R.layout.dailogbox_dwondwon_recycle);
final RecyclerView indview = (RecyclerView) dg_industries.findViewById(R.id.dialog_list);
final TextView title = (TextView) dg_industries.findViewById(R.id.tv_dialogTitle);
title.setVisibility(View.VISIBLE);
title.setText("Select Specialization");
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
indview.setLayoutManager(linearLayoutManager);
adaptor = new MultiCheckAdaptar(context, industresList, new MultiCheckAdaptar.OnItemClickListener() {
#Override
public void onClick(ModelSpacialization jobs, int pos, boolean type) {
jobs.setCheck(type);
selected_spaci.add(jobs);
Log.e("industries2", new Gson().toJson(industresList));
}
});
dg_industries.setCancelable(false);
dg_industries.show();