I'm new in TV layout design. According to reference, TV application use Leanback theme system wide and provide some widget and class specially developed for TV. But Leanback has very limited features.
I want to create a layout with TabLayout which provide by AppCompact. For that the activity must use AppCompact theme but in Fragment I am using BrowserFragment provide by Leanback. So when I add AppCompact in activity, fragment crash with layout parameter issue.
Binary XML file line #21: Binary XML file line #21: You must supply a layout_width attribute.
Is it not possible to use both theme in TV application?
Related
I want to add actionBar programmatically in android since I'm generating the page layout dynamically without using .xml layout (by reading json file )....I've tried it with defining other layout & then setting its view using LayoutInflater ...Pls let me know how to define it since I'm new to android .
Very simple
getActionbar().hide();
getActionbar().show();
I was using SVG's in my Android application as src of my ImageView, using appSrc attribute in order to give compatibility backwards (SDK<21).
But now I have tried to use them in my TextView Compound Drawables (drawableXXX attributes) and I get multiple errors when I use a device with KitKat (The same errores that I had when I used android:src instead of app:srcCompat).
Caused by: android.view.InflateException: Binary XML file line #261: Error inflating class
Is there anyone who know a way to use them in Compound Drawables?
as of now you cannot add a VectorDrawable from xml attributes, that functionality is only limited to app:srcCompat more on this in the android developers blog post
However you may do it programmatically using VectorDrawableCompat.create(Resources, int, Theme) and then add it as a compound drawable to the TextView using TextView#setCompoundDrawables
see :
developer.android.com/reference/android/support/graphics/drawable/VectorDrawableCompat.html
developer.android.com/reference/android/widget/TextView.html
I had the same issue and i found only two options:
1) set your drawable programmatically or create a custom view
2) remove "vectorDrawables.useSupportLibrary = true" from your build.gradle
This will cause Android Studio to generate PNGs at compile time for apps with a minSdkVersion less than API 21 while using your vectors on API 21+ devices, allowing you to keep the same code at the cost of additional APK size.
As is described in this article, you can create a little hack for this issue. Just create layer-list drawable file(ic_working_image.xml) like this:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_your_image_name"/>
</layer-list>
And then just use the new drawable on TextView as before with drawableXXX attributes.
I've been writing some Android apps but I don't really understand when to use app: and when to use android:. When styles are not being applied the way they're supposed to, I use trial and error and sometimes find that using app: instead of android: solves the issue but I don't understand why. It'd be great if someone could point me in the right direction. Thanks!
You can use the app namespace to have app compatibility with older API versions.
For example
app:srcCompat="#drawable/customborder" has the same effects with
android:background="#drawable/customborder"
The difference is that the first will work correctly with older API's and the second will not display what you would like.
You are talking about custom namespace.In android we can create custom views in additional to already available views.
As per in Google developer docs..
To add a built-in View to your user interface, you specify it in an XML element and control its appearance and behavior with element attributes. Well-written custom views can also be added and styled via XML. To enable this behavior in your custom view, you must:
Define custom attributes for your view in a resource element
Specify values for the attributes in your XML layout
Retrieve attribute values at runtime
Apply the retrieved attribute values to your view
Once you define the custom attributes, you can use them in layout XML files just like built-in attributes. The only difference is that your custom attributes belong to a different namespace. Instead of belonging to the http://schemas.android.com/apk/res/android namespace, they belong to http://schemas.android.com/apk/res/[your package name]
So for if you use default views you can use android namespace and if you want to set and use attributes for custom view you can define your own name.
Refer this
If you take a look at the beginning of the your layout xml files (in which you used app:) you will (probably) find lines like this:
<?xml version="1.0" encoding="utf-8"?>
<SOME_LAYOUT xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
in this case app: namespace will be used for custom attributes, specified by you inside attrs.xml file or by someone else in one of used libraries.
Sometime the property with android only available in new Android version like
In this case, you should use app:... to make it work with older version.
moreover you will find two variants
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/[packagename]"
the difference between xmlns lines is res-auto take care of resolving our package as sometime we will add .debug or .test in our package and we already provided the packageid of the app Ex:
xmlns:app="http://schemas.android.com/apk/com.test.io.debug"
xmlns:app="http://schemas.android.com/apk/com.test.io.test"
We start xml file with some layout. Within that layout we create our views like Button, TextView etc. But how does the system know what are Button and TextView? I mean we are not importing anything. Moreover inside Button, we write android:layout_width = "wrap_content" what is android in this? Since it is inside Button why can't we write layout_width = "wrap_content" directly?
From developer.android.com android: defines the Android namespace. This attribute should always be set to "http://schemas.android.com/apk/res/android".
xmlns:android is for identification that this xml is used for android, not for other function.
Namespaces uniquely identify code/libraries. If I write an api that uses all the same names and such as the android api the only way to distinguish between my api and android api is to use the android namespace, or mine. Read XML NameSpace
Check out tutorial on namespaces
It is called as the namespace. At the top of every XML file you will have this line xmlns:android="http://schemas.android.com/apk/res/android" That's where the namespace android comes from
XML is a document language. The meaning of the elements is given to them by the compiler.
When you compile the XML file using Android's resource compiler, it imparts meaning to them.
The prefix android: establishes the namespace of the tag that follows it.
You can learn more about XML namespaces here: http://www.w3schools.com/xml/xml_namespaces.asp
I have an android application that I'm working on with a custom theme applied to it in the android manifest on the main activity itself. This activity creates a listview, which has the style applied to it.
If I create a custom layout for that listview, including a separate xml file for the rows themselves, and I apply styling directly to those layouts, does this style overwrite the overall style for the application? I'm trying to get a grasp on the hierarchy of events as far as how styling and themeing works.
The way I'm GUESSING it works in my example is: apply style for row, referenced by row layout xml > apply style for overall listview, referenced by listview layout xml > apply style from custom theme, referenced by style xml referenced by android manifest
Am I right? Or am I approaching this incorrectly. (just for confusions sake, by ">" I mean has a greater importance than)
If you've specified the same attributes in multiple places, the list below determines which attributes are ultimately applied. The list is ordered from highest precedence to lowest:
Applying character- or paragraph-level styling via text spans to TextView-derived classes
Applying attributes programmatically
Applying individual attributes directly to a View
Applying a style to a View
Default styling
Applying a theme to a collection of Views, an activity, or your entire app
Applying certain View-specific styling, such as setting a TextAppearance on a TextView
Hope I am understand your question right here...
The styles you define in styles.xml will always overwrite the styles coming from the theme currently used by android.
But this only works for the attributes you overwrite.
If you leave an attribute untouched, android will provide the style for it, and sometimes this comes bite you in the butt :)
This system is best described like this:
A textview requires an attribute example
<item name="android:textColor">#00FF00</item>
Android will first look in the original layout.
If not found, it will look into your custom styles.
If not found, it will look into android styles.
Hope this helps.
The standard themes have lines like which define the ListView style:
<item name="listViewStyle">#android:style/Widget.ListView</item>
In your own theme you can do a
<item name="listViewStyle">#style/MyOwnListView</item>
Something that is not defined in the ListView style (own or default) will be what is defined in the theme if you have defined it there.