PopUpWindow Android Exception - android

I want to display a popupWindow in android
I did the following:
public class MyPopUp extends PopupWindow {
private View view;
private Context context;
public MyPopUp (Context context)
{
super(context);
this.context = context;
View myView = new myView(context);
this.setContentView(myView );
setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
}
public void showPopUp(){
view = new View(context);
this.showAtLocation(view, Gravity.CENTER, 0, 0);
}
}
There is an exception on this line
this.showAtLocation(view, Gravity.CENTER, 0, 0);
02-02 09:13:31.456: ERROR/AndroidRuntime(458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.popup.mypopup/com.popup.mypopup.Android_2_Activity}: java.lang.NullPointerException
when showPopUp is called

try this
public MyPopUp (Context context){
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custome_dialog);
Button dialogCancel = (Button) dialog
.findViewById(R.id.btnCancelPassword);
Button dialogSubmit = (Button) dialog
.findViewById(R.id.btnSubmitPassword);
dialogSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialogCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}

Related

AlertDialog doesn't wrap my content

I'm using an alert dialog to display a view with spinner to set the period and the scale of a chart
But it doesn't wrap my content and all the tuto I tried failed...
My code
LinearLayout vue = new LinearLayout(context);
vue.setOrientation(LinearLayout.VERTICAL);
vue.addView(diag.getView());
choixPeriode = (Spinner) vue.findViewById(R.id.spinner_periode);
choixEchelle = (Spinner) vue.findViewById(R.id.spinner_echelle);
if(choixEchelle == null)
choixEchelle = new Spinner(context);
final int oldPeriode = DiagramController.PeriodeToPos(this.periode);
final int oldEchelle = DiagramController.echelleToPos(this.echelle);
choixPeriode.setSelection(oldPeriode);
choixEchelle.setSelection(oldEchelle);
LinearLayout containerButton = new LinearLayout(context);
containerButton.setGravity(Gravity.CENTER);
final ImageView croix = ((ImageView) vue.findViewById(R.id.croix));
final Button ok = new Button(context);
ok.setText(R.string.valider);
final Button cancel = new Button(context);
cancel.setText(android.R.string.cancel);
containerButton.addView(ok);
containerButton.addView(cancel);
vue.addView(containerButton);
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
dialogBuilder.setView(vue);
final AlertDialog alertDialog = dialogBuilder.create();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
alertDialog.getWindow().setAttributes(lp);
alertDialog.show();
croix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
periode = DiagramController.posToPeriode(choixPeriode.getSelectedItemPosition());
echelle = DiagramController.posToEchelle(choixEchelle.getSelectedItemPosition());
changePeriode();
alertDialog.dismiss();
}
});
cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
diag.getView() return a LinearLayout with the title, the spinners and the chart.
Result in portrait
Result in landscape
Result with custom Dialog
1.) You will need to create a custom xml layout.
2.) Create a new class. And write your code in the onCreate.
public class MyDialog extends Dialog {
private TextView textView;
private Context context;
public MyDialog(Context context) {
super(context);
this.context = context;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.my_layout);
//Your code
textView = (TextView) findViewById(R.id.textView);
}
}

I want to use custom dialog at list view

I'm making a list view using a custom adapter.
In that, I want show a dialog when each item is clicked (surely, a different dialog at each list).
Here is my code.
MainActivity:
public class MainActivity extends AppCompatActivity {
ListView listView;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<MyCustomList> list = new ArrayList<MyCustomList>();
list.add(new MyCustomList("a", R.string.jangho_string1, R.string.jangho_string2, R.drawable.jangho));
list.add(new MyCustomList("b", R.string.dae_string1, R.string.dae_string2, R.drawable.dae));
list.add(new MyCustomList("c", R.string.an_string1, R.string.an_string2, R.drawable.an));
listView = (ListView)findViewById(R.id.listviewone);
MyCustomAdapter adapter = new MyCustomAdapter(getApplicationContext(), R.layout.listviewone, list);
listView.setAdapter(adapter);
}
}
MyCustomAdapter
public class MyCustomAdapter extends BaseAdapter {
Context ctx;
int layout;
ArrayList<MyCustomList> list;
LayoutInflater inf;
public MyCustomAdapter(Context ctx, int layout, ArrayList<MyCustomList> list){
this.ctx = ctx;
this.layout = layout;
this.list = list;
inf = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(final int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = inf.inflate(layout, null);
}
final View.OnClickListener makeListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
((Activity)ctx).showDialog(position);
}
};
convertView.setOnClickListener(makeListener);
return convertView;
}
protected Dialog onCreateDialog(int position){
Dialog dialog = null;
switch (position){
case 0:
break;
}
return dialog;
}
}
I'm using Android Studio.
you can create a method to show dialog, and pass the values from the listItem which is clicked
public void showDialog(Context context, String something){
Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_layout);
//Initialize your widgets from layout here
dialog.show();
//You can dialog.dismiss() to close the dialog.
}
Firstly,use ViewHolder when using listview. And just use listview.setOnItemClickListener
try this to create Custom Dialog
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.forgotpassword);
// Set dialog title
dialog.setTitle("ALERT!!");
// set values for custom dialog components - text, image and button
Button okbtn = (Button) dialog.findViewById(R.id.okbtn);
Button cancelbtn = (Button) dialog.findViewById(R.id.cancelbtn);
final EditText emailedittext = (EditText) dialog.findViewById(R.id.emailedittext);
dialog.show();
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
// if decline button is clicked, close the custom dialog
cancelbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
okbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email=emailedittext.getText().toString();
//do something more here
}
});
Refer here:- http://coderzpassion.com/android-show-alertdialog/

Show ListPopupWindow from PopWindow

I have a PopupWindow with a button. On I click on the button I want to show a ListPopupWindow but I get
FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W#439c79f0 is not valid; is your activity running?
and this is my show() method called onClick(). I get the error on the last line
popup.show();
full:
void showList(View view){
final ListPopupWindow popup = new ListPopupWindow(this);
popup.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Category));
popup.setAnchorView(view);
popup.setWidth(ListPopupWindow.WRAP_CONTENT);
popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
tvCategory.setText(Category[position]);
popup.dismiss();
}
});
popup.show();
}
FulllActivity code:
public class MapSights extends Activity implements OnMapReadyCallback {
ImageView btnPlus, close_add_sight;
PopupWindow pw, add_sg;
PopupMenu popupMenuCategory;
ListPopupWindow listPopupWindow;
int ONE = 1;
TextView tvCategory;
LinearLayout add_sight_btn;
Context context;
String TAG = "MapSights.java";
private static final String[] Category = { "Amphibian",
"Fish", "Reptile", "Bird","Mammal",
"Miriapode", "Insect", "Arachnid","Shellfish",
"Snail", "Shell", "Slime mold","Fungi",
"Plant", "Anemone", "Coral"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_sights);
Log.d(TAG, "onCreate");
context = getApplicationContext();
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
LayoutInflater inflater = (LayoutInflater) MapSights.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View pView = inflater.inflate(R.layout.maplusbtnadds, null, false);
View pViewSight = inflater.inflate(R.layout.add_sighting, null, false);
tvCategory = (TextView) pViewSight.findViewById(R.id.txt_category);
pw = new PopupWindow(pView, 450, 650, false);
add_sg = new PopupWindow(pViewSight, 450, 650, false);
btnPlus = (ImageView) findViewById(R.id.plusBtn);
close_add_sight = (ImageView) pView.findViewById(R.id.close_plus);
btnPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pw.showAtLocation(MapSights.this.findViewById(R.id.map), Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
}
});
close_add_sight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pw.dismiss();
}
});
add_sight_btn = (LinearLayout) pView.findViewById(R.id.add_sight_btn);
add_sight_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add_sg.showAtLocation(MapSights.this.findViewById(R.id.map), Gravity.BOTTOM | Gravity.RIGHT, 0, 0);
}
});
tvCategory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showList(v);
}
});
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG,"onResume");
}
#Override
protected void onStart() {
super.onStart();
Log.d(TAG,"onStart");
}
#Override
protected void onStop() {
super.onStop();
Log.d(TAG,"onStop");
}
void showList(View view){
final ListPopupWindow popup = new ListPopupWindow(this);
popup.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Category));
popup.setAnchorView(view);
popup.setWidth(ListPopupWindow.WRAP_CONTENT);
popup.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MapSights.this, "Clicked item " + position, Toast.LENGTH_SHORT).show();
tvCategory.setText(Category[position]);
popup.dismiss();
}
});
popup.show();
}
#Override
public void onMapReady(GoogleMap map) {
// map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
map.setMapType(MAP_TYPE_TERRAIN);
}
}
Change final ListPopupWindow popup = new ListPopupWindow(this);
to
final ListPopupWindow popup = new ListPopupWindow(MapSights.this);
Try to check if the activity is finishing before call popup.show(); to avoid Error is your activity running?
if(!isFinishing()) {
popup.show();
}

how Create setAdapter() for a AlertDialog

I created a CustomDialogBuilder that extends Dialog... I want to create the method setApater().. I created that section but the onClick method on the adapter is not working..
My customDialogBuilder class is given below.
public class CustomAlertDialog extends Dialog
{
public CustomAlertDialog(Context c, int theme) {
super(c, theme);
}
public static class Builder
{
private Context context;
private String title;
private String message;
private String positiveButtonText;
private String negativeButtonText;
private View contentView;
private ListAdapter adapter;
private ListView listView;
private DialogInterface.OnClickListener
positiveButtonClickListener,
negativeButtonClickListener,adapterListener;
public Builder(Context c)
{
context =c;
}
public Builder setTitle(String title)
{
this.title =title;
return this;
}
public Builder setMessage(String message) {
this.message = message;
return this;
}
public Builder setContentView(View v)
{
contentView =v;
return this;
}
public Builder setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener)
{
this.adapter=adapter;
return this;
}
public Builder setPositiveButton(String positiveButtonText,
DialogInterface.OnClickListener listener) {
this.positiveButtonText = positiveButtonText;
this.positiveButtonClickListener = listener;
return this;
}
public Builder setNegativeButton(String negativeButtonText,
DialogInterface.OnClickListener listener) {
this.negativeButtonText = negativeButtonText;
this.negativeButtonClickListener = listener;
return this;
}
public CustomAlertDialog create()
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final CustomAlertDialog dialog = new CustomAlertDialog(context,
R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_title_layout, null);
dialog.addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
((TextView) layout.findViewById(R.id.tv_custom_dialog_title)).setText(title);
if (positiveButtonText != null) {
((Button) layout.findViewById(R.id.bt_custom_dialog_positive))
.setText(positiveButtonText);
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.bt_custom_dialog_positive))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_POSITIVE);
}
});
}
}
else
layout.findViewById(R.id.bt_custom_dialog_positive).setVisibility(
View.GONE);
if (negativeButtonText != null) {
((Button) layout.findViewById(R.id.bt_custom_dialog_negative))
.setText(negativeButtonText);
if (negativeButtonClickListener != null) {
((Button) layout.findViewById(R.id.bt_custom_dialog_negative))
.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(
dialog,
DialogInterface.BUTTON_NEGATIVE);
}
});
}
} else {
// if no confirm button just set the visibility to GONE
layout.findViewById(R.id.bt_custom_dialog_negative).setVisibility(
View.GONE);
}
if (message != null) {
((TextView) layout.findViewById(
R.id.tv_custom_dilaog_message)).setText(message);
}
else if(adapter!=null)
{
listView = new ListView(context);
listView.setAdapter(adapter);
((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
.addView(listView);
if(adapterListener!=null)
{
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
}
});
}
}
else if (contentView != null) {
// if no message set
// add the contentView to the dialog body
((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
.removeAllViews();
((LinearLayout) layout.findViewById(R.id.Layout_custom_dialog_content))
.addView(contentView,
new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
}
return dialog;
}
}
}
how we can create the correct setAdapter() which is similar to setAdapter() in AlertDialog.
This is how I create dialog using this class:
Dialog dialog = null;
String[] items = {"Edit profile","Change doctor","Change password","Logout"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
R.layout.my_spinner_layout, items);
CustomAlertDialog.Builder customBuilder = new
CustomAlertDialog.Builder(Loged.this);
customBuilder.setTitle("Options")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.e("dis",""+which);
}
});
dialog = customBuilder.create();
dialog.show();
hureyyy...... got the answer........
change the setAdapter function in our CustomDialog class... as
public Builder setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener)
{
this.adapter=adapter;
this.adapterListener=listener;
return this;
}

Customized Spinner Dialog with unwanted space

I'm creating a custom Dialog that is started by a custom spinner. What I was trying to do is customize the dialog the spinner calls. However, there is an annoying space in the dialog. I've tryied all my resources to fix it, but nothing. I also followed this question's answer but didn't solve.
In the spinner xml file I pass it like this. It references the following class named CustomSpinner, that extends a Spinner:
<com.myproject.CustomSpinner
android:id="#+id/customSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="13dp"
android:prompt="#string/my_spinner"/>
And I have this class that is my custom dialog class:
public class CustomSpinnerDialog extends Dialog implements OnItemClickListener, View.OnClickListener
{
private OnItemSelectedListener onItemSelectedListener;
public DialogInterface.OnClickListener mListener;
public Context mContext;
public interface OnItemSelectedListener
{
public void onItemSelected(String itemValue);
}
public CustomSpinnerDialog(Context context, CustomSpinner.SpinnerAdapter spinnerAdapter, DialogInterface.OnClickListener listener)
{
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.setContentView(R.layout.custom_spinner);
mListener = listener;
mContext = context;
ListView listView = (ListView) this.findViewById(R.id.listview);
listView.setAdapter(spinnerAdapter);
listView.setOnItemClickListener(this);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
public void setOnItemSelectedListener(OnItemSelectedListener listener)
{
this.onItemSelectedListener = listener;
}
#Override
public void onClick(View v)
{
if(mListener != null)
mListener.onClick(this, DialogInterface.BUTTON_POSITIVE);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
if(mListener != null)
mListener.onClick(this, position);
String text = (String) parent.getItemAtPosition(position);
onItemSelectedListener.onItemSelected(text);
}
public void setDialogTitle(String title)
{
TextView titleText = (TextView) this.findViewById(R.id.titleText);
titleText.setText(title);
}
}
And this is my custom spinner:
public class CustomSpinner extends Spinner implements DialogInterface.OnClickListener
{
public Context mContext;
public String[] mDataList;
public CustomSpinner(Context context, AttributeSet attrs)
{
super(context, attrs);
this.mContext = context;
}
#Override
public boolean performClick()
{
boolean handled = false;
if (!handled)
{
handled = true;
CustomSpinnerDialog dialog = new CustomSpinnerDialog(mContext, (ListAdapter) getAdapter(), this, R.style.FullHeightDialog);
dialog.setDialogTitle(mContext.getResources().getString((R.string.my_dialog_text)));
dialog.show();
}
return handled;
}
#Override
public void onClick(DialogInterface dialog, int which)
{
setSelection(which);
dialog.dismiss();
}
}
My Spinner is created like this in an Activity:
cSpinner= (CustomSpinner) findViewById(R.id.customSpinner);
cSpinner.setDataList(dataList);
cSpinner.setTag("CustomSpinner");
cSpinner.setOnItemSelectedListener(controller);
Here is a screenshot of how the dialog looks like:
diaPhoto = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
I solved it following the documentation
And based in this code as advised by the documentation link above:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();

Categories

Resources