I'm trying to have a FragmentTabHost and set custom views for tabs. The FragmentTabHost is inflated inside a Fragment. This is the Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_friends, container, false);
mTabHost = (FragmentTabHost) root.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent);
TabHost.TabSpec friends = mTabHost.newTabSpec(FriendsTabFragment.TAG);
View tab = inflater.inflate(R.layout.friends_fragment_custom_tab,null);
ImageView view = (ImageView) tab.findViewById(R.id.tab_icon);
view.setImageResource(R.drawable.categoria_amici);
friends.setIndicator(view);
mTabHost.addTab(friends, FriendsTabFragment.class, null);
return root;
}
And here's the layout:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="-4dp"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
This is the FriendsTabFragment inflating code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_friends_tab, container,false);
return root;
}
This is the custom view tab (R.layout.friends_fragment_custom_tab):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tab_icon"/>
</LinearLayout>
If I run the project with this configuration it crashes on mTabHost.addTab() method saying that the specified child already has a parent. If I remove the LinearLayout around the ImageView inside the custom view tab, it works! But I need some customizations so I need that LinearLayout there. What am I doing wrong?
Change your layout to this one:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="-4dp"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
and also change:
mTabHost.setup(getActivity(), getChildFragmentManager(),android.R.id.tabcontent);
friends.setIndicator(view);
to
mTabHost.setup(getActivity(), getChildFragmentManager(),R.id.realtabcontent);
friends.setIndicator(tab);
Related
I'm unable to run two web views at once I've tried playing with it but need some help... its not as simple as saying View2 like in Swift.
layout
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:background="#ffb067"
tools:context=".FeedFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:text="Feed Fragment" />
<WebView
android:id="#+id/livefeed"
android:layout_width="match_parent"
android:layout_height="310dp"
android:layout_marginTop="200dp" />
<WebView
android:id="#+id/livestream"
android:layout_width="match_parent"
android:layout_height="200dp" >
</WebView>
</FrameLayout>
fragment
public class FeedFragment extends Fragment {
private WebView livestream;
private WebView livefeed;
public FeedFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_feed, container, false);
}
#Override
public void onViewCreated(View view , Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
livestream = (WebView) view.findViewById(R.id.livestream);
livestream.setWebViewClient(new WebViewClient());
livestream.loadUrl("website1");
livefeed = (WebView) view.findViewById(R.id.livefeed);
livefeed.setWebViewClient(new WebViewClient());
livefeed.loadUrl("website2");
}
}
One view is a video stream other is the notes for the stream.
You need to make your layout scrollable and chnage the textview's height to wrap_content
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#ffb067"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold"
android:text="Feed Fragment" />
<WebView
android:id="#+id/livefeed"
android:layout_width="match_parent"
android:layout_height="310dp"
android:layout_marginTop="20dp" />
<WebView
android:id="#+id/livestream"
android:layout_width="match_parent"
android:layout_height="200dp" >
</WebView>
</LinearLayout>
</ScrollView>
I strongly recommend that you read Google's a layout .
findViewById is returning null when searching for an ImageView inside a fragment, but not for a TextView.
Here is my code:
fragment.xml:
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.herecomesthefragmentname">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/card_1"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/txt_1"
android:textAppearance="#android:style/TextAppearance.Medium" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/2_card"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#android:color/white">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mr_layout">
<ImageView
android:id="#+id/2_imag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#drawable/img_1"
android:scaleType="fitStart"
android:adjustViewBounds="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txt_2"
android:textAppearance="#style/TextAppearance.AppCompat.Large.Inverse"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<!-- here goes another cardview -->
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
fragment.java (not all):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Get Views
txt_container = (TextView) view.findViewById(R.id.txt_2);
img_view = (ImageView) view.findViewById(R.id.2_imag);
}
The strange thing is that the txt_2 view is found but not the 2_imag one.
I have tried renaming it, but it does nothing. Also, I have notice that i can't find the RelativeLayout and CardView containig both either.
UPDATE: It seems that I can't find even the main CoordinatorLayout
Solved, a fragment-v21.xml file was being loaded instead of fragment.xml.
The v21 one had different code.
When I open fragment with scrollview and tabhost inside the scrollview, it automatically scrolls down to the start of the tabhost hiding the top part of the scrollview. I've tried changing various things, nothing worked so far. Will keep trying, just throwing this here in hopes someone can help me. Here is the relevant parts of the code.
FragmentProfile.java
public class FragmentProfile extends Fragment {
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile, container, false);
mTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("", ContextCompat.getDrawable(getActivity(), R.drawable.ic_c_text)),
FragmentProfileTab1.class, null);
return view;
}
}
fragment_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollview"
android:fillViewport="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="70dp"
android:background="#fff"
android:weightSum="4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:padding="2dp"
android:text="rolands"
android:textColor="#000"
android:textSize="18dp" />
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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:layout_weight="0"
android:background="#6e0d16"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"
android:background="#fff" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</ScrollView>
FragmentProfileTab1.java
public class FragmentProfileTab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_profile_tab1, container, false);
return view;
}
}
fragment_profile_tab1.xml
<?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"
android:background="#fff"
android:orientation="vertical"
tools:context=".DeviceFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="3000dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#333"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
The view atuomatically scrolls down to the start of the tabwidget. I'll keep editing as I try new things.
I tried scrollView.scrollTo(0, 0) and scrollView.fullScroll(ScrollView.FOCUS_UP) without success.
I need a fixed area (around 20%) at top and then 3 tabs below it. The xml is
mylist.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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.05"
android:visibility="invisible" />
<TextView
android:id="#+id/list_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textColor="#color/loclistitem_text"
android:textStyle="bold" />
<TextView
android:id="#+id/list_spec"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:visibility="invisible" />
<TextView
android:id="#+id/list_user"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
<FrameLayout
android:id="#+id/list_realtabcontent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.95" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCF8FB" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
MyListFragment.java
public class MyListFragment extends Fragment {
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.mylist, container,
false);
TextView name = (TextView) rootView.findViewById(R.id.list_name);
TextView spec = (TextView) rootView.findViewById(R.id.loc_spec);
TextView users = (TextView) rootView.findViewById(R.id.list_user);
name.setText(AppSession.getInstance().getData().getName());
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.id.list_realtabcontent);
Bundle arg1 = new Bundle();
arg1.putInt("Tab1", 1);
mTabHost.addTab(
mTabHost.newTabSpec("Tab1").setIndicator("Tab1",
getResources().getDrawable(R.drawable.tab_left)),
Tab1.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Tab2", 2);
mTabHost.addTab(
mTabHost.newTabSpec("Tab2").setIndicator("Tab2",
getResources().getDrawable(R.drawable.tab_middle)),
Tab2.class, arg2);
Bundle arg3 = new Bundle();
arg3.putInt("Tab3", 3);
mTabHost.addTab(
mTabHost.newTabSpec("Tab3").setIndicator("Tab3",
getResources().getDrawable(R.drawable.tab_rigth)),
Tab3Fragment.class, arg2);
return rootView;
}
#Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
I get "No Tab known for Tag null" at android.support.v4.app.FragmentTabHost.doTabChanged.
The error is resolved if I change return rootView to return mTabHost. But then the fixed area at top isn't showing. Its only displaying 3 tabs at the top of the screen.
I tried using Activity to set this particular screen and it worked fine(both fixed area at top and tabs are displayed correctly) but I need it using fragments as I am using Navigation Drawer so this has to be fragment. Please advice.
Modified xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCF8FB" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical" >
<TextView
android:id="#+id/loc_playlist_locname"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
<FrameLayout
android:id="#+id/playlist_realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
I made a few changes to your code:
- In implementation, use the FragmentTabHost specified in layout
- Use Framelayout specified within FragmentTabHost Element in layout in TabHost setup method. It seems to work, check out code below:
XML file:
<?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:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.05"
android:visibility="invisible" />
<TextView
android:id="#+id/list_name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"
android:textStyle="bold" />
<TextView
android:id="#+id/list_spec"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1"
android:visibility="invisible" />
<TextView
android:id="#+id/list_user"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.05"
android:visibility="invisible" />
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#CCF8FB" >
<FrameLayout
android:id="#+id/list_realtabcontent"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.95" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Fragment Class:
public class MyListFragment extends Fragment {
private FragmentTabHost mTabHost;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.activity_main, container,
false);
TextView name = (TextView) rootView.findViewById(R.id.list_name);
TextView spec = (TextView) rootView.findViewById(R.id.list_spec);
TextView users = (TextView) rootView.findViewById(R.id.list_user);
mTabHost = (FragmentTabHost)rootView.findViewById(R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.id.list_realtabcontent);
Bundle arg1 = new Bundle();
arg1.putInt("Tab1", 1);
mTabHost.addTab(
mTabHost.newTabSpec("Tab1").setIndicator("Tab1",
getResources().getDrawable(R.drawable.tab_chat_unselected)),
TabFragment.class, arg1);
Bundle arg2 = new Bundle();
arg2.putInt("Tab2", 2);
mTabHost.addTab(
mTabHost.newTabSpec("Tab2").setIndicator("Tab2",
getResources().getDrawable(R.drawable.tab_chat_unselected)),
TabFragment.class, arg2);
Bundle arg3 = new Bundle();
arg3.putInt("Tab3", 3);
mTabHost.addTab(
mTabHost.newTabSpec("Tab3").setIndicator("Tab3",
getResources().getDrawable(R.drawable.tab_chat_unselected)),
TabFragment.class, arg3);
return rootView;
}
You can use this library. Having a Tabs with Navigation Drawers is the not provided directly. If u want to have tabs in fragment just like google-play music. I advice you to use this library.
Just create the navigation drawer in the same way we create and jsut properly intergrate it. Thats set very simple.
The Solution provided by #Haresh may work. But if it doesn't apply the link I have provided will definetly work. I have used that library in my project refer link1 and link2
Hope it helps
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.20">
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.80"
android:background="#CCF8FB" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical" >
<TextView
android:id="#+id/loc_playlist_locname"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2" />
<FrameLayout
android:id="#+id/playlist_realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.1" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dip"
android:layout_height="0dip"
android:layout_weight="0" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Helo every one, i try to open fragment in FrameLayout, but the screen is white. My code is:
public class ProfileActivity extends FragmentActivity {
Database db;
TextView tvName, tvCity;
ImageView imageView;
private FragmentTabHost mTabHost;
FrameLayout frameLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
frameLayout = (FrameLayout) findViewById(R.id.frame_layout);
initialize();
}
private void initialize() {
tvName = (TextView) findViewById(R.id.tv_name);
tvCity = (TextView) findViewById(R.id.tv_city);
db = Database.getInstance(this);
imageView = (ImageView) findViewById(R.id.iv_profile);
Fragment fragment = new StatisticFragment();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.frame_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
activity_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#ffffff">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp" android:layout_weight="1">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="1">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/iv_profile" android:src="#drawable/background_profile_first_part"
android:scaleType="fitXY"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="fill_parent" android:layout_weight="1.4"
android:background="#drawable/background_profile_second_part">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tv_name" android:layout_gravity="right|center_vertical"
android:textColor="#ffffff"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/tv_city" android:layout_gravity="center"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:visibility="invisible"
android:layout_width="0dp"
android:layout_height="0dp"
/>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
StaticFragment.class
public class StatisticFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_statistic, null);
return v;
}
}
fragment_statistic.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Statistic"
android:id="#+id/textView" android:layout_gravity="left|center_vertical" android:textColor="#fff555"/>
</LinearLayout>
Apply a background to the linear layout in the statistic fragment xml
I think you have set visibility to invisible inside frame layout so please edit your layout file
from
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:visibility="invisible"
android:layout_width="0dp"
android:layout_height="0dp"
/>
</android.support.v4.app.FragmentTabHost>
remove this line and try again.
android:visibility="invisible"