Android findViewById(android.R.id.home) return null with support library

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

Answers (2)

Blundell
Blundell

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

https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v7/appcompat/src/android/support/v7/app/ActionBarActivity.java

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:

https://github.com/android/platform_frameworks_support/blob/5476e7f4203acde2b2abbee4e9ffebeb94bcf040/v7/appcompat/src/android/support/v7/app/ActionBarActivityDelegateBase.java

https://github.com/android/platform_frameworks_base/blob/master/core/java/com/android/internal/app/WindowDecorActionBar.java

which leads to the ActionBar class, this has a possible clue about why it returns null

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/app/ActionBar.java#L106

/**
 * 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

wwfloyd
wwfloyd

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

Related Questions