简体   繁体   中英

Make home icon clickable on the action bar, under api level 14

I am currently using getActionBar().setHomeButtonEnabled(true);

it works fine but it requires api level 14 and without it I would be able to get all the way down to api 8 which would be awesome! If someone knows a way around this please let me know.

You have to use support library and code will something look like this:

import android.support.v7.app.ActionBar

public class YourActivity extends ActionBarActivity {
ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actionBar = getSupportActionBar(); // now do whatever you want to do with this action bar
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

    @Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case android.R.id.home:
        // code for your action when you click home icon on action bar
        break;

    default:
        break;
    }

return true;
}


}

Just use ActionBarSherlock or ActionBarCompat . These will both handle low API levels and use the native ActionBar for higher APIs automatically.

You can use the ActionBar support library: http://developer.android.com/guide/topics/ui/actionbar.html

Instead of getActionBar() , use getSupportActionBar()

Also, make sure this is your ActionBar import:

import android.support.v7.app.ActionBar

Use the v7 appcompat support library to get api 8 support for this method .

Instructions are located :

http://developer.android.com/tools/support-library/features.html#v7-appcompat Actionbar http://developer.android.com/tools/support-library/setup.html#libs-with-res

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM