0
ImageView icon = new ImageView(this);
icon.setImageDrawable(R.drawable.alarm);

When I want to set drawable icon it gives me error that android graphics drawable can not apply to ImageView ?

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
Hamees Arshad
  • 187
  • 1
  • 10
  • What's kind of "this"? "this" should be context here. Check "alarm" drawable is exists. Also give the full logcat to easy to investigate. – NamNH Aug 08 '15 at 16:54
  • ImageView icon = new ImageView(this); icon.setImageDrawable(ContextCompat.getDrawable(MainActivity.this , R.drawable.alarm)); FloatingActionButton actionButton = new FloatingActionButton.Builder(this) .setContentView(icon) .build(); SubActionButton.Builder itemBuilder = new SubActionButton.Builder(this); – Hamees Arshad Aug 08 '15 at 17:37
  • tthis is code and still my icon is not apperaing on the floatingaction button .why? – Hamees Arshad Aug 08 '15 at 17:37
  • I can't see the error in your code. Please try this `getResources().getDrawable(R.drawable.alarm)`. And I think you were trying to use this [libs](https://github.com/cymcsg/UltimateAndroid/blob/master/UltimateAndroidNormal/DemoOfUI/src/com/marshalchen/common/demoofui/circularfloatingactionmenu/MenuWithFABActivity.java), so learn tutorial about it. – NamNH Aug 10 '15 at 02:59

3 Answers3

1

The problem is you are passing ID of your resource in

icon.setImageDrawable(R.drawable.alarm);

method but this method takes only Drawable as parameter. You should change your above call to this

icon.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.alarm));

Or you should call this method instead.

icon.setImageResource(R.drawable.alarm);
Noman Rafique
  • 3,735
  • 26
  • 29
  • i have put icon which is in png format in drawable.. now i want to set this icon which i named is alarm ...now i want to set that alarm icon to my floating action button and it is returning me same android graphics error ? – Hamees Arshad Aug 08 '15 at 17:27
  • Check it here http://stackoverflow.com/questions/30969455/android-changing-floating-action-button-color – Noman Rafique Aug 09 '15 at 04:20
0

Use setImageResource(R.drawable.alarm) method instead.

Vikas Patidar
  • 42,865
  • 22
  • 93
  • 106
0
<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/addPostBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="58dp"
    android:clickable="true"
    android:focusable="true"
    android:padding="0dp"
    android:scaleType="center"
    app:borderWidth="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/mainBottomNavBar"
    app:maxImageSize="45dp"
    android:backgroundTint="@color/White"
    app:srcCompat="@drawable/add_icon"
    app:useCompatPadding="true" />

Use the srcCompat keyword.