Android- Create tabs inside fragment without v4 support library - android

I am trying to create a 'tabs' w/o using support library for my application, but I didn't find even a simple working example/tutorial anywhere.
Since my entire application is using android.app.fragment, so I cannot change all of the existing fragments to android.support.v4.app.Fragment.
Creating a tab within fragment using support library is a piece of cake, but I am wondering whether we can create one w/o android.support.v4.app.Fragment.

This can be achieved, it's easily, follow my step:
1.Copy the offical source code of FragmentTabHost to your project, and change following 3 class refrence:
android.support.v4.app.Fragment to android.app.Fragment
android.support.v4.app.FragmentManager to android.app.FragmentManager
android.support.v4.app.FragmentTransaction to android.app.FragmentTransaction
2.Use the modify version of your FragmentTabHost in project, and forget the android.support.v4 library. My edited version: FragmentTabHost
Sample code: (Show the first tab, after 5 sec delay, you will see the second tab, I run on my Android Studio successfully!)
MainActivity.java
public class MainActivity extends Activity {
FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
mTabHost.setup(this, getFragmentManager(), R.id.container);
// Add each tab
mTabHost.addTab(mTabHost.newTabSpec("first").setIndicator("first"), BlankFragment1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("second").setIndicator("second"), BlankFragment2.class, null);
mTabHost.postDelayed(new Runnable() {
#Override
public void run() {
mTabHost.setCurrentTabByTag("second");
}
}, 5000);
}
}
activity_main.xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.example.bill.myapplication.FragmentTabHost
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/tabhost">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_height="0dp"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</com.example.bill.myapplication.FragmentTabHost>
</RelativeLayout>

Put the fragments inside tab content in your layout XML and add them to the TabHost in onCreate.

Related

Start a ListFragment from Activity

I am developing an app to present a map (using a fragment) and a list (using a ListFragment). So, I have an activity that starts the application as well as the mapfragment or the list fragment, according to the the user's preferences. I can easily replace the current activity for the mapfragment with FragmentManager and FragmentTransaction. The problem that I have is how I can call or start the ListFragment from the activity.
The listfragment uses a ListViewAdapter class to inflate a customized layout (repetedly with different information) so if i want to use transaction, it will be necessary to use a listview container in the layout. The problem is that I am using a layout for the items and not a container.
Any ideas to solve this problem or any other way to deal with starting a presenting the listfragment in the activity?
If you are trying to display two different fragments at the same time, have a look at this documentation, but the basic idea is to include multiple FrameLayouts in your activity layout. Also don't forget about View.setVisibility()
ListFragments are started the same as any other fragment.
For Example:
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, new MyListFragment()
.commit();
A more verbose example:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.plusButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, new MyListFragment())
.commit();
}
});
}
activity_main.xml
<RelativeLayout 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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/content_frame"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Press For List"
android:id="#+id/plusButton"
android:layout_gravity="center"/>
</RelativeLayout>

Add ViewPager to Activity which extends superclass with a Navigation Drawer

I have several activities which extend my class DrawerActivity, so they will have a navigation drawer. Only one of these activities requires a ViewPager.
Question: How can I add a ViewPager to only one of the activities, ActivityA?
I am somewhat familiar with creating a ViewPager, but in this situation I'm unsure of where to put the ViewPager code, and what layouts to use. I've seen a few examples including the official tutorial on Google's developer site, but I'm not sure how to best implement it into my application.
I've provided some relevant code below, but please let me know if you need to see more. Any advice is appreciated, thanks!
In DrawerActivity's onCreate:
setContentView(R.layout.activity_fragment);
if (fragment == null)
{
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragmentContainer, fragment)
.commit();
}
Layout activity_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<ListView android:id="#+id/leftDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
In ActivityA:
public class ActivityA extends DrawerActivity
{
//this is all I do in this activity
#Override
protected Fragment createFragment()
{
UUID id = (UUID) getIntent().getSerializableExtra(FragmentA.EXTRA_ID);
return FragmentA.newInstance(id);
}
}
In FragmentA's onCreateView:
View v = inflater.inflate(R.layout.fragment_a, parent, false);
Layout fragment_a.xml is a simple LinearLayout with a few Button and TableLayout children.
fragment_a.xml:
<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" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
and create an adapter for example FragmentStatePagerAdapter and fill it with what you want to show in viewpager. notice that because you are using viewpager inside a fragment you should pass ChildFragmentManaget instead of activity FragmentManager.

ActionBarActivity with TabHost

I am developing an app, with minimum sdk level 7 and that's why I use support library and ActionBarActivity to have a proper ActionBar, but also the layout of my page contains four tabs on the bottom of the activity, and as i already extend ActionBarActivity, I cannot extend TabActivity.
The problem is when I write
public class HomePageActivity extends ActionBarActivity {
private TabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_homepage);
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
TabHost.TabSpec tab1 = mTabHost.newTabSpec("TAB1");
tab1.setIndicator("TAB 1");
Intent popularContentIntent = new Intent().setClass(this, GuideActivity.class);
tab1.setContent(popularContentIntent);
mTabHost.addTab(tab1);
I get the error "Did you forget to call 'public void setup(LocalActivityManager activityGroup)?'" at the line when I trying to add tab to the tabhost.
If I try to set the id of the view in the tab.setContent(..) like tab.setContent(R.id.tab1) and have a view in the xml with the id tab1, I get the error that the view with such a number cannot be found.
My layout:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#android:id/tabs"
android:layout_alignParentTop="true" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</Button>
</TabWidget>
</RelativeLayout>
Is it possible somehow to have layout with tabs and actionbar at the same time? What I do wrong? What could be other alternatives?
Thanks a lot!
TabActivity was deprecated in API level 13. The updated documentation recommends using android.support.v4.app.FragmentTabHost instead.
An even better method would be to use the ViewPager sample provided here.

Adding Images to Tab in Android

I have developed a simple Android application where in i have implemented tabs using Fragments... but i am unable to display the images for the tabs.. i have attached the source code below.. please let me know what is the error...
My Activity class TabDemoFragmentActivity.java
public class TabDemoFragmentActivity extends FragmentActivity {
/**
* Instance variable of type {#link FragmentTabHost}
*/
private FragmentTabHost fragmentTabHost;
/**
* This is a call back method, which gets invoked
* when the instance of this class is created.
*
* <p>
* This method is used to set the tab host and
* add the tab host fragments to the screen,
* which acts as a UI.
* </P>
*/
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_fragment_main);
fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("DropCall Details").setIndicator("DropCall Details",
getResources().getDrawable(R.drawable.drop_call_image)).setContent(intent), QoSDropCallDetailsFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Network Details").setIndicator("Network Details",
getResources().getDrawable(R.drawable.network_details)), QoSNetworkDetailsFragment.class, null);
}
}
My XMl file
<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="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Image is not getting displayed here.. please let me know how to display images in the tabs.. Thanks
I realize it is an old question but hoping someone might land on this page. First, let me point out that FragmentTabHost is deprecated approach to adding tabs in your app. Google wants you to move to ActionTabBar to add tabs.
I had done a very similar implementation to yours and I don't see much difference. However, 2 things I can point out from your code is first, you need to make sure you copy your image to all 3 drawable folders. Second, remove the extra Framelayout(tabcontent) you have in your xml because I only had 1 Framelayout and I don't see you using the other one in your Activity class.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+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="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Here is a snippet of my Activity Class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
mTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Tab 1",
getResources().getDrawable(R.drawable.user_card)),
TabActivity.ListFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
getResources().getDrawable(R.drawable.menu)),
TabActivity.ListFragments.class, null);
}

How to display one activity inside another in Android?

I have one activity and want to display another inside it. Here's my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
How can I display an Activity in inner LinearLayout on button click?
Use one layout for several activities
Layout instance is inflated for each activity using a setContentView method, usually in onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_common_layout);
}
So there's no problem to use the same XML layout for different activities.
Display one activity inside another
You can use a Fragment API to complete the task. See full details in developer's guide.
Declare a layout like that and Android will create fragments for you:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.MyFragment"
android:id="#+id/my_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
Then create a MyFragment class and load it when appropriate.
Create fragments yourself. Do not define the Fragment in XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/my_parent_layout">
</LinearLayout>
After your parent Activity is created you can add a new Fragment in this way:
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment fragment = new MyFragment();
fragmentTransaction.add(R.id.my_parent_layout, fragment);
fragmentTransaction.commit();
Here MyFragment is defined like this:
public class MyFragment extends Fragment {
...
}
If you're targeting below Android 3.0, consider using support package for that.

Categories

Resources