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
public class MainActivity extends Activity implements HomeFragment.OnFragmentInteractionListener
{
FragmentManager fragmentManager = getFragmentManager();
HomeFragment homeFragment;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeFragment = new HomeFragment();
fragmentManager.beginTransaction().add(R.id.mainFrame, homeFragment).commit();
}
public void openHome(View view)
{
homeFragment = new HomeFragment();
fragmentManager.beginTransaction().replace(R.id.mainFrame, homeFragment).commit();
}
@Override
public void onFragmentInteractionHome(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
<FrameLayout xmlns: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" >
<FrameLayout
android:id = "@+id/mainFrame"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:layout_marginBottom = "@dimen/bottom_Main_Tabs">
</FrameLayout>
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "@dimen/bottom_Main_Tabs"
android:layout_gravity = "bottom"
>
<ImageButton
android: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;
public class HomeFragment extends Fragment
{
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public static HomeFragment newInstance(String param1, String param2)
{
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public HomeFragment()
{
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null)
{
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri)
{
if (mListener != null)
{
mListener.onFragmentInteractionHome(uri);
}
}
@Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
mListener = (OnFragmentInteractionListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach()
{
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener
{
// TODO: Update argument type and name
public void onFragmentInteractionHome(Uri uri);
}
}
fragment_home.xml
<FrameLayout xmlns: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 -->
<TextView
android: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?"