you require to create first one drawable xml file witch specify your switch background.
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false" >
<size
android:height="48dip"
android:width="48dip" />
<gradient
android:angle="270"
android:centerColor="#806CA5BC"
android:centerX="0.50"
android:centerY="0.50"
android:endColor="#66C2E0"
android:startColor="#517C8D"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
After Create this drawable file you require to create layout for switch.
<RelativeLayout
android:id="@+id/layEmailsub1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/laySwitchEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/switch_off_"></RelativeLayout> //one layout with you switch initial background
<SeekBar //create seek bar require for drags thumb
android:id="@+id/switchEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/laySwitchEmail"
android:layout_alignLeft="@id/laySwitchEmail"
android:layout_alignRight="@id/laySwitchEmail"
android:layout_alignTop="@id/laySwitchEmail"
android:layout_centerInParent="true"
android:indeterminate="false"
android:max="100"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:progress="0"
android:progressDrawable="@drawable/styled_progress_swich" // specify drawable file witch is initialy we created
android:thumb="@drawable/white_circle" /> //hear specify thumb of switch
</RelativeLayout>
And After put this code in your class file
//create following variable
SeekBar switchEmail;
RelativeLayout laySwitchEmail;
Boolean alertScribe = null;
//initialise above variable
switchEmail = (SeekBar) findViewById(R.id.switchEmail);
laySwitchEmail = (RelativeLayout) findViewById(R.id.laySwitchEmail);
//after all this set one switchEmail seek bar change listener like following
switchEmail.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int prograssvalue = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
prograssvalue = progress;
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (prograssvalue <= 50) {
swichOpt(1, 0);
} else {
swichOpt(1, 1);
}
}
});
// method for perform switch operation enable or disable
private void swichOpt(int switchid, int val) {
switch (switchid) {
case 1:
if (val == 0) {
switchEmail.setProgress(0);
laySwitchEmail.setBackgroundResource(R.drawable.switch_off_);
} else {
switchEmail.setProgress(100);
laySwitchEmail.setBackgroundResource(R.drawable.switch_on_);
}
break;
}
}
No comments:
Post a Comment