The Thom The Thom - 17 days ago 8
Android Question

Title on DialogFragment

I want to create a simple dialog that has a grid view in the middle and lets the user choose a letter.

Here is the XML I started with following: https://developer.android.com/guide/topics/ui/layout/gridview.html and some others:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>


And then I have this code to use it:

package com.example.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

/**
* Select a letter from the dialog
*/
public class SelectLetterDialogFragment extends DialogFragment {
GridView gridView;

static final String[] numbers = new String[] {
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"};

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle("Select a letter");

return dialog;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.select_letter_dialog, container);

gridView = (GridView) view.findViewById(R.id.gridview);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_list_item_1, numbers);

gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
String letter = ((TextView) v).getText().toString();
Log.e("Blah", letter);
dismiss();
}
});

setStyle(STYLE_NORMAL, android.R.style.Theme_Holo_Dialog);

return view;
}
}


So this creates a dialog with just a grid with letters on it. This confuses me. So first question is, what does
dialog.setTitle("Select a letter")
do since it doesn't appear to affect this?

So then I changed the XML file to add a TextView to put the title in like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a Letter"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>


And now the letters are in a single column. So my second question is, WTF? Why is this behaving this way?

My third and real question is how do I get a title on my dialog with the letters four across in a grid like the first example?

Answer

I never did get GridLayout or GridView to work properly. I ended up just adding LinearLayouts and setting the width on them so they worked into a grid. Using styles made it better.

Comments