Reputation: 2945
I am designing an app with minimum SDK as 9 and target SDK as 18. I want to display a clock on the screen. So I added a DigitalClock to the layout. I got a warning saying that Digital Clock was deprecated in API 17. So I added Text Clock. But TextClock is supported only by API 17 and above. What should I do to make the digital clock appear if SDK found on device is < 17 and make the Text Clock appear if SDK found is >= 17?
I am adding the DitialClock/TextClock through XML and not in the code behnd.
Upvotes: 0
Views: 1488
Reputation: 10673
You have to understand that the term "deprecated" does not mean "unusable." If you plan on maintaining your code in the future, it's not a terrible crime to continue using a deprecated API/widget/whatever because you can go back and fix things later. Deprecated code will eventually become unsupported, but it can be a matter of years before that actually happens.
A more elegant solution, however, would be to add your clock via code. By doing so, you'll be able to branch based on the user's detected API level, like so:
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.HONEYCOMB)
{
// Code for users with API version Honeycomb or greater
}
else
{
// Code for users with API versions prior to Honeycomb
}
You can even provide two layouts and decide which to use with this type of logic.
Upvotes: 2
Reputation: 1996
You can make your custom clockview class and in that class you can identify sdk version and clock accordingly. use your view in your xml then.
Upvotes: 0
Reputation: 2097
If you want to do it in xml you should create two different layouts, one of them will conatin digital clock and another text clock. And then put them in proper folders like: /res/layout-v11 or /res/values-v13
Upvotes: 0