0

I had searched for this but could not get the solution.

I had created 3 tabs (using buttons) in an activity. Whenever I click a button the corresponding activity should display below the tabs. I know this can be achieved with Tab Host, but I don't want to use it. If I declare a Frame Layout below the tabs, how can I set the content of Frame Layout to my desired Activity? And I am developing in Android 2.2.

Please provide me a solution for this.

UPDATE : I want to have custom tabs something like this in TabHost. enter image description here

Dileep Perla
  • 1,865
  • 7
  • 33
  • 54

6 Answers6

1

You cannot have this functionality without TabHost or another Tab container.

If you want to have this kind of functionality, a solution could be to have 3 activities with the same top "tabs" and start the respective activity each time, adjusting properly the selected "tab" and handling the activity lifecycle.

Hope this helps!

Dimitris Makris
  • 5,183
  • 2
  • 34
  • 54
  • Actually I want to have custom tabs (see the image updated in the question) in the TabHost itself. Can I achieve this ?? – Dileep Perla Apr 13 '12 at 08:37
  • 1
    Use one of the open-source solutions with Fragments to achieve this functionality. Try downloading Android UI Patterns app, it has gathered many examples to see. – Dimitris Makris Apr 13 '12 at 08:39
  • I achieved the custom tabs in TabHost simply and easily from this [link](http://joshclemm.com/blog/?p=136) – Dileep Perla Apr 13 '12 at 11:49
1

The correct way to achieve this would be to replace your Activity (the one in the FrameLayout) with a Fragment. To use fragments on in Android 2.2 you have to use the support package.

Jave
  • 31,598
  • 14
  • 77
  • 90
1

I would consider moving to Fragments for this kind of behaviour. That way you can set up one (Fragment)Activity as host for the three fragments that make up the actual content. It's easy enough to replace one fragment by another.

Since you're developing for pre-Honeycomb, have a look at the support library, which provides a backwards compatible (to 1.6 from the top of my head) implementation. It'll also be worth your time to go over some of the related API demos for the support library, if you haven't used fragments before, and the Fragments Fundamentals.

MH.
  • 45,303
  • 10
  • 103
  • 116
1

Hello Use the ViewPager instead of tabhost. This the sample and example of viewpager Android ViewPager

Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41
  • My problem got solved and ViewPager also seems like a good alternative for me, but in the link you have given, we can swipe the tabs horizontally but not select the tabs by clicking on an individual tab. Do you have any idea achieving this functionality too ?? – Dileep Perla Apr 13 '12 at 11:53
  • yes you can use the onclicklistener to text or LinerLayout and set the _mViewPager.setCurrentItem(0); – Munish Kapoor Apr 13 '12 at 12:58
0

use Actionbarsherlock It gives exactly what you are looking for, you don't need to create your own tab... this library has so many nice features

Nixit Patel
  • 4,435
  • 5
  • 30
  • 57
0

Your activity should extend FragmentActivity and in it, you should create a class that extends FragmentPagerAdapter because you should use ViewPager.and for each tab you should create its respective Fragment

Amrmsmb
  • 1
  • 27
  • 104
  • 226