How to detect which dynamically created button inside a Fragment was clicked, from Activity?

I have a Fragment class that dynamically creates a grid of Buttons. An instance of that fragment is then created within the main activity which displays the grid. Here is my code:

Grid.java

public class Grid extends Fragment {

    LinearLayout myLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myLayout = new LinearLayout(getActivity());
        LayoutParams lparams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        myLayout.setLayoutParams(lparams);
        myLayout.setOrientation(LinearLayout.VERTICAL);

        for (int i = 0; i < 4; i++) {
            LinearLayout row = new LinearLayout(getActivity());
            LayoutParams rowParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(rowParams);

            for (int j = 0; j < 4; j++) {
                final Button btnTag = new Button(getActivity());
                LayoutParams btnParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

                btnTag.setLayoutParams(btnParams);
                btnTag.setText(" " + (j + 1 + (i * 4)));
                btnTag.setId(j + 1 + (i * 4));
                row.addView(btnTag);
            }

            myLayout.addView(row);
        }
        return myLayout;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_mainactivity);
        Grid grid = new Grid();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.grid, grid);
        transaction.commit();
    }
}

And in addition here is my content_mainactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_mainactivity"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.owner.tables.MainActivity"
    tools:showIn="@layout/activity_mainactivity">

    <FrameLayout
        android:layout_width="320dp"
        android:layout_height="320dp"
        android:id="@+id/grid"
        android:layout_centerHorizontal="true">
    </FrameLayout>

</RelativeLayout>

So my question is, how can I detect if and which button was clicked in fragment from the Activity? Or if I can do this in the fragment, how to pass on the information to the activity?

3
Leave a Reply

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

Use an interface to pass the click event to your activity. Call its method inside the OnClickListener.

Jason
Guest

In MainActivity

public void doSomethingAtClick() {...}

In fragment

((MainActivity)getActivity()).doSomethingAtClick()

Jason
Guest

Create an interface: public interface MyGridInterface { void myAction(<Params...>); } Set Listener in Fragment: MainActivity mActivity = new MainActivity(); setEleListener(mActivity); public void setMyListener(MyGridInterface listener) { this.gListener = listener; } Implement the Listener in MainActivity public class MainActivity extends AppCompatActivity implements MyGridInterface{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_mainactivity); Grid grid = new Grid(); FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); transaction.add(R.id.grid, grid); transaction.commit(); } @Override public void myAction(<Params..>) { //Here do whatever you want } }