Android - set layout background programmatically - android

I have noticed that the setBackground method for the RelativeLayout object is targeted for API 16 (Android 4.1) and higher, but my application has the target API 8 and I cannot use it.
Is there any alternative solution for this problem (besides marking the class/method with TargetApi(16) or changing the target API in the manifest)?
Thank you!
Edit: Eclipse was buggy and it showed me the same error for setBackgroundDrawable but now it seems to work. Thank you for your help.

Use one of:
.setBackgroundColor(int) (if you're setting it to a color)
.setBackgroundDrawable(Drawable) (if you're setting it to a Drawable type; this is deprecated, and was replaced by .setBackground(Drawable))
.setBackgroundResource(int) (for setting a resource from R.java)
If you use the second one, make sure to do a conditional check on your API version:
if (Build.VERSION.SDK_INT >= 16)
view.setBackground(...);
else
view.setBackgroundDrawable(...);
... and mark it with #TargetApi(16) and #SuppressWarnings("deprecation").

It depends. If you want to set a color as the background, use setBackgroundColor(). For a Drawable, you can use the now deprecated method setBackgroundDrawable() for APIs below 16, and setBackground() for API 16 devices. You can also use setBackgroundResource() for setting a resource as the background.
Note that while a lot of methods are marked as deprecated, I'm yet to come across one that has actually been removed. So while you could use the deprecated method even in API 16, I'd recommend setting your target API to 16 and using an if else to switch between the methods, depending on the API version the device is running.

Use setBackgroundDrawable() instead. It does the same thing, but it's been deprecated since the new setBackground() method.

Related

Why is there a dash across my variables

I am really new to android so pardon me. Why is there a strike-through
for example (fill_parent)
in some android variables and not others
The strikeout shows deprecated methods or attributes (assuming you are using Android Studio / IntelliJ). In your case FILL_PARENT (LayoutParams?) is deprecated for a really long time (API Level 8). It is now called MATCH_PARENT (see https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html). Hovering the variable should show you a tooltip with more information on the deprecation and when it was deprecated. Just make sure you don't use those deprecated elements.

What is the difference between TextView and TextViewCompat

I need to know what actual difference between TextView and TextViewCompat.
When we should use TextViewCompat?
In much the same way as other compatibility classes, it exists to provide backwards compatibility for new functions to older versions of Android.
If you compare the two, you will see the difference.
One such example is getMaxLines(). In an ordinary TextView, this requires SDK level 16. TextViewCompat introduces such functions for SDK levels from 4.
define TextViewCompat in developer.android
A TextView which supports compatible features on older version of the platform, including:
Supports textAllCaps style attribute which works back to Gingerbread.
Allows dynamic tint of it background via the background tint methods in ViewCompat.
Allows setting of the background tint using backgroundTint and backgroundTintMode.

Android - setFocusedMonthDateColor() and setUnfocusedMonthDateColor() are depricated in SDK level 23

setFocusedMonthDateColor() and setUnfocusedMonthDateColor() are depricated from SDK level 23 onward and here is the what Android Docs has to say about the attribute focusedMonthDateColor:
May be a reference to another resource, in the form
"#[+][package:]type:name" or to a theme attribute in the form
"?[package:][type:]name".
May be a color value, in the form of "#rgb", "#argb", "#rrggbb", or
"#aarrggbb".
Constant Value: 16843587 (0x01010343)
What does that even mean? And how am I supposed to color the focused and unfocused months of CalendarView?
A lot of methods in CalendarView have been deprecated in API 23. All of them show:
This method was deprecated in API level 23.
No longer used by Material-style CalendarView.
It seems to me that those methods are mostly meant for customizing the colors of the view. And I therfore think (read: don't know exactly) that they want you to use the - in Android 5 introduced - colorPrimary etc. items. For example, a selected month would probably get the colorAccent.
Give it a try.

ListPopupMenu on pre android 11

I want to know if someone knows a library that contains a back ported implementation for ListPopupMenu that works on pre android 11
I don't but you could use PopupWindow whcich is available since API 1. You can create a list as your contentView. You can use showAsDropDown which could give you the same type of effect
This might be worth checking out. I like to use it for backwards compatibility sometimes for the effect it gives
PopupWindow
showAsDropDown

Android Edittext cursor invisible

I have an edittext in my application that will show the cursor correctly in Froyo or Gingerbread, however in later sdks the cursor is invisible. I have found online the solution is to set the android:textCursorDrawable="#null" so that the edittext will use the font color for the cursor. However, since my minsdkversion is 8, I do not have access to that property.
Is their either a way to selectively add a property based on the sdk you are on? Or possibly a way to work around this so that I can have visible cursors on both older and newer sdk versions?
I don't know your current setup, but one way to use that property is to use different layouts folders based on various android versions. android:textCursorDrawable is a property introduced in API 12 so you could have a layout folder like layout-v12 which will be used where the API is 12 or greater. In the layouts from this folder the use of the property is valid.
For lower versions, Froyo and Gingerbread, you don't have any problems so you would use a default folder, layout. In the end you'll have two layouts folder(containing the same layout files):
layout // don't need the property
layout-v12 // use the property
That leaves out API 11, I don't know if this is a deal breaker for you.
Try setting your targetSdkVersion to API level 13 or higher to gain access to the android:textCursorDrawable property
If you not has a cursor I guess the input is simple and say you only can enter new chars at end of the text?
I think you can use a TextView and to this add you own KeyListener?
But in this case you will has problem with the virtual keyboard not is show, see some of this links:
Forcing the Soft Keyboard open
android force keyboard visible
or create you own keyboard if the rang of valid keys is very limit as only digits.

Categories

Resources