How can I put tabs inside of my fragment? I have been struggling with this and would greatly appreciate some help.
Currently I am getting this error: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
However, even when I add a call to this I still get a fatal crash with an error:
Must call setup() that takes a Context and FragmentManager
This seems to be contradictory....
Below is the relevant Java and XML:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_profile, container, false);
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity().getApplicationContext(), getChildFragmentManager(), R.layout.testtabhost1);
//LocalActivityManager mLocalActivityManager = new LocalActivityManager(getActivity(), false);
//mLocalActivityManager.dispatchCreate(savedInstanceState);
//mTabHost.setup(mLocalActivityManager);
//LocalActivityManager mLocalActivityManager = new LocalActivityManager(getActivity(), false);
//mTabHost.setup(mLocalActivityManager);
Intent q_intent = new Intent (getActivity(), QuestionTabActivity.class);
Intent a_intent = new Intent (getActivity(), AnswerTabActivity.class);
View questionIndicator = createTabView(mTabHost.getContext(), "Questions");
View answerIndicator = createTabView(mTabHost.getContext(), "Answers");
mTabHost.addTab(mTabHost.newTabSpec("questions").setIndicator(questionIndicator).setContent(q_intent));
mTabHost.addTab(mTabHost.newTabSpec("answers").setIndicator(answerIndicator).setContent(a_intent));
mTabHost.setCurrentTab(0);
// Inflate the layout for this fragment
return v;
}
And the XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.juryroom.qollege_android_v1.ProfileFragment">
.........
...ETC...
.........
<android.support.v4.app.FragmentTabHost
android:id="#+id/testtabhost1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/edit_profile_picture_fragment_circView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:showDividers="none"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
.........
...ETC...
.........
</FrameLayout>
Here is the design look I'm making:
mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec(Constant.BuildingPicture.FRONT_PHOTOS).setIndicator(Constant.BuildingPicture.FRONT_PHOTOS),SiteDataFrontPhotoTab.class, null);
mTabHost.addTab(mTabHost.newTabSpec(Constant.BuildingPicture.SATELLITE_PHOTOS).setIndicator(Constant.BuildingPicture.SATELLITE_PHOTOS),SiteDataSatellitePhotoTab.class, null);
In order to display tabs we don’t have to use any other UI element like TabHost. Action bar has the inbuilt capability of adding tabs. All we have to do is enable it using setNavigationMode(ActionBar.NAVIGATION_MODE_TABS).
Here is a good description of what you are looking for.
Related
I'm replacing my main activity's layout with fragments in order to keep the same navigation menu. However I've done data binding to fragment_home.xml and now the toolbar no longer shows up, even though I still switch to the intended fragment. After some debugging, I believe it has something to do with the setContentView in HomeFragment.java.
Any ideas?
Here's my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#000"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/side_navbar"
app:itemTextColor="#color/black"
app:itemIconTint="#color/black"/>
</androidx.drawerlayout.widget.DrawerLayout>
Replacing frame layout with the fragment
getSupportFragmentManager().beginTransaction().replace(R.id.frag_container, new HomeFragment()).commit(); ```
And the fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/viewCities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:padding="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</layout>
And finally the HomeFragment.java
public class HomeFragment extends Fragment {
private CityViewModel cityViewModel;
private RetrofitAdapter retrofitAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_home, container, false);
FragmentHomeBinding activityMainBinding =
DataBindingUtil.setContentView(getActivity(), R.layout.fragment_home);
RecyclerView recyclerView = activityMainBinding.viewCities;
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setHasFixedSize(true);
cityViewModel = new ViewModelProvider(requireActivity()).get(CityViewModel.class);
retrofitAdapter = new RetrofitAdapter();
recyclerView.setAdapter(retrofitAdapter);
return view;
}
}
You do not have to set the content view again from the fragment. From the HomeFragnent for using the data-binding just do something as follows.
FragmentHomeBinding binding; // declare the binding variable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil
.inflate(inflater, R.layout.fragment_home, container, false);
/* FragmentHomeBinding activityMainBinding =
DataBindingUtil.setContentView(getActivity(), R.layout.fragment_home); */
RecyclerView recyclerView = binding.viewCities; // use the binding here
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.setHasFixedSize(true);
cityViewModel = new ViewModelProvider(requireActivity()).get(CityViewModel.class);
retrofitAdapter = new RetrofitAdapter();
recyclerView.setAdapter(retrofitAdapter);
return binding.getRoot();
}
I have looked for several times, but I think no one is facing the same issue that I'm facing right now. I have the following fragment:
FRAGMENT XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="8dp"
android:paddingEnd="16dp"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
RecyclerView XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/icon"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
/>
</LinearLayout>
FRAGMENT JAVA CODE SNIPPET
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View viewRoot = inflater.inflate(R.layout.fragment_xml, container, false);
recyclerView = (RecyclerView) viewRoot.findViewById(R.id.recyclerview_id);
recyclerView.setOnTouchListener(new OnSwipeTouchListener(getActivity()) {
#Override
public void onSwipeRight() {
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.enter_from_left, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_left);
ft.replace(R.id.frameLayout, new Fragment2()).commit();
}
});
So, what I'm doing on the code above is adding a touch listener, so when the user swipe from right to left, the fragment goes to the other fragment (Fragment2)
This works perfectly fine! However... I need to add onClickListener inside my recyclerAdapter, so I can catch the click listener of each item. And here comes the problem... When adding a click listener, the swipe listener does not work. It seems that the listener of the click "overrides" my listener of the swipe. The question is. How to add both on the recyclerview, and make them work together?
Currently i am in Tabbed Activity tab and i have set friends_fragment in layout! my friends_fragment contains a recycler view. Another layout named as activity_friends which is set as viewHolder for that recycler view. i have progress bar in activity_friends and want to access it from that tabbed activity. how i can do that
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friends, container, false);
LayoutInflater factory = LayoutInflater.from(getContext());
DialogView = factory.inflate(R.layout.activity_friends, null);
bar=(ProgressBar)DialogView.findViewById(R.id.pBar);
bar.setVisibility(DialogView.INVISIBLE); // or VISIBLE
return view;
}
Friends_Fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.umairahmed.tracknotesapp.Friends">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/friendRecyclerView"
>
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Activity_friends
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="14dp"
android:layout_marginEnd="14dp"
android:id="#+id/pBar"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/userImg"
android:layout_alignEnd="#+id/userImg" />
update fragment_activity layout to
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.umairahmed.tracknotesapp.Friends">
<FrameLayout
android:id="#+id/MainFrame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/friendRecyclerView">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
</RelativeLayout>
and Fragment_activity java class to
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_friends, container, false);
FrameLayout MainFrame = (MainFrame)view.findViewById(R.id.MainFrame);
LayoutInflater factory = LayoutInflater.from(getContext());
DialogView = factory.inflate(R.layout.activity_friends, null);
MainFrame.addView(DialogView);
bar=(ProgressBar)DialogView.findViewById(R.id.pBar);
bar.setVisibility(DialogView.INVISIBLE); // or VISIBLE
return view;
}
I have a Fragment
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="#+id/switchLayout">
</LinearLayout>
</FrameLayout>
and want to add switches to the LinearLayout as soon as the Fragment is loaded
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment, container, false);
LinearLayout switchLayout = (LinearLayout) rootview.findViewById(R.id.switchLayout);
Switch new_switch = new Switch(getActivity());
new_switch.setText("test");
new_switch.setChecked(true);
switchLayout.addView(new_switch);
return rootview;
}
but when the Fragment gets loaded nothing happens, the LinearLayout is displayed but the switch doesn't appear and I don't get any error messages.
I already tried adding
new_switch.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
);
but it makes no difference
note: this is my first question here, hopefully it's not too stupid ^^
I've tried to perform the same what you are doing and it works.
Main activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
<FrameLayout
android:id="#+id/containerFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Then in MainActivity.java:
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.containerFragment,
new MyFragment()).commit();
MyFragment and fragment layout is the same as you described.
Result:
I am using FragmentTabHost with android support library,
My layout file sub_main.xml:
<android.support.v4.app.FragmentTabHost
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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/tabFrameLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
The above layout is used in my fragment:
public class MyFragment extends Fragment{
...
private FragmentTabHost mTabHost;
private View content;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View content = inflater.inflate(R.layout.sub_main, null);
// the following code throw ClassCastException, why?
mTabHost = (FragmentTabHost) content.findViewById(android.R.id.tabhost);
return content;
}
...
}
But, I got the ClassCastException exception in the above code where mTabHost is initialized in onCreateView() :
07-03 16:35:09.704: E/AndroidRuntime(25350): java.lang.ClassCastException: android.widget.TabHost cannot be cast to android.support.v4.app.FragmentTabHost
It complains that TabHost can not be cast to FragmentTabHost, why & how to get rid of it. Thanks.
mTabHost = (FragmentTabHost) content.findViewById(android.R.id.tabhost);
should be
mTabHost = (FragmentTabHost) content.findViewById(R.id.tabhost);
and in the xml layout you should specify
android:id="#android:id/tabhost"
as
android:id="#+id/tabhost"