How to handle button clicks using the XML onClick within Fragments
You could just do this:
Activity:
Fragment someFragment;
//...onCreate etc instantiating your fragments
public void myClickMethod(View v) {
someFragment.myClickMethod(v);
}
Fragment:
public void myClickMethod(View v) {
switch(v.getId()) {
// Just like you were doing
}
}
In response to @Ameen who wanted less coupling so Fragments are reuseable
Interface:
public interface XmlClickable {
void myClickMethod(View v);
}
Activity:
XmlClickable someFragment;
//...onCreate, etc. instantiating your fragments casting to your interface.
public void myClickMethod(View v) {
someFragment.myClickMethod(v);
}
Fragment:
public class SomeFragment implements XmlClickable {
//...onCreateView, etc.
@Override
public void myClickMethod(View v) {
switch(v.getId()){
// Just like you were doing
}
}
How to Handle onClick in Fragments
Better approach would be implementing OnClickListener
to your fragment class and overriding onCreateView
in your fragment where you assign the listener to your button.
By putting onClick
attribute in your XML layout, your activity on load will look for the element in the activity, not in the fragment. This will throw exception.
I would suggest reading some fragment-activity hierarchy to understand when is it possible to access elements in your fragment.
public class StartFragment extends Fragment implements OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_start, container, false);
Button b = (Button) v.findViewById(R.id.save_keywords);
b.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.save_keywords:
...
break;
}
}
}
Reference from: here
Android Fragment onClick button Method
Your activity must have
public void insertIntoDb(View v) {
...
}
not Fragment .
If you don't want the above in activity. initialize button in fragment and set listener to the same.
<Button
android:id="@+id/btn_conferma" // + missing
Then
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
Button button = (Button) view.findViewById(R.id.btn_conferma);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// do something
}
});
return view;
}
Button OnClick in a fragment
Your TabLayout
requires a new instance of PlaceholderFragment
each time a new page is selected, so let's take a closer look at its onCreateView()
:
Depending on the value of getArguments().getInt(ARG_SECTION_NUMBER)
, a specific layout is inflated. If the value is 1, the layout will be the same as for SubPage02
.
But this does not mean that an instance of SubPage02
will be created, they just happen to share the same layout file.
So the OnClickListener
has to be set in the onCreateView()
of PlaceholderFragment
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
return rootView;
} else if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
View rootView = inflater.inflate(R.layout.fragment_sub_page02, container, false);
Button mButton = (Button) rootView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// here you set what you want to do when user clicks your button,
}
});
return rootView;
} else {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
Click Listener of button from the XML of a fragment that is MVVM based architecture not working
If you want to call viewmodel method from xml using data binding ,first you need to set viewmodel object for xml in your fragment.
profileViewModel =
new ViewModelProvider(requireActivity()).get(ProfileViewModel.class);
binding = FragmentProfileBinding.inflate(inflater, container, false);
binding.setLifecycleOwner(this);
binding.setProfileFragment(profileViewModel);
Fragment with buttons: onClick() vs. XML onClick
FWIW I never use the xml
onClick
attributes. Although they may save a couple of lines of typing, they make it more difficult to follow what's happening in your code.
If your class implements
View.OnClickListener
and you have correctly overriden the onClick
method (which it looks like you have), then you can safely remove any onClick
s in your layout
files and instead assign methods to your widget clicks in the following way:
public class MainFragment extends Fragment implements View.OnClickListener {
private Button viewOne, viewTwo, viewThree;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
viewOne = (Button) rootView.findViewById(R.id.view_one);
viewTwo = //etc...
//"this" refers to the current object. As the object is of a class that implements OnClickListener,
//passing "this" satisfies the View.OnClickListener parameter required for the setOnClickListener() method.
viewOne.setOnClickListener(this);
viewTwo.setOnClickListener(this);
viewThree.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View view) {
//To identify the correct widget, use the getId() method on the view argument
int id = view.getId();
switch (id) {
case R.id.view_one:
//viewOne clicked
break;
case R.id.view_two:
//And so on...
}
}
}
Android onClick inside Fragments not found
If used Fragment
then try below code.
issue in your onRotateButtonClicked
public class SecondFragment extends Fragment implements OnClickListener{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_second, container, false);
ImageButton button = (ImageButton) v.findViewById(R.id.button);
button.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button :
// your button click
i1 = (ImageView) getView().findViewById(R.id.imageView4);
Animation rotateAnimation = new RotateAnimation(
0,
360,
Animation.RELATIVE_TO_SELF,
0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
rotateAnimation.setDuration(500);
i1.startAnimation(rotateAnimation);
break;
}
}
}
Related Topics
Programmatically Obtain the Phone Number of the Android Phone
How to Change the Status Bar Color in Android
How to Prevent a Dialog from Closing When a Button Is Clicked
How to Filter Specific Apps For Action_Send Intent (And Set a Different Text For Each App)
How to Check a Certain Data Already Exists in Firestore or Not
How to Get the Web Page Contents from a Webview
Getapplication() Vs. Getapplicationcontext()
How to Do Something Like a Flowlayout in Android
How to Make Layout With Rounded Corners..
Static Way to Get 'Context' in Android
Retrieve a Fragment from a Viewpager
How to Set the Part of the Text View Is Clickable
How to Capture the "Virtual Keyboard Show/Hide" Event in Android