Filter from Android onCreateOptionsMenu Item - android

I want to add filter button to my onCreateOptionsMenu. When button was clicked i need to show layout with checkboxes and apply button.
Only way that i know how to do this - start new activity from onOptionsItemSelected and do all operations there. But is there more efficient way? Without leaving current activity. Something like pop-up window etc.

You can do it By Using this Material Dialog
https://github.com/drakeet/MaterialDialog Library and Below code, Just
put the gravity of Dialog TOP|RIGHT.
private void showMaterialDialog() {
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice);
for (Card card : cards) {
arrayAdapter.add(card.getName());
}
alert = new MaterialDialog(this);
View mView = getLayoutInflater().inflate(R.layout.custom_view_virtual_card, null);
ListView listView = (ListView) mView.findViewById(R.id.listView);
listView.setAdapter(arrayAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
AppCompatButton btnCancel = (AppCompatButton) mView.findViewById(R.id.btnCancel);
AppCompatButton btnOk = (AppCompatButton) mView.findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startAsyncForRequestVirtualCard(selectedCardTypeId);
alert.dismiss();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alert.dismiss();
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectedCardTypeId = cards.get(position).getId();
Log.d("request", cards.get(position).getId() + " " + cards.get(position).getName());
}
});
alert.setView(mView);
alert.show();
}
custom_view_virtual.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Select Card Type"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/colorAccent" />
<View
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:background="#color/divider" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="5dp"
android:divider="#color/transparent"
android:theme="#style/ProgressBarStyle"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnCancel"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="#string/cancel"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/btnOk"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>

Don't create a new activity instead create a new java class in that apply your sorting logic to filter the content and when your button is pressed that activity should return all your sorted content using List class or ArrayList class or anything else then display it.

Related

Scroll View not working in Alert Dialog

Somehow the scroll view is not working . The message alert dialog box is showing is really big and so I need to implement vertical scrollbar. I tried to get data from previous asked question but it isn't solving my issue please help.
I need to show the alert dialog on button click event.
benefits.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog ad = new AlertDialog.Builder(Panchgavya.this).create();
ad.setCancelable(false); // This blocks the 'BACK' button
ad.setMessage(getString(R.string.benefits));
ad.setTitle("Benefits");
ad.setButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
ad.show();
TextView textView = (TextView) ad.findViewById(android.R.id.message);
textView.setScroller(new Scroller(Panchgavya.this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
}
});
My XML File Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Design.Panchgavya"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/Green"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Panchgavya"
android:textColor="#color/white"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:orientation="vertical">
<TextView
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:text="#string/panchgavya"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:orientation="vertical">
<TextView
android:id="#+id/panchgavya_tv_cow_dung"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cow Dung"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_urine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Urine"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_cow_milk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Cow Milk"
android:textStyle="bold"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_ghee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Ghee"
android:textStyle="bold"
android:textColor="#color/black"/>
<TextView
android:id="#+id/panchgavya_tv_dahi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:text="Dahi"
android:textStyle="bold"
android:textColor="#color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<Button
android:id="#+id/panchgavya_btn_benefits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Benefits"
android:background="#color/Green"
android:textColor="#color/white"
/>
</LinearLayout>
</LinearLayout>
You can create custom layout for your dialog and set any property easily.
For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.
You can set custom layout on your by following method.
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Textview
android:id="#+id/txtDescription"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/margin_16dp"
android:paddingRight="#dimen/margin_16dp"
android:paddingBottom="#dimen/margin_16dp"
android:paddingTop="#dimen/margin_10dp"
android:text="#string/dummy_text_"
android:textSize="#dimen/font_14dp"
android:textColor="#color/colorPrimary"
android:layout_below="#+id/imgClose"/>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_close"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_10dp"
android:layout_marginTop="#dimen/margin_10dp" />
</RelativeLayout>
</ScrollView>
// in your java file put below code
private void showPopup() {
final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.dialog_terms_services,
null);
dialogBuilder.setView(dialogView);
TextView txtDescription = dialogView.findViewById(R.id.txtDescription);
ImageView imgClose = dialogView.findViewById(R.id.imgClose);
txtDescription.setText(message);
final AlertDialog b = dialogBuilder.create();
b.show();
imgClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
b.dismiss();
}
});
}
// after than call that showPopup() methos on button click.Hope it works for you
class extending DialogFragment can have UI elements as per your requirement.
public class OrderDetailFragment extends DialogFragment{
#Override
public void onStart() {
super.onStart();
Dialog d = getDialog();
if (d!=null){
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
d.getWindow().setLayout(width, height);
}
}
public static OrderDetailFragment getInstance(GeneralListDataPojo dataList){
OrderDetailFragment orderDetailFragment=new OrderDetailFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(DATA, dataList);
orderDetailFragment.setArguments(bundle);
return orderDetailFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root_view = inflater.inflate(R.layout.fragment_order_detail, container, false);
GeneralListDataPojo mDataList = getArguments().getParcelable(DATA);//My class which holds data implemented Parcelable
//population of data from mDataList
return root_view;
}
void closeDialog(){
this.dismiss();
}
}
fragment_order_detail is having my requirement specific element's (ScrollView/LinearLayouts/Buttons etc)
GeneralListDataPojo is the class which holds data implements Parcelable for transferring data between components
Now you can invoke this DialogFragment from your Fragment like this. (Im invoking from a Fragment. Change the FragmentManager retrieval accordingly if you are using from Activity)
OrderDetailFragment orderDetailFragment=OrderDetailFragment.getInstance((GeneralListDataPojo) responseObj);
FragmentManager fragmentManager=getFragmentManager();
orderDetailFragment.show(fragmentManager,"OrderDetailFragment");
Create The Custom Dialog layout Using Scroll View in your own custom Layout
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog= new Dialog(getApplicationContext());
dialog.setContentView(R.layout.activity_dialog);
Button click= dialog.findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});

buttons inside listview don't work properly?

in my list_item I have two buttons inside each item. the problem is that the buttons don't work unless I click the item then click the buttons.
Here for example I have to click on the item then click on the delete icon to delete the item.
ListView with two items
list_item :
<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="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
>
<ImageView
android:id="#+id/play_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="5dp"
android:src="#drawable/ic_play_circle_black_24dp"
android:tint="#4CAF50"
android:layout_weight="1"
/>
<ImageView
android:id="#+id/delete_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:focusable="true"
android:padding="5dp"
android:src="#drawable/ic_delete_black_24dp"
android:tint="#FF0000"
android:layout_weight="1"
/>
</LinearLayout>
Sorry everyone for lateness!
I've got the solution; the problem was that I deleting the ListView item in the Activity instead of doing it in the Adapter ;)
the Code that doesn't work (MainActivity) :
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
// delete button
ImageView deleteBtn = view.findViewById(R.id.delete_btn);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mAdapter.remove(mAdapter.getItem(i));
}
});
}
});
The correct code (Adapter) :
deleteBtn.setTag(position);
deleteBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer index = (Integer) deleteBtn.getTag();
remove(getItem(index));
}
});
Thank you so much everyone for helping :)

Get access to checked item and position outside setOnItemClickListener

I have a list view with single selection mode and would like to get access the position outside the setOnItemClickListener event
As of now I can access its position as shown below
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
//btnNxt.setEnabled(true);
Intent i = new Intent(getApplicationContext(),
NewActivity.class);
// Pass a single position
i.putExtra("position", position);
// Open SingleItemView.java Activity
startActivity(i);
}
});
Now I would like to know how do I access the selected position outside(ie..on button click event).
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here I need to get the position and selected item
}
});
Listview.xml
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#3366CC"
android:dividerHeight="2dp"
android:choiceMode="singleChoice"/>
ItemDetails.xml
<?xml version="1.0" encoding="utf-8"?>
<com.example.abc.widget.CheckableRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/sampleimg"
android:layout_width="150dip"
android:layout_height="100dip"
android:paddingRight="15dp"
android:paddingLeft="15dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="160dp"
android:layout_marginTop="10dp"
android:descendantFocusability="blocksDescendants">
<TextView android:id="#+id/itemname"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/itemdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/carname"
android:focusable="false"/>
<TextView android:id="#+id/itemprice"
android:textSize="19sp"
android:textStyle="bold"
android:textColor="#003399"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/cardesc"/>
<com.example.abc.widget.InertCheckBox android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:button="#drawable/checkbox" />
</RelativeLayout>
</com.example.abc.widget.CheckableRelativeLayout>
OrElse how do I check If any listview item is checked or not on button click ?
EDIT:
This is the way I'm trying to access the position from button click
btnNxt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),
myposition,
Toast.LENGTH_SHORT).show();
}
});
Simply make a member variable (declare it outside of a method like before onCreate() and use that.
public class ...
{
int pos;
// onCreate() and such
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
pos = position; // initialize it
...
}
});
then use pos inside your onClick() or wherever you need to in the Activity.
If I completely missed something in your question then please explain.

Spinner inside a PopUp Window

I've created a popup window and put a spinner inside it. But I can't get values when an item is selected from it.
My code to create popup:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popwindow=new PopupWindow(inflater.inflate(R.layout.addpain, null,false),300,350,true);
popwindow.showAtLocation(this.findViewById(R.id.tabHost), Gravity.CENTER, 0, 0);
XML file of PopUp
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#A0BBBBBB" >
<TextView
android:id="#+id/addpaintext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/addpain"
android:textColor="#000000"
android:textSize="18dp"
android:typeface="serif" />
<EditText
android:id="#+id/statustbox"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/addpaintext"
android:layout_marginTop="17dp"
android:hint="#string/addpain"
android:inputType="textMultiLine"
android:textColor="#000000"
android:textSize="20dp"
android:typeface="serif" />
<Button
android:id="#+id/addpainbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/statustbox"
android:layout_marginLeft="18dp"
android:layout_marginTop="98dp"
android:onClick="statusupload"
android:text="#string/statusupload" />
<Button
android:id="#+id/cancelbutton"
android:layout_width="80dp"
android:layout_height="45dp"
android:layout_alignBaseline="#+id/addpainbutton"
android:layout_alignBottom="#+id/addpainbutton"
android:layout_alignParentRight="true"
android:layout_marginRight="18dp"
android:onClick="canceladdpain"
android:text="#string/cancel" />
<Spinner
android:id="#+id/spinner_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/addpainbutton"
android:layout_alignRight="#+id/cancelbutton"
android:layout_below="#+id/statustbox"
android:layout_marginTop="20dp"
android:prompt="#string/testtxt"
android:entries="#array/list"/>
</RelativeLayout>
</LinearLayout>
My StatusUpload Function:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
category=parent.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
category="Miscellaneous";
}
});
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}
What happens is the vraible category isnt getting any values !! IT's just blank.
you're not seeing the results you want because everything hinges on the button to turn on your spinner listener. thus you have have to click the button, make a spinner selection, and then click the button again. I'd seriously reconsider the design. For instance, are you aware that you don't need the listener on to get the item selected? you could just use:
public void statusupload(View view)
{
EditText status=(EditText) popwindow.getContentView().findViewById(R.id.statustbox);
Spinner spinner=(Spinner) popwindow.getContentView().findViewById(R.id.spinner_list);
category = spinner.getSelectedItem().toString();
Toast.makeText(getBaseContext(),category, Toast.LENGTH_LONG).show();
}

is it possible to create listview inside dialog?

i have created a custom dialog class
public class NewPost extends Dialog
{
// functionality
}
now my requirement is to create listview inside it. i know we can create textboxes,buttons,dropdown list inside it.
but in order to create list view we should inherit our class from listActivity class
what you suggest is it possible or not if yes then how to achieve this using any interface or what?
this implementation doesn't require you to make any xml layouts. it was written as a case statement in "onCreateDialog" override, but you can adapt if for your purposes very easily:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");
ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
final Dialog dialog = builder.create();
dialog.show();
because you are making a dialog with only a ListView, you set the onItemClickListener of the ListView, as there isn't one for the basic dialog class.
modeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch(i) {
case 0:
//do something for first selection
break;
case 1:
//do something for second selection
break;
}
dialog.dismiss();
}
});
Yes.
You can always use a ListView inside a Dialog. And you definitely don't necessarily need ListActivity to create ListView.
Code may be something like this:
Dialog dlg = new Dialog(context);
LayoutInflater li = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.my_layout, null, false);
dlg.setContentView(v);
dlg.show();
my_layout.xml:
<ScrollView xmlns:android="blah"
android:id="xid"
android:layout_height="h"
android:layout_width="w">
<ListView blah blah blah attributes
/>
</ScrollView>
You don't really have to extend listActivity in order to use listviews.
Extending listActivity will give you some functionality for free, such as getListView() (if I recall the method name correctly), but that can just as well be done manually with findViewById() just as any other view.
The simplest possible way:
ListView listView = new ListView(this);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] {"item 1", "item 2", "item 3"}));
Dialog dialog = new Dialog(this);
dialog.setContentView(listView);
dialog.show();
You can create a custom dialog with this layout:
<?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:gravity="center_vertical|center_horizontal"
android:background="#drawable/dialogs">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:paddingTop="10dip">
<ImageView
android:id="#+id/dialog_title_image"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/info"/>
<TextView
android:id="#+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:text="Title"
android:layout_toRightOf="#id/dialog_title_image"
android:textColor="#android:color/black"
android:textSize="20sp"/>
<!-- Lista -->
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#1e90ff"/>
<ListView
android:id="#+id/component_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- Fine lista -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:gravity="bottom|center_horizontal"
android:paddingBottom="20dip">
<Button
android:id="#+id/positive_button"
android:layout_alignParentLeft="true"
android:layout_width="120dip"
android:layout_height="wrap_content"
android:background="#1e90ff"
android:textColor="#android:color/white"
android:text="#string/close"/>
</RelativeLayout>
</LinearLayout>
create a custom layout also for each item if you want:
<?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"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
and then use an arraylist to populate the list and set the view:
//creation and population of the list
List<Component> my_list = new ArrayList<Component>();
createComponents();
//adapter
array_adapter = new ComponentAdapter(context, R.layout.component,my_list);
//button to show the dialog
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
list_dialog = new Dialog(context);
list_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
list_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
list_dialog.setContentView(R.layout.list_dialog);
ListView list = (ListView)list_dialog.findViewById(R.id.component_list);
list.setAdapter(array_adapter);
Button positiveButton = (Button) list_dialog.findViewById(R.id.positive_button);
positiveButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
list_dialog.dismiss();
}
});
list_dialog.show();
}
});
}
References: http://pillsfromtheweb.blogspot.it/2014/10/android-listview-inside-alertdialog.html#links
Result:
You can use any layout for alert dialogs. If you want a listview I would do it like here

Categories

Resources