Reputation: 3829
I am trying to create a set of vertical tabs within an android project, I have been looking at a number of tutorials and posts on SO but cannot seem to piece it all together.
I created a new project with a fragment so I have a main.xml and a fragment_main.xml, a main activity class and a mainfragment class.
main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.android.MainScreenActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<FrameLayout android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.2">
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<LinearLayout
android:id="@+id/tab_btn_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/patient_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/relations_tab_btn"
android:onClick="tabHandler"/>
<Button android:layout_height="0dip"
android:layout_width="fill_parent"
android:layout_weight="1.0"
android:background="@color/sunshine_blue"
android:id="@+id/providers_tab_btn"
android:onClick="tabHandler"/>
</LinearLayout>
</FrameLayout>
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="fill_parent" android:layout_weight="0.8"/>
</LinearLayout>
The main activity doesnt do anything other than the defaults and I havent added to it as I thought I should use a fragment as the google sunshine app uses a fragment for the main screen.
main fragment class
private FragmentTabHost mTabHost;
Button patientButton, relationsButton, providersButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tab_btn_container);
patientButton = (Button)rootView.findViewById(R.id.patient_tab_btn);
relationsButton = (Button)rootView.findViewById(R.id.relations_tab_btn);
providersButton = (Button)rootView.findViewById(R.id.providers_tab_btn);
return rootView;
}
public void tabHandler(View target){
Log.d(LOG_TAG, "Calling tabHandler() method");
patientButton.setSelected(false);
relationsButton.setSelected(false);
providersButton.setSelected(false);
if(target.getId() == R.id.patient_tab_btn){
//mTabHost.setCurrentTab(0);
patientButton.setSelected(true);
} else if(target.getId() == R.id.relations_tab_btn){
//mTabHost.setCurrentTab(1);
relationsButton.setSelected(true);
} else if(target.getId() == R.id.providers_tab_btn){
//mTabHost.setCurrentTab(2);
providersButton.setSelected(true);
}
}
So I have added the tabHandler button and the xml is inflated inside the fragment main class but I get an error pressing any of the tab buttons:
java.lang.IllegalStateException: Could not find method tabHandler(View) in a parent or ancestor Context for android:onClick attribute
I am also not sure how I would update the tabcontent pane as all the examples seem to use an Activity and have the activity class extend from TabActivity but using a fragment my class already extends Fragment.
How do I get this to work with a fragment which contains the tabs?
Upvotes: 0
Views: 699
Reputation: 2198
The method that you've specified in android:onClick
(tabHandler
) should be located in the Activity, not the Fragment. This SO answer has more information: https://stackoverflow.com/a/4153842/3214339
Upvotes: 2