Skip to content Skip to sidebar Skip to footer

Android Studio - Is Possible To Add Tabs Pointing To Fragments From Designer?

i'm trying to add tabs for main activity which should pointing to several fragments. Is it possible from Android Studio designer? I added into my activity layout tabhost like on i

Solution 1:

I will reduce my example on one Button, you should be able to multiply it. I also cut out unneccessarry stuff like margins etc... If you have problems, just tell me and I will gladly help you out:

MainActivity.java - openHome method is interesting

publicclassMainActivityextendsActivityimplementsHomeFragment.OnFragmentInteractionListener
{
    FragmentManager fragmentManager = getFragmentManager();

    HomeFragment homeFragment;


    @OverrideprotectedvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        homeFragment = newHomeFragment();

        fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit();
    }

    publicvoidopenHome(View view)
    {
        homeFragment = newHomeFragment();
        fragmentManager.beginTransaction().replace(R.id.mainFrame, homeFragment).commit();
    }

    @OverridepublicvoidonFragmentInteractionHome(Uri uri)
    {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();//only to check, can be removed (the content, the method must be implemented!!!!!!!!!!!!!!!)
    }
}

activity_main.xml

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"tools:ignore="MergeRootFrame" ><FrameLayoutandroid:id = "@+id/mainFrame"android:layout_width = "match_parent"android:layout_height = "match_parent"android:layout_marginBottom = "@dimen/bottom_Main_Tabs"></FrameLayout><LinearLayoutandroid:layout_width = "match_parent"android:layout_height = "@dimen/bottom_Main_Tabs"android:layout_gravity = "bottom"
        ><ImageButtonandroid:id = "@+id/bottomButton_home"android:layout_height = "match_parent"android:layout_width = "0dp"android:layout_weight = "1.0"android:background = "@drawable/ic_home_white"android:onClick = "openHome"
            /></LinearLayout></FrameLayout>

HomeFragment.java - if you create a fragment, all this will be autogenerated plus comments, which I cut out.!!! Interesting is only the inner interface at the bottom!!!:

import android.app.Activity;
import android.app.Fragment;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


publicclassHomeFragmentextendsFragment
{
    privatestatic final StringARG_PARAM1 = "param1";
    privatestatic final StringARG_PARAM2 = "param2";

    privateString mParam1;
    privateString mParam2;

    privateOnFragmentInteractionListener mListener;

    publicstaticHomeFragmentnewInstance(String param1, String param2)
    {
        HomeFragment fragment = newHomeFragment();
        Bundle args = newBundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    publicHomeFragment()
    {
        // Required empty public constructor
    }

    @OverridepublicvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
        {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        // Inflate the layout for this fragmentreturn inflater.inflate(R.layout.fragment_home, container, false);
    }

    // TODO: Rename method, update argument and hook method into UI eventpublicvoidonButtonPressed(Uri uri)
    {
        if (mListener != null)
        {
            mListener.onFragmentInteractionHome(uri);
        }
    }

    @OverridepublicvoidonAttach(Activity activity)
    {
        super.onAttach(activity);
        try
        {
            mListener = (OnFragmentInteractionListener) activity;
        }
        catch (ClassCastException e)
        {
            thrownewClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @OverridepublicvoidonDetach()
    {
        super.onDetach();
        mListener = null;
    }

    publicinterfaceOnFragmentInteractionListener
    {
        // TODO: Update argument type and namepublicvoidonFragmentInteractionHome(Uri uri);
    }

}

fragment_home.xml

<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/home_fragment"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/blue"tools:context="com.domain.app.HomeFragment"><!-- TODO: Update blank fragment layout --><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="50dp"android:textSize="100sp"android:text="Home" /></FrameLayout>

I tthink that is all. If not, just tell me :)

Post a Comment for "Android Studio - Is Possible To Add Tabs Pointing To Fragments From Designer?"