Creating customs controls in Android - android

I want to create my own custom controls in Android application and use them in this way:
In the XML file:
<MyButton .... />
I would like to define MyButton some where in the resources XML

Create your own subclass of View (e.g., com.ofirbit.MyButton) and then reference it in your layouts (e.g., <com.ofirbit.MyButton android:id="..." />). Here is an Android library project and demonstration sub-project showing an implementation of a custom ColorMixer widget and its use in an application. The details of how to implement one of these can be found in some Android books, the Android documentation, and perhaps elsewhere.

Related

Xamarin.Forms Android Project not showing Resource.Layout folder

I have a Xamarin.Forms solution in which inside my Android project I have a custom BroadcastReceiver. Inside this class I'm trying to create a custom notification layout using RemoteViews which was explained to me using this thread.
However I am having problems with the construction of this class using the RemoteViews(Android.Content.Context,Resource.Layout) constructor. Specifically the Resource.Layout portion. When I go into the 'Resources' folder there is only a Drawable folder (even when 'Show All Files' is enabled). Which is interesting considering I can access layouts after typing Resource.Layouts.
Is there a way to get a custom .xml id int (or .axml if that's the case) to be referenced for this strategy inside the Layout 'folder', or any other way Xamarin does instead that I may not have knowledge of?
Essentially all you have to do is add a folder in the Android project named "Layout" under the Resources folder. Then create the .axml file. Then the file can be referenced using Resources.Layout and the id retrieved using the method from this thread.

Generating live resource class like R.java in android

How can I generate a live resource class just like what android generates based on its layout xmls, R.java, based on my customized xml file?
Is there any specific plugin for android studio to actively watch for a particular xml file, namely mylayout.xml, and generate a java class, namely myR.java as I'm writing codes?
EDIT: What I want to do is to create NON-VIEW objects from an xml file which defines objects and their attributes, but not in run-time. My intention is to auto-generate a before-run-time class which contains those objects ids and there would be such methods like getMyObject(int id) which automatically instantiate the object from its relevant class.
Android will automatically add the things from any custom XML layout to the R class. You do not need to do anything special for this, beyond coding your custom layout in line with Android's style and requirements. See Creating Custom Views.

Which is best, Java or Xml for developing android apps

I have know core java, I want to develop android apps, in few sites I saw apps in XML in few Java.Which is best and easy to use Java or XML?
I'm assuming you are talking about the UI, not the complete app:
For everything statically I use XML, because it is easy to find in the structure of your project.
Some parts you want to create dynamically and you have no other choice then to use Code. Be smart, in this, so if you have to add several Views that look the same do this
Make an XML with your views
In the loop where you are adding the several Views, inflate this xml, set your id's etc, and add them
You can have all the basics, styles etc in your XML, and still add stuff dynamically.
Maybe you want to check out some of the hello world code?
You can't build apps in XML. You use XML to define the UI and a few other things, but the logic of the app itself has to be written in Java.
Android development is in Java, but when you declare the layouts (where the buttons and so are going to be on the screen) you do that in XML.
So you have to use BOTH anyways.
XML for layout
JAVA for programming your app

Tips on making custom, reuesable components in Android

I want to create a component that will appear as a navigation menu for an Android application. Basically, the custom component is a rectangular "Div" (to use HTML terms) that contains six buttons. Each button provides a link to another part of the application. I want to use this on every "page", so I want to make it easy to maintain.
What is the recommended class to extend for creating custom components like this? (I've seen the "Widget" class, but not sure If this should only be used for widget that appear outside the app (like Google search))
And
Is the process as simple as creating the custom "Widget" class with it's own XML layout and then adding it to each Activity class?
The class to extend is View, the Widget class is for widgets in the homescreen. This is a nice doc to read: Building Custom Components, I suggest to look at the Compound Controls section, that seems suitable for your problem.
Is the process as simple as creating the custom "Widget" class with it's own XML layout and then adding it to each Activity class?
Yes, once you have written your own view, you just have to add it to your layouts in the XML (just like you do with the android views), something like this:
<com.your.package.YourNiceView
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

Android - Defining Custom Component Layouts in XML

i'm reading the android developer docs on creating custom components and one thing that's unclear, is whether you can define the layout of your component using xml and then reuse that across class libraries. like, say for instance, i want to create a class library called myComponents, and in there i want to have myTehAwesumsWidget or whatever, and i want the layout to be defined in xml, can i include that xml in the referenced class library?
If you replace "class library" with "Android library project", everything you describe should work just fine. Here is an Android library project that distributes a custom widget (also wrapped in a dialog and custom preference).

Categories

Resources