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)
Related
I need to reuse XML layout and change button color programmatically.
In Android 5 applying app:backgroundTint in XML changes button color but I need to do it programmatically and I do it in Recyclerview:
holder.button.supportBackgroundTintList = ContextCompat.getColorStateList(context, backgroundColorRes)
This has no effect.
The setSupportBackgroundTintList() method is annotated with #RestrictTo({Scope.LIBRARY_GROUP}), which means you are not supposed to call it directly. Instead, you should use ViewCompat.setBackgroundTintList().
Try changing your code to this instead:
val colorStateList = ContextCompat.getColorStateList(context, backgroundColorRes)
ViewCompat.setBackgroundTintList(holder.button, colorStateList)
If you look at the source code for ViewCompat.setBackgroundTintList(), you'll see that it does different things for API 21+ (Android 5 and higher) than earlier versions. Chances are good that the "support" background tint is only applied on earlier versions of Android, and ViewCompat will make it so that you don't have to think about that.
I'm trying to change the checkbox box's color programmatically to a different color than the theme's default. The problem is I was doing something like this:
checkbox.setSupportButtonTintList(ColorStateList);
It works but it seems, according its class documentation, this method has been restricted to be used only by classes from the same package (com.android.support). This is the warning I got from Android Studio:
AppCompatCheckBox.setSupportButtonTintList can only be called from within the same library group (groupId=com.android.support)
Is there a standard/correct way of doing this for all API levels?
Finally, found the answer here from one of the Google guys: https://code.google.com/p/android/issues/detail?id=202235. I was right about not using:
checkbox.setSupportButtonTintList(ColorStateList);
It seems is a private API. Instead, you have to use:
CompoundButtonCompat.setButtonTintList(checkbox, colorStateList);
Based on rylexr answer, you can specify the color in the following way:
CompoundButtonCompat.setButtonTintList(checkboxView, ColorStateList
.valueOf(getResources().getColor(R.color.red)));
chxAll.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC0000")));
chxAll is a object of android.widget.CheckBox
replace the hexacolor code for desigred color
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.
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 know that you should be able to get the primary color of current theme by calling
getContext().getTheme().resolveAttribute(R.attr.colorPrimary, typedValue, true);
int color = typedValue.data;
However, even with AppCompat v7:21 this doesn't seem to work in pre 21 devices ("No such static field" at runtime). However if I try to declare such an attribute somewhere it says that it is already defined while compiling (the file it says to contain a definition doesn't contain such). So how on earth does on get access to themes color values from code (not in the layout files)?
Actually I was just being plain stupid. In order to use R.attr.colorPrimary in pre 21.
You cannot use android.R but rather the resources of your application.