Reputation: 1751
I am working on an application in which i have one fragment class and another is MainActivity class. In MainActivity.java i am loading the Fragment class dynamically. Now when the Fragment class is loaded i want to put an Onclick event on Fragment class views.
My code snippet are as follows. MainHandlerFragment.java
public class MainHandlerFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_detail, container, false);
return view;
}
public void navigateToAnotherActivity(){ // I am not able to call this method
startActivity(new Intent(getActivity(), OrganizationActivity.class).putExtra("User",user));
}
}
home_detail.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/home_top_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shadow_layout"
android:padding="6dip"
android:layout_margin="10dp"
android:onClick="navigateToAnotherActivity()" // I am not able to call this method
>
Is there anyway to get onclick fired when a view of dynamically loaded fragment is called
Upvotes: 1
Views: 771
Reputation: 4487
Try this
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_detail, container, false);
RelativeLayout rl = (RelativeLayout)view.findViewById(R.id.home_top_layout);
rl.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
startActivity(new Intent(getActivity(), OrganizationActivity.class).putExtra("User",user));
}
});
return view;
}
Upvotes: 1
Reputation: 3339
public class MainHandlerFragment extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_detail, container, false);
public void navigateToAnotherActivity()
{ // I am not able to call this method
startActivity(newIntent(getActivity(),OrganizationActivity.class).putExtra("User",user));
}
return view;
}
}
Upvotes: 1
Reputation: 3776
First you are not calling correctly in your XML to your function, change
android:onClick="navigateToAnotherActivity()"
to
android:onClick="navigateToAnotherActivity"
Doing this way you should also change your function to
public void navigateToAnotherActivity(View v)
in order to get called.
Anyway, Android discourages this, and it will probably become a deprecated way of calling a function, so I would recommend you trying the following:
RelativeLayout rl = (RelativeLayout)findViewById(R.id.home_top_layout);
rl.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
navigateToAnotherActivity();
}
});
Upvotes: 1
Reputation: 47807
Try this way:
RelativeLayout layout = (RelativeLayout)view.findViewById(R.id.home_top_layout);
layout.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
startActivity(new Intent(getActivity(), OrganizationActivity.class).putExtra("User",user));
}
});
and remove android:onClick="navigateToAnotherActivity()"
Upvotes: 3