3

I am searching for default drawable of back arrow. In this question showed how to get it in xml. But I am searching for using in programming side (java/kotlin).

I want to use this drawable id in code like:

ContextCompat.getDrawable(context, homeAsUpIndicatorId);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Axbor Axrorov
  • 2,720
  • 2
  • 17
  • 35

2 Answers2

5

Create the drawable and pass the drawable id to the function. For example, I create a vector drawable called arrow_back.xml.

<?xml version="1.0" encoding="utf-8"?>
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0"
        android:autoMirrored="true">
        <path
            android:pathData="M20,11L7.8,11l5.6,-5.6L12,4l-8,8l8,8l1.4,-1.4L7.8,13L20,13L20,11z"
            android:fillColor="#fff"/>
    </vector>

Note: the drawable that homeAsUpIndicatorId is referencing has a private modifier, so you can't access it directly. However, the above code was copied from the vector drawable with little modification.

I will pass the id to the getDrawable() function like this.

ContextCompat.getDrawable(context, R.drawable.arrow_back);

EDIT: Do the following to get the drawable from homeAsUpIndicatorId:

    TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {R.attr.homeAsUpIndicator});
    int attributeResourceId = a.getResourceId(0, 0);
    ContextCompat.getDrawable(context, attributeResourceId);
    a.recycle()
devmike01
  • 1,971
  • 1
  • 20
  • 30
  • Thank you for answer, but I need default android drawable. – Axbor Axrorov Dec 06 '18 at 11:10
  • This worked fine. I don't really see the reason why you should downvote it. Moreover, the XML code I posted is copied from the default vector drawable. – devmike01 Dec 06 '18 at 11:13
  • You can find the original source for the back button here: https://chromium.googlesource.com/android_tools/+/1a05e6e0c759f1bb95f1c698150fa17a986d7619/sdk/extras/android/support/v7/appcompat/res/drawable/abc_ic_ab_back_material.xml and home as indicator here https://android.googlesource.com/platform/frameworks/support/+/ff6a18ccc12673e67ae2b143de1bb27048824365/v7/appcompat/res/values-v21/themes_base.xml – devmike01 Dec 06 '18 at 11:15
  • By default i mean not adding my drawable. – Axbor Axrorov Dec 06 '18 at 11:16
  • 1
    Check the edit I did to my post, if that doesn't work for you.Then you need to explain what you really want. – devmike01 Dec 06 '18 at 11:29
2

You can use this snippet

val a = theme.obtainStyledAttributes(R.style.AppTheme, (R.attr.homeAsUpIndicator))
val attributeResourceId = a.getResourceId(0, 0)
val drawable = ContextCompat.getDrawable(this, attributeResourceId)
DDk9499
  • 35
  • 5