I have 4 screens (4 layouts and 4 activities). Here is the code how I'am switching it:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new DetailFragment();
case 1:
return new ImageFragment(R.drawable.ic_launcher);
case 2:
return new ImageFragment(R.drawable.b);
case 3:
return new CompFragment(R.drawable.b);
default:
return null;
}
}
But when I add onClick method in comp layout and CompFragment activity it still trying execute method from MainActivity (and display error method not found):
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button" />
I want to layout has own activity. How to do this ?
EDIT: CompFragment extends Fragment not FragmentActivity
public class CompFragment extends Fragment {
}
You do not do this, as this is not supported. If you do not want the button click to go to the activity, get rid of the
android:onClick
attribute, and have your fragment callsetOnClickListener()
on theButton
instead.