DragSortListView and AlertDialog? - android

I have been trying to get this library:
https://github.com/bauerca/drag-sort-listview/
to work inside an AlertDialog. But I have has no success so far. Everything looks perfect but the drag and drop does not happen.
Any suggestion it can be done? If there is no way to do this inside a AlertDialog, is there a way to do this without using intents?
Here is my code:
public class FolderSorter
{
Context parent;
ArrayList<String> lists;
ArrayAdapter<String> adapter;
public FolderSorter(Context context)
{
this.parent = context;
lists = new ArrayList<String>();
adapter = new ArrayAdapter<String>(parent, R.layout.list_item1,
R.id.text1);
}
public void setList(List<String> mlist)
{
this.lists.addAll(mlist);
}
public void doSort()
{
LayoutInflater factory = LayoutInflater.from(parent);
final View lview = factory.inflate(R.layout.sort_folders, null);
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
builder.setTitle("Sort Folders");
builder.setView(lview);
// ListView list = (ListView) lview.findViewById(R.id.listView1);
DragSortListView list2 = (DragSortListView) lview
.findViewById(R.id.dragSortListView1);
adapter.addAll(lists);
// list.setAdapter(adapter);
list2.setAdapter(adapter);
// TODO: set buttons OK and CANCEL
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
}
});
final Dialog dialog = builder.create();
dialog.show();
}
private DragSortListView.DropListener onDrop = new DragSortListView.DropListener()
{
#Override
public void drop(int from, int to)
{
String item = adapter.getItem(from);
adapter.remove(item);
adapter.insert(item, to);
}
};
private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener()
{
#Override
public void remove(int which)
{
adapter.remove(adapter.getItem(which));
}
};
}

I had a similar problem when using the DragSortListView inside a dialog fragment - turned out this only happened when the soft keyboard was visible when the fragment was being displayed. I'm not sure what the exact issue is, but I think something is either interfering with the focus or touch events the DragSortListView uses.
As a work around I hide the softkeyboard before displaying the fragment, this appears to solve the issue for now - see the below answer for the code on how to do this:
https://stackoverflow.com/a/7696791/1062909

Related

AlertDialog - do not dismiss on item click

OK so I am creating an ArrayAdapter and using it in my Alert Dialog because I don't want to show the default radio buttons on SingleItemSelection dialog.
Instead I want to change the background of the item that is selected, and then when the user presses the positive button I will perform the action related to the item that has been selected.
private void showAlertDialog()
{
final String[] options = getResources().getStringArray(R.array.dialog_options);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, options);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("My Dialog");
dialogBuilder.setAdapter(adapter, new OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
Toast.makeText(getApplicationContext(), "item clicked at index " + which, Toast.LENGTH_LONG).show();
// Here I need to change the background color of the item selected and prevent the dialog from being dismissed
}
});
//String strOkay = getString(R.string.okay);
dialogBuilder.setPositiveButton("OK", null); // TODO
dialogBuilder.setNegativeButton("Cancel", null); // nothing simply dismiss
AlertDialog dialog = dialogBuilder.create();
dialog.show();
}
There are two problems I'm trying to tackle.
How do I prevent the dialog from being dismissed when the user clicks on an item
How do I change the background of the item that has been selected when the user clicks on it
To prevent dialog from dismissing on item click you can use AdapterView.OnItemClickListener instead of DialogInterface.OnClickListener.
Like this:
dialogBuilder.setAdapter(adapter, null);
...
AlertDialog dialog = dialogBuilder.create();
alertDialog.getListView().setOnItemClickListener(
new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// do your stuff here
}
});
You can set custom ListView as content of AlertDialog and set OnItemClickListener
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String[] items = ...;
ListView list = new ListView(this);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.select_dialog_item, items));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int pos, long id) {
...
}
});
builder.setView(list);
and then save reference to dialog
mDialog = builder.show();
in order to dismiss it if necessary
mDialog.dismiss();
How do I prevent the dialog from being dismissed when the user clicks on an item
How do I change the background of the item that has been selected when the user clicks on it
Here is example
public class MainActivity extends AppCompatActivity {
private static final String listFragmentTag = "listFragmentTag";
private static final String data[] = {"one", "two", "three", "four"};
public MainActivity() {
super();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnClick(View v) {
ListFragment lf = new ListFragment();
lf.show(getSupportFragmentManager(), listFragmentTag);
}
public static class ListFragment extends DialogFragment {
#Override #NonNull
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity());
adb.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("List")
.setItems(data, null)
.setPositiveButton("OK", null); // pass your onClickListener instead of null
// to keep dialog open after click on item
AlertDialog ad = adb.create();
ad.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
private int colorOrg = 0x00000000;
private int colorSelected = 0xFF00FF00;
private View previousView;
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// restoring color of previous view
if(previousView != null) {
previousView.setBackgroundColor(colorOrg);
}
// changing items's BG color
view.setBackgroundColor(colorSelected);
previousView = view;
}
});
return ad;
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
}
}
You can use setCanceledOnTouchOutside(false) or setCanceleable(false).
Set selector for the root element tag of the dialog layout xml.

Highlight selected Spinner Item

I have implemented Custom Spinner using Button following wildnove's answer. Everything works fine, but I am not able to display the highlighted radio button for the selected button.
Below is the code.
((Button) findViewById(R.id.btnSpinnerPlanets)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// How to highlight Radio button of a selected Item???
final String[] items = view.getResources().getStringArray(R.array.planets__entries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
((Button) findViewById(R.id.btnSpinnerPlanets)).setText(items[which]);
dialog.dismiss();
}
}).create().show();
}
});
Can somebody help me how to highlight selected Item's Radio button ...
Unfortunately this behavior is not natively implemented in Spinner component, however, you can always create your own BaseAdapter to show whatever you need weather is in the spinner it self or in the dropdown like this:
private class ExampleAdapter extends BaseAdapter{
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//Here is where you actually get the chance to return whatever you want in the spinner component (the single bar with the arrow)
return yourCommonView;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
//Here is where you get the chance to return whatever you want in the dropdown menu so here you should validate what's the currently selected element and return an image accordingly...
return yourSelectedView;
}
}
The important method here is, getDropDownView that is the one that gives you the chance to return an element with a checked CheckBox, or any mark you want to use, of course you have to create your own layout and validate if the element currently created need to be marked or not...
Regards!
The problem with this code is that you are creating the Spinner each time the Button is clicked. Try the following code:
#Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
switch(id) {
case 1:
Button b=((Button) findViewById(R.id.btnSpinnerPlanets));
builder = new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(get_the_adapter(b), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
b.setText(b.getResources().getStringArray(R.array.planets__entries)[which]);
dismissDialog(1);
}
})
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
public ArrayAdapter<String> get_the_Adapter(Button view){
String[] items = view.getResources().getStringArray(R.array.planets__entries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
return adapter;
}
And for the Button's onClick():
((Button) findViewById(R.id.btnSpinnerPlanets)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDialog(1);
}
});

Dialog from button on a customized ListView

I have a ListView and I populated it dinamically through an Adapter. Each row of my ListView have a button and I want to show a DialogBox when it is clicked. However I dunno how to pass the context to my AlertDialog, so it can show up on my activity. What I have is something like this:
....//ADAPTER
public View getView(int position, View view, ViewGroup parent) {
....
pay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder ad = new AlertDialog.Builder();
ad.setMessage("Are you sure?");
ad.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = ad.create()();
dialog.show();
}
});
....
}
I guess I must add some kind of listener at the activity which extends my ListView but Im lost on how do it and the documentation is way too large, couldn't find the solution.
You can pass an activity context to your adapter in a constructor and then use it in your adapter class:
public class MyAdapter {
Context context;
public MyAdapter(Context context, ... ) {
this.context = context;
}
}

How to create a dialog box having multiple types of lists in android?

I want to create a dialog box which contains a simple list having 2 choices. But I also want to add a single check-box entry in the bottom. Here is the code.
final String s1 = "item 1";
final String s2 = "item 2";
final CharSequence[] items = { s1, s2 };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item].toString().equalsIgnoreCase(s1)) {
item1selected();
} else if (items[item].toString().equalsIgnoreCase(s2)) {
item2selected();
}
}
}).create();
builder.setMultiChoiceItems(R.array.select_dialog_item,
new boolean[]{false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
preferences.edit().putBoolean(getString(R.string.pref_entry), isChecked).commit();
}
}).create();
builder.show();
Here, only the check-box item is shown.
You cannot combine the two.
setMultiChoiceItems is defined as:
public Builder setMultiChoiceItems(int itemsId, boolean[] checkedItems,
final OnMultiChoiceClickListener listener) {
P.mItems = P.mContext.getResources().getTextArray(itemsId);
P.mOnCheckboxClickListener = listener;
P.mCheckedItems = checkedItems;
P.mIsMultiChoice = true;
return this;
}
which resets the items set before.
public Builder setItems(CharSequence[] items, final OnClickListener listener) {
P.mItems = items;
P.mOnClickListener = listener;
return this;
}
The way to do what you are trying is to use a custom view in your dialog [1] or use the mergeadapter[1] and combine the different lists to one adapter and set it to the alertdialog using setAdapter.
[1] http://developer.android.com/reference/android/app/AlertDialog.html#setView(android.view.View, int, int, int, int)
[2] https://github.com/commonsguy/cwac-merge

Access controls within custom alert dialog in android

When displaying custom dialog box which is showing textView and EditView. How can i access these elements within dialog. Below is the code which is giving error.
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
string = uetd.getText().toString() + ":" + petd.getText().toString(); /////producing error
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
public class listdemo extends ListActivity {
/** Called when the activity is first created. */
private static final int DIALOG_TEXT_ENTRY = 7;
EditText uetd;
EditText petd;
SharedPreferences.Editor editor;
String string;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
uetd=(EditText)findViewById(R.id.username_edit);
petd=(EditText)findViewById(R.id.password_edit);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
showDialog(DIALOG_TEXT_ENTRY);
}
});
}
}
Instead of returning the dialog immedately with return new AlertDialog.builder....and so on....you should save an instance of the dialog by creating a class variable just like this
AlertDialog dialog;
Then you can easily access the views of the dialog within the callback of the dialog:
dialog.findViewById(R.id.username_edit);
You have to do this because when creating the callback it is in a way constructing a new class in memory so when you call findViewById() directly it can't find the activity
try this code
uetd=(EditText) textEntryView.findViewById(R.id.username_edit);
after following line in your code
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);

Categories

Resources