Android java class onClickListener - java

How can I set an onClick listener for a button in layout_one? Where do I put the code? when I put it in the onCreateView it gives me an error.
public class LayoutOne extends Fragment {
Button button;
public static Fragment newInstance(Context context) {
LayoutOne f = new LayoutOne();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_one, null);
return root;
}

set Layout attribute android:clickable="true", android:focusable="true" and android:focusableInTouchMode="true" from xml or setClickable(true) from code. set onClickListener as:
((LinearLayout)findViewById(R.id.layout_one_id)).setClickable(true); ((LinearLayout)findViewById(R.id.layout_one_id)).setOnClickListener(layoutOnClickListener);
private OnClickListener layoutOnClickListener = new OnClickListener() {
public void onClick(View v) {
//Get Click here
}
};

It wont give any error if You will do it something like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater
.inflate(R.layout.layout_one, null);
Button button_one=(Button)root.findViewById(R.id.button_one);// button_one is the id of the button in your xml file
button_one.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
return root;
}

Don't know what your button is called, but in onCreateView() you do:
button = (Button)root.findViewById(R.id.layout_one_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//stuff goes in here
}
});
Your other option is to do it in xml and just make a method that uses it. For example, setting the Buttons android:onClick="onButtonClicked" attribute, in code your method would look like this
public void onButtonClicked(View v) { /*Stuff*/ }

Related

Button in MainActivity, Reset spinner in a DialogFragment

I have two buttons in MainActivity, the first one opens a custom DialogFragment with some spinners and in the other button it resets the spinners of this DialogFragment.
When I click the reset button it calls this method that is in the DialogFragment:
public class FilterDialogFragment extends DialogFragment {
private View view;
// ...
#Nullable
#Override
public View onCreateView(#NotNull LayoutInflater inflater,
#Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dialog_filters, container, false);
// start the spinners and adapters here
return view;
}
public void resetFilters() {
if (view != null) {
categorySpinner.setSelection(0);
productSpinner.setSelection(0);
priceSpinner.setSelection(0);
}
}
// some more codes here
}
My MainActivity:
public class MainActivity extends AppCompatActivity {
private FilterDialogFragment filterDialog;
private Button button_clear;
private Button button_filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_clear = findViewById(R.id.button_clear);
button_filter = findViewById(R.id.button_filter);
filterDialog = new FilterDialogFragment();
button_filter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Show the dialog containing filter options
filterDialog.show(getSupportFragmentManager(), FilterDialogFragment.TAG);
}
});
button_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//reset all filters
filterDialog.resetFilters();
}
});
//some more codes here
}
//some more methods here
}
But by clicking the button that opens the DialogFragment, the values of the Spinners remain the same instead of returning the data of position 0 of each spinner.
Would anyone know how to solve this? I've tried everything and I can not.
The simplest way I ended up finding, was to delete the resetFilters method, and instead I did so:
button_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//reset all filters
filterDialog = new FilterDialogFragment(); }
});

Spinner Fragment continually crashes

I am making a program that will include a Spinner within a fragment rather than an activity. I have researched why this may be crashing, but to no avail. My initial concern was a .getView().findViewById(), but now I'm not so sure that's the problem because . Here's the code.
public class Add extends Fragment {
public Add() {
// Required empty public constructor
}
ArrayList<String> ingredients = new ArrayList<>();
SpinnerDialog spinnerDialog;
Button add;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
add = (Button) getView().findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinnerDialog.showSpinerDialog();
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add, container, false);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
spinnerDialog = new SpinnerDialog(getActivity(), ingredients, "Select An Ingredient");
spinnerDialog.bindOnSpinerListener(new OnSpinerItemClick() {
#Override
public void onClick(String Ingredient, int i) {
Toast.makeText(Add.super.getContext(), "Selected ", Toast.LENGTH_SHORT).show();
}
});
Without seeing the stack trace I cannot say for sure, but I suspect this line is crashing:
add = (Button) getView().findViewById(R.id.add);
This is because getView() will return the View instance returned by onCreateView()... but you're currently inside onCreateView(), so getView() will return null.
You can re-write your onCreateView() as follows and it should work.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_add, container, false);
add = (Button) root.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
spinnerDialog.showSpinerDialog();
}
});
return root;
}

android show hide dialogfragment

I have custom DialogFragment which called from another fragment:
final CustomCalendarDialogFragment newFragment = new CustomCalendarDialogFragment("CHOOSE_WEEK");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (newFragment.isAdded()){
newFragment.getDialog().show();
} else {
newFragment.show(getFragmentManager(), "CUSTOM_CALENDAR");
}
}
});
In CustomCalendarDialogFragment when pressed "OK":
getDialog().hide();
After pressed on "OK" DialogFragment is hide, but when I unlock screen DialogFragment is displayed.
How to eliminate it?
You can track the state of showing/hiding the dialog in the tag attribute of the fragment view.
Initially set it as true (equivalent to shown) in onCreateView()
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_sheet, container, false);
view.setTag(true);
return view
}
And whenever you show/hide it set it to true/false:
final CustomCalendarDialogFragment newFragment = new CustomCalendarDialogFragment("CHOOSE_WEEK");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (newFragment.isAdded()){
newFragment.getDialog().show();
} else {
newFragment.show(getFragmentManager(), "CUSTOM_CALENDAR");
newFragment.requireView().setTag(true);
}
}
});
And set newFragment.requireView().setTag(false); when you call getDialog().hide();
And when the app goes to the background, check that tag on onResume() to see if you want to keep the dialog shown or hide it:
#Override
protected void onResume() {
super.onResume();
Object tag = newFragment.requireView().getTag();
if (tag instanceof Boolean){
if ((!(boolean)tag))
newFragment.getDialog().hide();
}
}

Issue with onClickListener for Button in Fragment

I have a button in a fragment class that I'd like to have trigger a method in the parent activity. I've implemented an interface for this.
My issue is that the View.onClickListener is giving me the following error:
Class 'Anonymous class derived from onClickListener' must either be declared abstract or implement abstract method 'onClick(View)' in 'onClickListener'
Which is odd, because I'm implementing onClick(View).
Here is the code in my Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(com.zlaporta.chessgame.R.layout.gamedescfragment, container, false);
final Button make_move = (Button) v.findViewById(R.id.make_move);
make_move.setOnClickListener(new ***View.OnClickListener()*** {
public void OnClick(View v) {
makeMoveCallback.makeMoveMethod();
}
});
The stars indicate the portion of the code that Android Studio doesn't like.
Do it using Anonymous inner class in an object:
//declaring OnClickListener as an object
private OnClickListener btnClick = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
//passing listener object to button
make_move.setOnClickListener(btnClick);
Hope this will help :)
Just a typo: Replace the method name
OnClick
with
onClick
Could you try passing the OnClickListener from a method
e.g.,
private View.OnClickListener getButtonOnClickListener() {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
// this is the code
}
};
}
and then using make_move.setOnClickListener(getButtonOnClickListener());
This is best way to using click event for button. using onClick listener implementing. use below code.
public class MyFragment extends Fragment implements OnClickListener {
Button mButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_layout, null);
mButton = (Button) view.findViewById(R.id.button1);
mButton.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
if (v == mButton) {
// Do something on click button
}
}
}
If using every click event separately it take more space. its better to use this code.
Add #Override above onClick method
Or you can use below method of button click Listener in OnActivityCreated() methos of Fragment.
make_move.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
makeMoveCallback.makeMoveMethod();
}
});

android fragment - how to inflate the layout and then make the UI functions

I have a Fragment instance, and I would like to be able to implement click listeners and other attributes after the fragment is loaded and shown on screen. How can I achieve this? Where should I be doing this implenentation?
Try the following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.rap_prog_fields, container, false);
TextView tv = (TextView) v.findViewById(R.id.text_view_1);
Button b = (Button) v.findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
//the rest of your views... in the same manner
return v;
}
You can also find your views after returning from onCreateView like such:
public void onStart() {
super.onStart();
Button b = (Button) getView().findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
//Everything else...
}
TO complete actions once the view is displayed and on the screen you need to implement the following:
final ViewTreeObserver observer= button.getViewTreeObserver(); //you need an anchor view here that will be drawn via xml
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//do stuff here.....
}
});

Categories

Resources