Android Fragment Onclick Button Method

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;
}

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

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;
}
}

Android/Kotlin onClick from a Fragment

I think you are getting tripped up by variable shadowing. Normally, in a Fragment with Kotlin, view would refer to whatever View you returned from onCreateView(), which likely includes the EditTexts you're looking for. However, your implementation of onClick() names its parameter view, so view.findViewById() will only scan the hierarchy of the view that was clicked.

override fun onClick(view: View?) {
// ...
// these only search within the parameter view, i.e. a Button
view.findViewById<EditText>(R.id.offsetNum).setText("1")
view.findViewById<EditText>(R.id.offsetDen).setText("1")
}

Change your onClick() signature to use a different parameter name:

override fun onClick(v: View?) {
// ...
// these now search the entire fragment hierarchy
view.findViewById<EditText>(R.id.offsetNum).setText("1")
view.findViewById<EditText>(R.id.offsetDen).setText("1")
}

Unrelated, but I recommend not implementing View.OnClickListener in your fragments/activities. Rather, just use a lambda when you set the click listener:

val offsetResetButton: Button = view.findViewById(R.id.offsetResetButton)
offsetResetButton.setOnClickListener { onOffsetResetClick() }
private fun onOffsetResetClick() {
// your code here
}

When you do it this way, you don't have to worry about when(view.id) or anything like that, because you know the method can only be called by clicks on one particular button.

Button OnClick from Fragment to MainActivity

I understand what you trying to do but android:onClick parameter only links for the context declared in tools:context field on the parent tag of your layout file.

So it is not possible to reference your imageClicked() function found in your MainActivity from your fragment's ImageView because they are in different context. Check this for more info.

Kotlin button onClickListener event inside a fragment

You're returning before you can setup the listener here:

 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {

// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_setup, container, false)

val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)

btnSetup.setOnClickListener { view ->
Log.d("btnSetup", "Selected")
}

// Return the fragment view/layout
return view
}

Try like this:

 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {


val view: View = inflater!!.inflate(R.layout.fragment_setup, container, false)

view.btnSetup.setOnClickListener { view ->
Log.d("btnSetup", "Selected")
}

// Return the fragment view/layout
return view
}

Why Android cannot find my onClick method inside fragment?

You can not handle the click event in your fragment by refer a method in XML. This only works if the method is implemented in your activity. I think it‘s because the activity ist the context of inflated view but the view doesn‘t have any reference to the fragment.

And this is why the exception is thrown because the method is missing in your activity.

You should use findViewById().setOnClickListener() inside your fragment.

Button's onClick method modifying a global variable in Fragment

If I understand what you need, you want to replace your first fragment with a new one. You can do that like so:

//Where Article fragment is your new fragment.
ArticleFragment newFragment = new ArticleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

Source: https://developer.android.com/training/basics/fragments/fragment-ui.html



Related Topics



Leave a reply



Submit