Search code examples
androidlistviewandroid-fragmentsandroid-dialogfragment

Android DialogFragment No_Title feature affects layout


I have an android DialogFragment that displays a ListView, each row containing a title and image. Everything displays fine until I remove the title from the DialogFragment with getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

Once I remove the title, the images stop displaying in the ListView and all that displays are the titles. How could FEATURE_NO_TITLE possibly affect the display of the ListView within the DialogFragment? Literally no other lines of code change - just that one, and the images stop displaying in the ListView.

Dialog Fragment OnCreate

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.pick_photo_dialog_fragment, container);

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    Button closeButton = (Button) view.findViewById(R.id.closeButton);
    closeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            dismiss();
        }
    });

    listview = (ListView) view.findViewById(R.id.photosListView);
    listadapter = new PhotoListAdapter(getActivity());
    listview.setAdapter(listadapter);

    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            //do stuff
        }
    });

    return view;
}

XML for the fragment contains nothing more than a ListView and Button inside a LinearLayout...

<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/dark_gray">

    <ListView android:id="@+id/photosListView"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:clickable="false"
        android:listSelector="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        />

    <Button android:id="@+id/closeButton"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:text="Close"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@color/white"
        android:typeface="sans"
        android:background="@color/transparent"/>


</LinearLayout>

ListAdapter relevant classes

int[] mResources = {R.drawable.basketball, R.drawable.bodyweight, R.drawable.boulder, R.drawable.boxing, R.drawable.cardio,
        R.drawable.crossfit, R.drawable.olympic, R.drawable.racquet, R.drawable.run, R.drawable.spin, R.drawable.squash,
        R.drawable.swim, R.drawable.volleyball, R.drawable.weight, R.drawable.yoga};
String[] pictureNames = {"basketball", "bodyweight", "boulder", "boxing", "cardio", "crossfit", "olympic", "racquet", "run",
        "spin", "squash", "swim", "volleyball", "weight", "yoga"};

public PhotoListAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
    mContext = context;
}

public int getCount() {
    return mResources.length;
}

public Object getItem(int position) {
    return pictureNames[position];
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.photo_list_row_view, null);
        holder = new ViewHolder();
        holder.activityNameTV = (TextView) convertView.findViewById(R.id.activityName);
        holder.activityImageIV = (ImageView) convertView.findViewById(R.id.activityImage);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    task = new CreateClassSetImageTask(position, holder.activityNameTV, holder.activityImageIV);
    task.execute();

    return convertView;
}

static class ViewHolder {
    TextView activityNameTV;
    ImageView activityImageIV;
}

XML for ListView row

<?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:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


        <TextView android:id="@+id/activityName"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20dp"
            android:textStyle="normal"
            android:textColor="#FFFFFF"
            android:typeface="sans"
            android:layout_gravity="left|center_vertical"
            />

        <ImageView android:id="@+id/activityImage"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:scaleType="centerInside"/>

</LinearLayout>

Async Task to Load Images

public class CreateClassSetImageTask extends AsyncTask<Void, Void, Bitmap> {
    WeakReference<ImageView> imageViewReference;
    WeakReference<TextView> textViewReference;
    int pictureNum;

    public CreateClassSetImageTask(int pictureNumIn, TextView activityNameTVIn, ImageView activityIVIn) {

        pictureNum = pictureNumIn;
        imageViewReference = new WeakReference<ImageView>(activityIVIn);
        textViewReference = new WeakReference<TextView>(activityNameTVIn);
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onPostExecute(Bitmap resultBitmap) {
        if (imageViewReference != null && resultBitmap != null && textViewReference != null) {
            final ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(resultBitmap);
            }
            final TextView textView = textViewReference.get();
            if (textView != null){
                textView.setText(pictureNames[pictureNum]);
            }
        }
    }

    @Override
    protected Bitmap doInBackground(Void... params) {

        return decodeSampledBitmapFromResource(mContext.getResources(), mResources[pictureNum], 100, 100);
    }


}

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
                                                     int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

Solution

  • Try this...instead of using getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    Set DialogFragment.STYLE_NO_TITLE in the onCreate method of the Dialog like this:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments().getInt("num");
        style = DialogFragment.STYLE_NO_TITLE; break;
        theme = android.R.style.Theme_Holo; break;
        setStyle(style, theme);
    }