0

I need to be able to supply a dynamic behavior for the buttons in my project. So far the closest I've gotten is using an XML like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:drawable="@drawable/mainmenu_button_pressed" /> 
<item android:state_enabled="false" android:drawable="@drawable/mainmenu_button_disabled" />  
<item android:drawable="@drawable/mainmenu_button" /> 

</selector>

This does exactly the trick, but as stated on the question's topic my project needs this to be dynamic, I'll explain what I mean...

This application has an intro menu and the several options have different images on them. Depending on a user-made choice at runtime (before this menu) I need to be able to supply a different set of images to the buttons. The ideal way to do this would be:

<item android:state_pressed="true" android:drawable="%MAINMENU_BUTTON_PRESSED%" />

So I could just set %MAINMENU_BUTTON_PRESSED and it would use set A (A_mainpressed.png) or B (B_mainpressed.png) without requiring extra coding.

I'm aware the probably proper way to do this would be using the OnClick function and doing everything programmatically. Considering, however, the ammount of manual work that implies on my project I wanted to first make sure there was really no way of creating a dynamic XML and supplying it to the button so it would work just like that (AFAIK all ressources are compiled and therefore cannot be modified at runtime, also, an XML file on the assets folder won't work because those are not compiled and hence do not generate an ID that I can supply).

Is there any way to achieve this behavior or should I just swallow it and stick with the OnClick method? Thanks

h4lc0n
  • 2,730
  • 5
  • 29
  • 41

2 Answers2

0

I have the same problem, resolved by defining level-list for each drawable state, for exemple, your mainmenu_button_pressed.xml drawable would be

<level-list
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/A_mainpressed.png"
        android:maxLevel="0"
        android:minLevel="0" />
<item
        android:drawable="@drawable/B_mainpressed.png"
        android:maxLevel="1"
        android:minLevel="1" />
<item
        android:drawable="@drawable/C_mainpressed.png"
        android:maxLevel="2"
        android:minLevel="2" />
</level-list>

Create three file for your state drawable like this, within your code just call the method setImageLevel according to your configuration. This worked fine for Showing (User/Root) image button.

Anis BEN NSIR
  • 2,555
  • 20
  • 29
0

Nevermind, found the answer on another post...** Keyword was "selector". I barely have any experience on Android so I didn't exactly know how to look for this (a friend actually hinted about the selector and so I found that). Thanks for the help anyways guys.

Community
  • 1
  • 1
h4lc0n
  • 2,730
  • 5
  • 29
  • 41