How my xml layout file on Button can be called in Android Titanium

You must Login before you can answer or comment on any questions.

I am trying to make custom button with xml layout, and I want to call that layout file on my button background. So how to use my xml layout file in Titanium Android, as in native I use to set button background from xml layouts file

background:"@drawable/custom_button"    // where custom_button is an xml file present in drawable folder of native android.
Actually reason for making custom button is to maintain 3 state as tabbar.

1 Answer

You can do this by putting all the resources to /Platform/android/res/drawable. These resources will be copied to res/drawable folder when you build your project. Then to give the reference of this drawable xml file you can do following.

var tab1 = Ti.UI.createTab({
        title : 'Tab1',
        window : selectProductWin,
        icon : globals.isIos ? '/images/tab1.png' : Ti.App.Android.R.drawable.tabbar1  ,
    });
tabbar1 is the drawable xml file which is into the /Platform/android/res/drawable folder. tabbar1.xml will look like below
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 
    <item android:drawable="@drawable/tab1act" android:state_pressed="false" android:state_selected="true"/>
    <item android:drawable="@drawable/tab1inact"/>
 
</selector>
Here tab1act and tab1inact are the images which is into the same folder where we have putted tabbar1.xml ie. /Platform/android/res/drawable folder

— answered 5 months ago by Dharmendra Patel
answer permalink
1 Comment
  • Hi Dharmendra Patel, thanks for this answer, this has been working nicely for us in a few apps already. Did you manage to also change the selectedBackground/focusedBackground and textsize/color in this manner, specifically for tabs? Really struggling to get that working with Titanium.

    Should you have any pointers to use the /res/drawable solution for background properties for tabs on Android as well, this would be great if you could share them!

    — commented 4 months ago by Roeland P

Your Answer

Think you can help? Login to answer this question!