Jai
Jai

Reputation: 486

How to access the fragment's button in an activity

i am creating two java file 1st main activity.java file 2nd fragment.java file create button on fragment.java how to click listener written on activity.java help me

fragment.java

public class fragment extends fragment{
Button btn;
// some code
btn = (Button)layout.findviewbyid(R.id.btn1);

}

}

activity.java

public class activity extends Activity
{


 // how to access the click action btn here
 btn.setOnclicklistner(new View.OnClickLisitner(){
 public OnClick(){


 }

Upvotes: 5

Views: 11429

Answers (4)

Linus Karlsson
Linus Karlsson

Reputation: 546

Here is my take on the issue, both in Java and Kotlin.

Java:

public final class YourActivity extends AppCompatActivity {

   /***/

   public final void yourMethod() {
      printIn("Printing from yourMethod!")
   }
}

public final class YourFragment extends Fragment {

   /***/
    
    @Override
    public void onViewCreated(View v, Bundle savedInstanceState) {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                YourActivity yourActivity = (YourActivity) getActivity();
                yourActivity.yourMethod();
            }
        }));
    }
}

Kotlin:

class YourActivity : AppCompatActivity() {

    /***/

    fun yourMethod() {
        print("Printing from yourMethod!")
    }
}

class YourFragment: Fragment() {

    /***/

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        button.setOnClickListener {
            val yourActivity = activity as YourActivity
            yourActivity.yourMethod()
        }
    }
}

I hope it helps someone out there =)

Upvotes: 2

SAM
SAM

Reputation: 418

You can define custom clickListener class and create it's instance in fragment and set listener instance there. Now you can write code in that class. Hope it will help you.

public class MyCustomListener implements OnClickListener{

     @override
     public void onClick(View v){

     // you stuff
     }  
}

then in your fragment call this

MyCustomListener listener=new MyCustomListener();
btn.setOnClickListener(listener);

Upvotes: 2

reverie_ss
reverie_ss

Reputation: 2694

To use the button in activity from the fragment, you have to use getActivity()

In your fragment,

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_item_select, container, false);

        btn = (Button) getActivity().findViewById(R.id.btn);
}

btn is the button in activity

getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).

Upvotes: 4

Skywalker10
Skywalker10

Reputation: 968

If I understand your problem correctly, you want to delegate a button click inside a fragment back to its parent activity.

Keep a reference to the parent activity inside your fragment. Then set the listener to your button in your fragment like this:

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        parentActivity.doStuff();
    }
};

In your parent Activity, define method doStuff():

public void doStuff() {
    // handle button click event here
}

Upvotes: 1

Related Questions