Implementing (switch\toggle button) depending on API level - android

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!

Related

Inline sdk version check in Android XML?

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+
}

change FloatingActionButton and SubActionButton background color

Is it possible to change the background color of android Floating Action Button and Sub ActionButton when using min APi 16?
I have tried a lot of different situations like:
actionButton.setBackgroundTintList(getResources().getColorStateList(R.color.myColor));
But all say that the min API level is 16 and required is 21.
Is there a way to do it in API level 16 programmatically in the Java file?
To Change the floating action bar color, just write this in your xml
app:backgroundTint="#color/desired_color"
and in java is
actionButton.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(context, your color)));
I think you can use
android:backgroundTint="#color/white"

Android Studio Right to Left ListView

I've been trying to make a ListView to work as RTL (Right to Left).
I've added the following line in the ListView and LinearLayout properties:
android:layoutDirection="rtl"
and it still shows the list from left to right.
Any ideas?
Thank you all but I solved it with:
android:textDirection="rtl"
I've added it to the ListView and the layouts, and it worked. Next time you should try considering using this too for RTL layouts.
To take advantage of RTL layout mirroring, simply make the following changes to your app:
Declare in your app manifest that your app supports RTL mirroring.
Specifically, add android:supportsRtl="true" to the <application> element in your manifest file.
Change all of your app's left/right layout properties to new start/end equivalents.
If you are targeting your app to Android 4.2 (the app's
targetSdkVersion or minSdkVersion is 17 or higher), then you should
use start and end instead of left and right. For example,
android:paddingLeft should become android:paddingStart.
If you want your app to work with versions earlier than Android 4.2
(the app's targetSdkVersion or minSdkVersion is 16 or less), then you
should add start and end in addition to left and right. For
example, you’d use both android:paddingLeft and android:paddingStart.
RTL layout support feature is supported on Android 4.2(API level 17) or above only . Please Add android:layout_gravity="left/right" in your parent Layout . And also allow android:textAlignment.
Set minSdkVersion=17
Please read http://developer.android.com/reference/android/view/View.html#attr_android:layoutDirection
This will automatically adjust according to the device locale
android:textDirection="locale"

Android - switch between button styles

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)

android - how can i use the “android:layoutDirection”? - again

can someone help me how can i use this attribute android:layoutDirection ,
and please don't refer me to this question What is the status of Right To Left languages on Android? because it doesn't answer my question!
layoutDirection is not be published in 4.0,so you cannot use the attr,but if you not be using sdk,but build with all code of android.
first get the Linearlayout in your code when it created,normally after setContent,and using #hide api setLayoutDiredtion(View.LAYOUT_DIRECTION_LOCALE),which define in View.java
,of course,after 4.2 you can use layoutDirection in xml directly

Categories

Resources