AlertDialog custom view is not appearing - android

I'm building a simple AlertDialog with custom layout from XML. Here is how it's supposed to look (taken from Eclipse): It's simply 4 columns:
Here is the xml of the image above:
<?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:orientation="horizontal"
android:weightSum="1.0" >
<ImageView
android:id="#+id/colorpicker_dialog_color1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFF"
android:tag="0xFFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#FFDD66"
android:tag="0xFFDD66" />
<ImageView
android:id="#+id/colorpicker_dialog_color3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#66CCFF"
android:tag="0x66CCFF" />
<ImageView
android:id="#+id/colorpicker_dialog_color4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:background="#B6C0D2"
android:tag="0xB6C0D2" />
</LinearLayout>
Here is the code of the costume Alert Dialog:
public void showColorPickerDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater
.inflate(R.layout.color_picker_dialog, null);
ImageView clr1 = (ImageView) layout
.findViewById(R.id.colorpicker_dialog_color1);
clr1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// .. code
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("cccc");
builder.setView(layout)
.setCancelable(true)
.setPositiveButton("ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
}).show();
}
The problem is that in my dialog I dont see any column.

Try using Views instead of ImageViews in your layout. The problem using ImageViews in your case is that since you do not specify the src attribute for the ImageView, it will display a 0x0-sized image, which leaves your views invisible.

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

Android alert dialog blank space when setting a view

I have a question. I made an alert dialog that is showing an message and view with a "do not show again" check-box. When i'm running this app on android KK it is displaying fine, however when I run it on android JB it is showing me a blank space like on this image(i also get this blank space when the view contains only a linear layout with a height of 0dp):
http://foto.modelbouwforum.nl/images/2014/08/15/Screenshot2014-08-15-14-33-40.png
My code of the alert dialog:
final SharedPreferences sharedPreferences=getSharedPreferences("appPrefs", Context.MODE_PRIVATE);
if(!sharedPreferences.getBoolean("isTutorialMainScreenChecked", false)){
View checkBoxView = View.inflate(MainScreenHandler.this, R.layout.checkbox_alert_dialog,null);
checkBox = (CheckBox) checkBoxView.findViewById(R.id.checkbox);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setMessage(getString(R.string.alertDialog_main_screen_tutorial));
builder.setCancelable(false);
builder.setView(checkBoxView);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if(checkBox.isChecked()){
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putBoolean("isTutorialMainScreenChecked", true);
editor.commit();
}
dialogInterface.dismiss();
}
});
builder.setNegativeButton(getString(R.string.alertDialog_cancel),
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialogInterface, int i){
dialogInterface.cancel();
}
});
alertDialog = builder.create();
alertDialog.show();
And here is my checkbox layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:baselineAligned="false">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ffff3bb2"/>
<CheckBox
style="#android:style/TextAppearance.Small"
android:textColor="#ff000000"
android:text="#string/alertDialog_checkbox_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkbox"/>
</LinearLayout>
Thanks in advance
P.s:
This is how it looks on android KK:
http://foto.modelbouwforum.nl/images/2014/08/15/QuickMemo2014-08-15-14-42-41.png
I looked through sources of alert_dialog.xml layout in API 19 and found the following layout for inserting a custom view:
<FrameLayout android:id="#+id/customPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<FrameLayout android:id="#+android:id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"
android:paddingBottom="5dip" />
</FrameLayout>
Then I tried to set background to see which view takes the space. customPanel was colored in red, custom view was colored in green.
AlertDialog d = builder.create();
d.show();
View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setBackgroundColor(Color.RED);
This picture means that customPanel should have some paddings, or custom should have non-zero layout params or something else. After checking those attributes which were equal to zero I tried to test minimumHeight of the customPanel and got 192 on xxhdpi or 64dp. This setting cause the frame to be larger than a nested custom view.
I haven't figured out where this setting is applied, but here is the workaround:
AlertDialog d = builder.create();
d.show();
View v = (View)d.getWindow().findViewById(android.R.id.custom).getParent();
v.setMinimumHeight(0);
Tested on API 18,19.
In addition to vokilam's answer:
If you're using android.support.v7.app.AlertDialog.
Tested on API 27 (pretty sure this works on API 28 too)
The layout:
<FrameLayout
android:id="#+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Space
android:id="#+id/textSpacerNoTitle"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="#dimen/dialog_padding_top_material" />
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingEnd="?attr/dialogPreferredPadding"
android:paddingStart="?attr/dialogPreferredPadding"
style="#style/TextAppearance.Material.Subhead" />
<Space
android:id="#+id/textSpacerNoButtons"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="#dimen/dialog_padding_top_material" />
</LinearLayout>
</ScrollView>
</FrameLayout>
<FrameLayout
android:id="#+id/customPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp">
<FrameLayout
android:id="#+id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
The android:minHeight="48dp" for contentPanel and customPanel are causing the unwanted blank space.
Unwanted space dialog
What I did is:
alertDialog.SetView(view);
alertDialog.show();
...
View customPanel = (View) view.getParent().getParent();
if (customPanel != null)
customPanel.setMinimumHeight(0);
View contentPanel = (View) alertDialog.findViewById(android.R.id.message).getParent().getParent().getParent();
if (contentPanel != null)
contentPanel.setMinimumHeight(contentPanel.getMinimumHeight() * 2 / 3);
Result:
Blank space removed
Did you try playing around with paddings and margins?
You could copy your layout in res/layout-vXX and set a specific (negative) margin.

Android AlertDialog not wraping content in KK

I've created a multi purpose AlertDialog with one WebView in it. In all the devices that I've tested the dialog size is ok, that it, the height is always wrapping to the content. Unfortunately, on a Nexus 5 with 4.4.2 it's not showing properly. Sometimes it wraps, other don't.
Here is a screenshot of what I mean on a emulated Nexus 5.
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
tools:ignore="DisableBaselineAlignment"
android:id="#+id/alertdialog_title"
android:weightSum="4">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:fitsSystemWindows="true"
android:id="#+id/alertdialog_icon"
android:layout_margin="12dp"
android:layout_gravity="center"
android:background="#drawable/info"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:gravity="center"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textColor="#color/white"
android:textStyle="bold"
tools:ignore="HardcodedText"
android:layout_gravity="center"
android:id="#+id/alertdialog_titletext"
android:layout_marginTop="12dp"
android:layout_marginRight="12dp"
android:layout_marginBottom="12dp"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Large"
style="#android:style/TextAppearance.Large"/>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/orange"
android:orientation="vertical"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
tools:ignore="DisableBaselineAlignment"
android:id="#+id/alertdialog_content">
</LinearLayout>
</LinearLayout>
And the code for creating the AlertDialog
void showAlertDialog(String title, String message, final int messageType) {
int padding = (int) getResources().getDimension(R.dimen.all_margins);
LayoutInflater inflater = getLayoutInflater();
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
View layout = inflater.inflate(R.layout.alertdialog, null);
LinearLayout alertDialogContent = (LinearLayout) layout.findViewById(R.id.alertdialog_content);
ImageView alertDialogIcon = (ImageView) layout.findViewById(R.id.alertdialog_icon);
TextView alertDialogTitle = (TextView) layout.findViewById(R.id.alertdialog_titletext);
alertDialogTitle.setTypeface(fontBold);
alertDialogIcon.setBackgroundDrawable(getResources().getDrawable(R.drawable.info));
alertDialogTitle.setText(title);
alertDialogTitle.setTextColor(getResources().getColor(R.color.orange));
WebView webview = new WebView(this);
ScrollView scrollPane = new ScrollView(this);
webview.loadDataWithBaseURL(null, message, "text/html", "utf-8", null);
scrollPane.setPadding(padding, padding, padding, padding);
scrollPane.addView(webview);
alertDialogContent.addView(scrollPane);
alertDialog.setView(layout);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do something
}
});
if (messageType == 0 || messageType == 3) {
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do something
}
});
}
AlertDialog alert = alertDialog.create();
alert.show();
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
DisplayMetrics metrics = getResources().getDisplayMetrics();
alert.getWindow().setLayout((int) (metrics.widthPixels * 0.8), ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
What should I change to the AlertDialog always wraps it's content??
It is the ScrollView default behaviour to get all the available space (that's my direct experience). I came across this issue some days ago, My solution was to measure the content of the ScrollView, and forcing its height. Something like (my situation was a LinearLayout as direct child of theScrollViewthat containsTextView`s - pseudo-code)
int height = 0;
for (view I am going to add in the LL) {
view.measure(0, 0);
height += view.getMeasuredHeight();
}
after this measure operation, I retrieve the root layout of the view hierarchy (FrameLayout in my case), and I forced its params.height to the calculated

Android ListVIew Swipe to dismiss

I need some help.I'm using this EnhanceListView for swipe to dismiss in my alertDialog but the swipe is not working. Does the swipe to dismiss doesn't work on alertdialog? or Am i missing something.? I tried setting the directions but still not working.
here is my code
public void viewDataList() {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert_opendata, null);
lv = (EnhancedListView) view
.findViewById(R.id.data_list);
TextView emptyText = (TextView) view.findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
DisplayDataAdapter adapter = displayData();
lv.setAdapter(adapter);
lv.setDismissCallback(new OnDismissCallback() {
#Override
public Undoable onDismiss(EnhancedListView arg0, int arg1) {
// TODO Auto-generated method stub
return null;
}
});
lv.setSwipingLayout(R.id.swiping_layout);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setView(view);
builder.setNegativeButton("Close",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
return;
}
});
builder.create().show();
}
my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<de.timroes.android.listview.EnhancedListView
android:id="#+id/data_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C0C0C0"
android:minHeight="30dp" >
</de.timroes.android.listview.EnhancedListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="No Data"
android:textSize="20sp" />
</LinearLayout>
my List Item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swiping_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:background="#F8f8f8"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/datalist_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F8f8f8"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
</LinearLayout>
i don't see you setting anywhere lv.enableSwipeToDismiss();
also, you may want to add stuff like:
lv.setUndoStyle(EnhancedListView.UndoStyle.MULTILEVEL_POPUP);
lv.setSwipeDirection(EnhancedListView.SwipeDirection.BOTH);

Android : How to add an item to a listview onclick of a button?

I have a listview and a button in my layout file. I want to add items to listview on click of that button. The listview should be empty when the activity is started but it should grow by adding the items to it. On click of "AddItem" button i'am showing one AlertDialog to take item/input from user. How do i add that item to listview? Please help me with code. Which adapter should i use to use my own layout inside ListView? Should i extend ListActivity ?
Here is my layout file : my_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#android:color/black"
android:dividerHeight="2dp" >
</ListView>
<Button
android:id="#+id/addItemButtom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:text="Add Item" />
</LinearLayout>
Here is my row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:gravity="center"
android:orientation="horizontal" >
<TextView android:layout_weight = "1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Newly Added Item : "
android:textColor="#android:color/black"
android:textSize="17dp" />
<TextView android:layout_weight = "1"
android:id="#+id/itemTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Here is my java file :
public class MyList extends Activity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_list);
findViewById(R.id.addItemButtom).setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.enterInverter:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText et = new EditText(this);
et.setHint("Enter Item");
et.setMaxLines(1);
et.setTextSize(17);
alert.setTitle("Enter Item");
alert.setView(et);
alert.setInverseBackgroundForced(true);
alert.setPositiveButton("Add", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String item = et.getText().toString().trim();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
dialog.cancel();
}
});
alert.show();
break;
default:
break;
}
}
}
The ArrayAdapter might be the point to start. Of course it would change according to your list, but I suggest you to use ArrayAdapter. Get the reference of your list, define a new adapter for it and set it with setAdapter method. Then, you can add/remove items to your adapter.
where is your listview?
when you add your list item in your list(Array or Arraylist or...)
your_adapter.notifyDataSetChanged();
it makes draw your fresh listview

Categories

Resources