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"
Related
I'm using CoordinateLayout in fragment's layout file. There I need to use ImageButton between two layouts. I got sample code from [this][1] answer.
but when i put my fragment in viewpager it dose not work correctly here is my code.
<img src="https://i.stack.imgur.com/wZasU.png"/>
public class Profile extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View Rootview =inflater.inflate(R.layout.tab_profile_fragment, container, false);
CircleImageView fab = (CircleImageView) Rootview.findViewById(R.id.fab);
return Rootview;
}
}
Xml code :
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/aasda"
android:weightSum="1"
android:orientation="vertical">
<LinearLayout
android:id="#+id/viewA"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".3"
android:orientation="horizontal">
</LinearLayout>
<RelativeLayout
android:id="#+id/viewB"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".7"
android:background="#color/white"
android:gravity="top"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/tab_profile_cardviewname"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
</android.support.v7.widget.CardView>
</RelativeLayout>
</LinearLayout>
<com.citizenassistant.View.Componnent.CircleImageView
android:id="#+id/fab"
android:layout_width="90dp"
android:layout_height="90dp"
android:clickable="true"
android:src="#color/blue"
android:backgroundTint="#color/white"
app:layout_anchor="#id/tab_profile_cardviewname"
app:layout_anchorGravity="top|center_horizontal"/>
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.
With the layout below the buttons are being shown on top of the page. How can I make them appear below, after the container, down on the page?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/map"
android:onClick="ClickClients"
android:text= "#string/Clients"
>
</Button>
</LinearLayout>
</LinearLayout>
The MainActivity looks as follows and the HomePage.xml is also below
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetButtonClicked(5);
setContentView(R.layout.activity_main);
}
public void GetButtonClicked(int position)
{
// update the main content by replacing fragments
Fragment fragment = null;
switch (position){
case 0:
fragment= new FirstPageFragment();
break;
default:
fragment = new FirstPageFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void ClickHomePage(View view){
GetButtonClicked(0);
}
public void ClickClients(View view){
GetButtonClicked();
}
}
<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" tools:context="com.test.FirstPageFragment" android:background="#drawable/theme">
</FrameLayout>
This is the FirstPage
public class FirstPageFragment extends Fragment {
public FirstPageFragment() {
// 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_first_page, container, false);
}
}
Use ralative layout insted of linear.
Make a different layout for bottom buttons, and then at the last of your container layout use this:
<include android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/bottom_buttons"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="#string/Home" />
<Button
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="#string/Clients" >
</Button>
</LinearLayout>
Try this
Use RelativeLayout with android:layout_alignParentBottom="true" step by step guide is available here.
Your skeleton for layout should look like this
<RelativeLayout
<LinearLayout android:layout_alignParentBottom="true">
<Button
<Button
</LinearLayout>
</RelativeLayout>
Final Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="Home" />
<Button
android:layout_marginLeft="30dp"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="Clients" >
</Button>
</LinearLayout>
The issue was that I did not include the container in another layout...
<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.test.MainPageActivityActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/container">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentTop="#+id/container">
and insert buttons layout here....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
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>
I have a fragment which has a horizontal scrollview. I want to add another fragment to this scrollview. How can I do that?
Here is my first fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="220dp"
android:background="#drawable/arrow_bg">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/horizontalScrollView"
android:layout_marginTop="15dp" >
<LinearLayout
android:id="#+id/offerLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</HorizontalScrollView>
</LinearLayout>
I am trying to add the below fragment to the scrollview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="174dp"
android:layout_height="214dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="HEALTH"
android:id="#+id/offerTitle"
android:capitalize="characters" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/offer_image_text_bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="135dp"
android:id="#+id/offerIcon"
android:src="#drawable/health_img"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/favLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ratingBar"
android:numStars="5"
android:rating="4"
android:isIndicator="true"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
style="#style/ratingBarStyle"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:id="#+id/favIcon"
android:src="#drawable/like_icon"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/favLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/txt_OfferTitle2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/txt_OfferDesc" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
This is the code I am using for the same
private void loadSelectedOffers() {
for(int i=0;i<3;i++)
{
OfferItemFragment offerItem = new OfferItemFragment();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
offerItem.getView().setLayoutParams(params);
offerLayout.addView(offerItem.getView());
}
}
But I am getting a null pointer exception at line
offerItem.getView()
How can I do this properly?
Add a FrameLayout to your first fragment and then add the fragment to that framelayout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="220dp"
android:background="#468494">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/horizontalScrollView"
android:background="#BB099D"
android:layout_marginTop="15dp" >
<LinearLayout
android:id="#+id/child"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Then in your code add the fragment to the view using FragmentManager and FragmentTransaction
public class MainActivity extends Activity {
Fragment f;
EditText send;
LinearLayout parent;
LinearLayout child;
HorizontalScrollView scroll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup vg = (LinearLayout) inflater.inflate(R.layout.activity_main, null );
child = (LinearLayout) vg.findViewById(R.id.child);
loadSelectedOffers();
setContentView(vg);
}
private void loadSelectedOffers() {
for(int i=0;i<3;i++)
{
FrameLayout frame = new FrameLayout(this);
child.addView(frame);
frame.setId(i);
frame.setBackgroundColor(getResources().getColor(R.color.black));
frame.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
SenderFragment offerItem = new SenderFragment();
transaction.add(i, offerItem, "offer_item_" );
transaction.commit();
}
}
I believe something like this should work now.
You are getting view null because its onCreateView() has not been executed yet, that in turn is because you have merely instantiated the fragment and not attached it to your activity.
Try adding FrameLayouts in your HSV and replacing them by the fragments.