Adding custom attribute to a view - android

I'm using a libary for creating a donut shaped pie chart.
https://www.numetriclabz.com/donut-chart-tutorial-using-numandroidcharts-library-tutorial/
In this provided tutorial under step 4 there is a custom attribute called custom:pieInnerCircleRatio being added to the donut chart item. However there is no specific explanation written, on how that is done.

This link will guide you: https://developer.android.com/training/custom-views/create-view.html
You need to define attrs.xml file as explained in the Define Custom Attributes section.

Add following line to your parent element in xml :
xmlns:custom="http://schemas.android.com/apk/res-auto"

Step 4 shows how to use the custom attribute. Unfortunately, the code snippets include literal HTML tags and entities. Here is a translated version:
<com.numetriclabz.numandroidcharts.PieChart
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:id="#+id/piegraph"
custom:pieInnerCircleRatio="128"/>

Related

app:something...What is app:?

I am using this famous ViewPagerIndicator library in my project. I wanted to add color on ViewPageIndicator. I found a solution from here on StackOverflow.
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/indicator"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
app:fillColor="#FF888888"
app:pageColor="#88FF0000"
app:strokeColor="#FF000000" />
But one thing I can't understand here: why it is written app: instead of android:? What does app: mean? I have never used app:? What is its namespace? I googled but no solution found.
For the namespace, add this: xmlns:app="http://schemas.android.com/apk/res-auto"
The app namespace is used for all 'new' attributes. For example: if I make a custom view, that has its own attributes (let's say RainbowView with new attribute 'numberOfColors'), then I have to use that attribute in the app namespace because it isn't declared in the android namespace. So use the app namespace for all custom attributes. Either attrs you defined yourself, or attrs the author of a library added (in your case for CirclePageIndicator). But most of the time, when you start typing 'strokeCo...' and you see 'strokeColor' in the autocomplete list, the namespace will be added automatically when you select it.
Probably you only need to add the missing namespace:
xmlns:app="http://schemas.android.com/apk/res-auto"
Or take a look at this answer for referencing a library:
How can I set up custom namespaces in layout file in Android studio?

Error parsing XML: unbound prefix in custom view

I tried customizing a view from a library I'm using for my android app. The default xml code for this looks like this:
<it.gmariotti.cardslib.library.view.CardView
android:id="#+id/carddemo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"/>
This works without any problems. But when I add a new line in the element, to use my custom layout, I get an error in the opening line, that you can see in the title of this question: This is the line I was adding in the end:
card:card_layout_resourceID="#layout/custom_layout" />
This is also exactly the code that was used as an example in the docs of the library.
You probably forgot to "define" a namespace for the custom attribute card. So add this to your root-view in your xml-file:
xmlns:card="http://schemas.android.com/apk/res-auto"

Android custom attributes on "normal" views?

After taking a look at theming for Fede's UberMusic I came across the file album_screen.xml. Below is the source of that file. Basically I noticed that his themes have the ability to use custom views that are a part of his application, and that they have this XML namespace at the top theme. I am missing the connection as to how he is able to apply his attributes to views that he does not control, and how Eclipse will compile the cod below without fail. I placed the URL http://schemas.uberdroidstudio.com/theme into my browser's address bar but nothing came up, and I cannot figure out where/ how Eclipse knows the attributes that the namespace supports. Thank you ahead of time for your help.
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:theme="http://schemas.uberdroidstudio.com/theme">
<TextView
android:id="#id/artist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.0"
theme:textFilter="uppercase" /> <!-- This is the interesting line -->
I suspect that the theme:textFilter="uppercase" line isn't actually having an effect on the (apparently vanilla) TextView.
As for the URL of the namespace, it is interesting that you can't access it, since it does not appear to be a local styleable (Android would have you refer to a local styleable namespace as http://schemas.android.com/apk/res/your.package). +1 for novelty.
The solution is actually not as complicated as I originally thought. XML namespaces are arbitrary strings that just need to be unique. If your namespace does not start with http://schemas.android.com/apk/res then it is not validated and the APK package is not checked for declare-styleable or the like.
Now a custom namespace if very simple to implement, as illustrated by this code snippet in GitHub. Finally, applying custom XML attributes to "normal" widgets can be accomplished by using the LayoutInflater.Factory and calling setFactory before you inflate your views. Throw it all together and you have a highly theme-able, XML driven application like Fede's UberMusic.

Can I create a statich part of view

I`m wondering about one thing. If I decide to have a part of my View same in every Intent. For example 2 buttons at the bottom of screen and for example I have 3 diffrent views, List, Detail and a third one :) Do I need to put the buttons on every xml schem for each view or can I create other xml and attach it in each activity with all listener etc.
If I can attach in activity how can I do that ?
Create a XML buttons.xml with the common elements
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton
android:id="#+id/myButton"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
<ImageButton
android:id="#+id/myButton2"
android:src="#drawable/ic_title_search"
android:onClick="myHandle" />
</merge>
Include it in another xml:
<include layout="#layout/buttons"/>
where buttons is the name of the xml file to be included
You can use the <include /> tag in your XML files.
See Layout Tricks for an example.
Create a separate layout for your buttons then in the layout you wish to display them use the include tag.
This will allow you to reuse the same layout in multiple parent layouts.
The include works as stated above you could also consider using fragments
Fragments
You can use these in older versions of Android by including the compatibility library in your application. It's definitely more work than a simple include but if you need some reusable UI for more sophisticated features than simple buttons you might want to look at that as well
How to use compatibility API

Android custom xml tag in main.xml

I'm modifying the source code of a terminal emulator program for Android that contains the following confusing xml code in the linear layout of main.xml:
<com.vtrandal.bluesentry.EmulatorView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
This same xml code also appears in another xml file also in /res/layout along with main.xml. I can see that it appears to begin with a custom tag created using my package name. But why? How is this possible and why would someone want something like this?
When you extend a View to create a custom component, you need a way to add this objects to the xml layout. You do that by using the full class name, including the package name. The link to the dev guide the Nicholas posted previously explains it.
The attributes used inside that tag can be either attributes used for the View you are extending, or custom attributes defined in a new schema.
You can follow a nice tutorial about this here: http://www.anddev.org/creating_custom_views_-_the_togglebutton-t310.html
This is the syntax for creating custom components and views in Android.
Not sure what exactly is confusing. I'll assume its com.vtrandal.bluesentry.EmulatorView
part. This is name of the class that will be instantiated when inflating xml file.
If you got used to something like this:
<TextView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
Then you should know that this is because TextView is located in android.widget, and you should've wrote this:
<android.widget.TextView
android:id="#+id/emulatorView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
But because inflater knows about several special packages (android.widget, android.view, android.webkit) you can skip them in xml.

Categories

Resources