0

Obviously, I am new to Android - XML programming... So I have a navigation drawer, and once item is selected from the drawer, a corresponding fragment on the right side will display. Inside that fragment, I have linear layouts. I'd like to get redirected to another activity once that linear layout has been tapped. I was able to make it work on activities, by using android:onClick on XML file, but can't make it work on fragment. Somebody help me please.

App interface, refer to this image:

https://i.stack.imgur.com/u6dHi.jpg

Code:

fragment_smart.xml - the display once item's selected. I am trying to use the onClick on xml.

<LinearLayout
    android:id="@+id/linearLayoutsmart1"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_below="@+id/smart_title"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:orientation="horizontal"
    android:weightSum="1"
    android:onClick="smart_recommended_link">

Here's my Java code:

public class FragmentSmart extends Fragment {
public static final String TAG = "stats";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View myfragment = inflater.inflate(R.layout.fragment_smart, container, false);
    return myfragment;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}

public void smart_recommended_link(View view) {
    Intent smartRecommendedIntent = new Intent(this, SmartRecommended.class);
    startActivity(smartRecommendedIntent);
}

}

The app is crashing when I clicked on the linearlayout using this code. What's the best thing to do here? Thank you!

nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • This is the best answer and a duplicate of you question. http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments – Bilal Haider Sep 14 '15 at 14:35

3 Answers3

0

For fragments you need to add the listener programmatically:

public class MyFragment extends Fragment implements View.OnClickListener {

  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.findViewById(R.id.my_layout).setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {
    // Handle click based on v.getId()
  }

}
Simas
  • 43,548
  • 10
  • 88
  • 116
0

First of all, linear layouts cant trigger onClick events by default. Check this answer for more information: LinearLayout onClick.

You can get the LinearLayout from the view in your onCreateView() like this:

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

    LinearLayout layout = (LinearLayout)myFragment.findViewById(R.id.linearLayoutsmart1);
    // here you can set a listener of any type you want to the layout

    return myfragment;
}
Community
  • 1
  • 1
cvetanov
  • 363
  • 1
  • 3
  • 12
0

In fragments, you must use getActivity() method instead of this to reference the activity that the fragment is attached to.

public void smart_recommended_link(View view) {
    Intent smartRecommendedIntent = new Intent(getActivity(), SmartRecommended.class);
    startActivity(smartRecommendedIntent);
}
Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57