I am creating a popup window with a listview. In the listview each row contains a textbox and radio button but the popup window not displaying. I can't figure out the mistake.
This is the code.
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
public class SettingsActivity extends Activity {
PopupWindow popup;
ListView fontSizeListView;
View popupView;
int currentFontSize;
Vector<FontSize> fontData;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_view);
TextView tv = (TextView) findViewById(R.id.font_size_opt);
Typeface tf = TsciiTypeface.getTypeface(this);
tv.setTypeface(tf);
tv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showPopupMenu();
}
});
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
currentFontSize = Integer.parseInt(sp.getString("font_size", "1"));
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
}
private void showPopupMenu() {
try {
popup = new PopupWindow(this);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.font_popup_view, null, false);
fontSizeListView = (ListView) popupView
.findViewById(R.id.font_size_list);
String[] arrLabel = getResources().getStringArray(
R.array.font_size_label_array);
String[] arrValue = getResources().getStringArray(
R.array.font_size_value_array);
fontData = new Vector<FontSize>();
for (int i = 0; i < arrLabel.length; i++) {
fontData.add(new FontSize(Integer.parseInt(arrValue[i]), arrLabel[i]));
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_list_pref_row, fontData);
fontSizeListView.setAdapter(adapter);
popup.setContentView(popupView);
popup.showAtLocation(this.findViewById(R.id.font_size_opt), Gravity.CENTER, 0, 0);
adapter.notifyDataSetChanged();
} catch (Exception e) {
MessageBox(e.toString());
}
}
private View getRowView(String label, int value,ViewGroup parent) {
try {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.custom_list_pref_row, parent,false);
TextView text = (TextView) row
.findViewById(R.id.custom_list_view_row_text_view);
text.setText(label);
RadioButton rButton = (RadioButton) row
.findViewById(R.id.custom_list_view_row_radio_button);
rButton.setId(value);
row.setTag(value);
row.setClickable(true);
if (value == currentFontSize) {
rButton.setChecked(true);
}
row.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return row;
} catch (Exception e) {
MessageBox(e.toString());
return null;
}
}
public void MessageBox(String message) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Error");
adb.setMessage(message);
adb.setPositiveButton("Ok", null);
adb.show();
}
class CustomAdapter extends ArrayAdapter<FontSize> {
public CustomAdapter(Context context, int resource, List<FontSize> objects) {
super(context, resource, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// widgets displayed by each item in your list
FontSize rowData = getItem(position);
if (null == convertView) {
convertView = getRowView(rowData.label,rowData.value,parent);
}
return convertView;
}
}
private class FontSize {
public int value;
public String label;
FontSize(int pvalue, String plabel){
value = pvalue;
label = plabel;
}
@Override
public String toString() {
return value + " " + label;
}
}
}
This is the popup window layout xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/popup_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="demo value"
/>
<ListView
android:layout_height="wrap_content"
android:id="@+id/font_size_list"
android:layout_width="wrap_content"></ListView>
</LinearLayout>