ClassCastException when using FragmentTabHost - java

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"

Related

How to set up a tablayout with viewpager in a bottom navigation view fragment?

I have a bottom Navigation view navigation view that contains 5 fragments, in one of those fragments I need to have a card slider (basically a view pager with a tab layout that slides between two cards back and forth) I have tried a lot of solutions but nothing worked, my most successful attempt is the app not crashing and the pager fragments are not showing.
Q: How to setup a view pager that has two tabs inside a fragment of a bottom Navigation View ?
This is my main fragment's XML and JAVA:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:calendarview="http://schemas.android.com/apk/res-auto"
xmlns:weekview="http://schemas.android.com/apk/res-auto"
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.HealthRecordFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="12dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.PagerTitleStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</androidx.viewpager.widget.ViewPager>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
.
private TabLayout tabLayout;
private ViewPager viewPager;
/*******/
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
/*******/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
TabOne tabFirst = new TabFirst();
TabTwo tabSecond = new TabSecond();
adapter.addFragment(tabFirst, "tab title 1");
adapter.addFragment(tabSecond, "tab title 2");
viewPager.setAdapter(adapter);
}
This is my view pager's first tab (XML & JAVA):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Tabs.TabOne">
<androidx.cardview.widget.CardView
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="this is tab 1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
</FrameLayout>
.
public class TabOne extends Fragment {
public TabOne() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
return view;
}
}
This is my view pager's second tab (XML & JAVA):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Tabs.TabTwo">
<androidx.cardview.widget.CardView
app:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="this is tab 2"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.cardview.widget.CardView>
</FrameLayout>
.
public class TabTwo extends Fragment {
public TabTwo() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_second, container, false);
return view;
}
}

I can't use transaction.replace

I am trying to replace the content of frame_layout with a view of fragment.
I used to do that by just using transaction.replace but it just put the view on the top of the main content.
Here is the code:
That's where I replace the frame_layout whose ID is contain:
VideosFragment videosFragment = new VideosFragment();
Bundle bundle = new Bundle();
bundle.putCharSequence("Character", view.getContentDescription());
videosFragment.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.contain , videosFragment).addToBackStack(null).commit();
here is the xml of main_Activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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"
tools:context="com.shobakyDC.muchannel.MainActivity">
<FrameLayout
android:id ="#+id/contain"
android:layout_width="match_parent"
android:background="#drawable/islamic_green"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:paddingStart="50dp"
android:paddingTop="10dp"
android:verticalSpacing="10dp"
android:paddingRight="0dp"
android:paddingLeft="50dp"
android:paddingEnd="0dp">
</GridView>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/Navigator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/header"
android:layout_gravity="start"
android:background="#android:color/white"
app:itemTextColor="#color/navcolor"
app:itemIconTint="#color/navcolor"
app:menu="#menu/navigationmenu">
</android.support.design.widget.NavigationView>
the video_fragment:
public class VideosFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.videofragment_layout , container , false);
Bundle args = getArguments();
TextView textView = view.findViewById(R.id.textView);
textView.setText(args.getString("Character"));
return view;
}
}
In xml of Main Activity, you have to make an empty FrameLayout with an ID like this:
<FrameLayout
android:id ="#+id/contain"
android:layout_width="match_parent"
android:background="#drawable/islamic_green"
android:layout_height="match_parent">
</FrameLayout>
and then make a separate xml layout for this fragment. Now you have to replace this fragment layout with frame layout in Main Activity.

Android: Fragment with tabs

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.

Adding views programmatically with a xml layout

I want to programmatically add a view to ScrollView that is apart of a Fragment layout.
I have attempeted the following code:
public class MessageDetail extends Fragment {
View layout;
TextView messageText;
Conversation conversation;
List<Message> messages;
LayoutInflater inflater;
ViewGroup container;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
this.inflater = inflater;
this.container = container;
layout = inflater.inflate(R.layout.messenger_detail, container,
false);
messageText = (TextView) layout.findViewById(R.id.tvMessage);
return layout;
}
public void loadConversation(Conversation conversation) {
messageText.setText(conversation.getSubject());
LinearLayout messagesView = (LinearLayout)layout.findViewById(R.id.llMessageDetail);
LinearLayout messageBody = new LinearLayout(getActivity());
View body = inflater.inflate(R.layout.messagebody, container);
messagesView.addView(body);
}
}
messagebody.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llMessageBody"
android:layout_width="fill_parent"
android:layout_margin="5dp"
android:layout_height="200dp"
android:background="#353535"
android:orientation="vertical" >
<TextView
android:id="#+id/tvMSBodyFrom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Research Station:"
android:textColor="#FFFFAA"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tomatohigh"
/>
</LinearLayout>
message_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d3d6da"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#FFAA00" >
<TextView
android:id="#+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:text="Select a Message:"
android:textColor="#353535"
android:textSize="30dp" />
</LinearLayout>
<ScrollView
android:id="#+id/svMessageDetail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#9900AA"
android:minHeight="300dp" >
<LinearLayout
android:id="#+id/llMessageDetail"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
</LinearLayout>
The final implementaion would see me loop through a list of messages and add the a view with the message_body layout and change each part of the layout to the correct values of each message
Any ideas how to achieve this?
You can do like this:
YourView varView = new YourView(this);
myButton.setLayoutParams(new YourLayout.LayoutParams());
yourParentLayout.addView(varView);
Example: Add a button in parent linear layout
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
myLayout.addView(myButton);

How to add custom controls to MapFragment in Google Maps Android API v2?

I have SupportMapFragment and I need to add custom controls into it for changing a map type. Calling getView(), I get NoSaveStateFramelayout and I don't think it is a good idea to add it directly into it or its children.
What is the best way how can I can add a button over my map for changing the map type?
I have decided to override onCreateView and encapsulate the map in the code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
View mapView = super.onCreateView(inflater, viewGroup, bundle);
RelativeLayout view = new RelativeLayout(getActivity());
view.addView(mapView, new RelativeLayout.LayoutParams(-1, -1));
// working with view
return view;
}
And it works as I needed.
Try to put your map inside of a Frame Layout with Relative layout together when the map and the relative layout match_parent for the width and the height. In the relative layout place all your desired extended controls. That way the will sit on top on the map.
some thing like that:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/title_gray_background" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:text="#string/tasks"
android:paddingRight="3dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/my_even_darker_gray" />
<Button
android:id="#+id/bLocateMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:background="#drawable/locate_me_button_selector"
android:clickable="true"
android:onClick="locateMeButtonOnClick"
android:text="#string/locate_me"
android:textColor="#color/my_white" />
</RelativeLayout>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
The view returned from onCreateView of MapFragment is a FrameLayout. So let use it, no need to add another layout (we will reduce view hierarchy).
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
FrameLayout mapView = (FrameLayout) super.onCreateView(inflater, viewGroup, bundle);
mapView.add(someView);
return view;
}
Use this code to add you layout above of the map
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mapView = super.onCreateView(inflater, container, savedInstanceState);
#SuppressLint("InflateParams")
FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_map, null);
view.addView(mapView, 0, new RelativeLayout.LayoutParams(-1, -1));
return view;
}
I did the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--suppress AndroidDomInspection -->
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:src="#android:drawable/ic_menu_mylocation"
/>
</RelativeLayout>

Categories