1

I have two layouts one for before API level-14 and the other is for what's after. I need to use toggle button in before level-14 API and a switch button for the one after level-14.

The problem is that I want to use both with the same id in R.id.button1 or Do I have to make each one with a different id and check inside the Java code the version of API running so I find the view by id that works for the API. And if so how can I do that please provide a sample of code. Thanks for your time.

These are the XML files

layout-v14:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <Switch
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp" />

</RelativeLayout>

Ordinary layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

    <ToggleButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp" />

</RelativeLayout>

The Java code

CompoundButton button = (CompoundButton) findViewById(R.id.button1);
Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45

2 Answers2

1

Both of them extends CompoundButton, so you can use the same ID, but if you want the same code, you have to limit your useness to CompoundButton class.

Edit

CompoundButton button = (CompoundButton) findViewById(R.id.button1);
button.setChecked(checked);
code_burgar
  • 12,025
  • 4
  • 35
  • 53
Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
1

You can use Build.VERSION for getting the api-level Programmatically.

Edit

if(Build.VERSION_CODES.ICE_CREAM_SANDWICH == Build.VERSION.SDK_INT);
    ((Switch)v).switchMethodSpecefic();
Khaled Annajar
  • 15,542
  • 5
  • 34
  • 45
Rahul
  • 44,383
  • 11
  • 84
  • 103