10

Hello everyone i create ActionBar. viewpages and custom action bar.Now i want to remove(hide) app icon in ActionBar i used setDisplayShowHomeEnabled(false); but nothing happened. This is a my code

public class MainActivity extends FragmentActivity implements TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "test1", "test1", "test1", "test1",
        "test1" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getActionBar();
    actionBar.setHomeButtonEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setDisplayShowHomeEnabled(false);
    actionBar.setDisplayShowTitleEnabled(false);
    //actionBar.setIcon(R.color.white);
    actionBar.setDisplayShowTitleEnabled(true);
    Drawable d = getResources().getDrawable(R.drawable.acttitle);
    getActionBar().setBackgroundDrawable(d);

    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);

    viewPager = (ViewPager) findViewById(R.id.vp_main);
    viewPager.setAdapter(mAdapter);

    getActionBar().setCustomView(R.layout.menu_example);
     actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM |
     ActionBar.DISPLAY_SHOW_HOME );
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {

            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });

    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // actionBar.setStackedBackgroundDrawable(getResources().getDrawable(
    // R.drawable.background)); background viewpager

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {

    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}

what is wrong ? if anyone know solution please help me thank you.

Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
user3272799
  • 111
  • 1
  • 2
  • 6

5 Answers5

29

You use

 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
            | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE); 

So You Are getting Home icon.

If You want to hide app icon in Particular Activity Use

getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent))); 

If You want to hide app icon in Full Application Use setDisplayShowHomeEnabled(false) and setDisplayShowTitleEnabled(false)

duggu
  • 37,851
  • 12
  • 116
  • 113
android learner
  • 1,804
  • 3
  • 15
  • 13
  • thank you i added (actionBar.setDisplayShowHomeEnabled(false) ; actionBar.setDisplayShowTitleEnabled(false);) and app icon has hidden but i got different design .my viewpager is above a custom actionbar this is a different design :( – user3272799 Feb 07 '14 at 11:55
  • 2
    setIcon(android.R.color.transparent); seems to be enough to hide app icon in a particular activity – crubio Aug 18 '14 at 15:06
9

Try this ..

setDisplayShowHomeEnabled(false);

More Reference

UPDATE ::- If you have customized your action bar using XML then you can try some thing like ..

<item name="android:displayOptions"></item>

This eventually will hide your app icon and title ..

As suggested on stack you can also try some combination like ..

getSupportActionBar().setHomeButtonEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setIcon(R.color.transparent);
    getSupportActionBar().setDisplayShowTitleEnabled(true);

UPDATE2

Action Bar automatically adjusts every stuff for you. For Ex: If you have lot of stuffs in you title bar then Action Bar adjusts title some thing like YourPr... instead of your complete title like YourProject.

You need not to wonder for that.

UPDATE 3

If some one want to customize ActionBar then it can easily be accomplished like ..

actionBar = getSupportActionBar();

        actionBar.setCustomView(R.layout.action_bar_confirm_cabs);

        fare_ll = (TextView) actionBar.getCustomView().findViewById(
                R.id.fare_ll);
        confirm_back = (LinearLayout) actionBar.getCustomView().findViewById(
                R.id.confirm_cabs_back);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

Then now here you have your textview .. or any thing you wish like you may have a btn over here ..

Simply now apply click listener or any thing you like ..

Hope it helps!

AndroidHacker
  • 3,596
  • 1
  • 25
  • 45
  • it written in my. please look my code i use custom action bar and meybe that is a problem – user3272799 Feb 06 '14 at 12:26
  • thank you done.but i tested my app large screen resolution and i have different design then small screen resolution.do you know what should be problem ? – user3272799 Feb 06 '14 at 12:55
  • thank you i used you code but i have problem large screen resolution ..i have different design between large and small screen resolution .(please look my code) mybe problem is in this line (getActionBar().setCustomView(R.layout.menu_example);) – user3272799 Feb 06 '14 at 13:15
  • @user3272799 If you need to define custom view for Action Bar then you can use same by defining in your styles .. Just fallow android developer side. There's lot available over there .. – AndroidHacker Feb 10 '14 at 04:16
8

Use

getActionBar().setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent))); 

or

getActionBar().setCustomView(R.layout.menu_example);
         actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM |
         ActionBar.DISPLAY_SHOW_HOME ); 

will display the home icon. So Use Only getSupportActionBar().setCustomView(R.layout.menu_example);

and to hide the app icon use

getSupportActionBar().setDisplayShowHomeEnabled(false); getSupportActionBar().setDisplayShowTitleEnabled(false);

Subhalaxmi
  • 5,687
  • 3
  • 26
  • 42
8

There are multiple way to do it:

Runtime handling:

getActionBar().setIcon(
       new ColorDrawable(getResources().getColor(android.R.color.transparent)));  


getActionBar().setDisplayShowHomeEnabled(false);

From xml:

<item name="android:icon">@android:color/transparent</item>

find more information on following thread: Remove icon/logo from action bar on android

Community
  • 1
  • 1
CoDe
  • 11,056
  • 14
  • 90
  • 197
0

My requirement is to keep back button and action bar title without app icon/logo. This worked for me:

//For Displaying Back button

getActionBar().setDisplayHomeAsUpEnabled(true);

//For hiding logo:

getActionBar().setLogo(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
Ravi Yadav
  • 2,296
  • 3
  • 25
  • 32