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.
Related
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();
}
});
I am trying to display two RecyclerViews inside alert dialog contents. Please Help what am I doing wrong?
The onCreateViewHolder function is not being called
Please note that the RecyclerViews are inside the ConstraintLayout in the xml. If that is causing them not to display what should be the correct procedure?
public class MainActivity extends AppCompatActivity {
private View inflatedView;
private RecyclerView fontList,sizeList;
private String[] fonts = {"normal","sans","serif","monospace"};
private String[] sizes = {"14","16","18","20","22","24"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);
builder.setView(inflatedView).setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
fontList = inflatedView.findViewById(R.id.fontlist);
sizeList = inflatedView.findViewById(R.id.sizelist);
fontList.setLayoutManager(new LinearLayoutManager(this));
fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));
sizeList.setLayoutManager(new LinearLayoutManager(this));
sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));
}
private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private List<String> itemList;
public MyAdapter(List<String> itemList) {
this.itemList = itemList;
Log.d("lol","MyAdapter");
}
#NonNull
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.design_fontcell,viewGroup);
Log.d("lol","onCreateViewHolder");
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.mtextView.setText(itemList.get(i));
Log.d("lol",itemList.get(i));
}
#Override
public int getItemCount() {
Log.d("lol","getItemCount: "+itemList.size());
return itemList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView mtextView;
MyViewHolder(View cellView){
super(cellView);
mtextView = cellView.findViewById(R.id.textView_font);
}
}
}
}
Change your code like this
AlertDialog.Builder builder = new AlertDialog.Builder(this);
inflatedView = getLayoutInflater().inflate(R.layout.design_style,null);
//No need of setView() for buttons
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"OK Pressed",Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Cancel",Toast.LENGTH_SHORT).show();
}
});
fontList = inflatedView.findViewById(R.id.fontlist);
sizeList = inflatedView.findViewById(R.id.sizelist);
fontList.setLayoutManager(new LinearLayoutManager(this));
fontList.setAdapter(new MyAdapter(Arrays.asList(fonts)));
sizeList.setLayoutManager(new LinearLayoutManager(this));
sizeList.setAdapter(new MyAdapter(Arrays.asList(sizes)));
builder.setView(inflatedView); //Set the view at the end
AlertDialog dialog = builder.create();
dialog.show();
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();
So this is my Dialog class:
public class SecondActivity extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.second, null))
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}
}
In my dialog there are 2 edit texts which get 2 strings. I want to use those 2 strings in my MainActivity if the user presses on the save button. How do I do that?
The inflate method returns a view where you can execute a findViewById, like the following:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View v = inflater.inflate(R.layout.second, null);
EditView editView = (EditView)v.findById(R.id.your_edit_view);
builder.setView(v)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss();
}
});
return builder.create();
}