Reputation: 433
I want to change ActionBar home button left padding. I've tried this solution. But when I try findViewById(android.R.id.home)
I get null. In the same time android.R.id.home
is not 0.
And this happens only if I use android-suppot-v7. If I don't use support library all goes good.
Maybe someone can help me?
Here is my simple code:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
ImageView view = (ImageView)findViewById(android.R.id.home);
if (view !=null){
view.setPadding(10, 0, 0, 0);
}
}
}
Layout:
<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"
tools:context="com.example.timoshkois.actionbar.MainActivity">
</RelativeLayout>
Upvotes: 4
Views: 3716
Reputation: 76496
Hmm you're not wrong, if you look at the source for the Activity
you inherit from, they also use android.R.id.home
Like this
@Override
public final boolean onMenuItemSelected(int featureId, android.view.MenuItem item) {
if (super.onMenuItemSelected(featureId, item)) {
return true;
}
final ActionBar ab = getSupportActionBar();
if (item.getItemId() == android.R.id.home && ab != null &&
(ab.getDisplayOptions() & ActionBar.DISPLAY_HOME_AS_UP) != 0) {
return onSupportNavigateUp();
}
return false;
}
Looking how the ActionBar is created it uses these classes:
which leads to the ActionBar class, this has a possible clue about why it returns null
/**
* Standard navigation mode. Consists of either a logo or icon
* and title text with an optional subtitle. Clicking any of these elements
* will dispatch onOptionsItemSelected to the host Activity with
* a MenuItem with item ID android.R.id.home.
*
* @deprecated Action bar navigation modes are deprecated and not supported by inline
* toolbar action bars. Consider using other
* <a href="http://developer.android.com/design/patterns/navigation.html">common
* navigation patterns</a> instead.
This deprecation means the new ToolBar won't use nav modes so maybe this also means Toolbar will not have this Android id (R.id.home
) - which makes sense as the previous links show that app compat not uses a Toolbar
under the hood, which legacy implementations will not be using.
As a test you could do what the comment says and override onOptionsItemSelected
press the logo and query the view you are passed to find it's id getId()
Upvotes: 2
Reputation: 312
Apparently, 'home' is the name of your layout file? [No, I see that's activity_main.] To access the relativelayout item, it needs an ID of its own, like android:id="@+id/home"
Upvotes: 0