Apply style to spinner - android

I want to work with the spinner item.
I want the drop down to have the same width and height than the button.
How can I do this?
Finally i solved it using a listview into a alertdialog like this:
public void seleccionaTemporada(View view) {
AlertDialog.Builder selector = new AlertDialog.Builder(SeleccionaTemporadaActivity.this);
selector.setTitle("Temporadas");
selector.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
selector.setAdapter(temporadas, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), DashBoardActivity.class);
intent.putExtra("temporada", temporadas.getItem(which));
startActivity(intent);
}
});
selector.show();
}
<Button
android:id="#+id/btn_selecciona_temporada"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="70dp"
android:drawableStart="#mipmap/ic_balon"
android:onClick="seleccionaTemporada"
android:text="#string/seleccionarTemporada"
android:textSize="25sp" />
For my needs it is the best solution i think. Sorry for my bad explanation before.

You can use Popup Window. And create your own Spinner Popup.
Example Code below
LayoutInflater inflater = (LayoutInflater)parentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.pop_up_window, null);
RelativeLayout layout1 = holder.relativeLayout_multiChoiceDropDown;
pw = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setTouchable(true);
pw.setOutsideTouchable(true);
pw.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pw.setTouchInterceptor(new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_OUTSIDE)
{
pw.dismiss();
return true;
}
return false;
}
});
pw.setContentView(layout);
pw.showAsDropDown(layout1, -5, 0);
final ListView list = (ListView) layout.findViewById(R.id.dropDownList);
Adapter_DropDown adapter = new Adapter_DropDown(parentActivity, items);
list.setAdapter(adapter);
pop_up_window.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/PopUpView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/qatool_basic_info_dropdown"
android:orientation="vertical">
<ListView
android:id="#+id/dropDownList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="0.5dp">
</ListView>
</LinearLayout>

Related

Add dynamically components to a Custom Dialog in Android

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);

setOnChildClickListener of ExpandableListView inside a Custom Alter dialog does not work

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.

ListView not clickable even with descendantFocusability

I'm trying to implement a ContextMenu, but by doing so I checked that my listview was not clickable.
I went over different threads based on this subject and tried everything proposed android:focusable="false" android:focusableInTouchMode="false"
even
descendantFocusability="blocksDescendants"
but still not working
Here is my main xml :
<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="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".HomePage">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnAddTask"
android:id="#+id/btnAddTask"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="goToAddTask" />
<ListView
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listViewTasks"
android:layout_alignParentStart="true"
android:layout_above="#+id/btnAddTask"
android:layout_below="#+id/txtTaskList"
android:longClickable="true"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/txtTaskList"
android:id="#+id/txtTaskList"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Here is my 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="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:descendantFocusability="blocksDescendants"
android:layout_alignParentStart="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnDelete"
android:src="#drawable/no_cross"
android:onClick="goToDeleteTask"
android:layout_gravity="center_vertical"
android:contentDescription="Delete button" />
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical"
android:clickable="true">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/txtTaskListName"
android:id="#+id/txtTaskListName"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/txtTaskListDate"
android:id="#+id/txtTaskListDate"
android:layout_below="#+id/txtTaskListName"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/txtTaskListTime"
android:id="#+id/txtTaskListTime"
android:layout_marginLeft="10dp"
android:layout_below="#+id/txtTaskListName"
android:layout_toEndOf="#+id/txtTaskListDate"
android:layout_marginStart="24dp" />
</LinearLayout>
</LinearLayout>
And finally my java code:
public class HomePage extends ActionBarActivity {
final Context context = this;
private static final int EDIT = 0, DELETE = 1;
DatabaseHandler dbHandler;
ListView taskListView;
ArrayAdapter<Task> taskAdapter;
int longClickedItemIndex;
List<Task> Tasks = new ArrayList<Task>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
dbHandler = new DatabaseHandler(getApplicationContext());
taskListView = (ListView) findViewById(R.id.listViewTasks);
// The following piece of code allows to empty database before testing when reinstalling the app for test.
//getApplicationContext().deleteDatabase("myTasks");
registerForContextMenu(taskListView);
taskListView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
taskListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
longClickedItemIndex = position;
return false;
}
});
if (dbHandler.getTasksCount() != 0)
Tasks.addAll(dbHandler.getAllTasks());
populateTasksList();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home_page, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class TaskListAdapter extends ArrayAdapter<Task> {
public TaskListAdapter(){
super(HomePage.this, R.layout.tasklist_item, Tasks);
}
#Override
public View getView(int position, View view, ViewGroup parent){
if (view == null) {
view = getLayoutInflater().inflate(R.layout.tasklist_item, parent, false);
}
Task currentTask = Tasks.get(position);
TextView taskListName = (TextView) view.findViewById(R.id.txtTaskListName);
taskListName.setText(currentTask.getTitle());
TextView taskListDate = (TextView) view.findViewById(R.id.txtTaskListDate);
taskListDate.setText(currentTask.getDate());
TextView taskListTime = (TextView) view.findViewById(R.id.txtTaskListTime);
taskListTime.setText(currentTask.getTime());
/*ImageButton deleteButton = (ImageButton) view.findViewById(R.id.btnDelete);
//deleteButton.setTag(currentTask.getId());
deleteButton.setTag(R.id.taskId,currentTask.getId());
deleteButton.setTag(R.id.position,position);*/
return view;
}
}
private void populateTasksList() {
taskAdapter = new TaskListAdapter();
taskListView.setAdapter(taskAdapter);
}
public void goToAddTask(View view){
Intent intent = new Intent(this, AddTask.class);
startActivity(intent);
}
public void goToDeleteTask(final View view){
AlertDialog alertDialog = new AlertDialog.Builder(context)
// Set Dialog Icon
.setIcon(R.drawable.ic_bullet_key_permission)
// Set Dialog Title
.setTitle(R.string.removeAlert)
// Set Dialog Message
.setMessage(R.string.deleteMessage)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ImageButton deleteButton = (ImageButton) view.findViewById(R.id.btnDelete);
// Retrieving the task to delete
int taskId = (int) deleteButton.getTag(R.id.taskId);
Task taskToDelete = dbHandler.getTask(taskId);
// Removing the task from the database and from the calendar
dbHandler.deleteTask(taskToDelete, context);
// Removing the task from the listView
TaskListAdapter tAdapter = (TaskListAdapter)taskListView.getAdapter();
int position = (int)deleteButton.getTag(R.id.position);
tAdapter.remove(tAdapter.getItem(position));
tAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), R.string.removedTask, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which){
}
})
.create();
alertDialog.show();
}
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, view, menuInfo);
menu.setHeaderIcon(R.drawable.edit_pencil_icon);
menu.setHeaderTitle(R.string.contextMenu_title);
menu.add(menu.NONE, EDIT, menu.NONE, R.string.contextMenu_edit);
menu.add(menu.NONE, DELETE, menu.NONE, R.string.contextMenu_delete);
}
public boolean onContextItemSelected (MenuItem item) {
switch (item.getItemId()) {
case EDIT:
// TODO : Implement editing a task
break;
case DELETE:
deleteTask();
//TODO : to be tested
/*
dbHandler.deleteTask(Tasks.get(longClickedItemIndex);
Tasks.remove(longClickedItemIndex);
taskAdapter.notifyDataSetChanged();
*/
break;
}
return super.onContextItemSelected(item);
}
public void deleteTask(){
AlertDialog alertDialog = new AlertDialog.Builder(context)
// Set Dialog Icon
.setIcon(R.drawable.ic_bullet_key_permission)
// Set Dialog Title
.setTitle(R.string.removeAlert)
// Set Dialog Message
.setMessage(R.string.deleteMessage)
.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ImageButton deleteButton = (ImageButton) findViewById(R.id.btnDelete);
// Retrieving the task to delete
int taskId = (int) deleteButton.getTag(R.id.taskId);
Task taskToDelete = dbHandler.getTask(taskId);
// Removing the task from the database and from the calendar
dbHandler.deleteTask(taskToDelete, context);
// Removing the task from the listView
TaskListAdapter tAdapter = (TaskListAdapter) taskListView.getAdapter();
int position = (int) deleteButton.getTag(R.id.position);
tAdapter.remove(tAdapter.getItem(position));
tAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), R.string.removedTask, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.create();
alertDialog.show();
}
}
(2 delete functions, cause of my test I'm currently performing).
I finally tried several things and find my solution (for whom it could be usefull).
First of all I had to remove all the things related to "clickable" attribute from all my xml
Then, if you check my list item xml, I have 2 layouts (2 linear one, don't know if this could be different with Relative, did not test this).
What I did is to remove the second one, and modify the core LinearLayout to Relative Layout (to display my elements as I want).
Then I launched again my app and it's perfectly working.

How to show Dialogue in fragment | Android

Using fragment in my activity , i want to show a dialogue On ItemLongClickListener of viewlist. I have tried a few codes but failed. Is there anyway to do it?
code
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
Toast.makeText(getActivity(),"thul thuk!",Toast.LENGTH_SHORT).show();
//show-dialogue here
return false;
}
});
Try this..
custom_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
</LinearLayout>
Java
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,int index, long arg3) {
Toast.makeText(getActivity(),"thul thuk!",Toast.LENGTH_SHORT).show();
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
dialog.show();
return false;
}
});
EDIT
You can use Context Menu
Try this:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button proceed = (Button)popupView.findViewById(R.id.button1);
Button cancel = (Button)popupView.findViewById(R.id.button2);
cb = (CheckBox)popupView.findViewById(R.id.checkBox1);
proceed.setOnClickListener(onProceed);
cancel.setOnClickListener(onCan);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
anchorView.getLocationOnScreen(location);
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}
The above will result in a popup, just beneath the view clicked.
popup_layout is the XML layout file which will be inflated.

ListView setOnItemClickListener doesn't response

i try to get selected item from ListView but i dont get any respone from my listener
I dont get any calls at my log from this code:
public void onItemSelected(AdapterView<?> spinnerReference,
View rowReference, int arg2, long arg3) {
chooseExercise = itemsList.getSelectedItem().toString();
Log.i("choosed", chooseExercise);
}
My adapter:
public void setItemList(String muscle)
{
if (muscle.equals("All"))
items = data.getAllExercies();
else
items = data.getMuscleEx(muscle);
ArrayAdapter<CharSequence> exerciseAdapter;
if (items == null || items.length < 1)
items = new String[] {};
exerciseAdapter = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item, items)
{
#Override
public View getView(int position, View convertView,
ViewGroup parent) {
String text = items[position];
TextView listItem = new TextView(AddWorkOutPage.this);
listItem.setBackground(getResources().getDrawable(R.drawable.shape_box));
listItem.setTextSize(18);
listItem.setText(text);
listItem.setPadding(10, 20, 0, 20);;
listItem.setTextColor(Color.BLACK);
return listItem;
}
};
// Specify the layout to use when the list of choices appears
// Apply the adapter to the spinner
searchItemField.setAdapter(exerciseAdapter);
itemsList.setAdapter(exerciseAdapter);
}
My listview object code:
public void addExerciseDialog()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose exercise");
View prefView = View.inflate(this, R.layout.choose_from_list, null);
searchItemField = (AutoCompleteTextView) prefView.findViewById(R.id.searchItemField);
itemsList = (ListView) prefView.findViewById(R.id.itemsList);
searchItemField.setDropDownHeight(0);
searchItemField.addTextChangedListener(this);
setItemList(wantedMuscle);
itemsList.setOnItemClickListener(this);
builder.setPositiveButton("Choose selected", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
Log.i("exercise choose", chooseExercise);
dialog.dismiss();
}
});
builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
}
});
builder.setNeutralButton("filter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton)
{
createDialog();
}
});
builder.setView(prefView);
builder.show();
}
My dialog XML code:
<?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:background="#color/White"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#android:drawable/ic_menu_search" />
<ListView
android:id="#+id/itemsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/searchItemField"
android:descendantFocusability="beforeDescendants"
android:layout_marginTop="5sp" >
</ListView>
<AutoCompleteTextView
android:id="#+id/searchItemField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/imageView1"
android:ems="10" >
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
What Listener are you using exactly? The OnItemClickListener has a method onItemClick() instead of your onItemSelected(). I'm not sure if yours would work too, but this works:
ListView listview = (ListView) v.findViewById(R.id.itemsList);
listview.setAdapter(...);
listview.setOnItemClickListener(new MyOnItemClickListener());
And the OnItemClickListener as an inner class:
private class MyOnItemClickListener implements OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Object o = parent.getItemAtPosition(position);
}
}

Categories

Resources