This is the follow up post from Android Module Development series. Today I am
going to continue with next topic which is Using XML layouts in modules.
LayoutInflater class is used to instantiate layout XML file into its corresponding View objects. In other words, it takes as input an XML file and builds the View objects from it.
Lets consider our RatingBar module, here I am using xml layout file (raingbar_layout.xml) to create RatingBar with custom styles. In module I can access the xml layout elements in following way
XML layout file (raingbar_layout.xml)
Source Code:
You can download entire RatingBar module source code here https://github.com/railskarthi/Ratingbar
going to continue with next topic which is Using XML layouts in modules.
- Understanding methods, properties, constants and module life cycle
- Using event listeners and callbacks in Module
- Converting native control into Titanium view
- Accessing module and app resources
- Using XML layouts in modules
LayoutInflater class is used to instantiate layout XML file into its corresponding View objects. In other words, it takes as input an XML file and builds the View objects from it.
Lets consider our RatingBar module, here I am using xml layout file (raingbar_layout.xml) to create RatingBar with custom styles. In module I can access the xml layout elements in following way
//declaration View raingBarWrapper; int resId_raingBarHolder = -1, resId_ratingBar = -1; //fetching app package name and resources String packageName = proxy.getActivity().getPackageName(); Resources resources = proxy.getActivity().getResources(); //fetching resource id resId_raingBarHolder = resources.getIdentifier("raingbar_layout", "layout", packageName); resId_ratingBar = resources.getIdentifier("ratingbar_default","id", packageName); LayoutInflater inflater = LayoutInflater.from(getActivity()); //inflating "raingbar_layout" xml file raingBarWrapper = inflater.inflate(resId_raingBarHolder, null); //getting reference to RatingBar component in layout ratingBar = (RatingBar) raingBarWrapper.findViewById(resId_ratingBar); setNativeView(raingBarWrapper); //adding properties to RatingBar component ratingBar.setNumStars(stars); ratingBar.setStepSize(stepSize); ratingBar.setRating(rating);
XML layout file (raingbar_layout.xml)
Source Code:
You can download entire RatingBar module source code here https://github.com/railskarthi/Ratingbar
These tutorials are great! Have you ever though about exposing Android's Drawer Layout as a Titanium Module? (http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html).
ReplyDeleteI don't have any idea about converting Android's Drawer layout into a Titanium module. It has more challenges. Meantime you can do similar navigation using with TiDraggable module( https://github.com/pec1985/TiDraggable ).
ReplyDeleteYep I got it work.
ReplyDeleteOne more issue I am facing is to refer external jar file android.support.v4.widget.DrawerLayout after adding this I am unable to build module.
what exactly you are trying to do?
ReplyDeleteI am trying to user DrawerLayout for this I have added android-support-v4.jar in modules lib folder and then added it in library via Java Build Path.By this modules gets generated but while using it in app it say
ReplyDeleteERROR] UNEXPECTED TOP-LEVEL EXCEPTION:
[ERROR] java.lang.IllegalArgumentException: already added:
What to do with this?
Also I did one hack to delete jar from compiled module lib folder after that It say
ReplyDeleteSending event: exception on thread: main msg:java.lang.VerifyError:
Thanks! these posts show the magic of Titanium.
ReplyDeleteIs is possible to pack a pure native android activity(without layout xml) into a module? Or to repack the apt file into a Titanium module and launch the native activity from JS? and how about so library?