How to Create a Custom Button (With Images) in Android Studio

46
2



In this video we will learn how to create a custom button that uses image files to replace the background of the default Android button.
For this we will create a StateListDrawable xml file, where we define appropriate png files from the drawable folder for the different button state combinations of state_pressed and state_enabled. We put these states as items between a selector tag to create a state list and set it as the background on our Button widget.
Just like for any other button, we can set an OnClickListener on our custom button, disable it etc.

Example code + button image files:

____________________
❗ Subscribe to the channel:

⏯ Watch more tutorials:

⭐ Become a channel member for exclusive tutorials and other perks:

㊙ Help translating the videos:

💚 Support the content:

💬 Join the Discord chat:

🎧 Audiobooks and podcasts for programmers:

👶 How to get started with Android development:

📣 Follow Coding in Flow on social media:

Facebook:

Instagram:

Twitter:

Blog:

Nguồn:https://wijstaanvooronzegrondrechten.org/

46 COMMENTS

  1. Hello. I like your way of explanation! But it didn't help me, if I add an image into the /drawable the picture appears there, but I can't use it in the code(there is no option for it). Can you help me with it? Thank you!

  2. I have a question. I've got a problem when I try to check onClickListener the button that is on another layout. My button is on the FrameLayout that change by navigation bar. And when I started my application, it always crashed by runtime error. (it seems myMainActivity cannot find the button from another Layout)

  3. Fantastic, but errors below, any clues? (running on AndroidStudio,3.4.2, using emulator Nexus5X):
    error: package android.support.v7.app does not exist

    error: cannot find symbol class AppCompatActivity

    error: method does not override or implement a method from a supertype

    error: cannot find symbol variable super

    error: package R does not exist

    error: package R does not exist

    error: package R does not exist

    error: no suitable method found for makeText(MainActivity,String,int)

    method Toast.makeText(Context,CharSequence,int) is not applicable

    (argument mismatch; MainActivity cannot be converted to Context)

    method Toast.makeText(Context,int,int) is not applicable

    (argument mismatch; MainActivity cannot be converted to Context)

  4. hi, thanks for this good and short tutorial.
    i have problem, after do all my new activity doesn't open and app crash!
    what should i do?

  5. thanks so much for your helpful videos , i'm really appreciate your effort
    please if i need to make a counter i mean with every click the text change with +1 , should i use a button with increment numbers or is there a better way? thanks in advance 🙂

  6. I want to put two icons unchecked and checked radio button i know how to create drawable file in xml what next how to check it in java? Can i get the idea please asap!

  7. Hey, ich habe das 1 zu 1 kopiert und bei mir funktioniert es nicht, ich habe ein Videos stumm geschaltet, hast du was wichtiges dazu gesagt?
    Btw. ich finde super, das du im Video an den Code ranzoomst

  8. Hi, I follow your channel. I'm an android studio student and I think he's an excellent teacher.

    I liked this lesson with the buttons. Is that I am trying to develop an application where the user clicks the button and it changes the color, however, the button must remain with the color clicked until the user comes back and click the button again. That is, the button should remain with the color chosen by the user even if he closes the application.

    Is it possible to do in android studio as well as html5 and css3?

    PS: The application has to do the things I mentioned above online, so that other users interact

  9. Would You be interested to work with me on an application? If so send me a PrivateMessage about your requirements in order to do so 🙂 Awesome Tutorial!! have a great Day!!

  10. Thanks bro i was finding this.. but if i have a vector asset(XML file button) then what to do for different states… i did the same as you but my app crashed

  11. Dear sir,
    Can you make video tutorial on android app licensing for paid app ? If possible please make the tutorial fast, it's very urgent & important to me.

    Thank you.

LEAVE A REPLY

Please enter your comment!
Please enter your name here