Why is my showPopup grey when I already have an OnClick method on that fragment's xml?
I'm trying to make the popup_about_app XML popup when I click on the button with the OnClick.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myDialog = new Dialog(getContext());
return inflater.inflate(R.layout.fragment_settings, container, false);
}
public void showPopup(View view) {
TextView txtclose;
myDialog.setContentView(R.layout.popup_about_app);
txtclose =(TextView) myDialog.findViewById(R.id.txtClose);
txtclose.setText("X");
txtclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myDialog.dismiss();
}
});
myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myDialog.show();
}
In Android Studio, gray means its not being used yet. Either nothing is calling showPopup, or possibly that Android Studio isn't smart enough to find the thing calling showPopup (which can happen in various scenarios). I wouldn't worry about it unless you hit the button that's supposed to call showPopup and nothing happens (and then it means you forgot to call it). If the popup shows like you expect, its a non-issue.
Related
I want to display the alert dialog when onClick of the button in fragment layout. I have tried but the app is crashing when it runs.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
binding = FragmentWalletBinding.inflate(inflater, container, false);
Button button = (Button)getView().findViewById(R.id.add_money);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder alertdialog = new AlertDialog.Builder(view.getContext());
alertdialog.setMessage("this is message");
alertdialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(),"toast message",Toast.LENGTH_SHORT).show();
}
}).show();
}
});
return binding.getRoot();
}
onCreateView returns View which can be further obtained by getView() method. and you are calling it before onCreateView had a chance to return own View for framework (its still inside method, didn't return binding.getRoot(); yet)
if you are using view binding then obtain your Button from there
Button button = (Button) binding.addMoney;
or even just use strictly (without additional local Button)
binding.addMoney.setOnClickListener(...
and if you REALLY WANT to use findViewById (but why...), then you can look inside binding.getRoot() even when it isn't returned/drawn yet
Button button = (Button) binding.getRoot().findViewById(R.id.add_money);
there is a problem in
(Button)getView().findViewById(R.id.add_money);
please make sure id is same as in XML.
The problem that Teno I want to use an intro slide in my app through fragments and the third fragment I want to put a button that leads to another activity and in Android studio the code does not make any error but when I run the app and click the app button It stops what is it?
public ThirdFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_third, container, false);
viewPager = getActivity().findViewById(R.id.viewPager);
back1 = view.findViewById(R.id.slideThereback);
back1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewPager.setCurrentItem(1);
}
});
//The TextView "Done " is the one I want to click on the Take me to another activity and that up to now gives me an error to run in the emulator
done = view.findViewById(R.id.Done);
done.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), MenuP.class);
startActivity(myIntent);
}
});
return view;
}
I hope I can provide the code or say my error to open a new activity in a fragment and not close the app to Ejecutarce on a mobile device
Use the below code in your onClick() method.
Intent myIntent = new Intent(getActivity(), MenuP.class);
getActivity().startActivity(myIntent);
Check that R.id.Done does exists in R.layout.fragment_third. if it doesn't exist then done View might be null.
I have to build a simple app where I have 3 fragments, each contains 1 button.
If one of these buttons is pressed a site should load within a webview which is contained by the main activity. So far i have the basic callback to the activity working, but my question is. How do I check which button is pressed? Since they all seem to send the same view to the activity. I have tried to use the view.setTag() method but I couldn't get it to work.
Here is some of my code:
MainActivity.java
#Override
public void onClick(View v){
if(v.getId() == R.id.btnApple){
wvResult.loadUrl("http://www.apple.com");
}else if (v.getId() == R.id.btnMic){
wvResult.loadUrl("http://www.microsoft.com");
}else if(v.getId() == R.id.btnOracle){
wvResult.loadUrl("http://www.oracle.com");
}
}
One of the fragment classes (each is different in the names (apple, microsoft and oracle), the rest is exactly the same):
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.btn_a, container, false);
btnApple = (Button)v.findViewById(R.id.btnApple);
btnApple.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callBack.onClick(v.getRootView());
}
});
return v;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
callBack = (View.OnClickListener) activity;
}
I hope someone can help me
Just pass v in your callback method (not v.getRootView()) because it is the view which was clicked.
View.getRootView() retrieves the root view element of view hieararchy, which is most likely one of the ViewGroup classes (LinearLayout, RelativeLayout, ...). That is the view which contains the clicked button.
I have a button in fragment, and for that button i had override onClick() method, but its not working.
i have a Taost and a Log too when the button is being clicked.
public class DataShown extends Fragment implements OnClickListener{
Button tv;
TextView textview;
Activity activity=getActivity();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("3", "started");
View rootView = inflater.inflate(R.layout.datashown, container, false);
Log.d("3", "closed");
textview=(TextView) rootView.findViewById(R.id.textView1);
tv = (Button) rootView.findViewById(R.id.configButton);
tv.setOnClickListener((OnClickListener) activity);
return rootView;
}//onCtreate
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.d("onClick","1");
Toast.makeText(activity, "on click", Toast.LENGTH_SHORT).show();
}
}
why its happening, i have no idea, may be its silly mistake.
Now the toast is showing error, is accepts context object so i provided activity, not working.
Change
tv.setOnClickListener((OnClickListener) activity);
to
tv.setOnClickListener(this);
and in onClick method change
Toast.makeText(activity, "on click", Toast.LENGTH_SHORT).show();
to
Toast.makeText(getActivity(), "on click", Toast.LENGTH_SHORT).show();
and remove
Activity activity=getActivity();
because you did initiate it before fragment attached to activity.
You are creating the view in the fragment, but binding the on click listener to the activity.
tv.setOnClickListener(this); since fragment is the one tat is overriding the onClick listener and not the activity
Yon can do this:
tv.setOnClickListener(getActivity);
This is the problem with your context in setOnClickListener, you need to give your actvivities context to work fine.
setOnClickListener(getActivity); will pass the actvities context to the onclicklistner.
i use fragment for my application,they are : Fragment A, Fragment B and Fragment C.
to move from one fragment to another fragment, I use two buttons, the first button is used to move to the next fragment, and the second button is used to return to the previous fragment.
the problems when I move from one fragment to another fragment (such as from A to B and back to A and to B again and to C). all entries in the EditText that is in a fragment is lost and look back empty,it when I go back to the previous fragment, or when I want to go back to the next fragment. I've been using code addToBackStack () when switching to another fragment, indeed EditText that I made not lost when I press the back button on my phone, but it does not affect the buttons (Next And Prev) that I've created. this is Fragment A:
public class AFragment extends Fragment {
EditText text,text2;
public AFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_a, container, false);
ImageView next=(ImageView)rootView.findViewById(R.id.next);
text=(EditText)rootView.findViewById(R.id.text);
text2=(EditText)rootView.findViewById(R.id.text1);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getFragmentManager().beginTransaction().replace(R.id.frame_container, new BFragment()).addToBackStack(null).commit();
}
});
return rootView;
}
this is Fragment B:
public class BFragment extends Fragment {
EditText cut,cut2;
public BFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View Fragment B = inflater.inflate(R.layout.fragment_c, container, false);
ImageView next=(ImageView)rootView.findViewById(R.id.next);
ImageView prev=(ImageView)rootView.findViewById(R.id.prev);
cut=(EditText)rootView.findViewById(R.id.cut);
cut2=(EditText)rootView.findViewById(R.id.cut2);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getFragmentManager().beginTransaction().replace(R.id.frame_container, new CFragment()).addToBackStack(null).commit();
}
});
prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
getFragmentManager().beginTransaction().replace(R.id.frame_container, new AFragment()).addToBackStack(null).commit();
}
});
return Fragment B;
}
my question is how can i save view in Fragment when user click on next and prev button in my application? is there something missing in my code? i hope someone can help me to solve my problem. thank you very much.
Each fragments have various life cycle methods, you can use onDestroy method and save the required edit text view value calling Activity. For this expose some methods in the calling activity which can be access via fragment. Also else create a singleton class or use Shared Prefences.
I hope this helps.
try to change:
getFragmentManager().beginTransaction().replace(R.id.frame_container, new BFragment()).addToBackStack(null).commit();
to:
getFragmentManager().beginTransaction().add(R.id.frame_container, new BFragment()).addToBackStack(null).commit();