Reputation: 33
Good evening out there,
i am trying to use an alert dialog in an Fragment (Cause of the TabNavigation). It is nessesary that i use the layout "privacy".
But eclipse gave me an error at the "AlertDialog.Builder": (The constructor AlertDialog.Builder(AboutActivity2) is undefined)
and at the ".from" after the inflate: (The method from(Context) in the type LayoutInflater is not applicable for the arguments (AboutActivity2))
Thanks for help, greetings
View rootView = inflater.inflate(R.layout.activity_about2, container, false);
rootView.findViewById(R.id.privacybutton).setOnClickListener(this);
return rootView;
}
final OnClickListener mGlobal_OnClickListener = new OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.privacybutton:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
LayoutInflater factory = LayoutInflater.from(getActivity());
final View view = factory.inflate(R.layout.privacy, null);
alertDialog.setView(view);
alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}
});
alertDialog.show();
break;
}
}
Upvotes: 2
Views: 9619
Reputation: 327
Better you go for the DialogFragment instead of AlertDialog or Dialog becasue DialogFragment is easy to implement and it has it own lifecycle methods which can be useful to handle other events or data handling. See this Google link -
https://developer.android.com/guide/fragments/dialogs
Upvotes: 0
Reputation: 656
your_dialouge_fragment dppr = new your_dialouge_fragment();
dppr.show(getActivity().getSupportFragmentManager(),"mmtag");
This worked for me.
Upvotes: 1
Reputation: 20500
AlertDialog.Builder
receives a context as parameter. And not a fragment.
Use getActivity() instead :
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(getActivity());
LayoutInflater factory3 = LayoutInflater.from(getActivity());
You also need to add the listener to your button. You can do it like this:
View rootView = inflater.inflate(R.layout.activity_about2, container, false);
rootView.findViewByID(R.id.privacybutton).setOnClickListener(this);
return rootView;
FINAL CODE
View rootView = inflater.inflate(R.layout.activity_about2, container, false);
rootView.findViewById(R.id.privacybutton).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
LayoutInflater factory = LayoutInflater.from(v.getContext());
final View view = factory.inflate(R.layout.privacy, null);
alertDialog.setView(view);
alertDialog.setNegativeButton("Schließen", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
});
return rootView;
Upvotes: 2
Reputation: 473
In my opinion, the problem is about Context and it's related to fragment. Try getting the context of the application instead of the context of the class (AboutActivity2.this)
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(getActivity());
or
AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(v.getContext());
Please try and tell if it works.
Upvotes: 0
Reputation: 98
You can use normal dialog for showing alert. Alert dialog has also derived from dialog, You need to just write an custom XML for view and set the view to dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/image"/>/>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/image"
/>
</RelativeLayout>
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Upvotes: 0