0

I have developed an Android application which has 4 TabHost's which are in fragments.

I Know how to customize the ActionBar in MainActivity.

But problem is how can I customize my ActionBar according to the 4 different TabHost's in different Fragments?

Here is my tabHost code -

public class MainActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


    /** Creating ANDROID Tab */
    Tab tab = actionBar.newTab()
            //.setText("Android")

            .setTabListener(new CustomTabListener<PlayFragment01>(this, "play", 
    PlayFragment01.class))
            .setIcon(R.drawable.playtabhosticon);

    actionBar.addTab(tab);


    /** Creating APPLE Tab */
    Tab tab1 = actionBar.newTab()
            //.setText("Apple")
            .setTabListener(new CustomTabListener<VenueFragment01>(this, "venu", 
    VenueFragment01.class))
            .setIcon(R.drawable.venutabhosticon);

    actionBar.addTab(tab1);  

    /** Creating APPLE Tab */
    Tab tab3 = actionBar.newTab()
            //.setText("Apple")
            .setTabListener(new CustomTabListener<SocialFragment01>(this, "social", SocialFragment01.class))
            .setIcon(R.drawable.socialtabhosticon);

    actionBar.addTab(tab3);  

    /** Creating APPLE Tab */
    Tab tab4 = actionBar.newTab()
            //.setText("Apple")
            .setTabListener(new CustomTabListener<ActivityFragment01>(this, "activity",         
   ActivityFragment01.class))
            .setIcon(R.drawable.actionbartabhosticon);

    actionBar.addTab(tab4);  



}   
}

Here is my first fragment code -

package in.wptrafficanalyzer.actionbarnavtab;
 /** This is a listfragment class */
 public class PlayFragment01 extends Fragment {

/** An array of items to display in ArrayList */



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)    
{
    /** Creating array adapter to set data in listview */
    View rootView = inflater.inflate(R.layout.fragment_play, container, false);         
    //new DownloadJSON().execute();

    ActionBar actionBar = getActivity().getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    getActivity().getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#0077d1")));
    ActionBar mActionBar = getActivity().getActionBar();
    getActivity().getActionBar().setIcon(
       new ColorDrawable(getResources().getColor(android.R.color.transparent)));    
    mActionBar.setDisplayShowHomeEnabled(true);
    mActionBar.setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);
    View mCustomView = mInflater.inflate(R.layout.custom_actionbar2, null);
    mActionBar.setCustomView(mCustomView);
    mActionBar.setDisplayShowCustomEnabled(true);

    return rootView;
}
}
manfcas
  • 1,933
  • 7
  • 28
  • 47
madhavan
  • 50
  • 7

3 Answers3

0

The question your asking is not very clear but you can get the ActionBar instance from within a Fragment using getActivity().getActionBar().

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
  • Thanks!! Above I had explain it with an example also! – madhavan Nov 03 '14 at 09:34
  • If this answer helped you, please accept it. This will help other users having the same problem to quickly solve it. – Rolf ツ Nov 03 '14 at 09:35
  • I work with this but this is not solving my problem here is my fragment code - "/** This is a listfragment class */ public class PlayFragment01 extends Fragment { /** An array of items to display in ArrayList */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { /** Creating array adapter to set data in listview */ View rootView = inflater.inflate(R.layout.fragment_play, container, false); //new DownloadJSON().execute(); return rootView; } }" – madhavan Nov 03 '14 at 09:42
0

ActionBar.setNavigationMode() and TabHost is now deprecated.

The new ToolBar introduced in API-21 is more powerfull.

But since you asked for a solution, here is a simple one from Google SlidingTabLayout: This is like a PagerTabStrip that you can easily customize and works like great.

It works with ViewPager and Fragments of course.

shkschneider
  • 17,833
  • 13
  • 59
  • 112
0

In my point of view ,you should read this ,so that you can get the basic idea for creating a fragment.

http://developer.android.com/guide/components/fragments.html

Try this sample code..this sample code includes 3 tab ,first try to understand this code ,then you can make your own app with 4 tabs

http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/

Also try to modify your question ,It is unclear and also it will be good that you post your errors

user3173628
  • 182
  • 5
  • Thanks!! I had read full block. I have an query How can I add images in places of this text ? – madhavan Nov 03 '14 at 12:06
  • do you mean to change the title name of tabs)top rated ,games to image or the text inside the tab to image? – user3173628 Nov 03 '14 at 12:16
  • read this link :-http://www.mkyong.com/android/android-tablayout-example/ If you find this useful you can accept my answer – user3173628 Nov 03 '14 at 12:31
  • Thanks it help a lot!! can you please tell me if I want to make background of tab - white color what I do please help me!! – madhavan Nov 03 '14 at 12:38
  • Inside manifest file :- android:theme="@android:style/Theme.NoTitleBar" instead of this ,use android:theme="@android:style/Theme.Light" then Build the project then clean it. – user3173628 Nov 03 '14 at 12:50
  • error: Error: No resource found that matches the given name (at 'theme' with value '@style/Theme.Light'). – madhavan Nov 03 '14 at 12:52
  • which link are you following?...first link or second one? – user3173628 Nov 03 '14 at 12:56
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64173/discussion-between-madhavan-and-user3173628). – madhavan Nov 03 '14 at 12:57
  • your second link code is deprecate!! so I search some other code and using other!! – madhavan Nov 03 '14 at 13:01