Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 10 months ago.
Improve this question
hello i have a issue i can't put a pop up on a fragment in android studio
here's the code i try to use , the thing i try is to add a pop up to my fragment that when i click on a button a pop up show and show some text in it tho i tried to add a working code in the fargemnt it just dont work
public class fragment2 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2_layout,container,false);
}
private Activity ControllerCompat;
android.widget.Button Bouton1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment2_layout);
Bouton1 = (android.widget.Button) findViewById(R.id.button1);
Bouton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
AlertDialog.Builder alert = new AlertDialog.Builder(fragment2.this);
alert.setTitle("voici les boutons");
alert.setPositiveButton("yes ", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("no", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}
});
}
}
like this? if yes then i have an error on the buuton1 saying that he cant resolve symbol "bouton 1"
`public class fragment2 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2_layout, container, false);
buton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
alertDialogBuilder.setTitle("Title");
alertDialogBuilder.setMessage("Message");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton("yes", (dialog, id) ->
{
}
);
alertDialogBuilder.setNegativeButton("no", (dialog, id) -> dialog.cancel());
AlertDialog myAlertDialog = alertDialogBuilder.create();
myAlertDialog.show();
}
});
}
}
`
Initialize your buton first
Bouton1 = (android.widget.Button) findViewById(R.id.button1);
Bouton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
alertDialogBuilder.setTitle("Title");
alertDialogBuilder.setMessage("Message");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton("yes", (dialog, id) ->
{
}
);
alertDialogBuilder.setNegativeButton("no", (dialog, id) -> dialog.cancel());
AlertDialog myAlertDialog = alertDialogBuilder.create();
myAlertDialog.show();
}
});
Related
In my application I want use custom dialog into DialogFragment . I can use AlertDialog into DialogFragment but I want use custom dialog instead of AlertDialog.
I write below codes into DialogFragment :
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View rootView = LayoutInflater.from(mActivity).inflate(R.layout.fragment_dialog_comment, null);
initVar();
initView(rootView);
initFunctionality();
return new AlertDialog.Builder(mActivity)
.setView(rootView)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
authorName = edtAuthorName.getText().toString().trim();
authorEmail = edtAuthorEmail.getText().toString().trim();
authorComment = edtAuthorComment.getText().toString().trim();
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_EMAIL, authorEmail);
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_NAME, authorName);
if (commentId == AppConstant.THIS_IS_COMMENT) {
sendComment(authorName, authorEmail, authorComment);
} else {
sendReply(authorName, authorEmail, authorComment);
}
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (dialogInterface != null) {
dialogInterface.dismiss();
}
}
})
.create();
}
I want use below code instead of above AlertDialog :
final Dialog dialog = new Dialog(mActivity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.setContentView(R.layout.fragment_dialog_comment);
edtAuthorName = dialog.findViewById(R.id.edt_author_name);
edtAuthorEmail = dialog.findViewById(R.id.edt_author_email);
edtAuthorComment = dialog.findViewById(R.id.edt_author_comment);
dialogComment_okBtn = dialog.findViewById(R.id.dialogComment_okBtn);
dialogComment_cancelBtn = dialog.findViewById(R.id.dialogComment_cancelBtn);
edtAuthorName.setText(AppPreference.getInstance(mActivity).getString(PrefKey.KEY_NAME));
dialogComment_cancelBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (dialog != null) {
dialog.dismiss();
}
}
});
dialogComment_okBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
authorName = edtAuthorName.getText().toString().trim();
authorEmail = edtAuthorEmail.getText().toString().trim();
authorComment = edtAuthorComment.getText().toString().trim();
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_EMAIL, "noEmail#gmail.com");
AppPreference.getInstance(mActivity).setString(PrefKey.KEY_NAME, authorName);
if (commentId == AppConstant.THIS_IS_COMMENT) {
sendComment(authorName, "noEmail#gmail.com", authorComment);
} else {
sendReply(authorName, "noEmail#gmail.com", authorComment);
}
dialog.dismiss();
}
});
dialog.show();
How can I it? Please don't give me negative points and please help me
You can extend your class with DialogFragment and rest will be like fragments like
public class YOurDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
//If don't want toolbar
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.customLayout, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
My page is activity page and now i would like to change to fragment but it crashed,
Caused by: java.lang.ClassCastException: com.mac.Activity cannot be cast to android.app.Activity
I dont know which part of code is crashed. So I put my code in below.
Code:
public class Activity extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.activity, container, false);
final TextView nameTxt = (TextView) view.findViewById(R.id.nameTxtDetail);
final TextView descTxt = (TextView) view.findViewById(R.id.descDetailTxt);
final Button btn = (Button) view.findViewById(R.id.btn);
//RECEIVE
Intent i = getActivity().getIntent();
String name = i.getExtras().getString("NAME_KEY");
String desc = i.getExtras().getString("DESCRIPTION_KEY");
//BIND
nameTxt.setText(name);
descTxt.setText(desc);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
getContext());
alertDialogBuilder.setTitle("Do you want to login?");
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setNeutralButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getActivity(), FacebookLogin.class);
startActivity(i);
}
})
.setPositiveButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close
// current activity
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
return view;
}
#Override
public void onResume() {
super.onResume();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
TextView toolbar_title = (TextView) getActivity().findViewById(R.id.toolbar_title);
toolbar_title.setText("DETAIL");
}
#Override
public void onStop() {
super.onStop();
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(false);
}
}
First I though crashed because forget put final for textview and button but i already put now, still crashed. I am using android studio. Hope somebody help thanks.
There are a lot of factors here that could cause this. But firstly did you remove it from Android Manifest, because fragments are not shown in Android Manifest, and if you didn't it would try opening it as an activity.
Override another method onViewCreated(). Get all the codes you put in onCreateView(), except the first line that inflates, put them in onViewCreated(). This way you are sure the view has been created and is ready to be used.
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity, container, false);
}
#Override
public void onViewCreated (View view, Bundle savedInstanceState){
super.onViewCreated (view, savedInstanceState);
final TextView nameTxt = (TextView) view.findViewById(R.id.nameTxtDetail);
final TextView descTxt = (TextView) view.findViewById(R.id.descDetailTxt);
final Button btn = (Button) view.findViewById(R.id.btn);
//RECEIVE
Intent i = getActivity().getIntent();
String name = i.getExtras().getString("NAME_KEY");
String desc = i.getExtras().getString("DESCRIPTION_KEY");
//BIND
nameTxt.setText(name);
descTxt.setText(desc);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( getContext());
alertDialogBuilder.setTitle("Do you want to login?");
// set dialog message alertDialogBuilder .setCancelable(false)
.setNeutralButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent i = new Intent(getActivity(), FacebookLogin.class); startActivity(i);
}
})
.setPositiveButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// if this button is clicked, close //
current activity dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it alertDialog.show();
}
});
}
This question already has answers here:
Problem inflating custom view for AlertDialog in DialogFragment
(9 answers)
Closed 7 years ago.
I am trying to build an AlertDialog and want to show it when the main activity starts. But when the activity starts, an error comes up:
android.util.AndroidRuntimeException: Window feature must be requested before adding content
What I do first is creating an AlertFragment:
public class AlertFragment extends DialogFragment{
public static AlertFragment newInstance(){
return new AlertFragment();
}
public AlertFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState){
super.onCreateView(inflater, container, SavedInstanceState);
return inflater.inflate(R.layout.alert_dialog, container, false);
}
#Override
public Dialog onCreateDialog(Bundle SaveInstanceState){
return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
}
}).create();
}
}
The following is the main activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertFragment AF = AlertFragment.newInstance();
AF.show(getSupportFragmentManager(), "Dialog");
}
}
Can someone tell me where the problem lies?
In your AlertFragment remove oncreateview and run
public class AlertFragment extends DialogFragment{
public static AlertFragment newInstance(){
return new AlertFragment(); }
public AlertFragment(){}
#Override
public Dialog onCreateDialog(Bundle SaveInstanceState){
return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
}
}).create();
}
}
just use show in place of create in your code like following:
public Dialog onCreateDialog(Bundle SaveInstanceState){
return new AlertDialog.Builder(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Dialog)).setMessage("Alert Dialog").setPositiveButton("Set", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "Positive", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getContext(), "Negative", Toast.LENGTH_SHORT).show();
}
}).show();
i have this code in java:
public class AboutFragment extends Fragment {
public AboutFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about, container, false);
new AlertDialog.Builder(this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(R.drawable.ic_dialog_alert)
.show();
return rootView;
}
and error is:
The constructor AlertDialog.Builder(AboutFragment) is undefined,
ic_dialog_alert cannot be resolved or is not a field
That's because the AlertDialog.Builder requires a Context and a Fragment is not a Context. Try getActivity() instead of this.
I have an ItemClickListener in a gridview. But my itemclicklistener is not being called. There is no activity on item click of the gridview
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
vi = inflater.inflate(R.layout.home, container, false);
Button startdialog = (Button) vi.findViewById(R.id.btnCreateDialog);
startdialog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent startdialog = new Intent(getActivity(),
start_dialog.class);
startActivity(startdialog);
}
});
Button iv = (Button) vi.findViewById(R.id.btnMoreDialog);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu homepopup = new PopupMenu(getActivity(), v);
MenuInflater inflater = homepopup.getMenuInflater();
inflater.inflate(R.menu.moredialog, homepopup.getMenu());
homepopup.show();
}
});
PremiumgridView = (StaggeredGridView) vi
.findViewById(R.id.premiumstaggeredGridView);
new Dialogs().execute(urls);
return vi;
}
private class Dialogs extends AsyncTask<String[], Void, String[]> {
#Override
protected String[] doInBackground(String[]... params) {
return params[0];
}
protected void onPostExecute(String[] result) {
int premiummargin = getResources().getDimensionPixelSize(
R.dimen.margin);
PremiumgridView.setItemMargin(premiummargin);
PremiumgridView.setPadding(premiummargin, 0, premiummargin, 0);
final StaggeredAdapter premiumadapter = new StaggeredAdapter(
vi.getContext(), R.id.photoimageview, result,
R.layout.row_staggered_demo);
PremiumgridView.setAdapter(premiumadapter);
premiumadapter.notifyDataSetChanged();
premiumadapter.onClick(vi);
PremiumgridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(StaggeredGridView parent, View view,
int position, long id) {
String item = premiumadapter.getItem(position).toString();
Toast.makeText(getActivity(), premiumadapter.getItem(position), Toast.LENGTH_SHORT).show();
// Toast.makeText(getActivity(), "You have chose: "+ item, Toast.LENGTH_LONG).show();
}});
}
#Override
protected void onPreExecute() {
}
}
Anyone please?
Thanks,
Solved the problem by removing the button from the xml. Clickable item cannot have another clickable item inside it. Reference OnItemClickListener Not Triggered on Android GridView
Does it crash? If yes, always provide us with the Stacktrace/Logcat.
As far as I know, it´s impossible to directly change the interface from within any Thread other than the UI-Thread - you could either try Handler or use this.
EDIT: OnPostExecute is actually called on the UI-Thread, so this is not a solution for this problem. (see here)