I needed to add a digital clock in the layout. My min-supported API level is 14. I found out that there is a digital clock widget for API level below 17 (After that it got deprecated), and there is a text clock for API 17 and above.
So one thing I can do is to add both layout in xml and dynamically set the visibility based on version number in the code. But the xml will continuously show me that I have used Text Clock while my min-supported level is 14. So:
Is there a way in the xml to specify to ignore the min-sdk error. Something like
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
That we use in the normal java code.
Is there a way in XML to specify to use a particular widget based on version number. Some kind of if else statement in the xml that says:
if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
//Use text clock
} else {
// Use digital clock
}
you can use the tools target api
<item name="android:fontFamily" tools:targetApi="jelly_bean">sans-serif-light</item>
this will also work
<item name="android:fontFamily" tools:targetApi="17">sans-serif-light</item>
Related
Sometimes, when I want to add SDK 21+ feature to my layout, I need to create whole layout in another file. It's heavy to me because I want to do or check everything in one layout. More of layouts are looking complex and hard to manage. Instead of having two layouts for different SDK versions, can I do something like this:
<ImageView
android:id="#+id/x"
android:layout_width="16dp"
android:layout_height="wrap_content"
<compatibility sdk_higher_than="21">
android:elevation="xdp" //my problem not about the elevation. Its just an example that pops in my mind about the compatibility.
</compatibility>
app:srcCompat="#drawable/ic_x" />
I can make this stuff programmatically but when I should see the view instantly on designer, making it programmatically is not a good way for me. If there is a good practice or idea for this problem can anybody illuminate me?
Yes you can do that by adding tool target API:
First add: <RootTag xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" >
Example:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:targetApi="14" >
or by name: tools:targetApi="jelly_bean"
If you want your layout directries to be use in different versions, name your files as:
/res/layout/layout.xml - (default directries)
/res/layout-v14/layout.xml
/res/layout-v17/layout.xml
Also, if you want to dynamically create element in your code:
You can also use annotations in your java code to make things easy:
First import: import android.annotation.TargetApi;
Then, use this annotation above your method:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
There are more annotation that you can get help:
#RequiresApi(Build.VERSION_CODES.LOLLIPOP)
Above annotation to warn for methods that are used lower API level. Read more about requiresApi: Android API level annotation for Android libraries
Now, inside your method you can dynamically generate views.
Example from the doc:
private void setUpActionBar() {
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Read doc for more details about annotations: https://developer.android.com/studio/write/annotations
You can not give your view elevation in your XML and check for your SDK version in your code - if it's over 21 give the view elevation programmatically.
For example:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
//the API level is above 21 and you can manipulate your view with all the features that available to API level 21+
}
I can change the textview's fonts with using android:fontFamily="#font/test2"
but this is not working on switches, so i created a custom style includes font but still no action. Also I tried with android:textAppearance="#font/test2"
still no action. I can change the font just programmatically. How can I change with using xml attributes?
android:fontFamily atribute in xml is not available for API levels below 16.
For API below 16 use app namespace.
My application will run on api level 9 devices or higher. Now i have a toggle_button on one of the layouts. I want to change this toggle button to switcher if the device is apilevel 14 or higher. How can i implement this? Sorry for my english.
If you are creating the content/pages in xml, simply copy your xml with same name but to the layout-v14 folder. Then change ToggleButton to SwitchButton, and Android will take care of rest :)
If you are coding it in jave, use:
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH){
//Add Switch
}else{
//add toggle
}
And when you'll want to use both (when using defining in xml) with the same java code use:
CompoundButton bt_toggle= (CompoundButton) findViewById(R.id.some_button);
bt_toggle.setChecked(false);
I have found a solution! link
It just adds a support library. And you can use switchers from api-level7!
I am new to Android development and am wondering what happens if you use attributes on XML tags from an API level greater than your minSdkVersion.
For example having:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="20" />
And then using this:
<activity android:logo="#drawable/iconwhatever"></activity>
The "android:logo" attribute is from API level 11.
In Android Studio it gives the following error, but I want to know what could happen if this is left alone:
Attribute "logo" is only used in API level 11 and higher. (Current min is 9)
Any help regarding this would be greatly appreciated.
Unsupported attributes are safely ignored.
From SDK documentation:
When parsing XML resources, Android ignores XML attributes that aren’t supported by the current device. So you can safely use XML attributes that are only supported by newer versions without worrying about older versions breaking when they encounter that code.
For XML attributes, its safe to use attributes from newer APIs (they will simply be ignored as the XML parser won't even look for them on older versions).
In this case, the manipulation of attributes that are different APIs must be made via code and not in XML.
In code, you can treat it.
For example:
if(Build.Version.SDK_INT > 10){
.... use an attribute
}
else{
.... use other
}
For your specific case, use "icon"
<activity android:icon="#drawable/iconwhatever"></activity>
I am starting with Android development. I am trying to apply a different style on a button after a event occurred, programmatically.
I have two xml files with two styles. The first style is set to the button in the activity xml file.
I found a piece of code that works perfectly! This:
btnX1.setBackground(getResources().getDrawable(R.drawable.custom_btn_set1));
However, to use it, I have to switch my android:minSdkVersion="16" from 11.
Is there a way to write this line of code compatible with android:minSdkVersion=11 ?
setBackground was introduced in API level 16, while
setBackgroundResource in API level 1.
Use the API level 1 function Ex:
btnX1.setBackgroundResource(R.drawable.custom_btn_set1);
The whole Android project has extensive documentation, of every single object and XML option.
Try to search Google for things like Android Button and it will take you to the specific developer.android.com page with all methods.
You can use one of these:
public void setBackgroundDrawable (Drawable background)
Added in API level 1
This method was deprecated in API level 16.
use setBackground(Drawable) instead
public void setBackgroundResource (int resid)
Added in API level 1
Set the background to a given resource. The resource should refer to a Drawable object or 0 to remove the background.
Related XML Attributes
android:background
Parameters
resid The identifier of the resource.
Its probably best to just use setBackgroundResource, no use in first making a Drawable from your Resource.
try
btnX1 .setBackgroundResource(R.drawable.custom_btn_set1)
http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int)