KRISHNA TEJA
KRISHNA TEJA

Reputation: 147

why setOnClickListener not working for inflated button?

I am a beginner and I am inflating the button from tab_fragment.xml, when i am trying to setOnClickListener to that button, it is not working

LayoutInflater inflater=getLayoutInflater();
    View v=inflater.inflate(R.layout.tab_fragment, (ViewGroup) findViewById(R.id.line1));
    Button button= (Button) v.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
            viewPagerAdapter.addFrag(new Tab(), "two");
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
        }
    });

this is my tab_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/line1"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
    <TextView
    android:layout_margin="50dp"
    android:text="some text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_gravity="center_horizontal"
    />

</LinearLayout>

Upvotes: 0

Views: 1449

Answers (2)

David Rauca
David Rauca

Reputation: 1583

ok. Got it. The button is part of the fragment layout. So you don't have an instance of the button in the activity class. If you want to handle the button click behaviour on the activity class, you could make the activity to implement OnClickListener. Then override the onAttach() method from fragment like this:

@Override
public void onAttach(Context activity) {
    super.onAttach(activity);
    if(activity instanceof OnClickListener)
        mListener = (OnClickListener) activity;
    }
}

@Override
public void onDetach() {
    mListener = null;
    super.onDetach();
}

then on the onCreateView method:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle    savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_fragment, container, false);
    Button button = (Button) v.findViewById(R.id.button);
    button.setOnClickListener(mListener);
    return v;
}

Also, move the logic from onClick (from fragment) in onClick() from activity like below:

Override
public void onClick(View view) {
    switch (view.getId() == R.id.button) {
          // move your logic here
    }
}

Upvotes: 1

David Rauca
David Rauca

Reputation: 1583

You should have something like this in the fragment class:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_fragment, container, false);
    Button button = (Button) v.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
            viewPagerAdapter.addFrag(new Tab(), "two");
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    return v;
}

Upvotes: 0

Related Questions