Android ListView in DialogFragment bugs when using setStyle

advertisements

I have a problem using DialogFragment with a ListView in it. The problem arises when I use setStyle in onCreate.

Here is the code (to be clearer about the issue, I have colored the list textviews in magenta) :

import android.app.DialogFragment;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MenuDialog extends DialogFragment {

    public static MenuDialog create() {
        MenuDialog frag = new MenuDialog();
        return frag;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ListView v = new ListView(getActivity());
        v.setAdapter(new BaseAdapter() {

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView view = null;
                if (convertView == null) {
                    view = new TextView(getActivity());
                    view.setTextAppearance(getActivity(), android.R.style.TextAppearance_Large);
                    view.setPadding(15, 15, 15, 15);
                    view.setBackgroundColor(Color.MAGENTA);
                }
                else
                    view = (TextView) convertView;

                switch (position) {
                case 0: view.setText("Random"); break;
                case 1: view.setText("Community favourites"); break;
                case 2: view.setText("Change image"); break;
                case 3: view.setText("Share"); break;
                case 4: view.setText("Informations"); break;
                }

                return view;
            }

            @Override public long getItemId(int arg0) { return 0; }
            @Override public Object getItem(int arg0) { return null; }
            @Override public int getCount() { return 5; }
        });
        return v;
    }
}

And here is the image of the dialog it provides :

Now, if I simply comment the setStyle(STYLE_NO_TITLE, 0); line in the onCreate method, everything works as expected :

What am I doing wrong ? I cannot use a simple AlertDialog because I need to customize way more those list items.

EDIT: You can download here a demo Android Eclipse project that demonstrates the problem


  1. list.xml

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical" >
    
      <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    
         />
    
     </LinearLayout>
    
    
  2. menu_rowview.xml

     <?xml version="1.0" encoding="utf-8"?>
           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
        android:orientation="horizontal"
      android:id="@+id/linear"
             >
    
           <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_marginBottom="10dp"
              android:layout_marginLeft="20dp"
              android:layout_marginRight="20dp"
              android:layout_marginTop="10dp"
              android:text="@android:string/unknownName"
              android:textAppearance="?android:attr/textAppearanceLarge" />
    
             </LinearLayout>
    
    
  3. Change onCreateView() (in your project)

      public View onCreateView(final LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list, container);
    ListView v=(ListView) view.findViewById(R.id.list);
    v.setAdapter(new BaseAdapter() {
    
        public View getView(int position, View convertView, ViewGroup parent) {
            View rowView = null;
            if (convertView == null) {
                rowView = inflater.inflate(R.layout.menu_rowview, parent, false);
    
            }
            else
                rowView = convertView;
            LinearLayout lay=(LinearLayout) rowView.findViewById(R.id.linear);
            lay.setBackgroundColor(Color.MAGENTA);
            TextView textView = (TextView) rowView.findViewById(R.id.textView1);
            switch (position) {
            case 0: textView.setText("Random"); break;
            case 1: textView.setText("Community favourites"); break;
            case 2: textView.setText("Change image"); break;
            case 3: textView.setText("Share"); break;
            case 4: textView.setText("Informations"); break;
            }
    
            return rowView;
        }
    
        @Override public long getItemId(int arg0) {
            return arg0; }
        @Override public Object getItem(int arg0) { return arg0; }
        @Override public int getCount() { return 5; }
    });
    return view;
    }