[Android Q&A, Help & Troubleshooting] squimer:
I need a help to create a toggle slide button like the one in the instagram android app. With smaliToJava tool i got the code, but, some methods came very messy.

If we succeed, we may create a github project for everyone who want use this feature too :p //Sorry about the english :P

Smali2Java Code:

package com.instagram.ui.widget.switchbutton;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.view.ViewParent;
import android.widget.CompoundButton;

public class IgSwitch
extends CompoundButton
{
private Drawable a;
private Drawable b;
private Drawable c;
private Rect d = new Rect();
private int e;
private float f;
private int g;
private VelocityTracker h;
private float i;
private float j;
private int k;
private int l;
private int m;
private boolean n;

public IgSwitch(Context paramContext)
{
super(paramContext);
a();
}

public IgSwitch(Context paramContext, AttributeSet paramAttributeSet)
{
super(paramContext, paramAttributeSet);
a();
}

public IgSwitch(Context paramContext, AttributeSet paramAttributeSet, int paramInt)
{
super(paramContext, paramAttributeSet, paramInt);
a();
}

private void a()
{
setClickable(true);
ViewConfiguration localViewConfiguration = ViewConfiguration.get(getContext());
this.l = localViewConfiguration.getScaledTouchSlop();
this.m = localViewConfiguration.getScaledMinimumFlingVelocity();
this.a = getResources().getDrawable(R.drawable.toggle);
this.b = getResources().getDrawable(R.drawable.toggle_active);
this.c = getResources().getDrawable(R.drawabletoggle_nub);
this.e = this.c.getIntrinsicWidth();
this.g = (this.a.getIntrinsicWidth() - this.e);
}

private void a(MotionEvent paramMotionEvent)
{
MotionEvent localMotionEvent = MotionEvent.obtain(paramMotionEvent);
localMotionEvent.setAction(3);
super.onTouchEvent(localMotionEvent);
localMotionEvent.recycle();
}

private void a(boolean paramBoolean)
{
this.n = true;
setChecked(paramBoolean);
}

private boolean a(float paramFloat1, float paramFloat2)
{
int i1 = getPaddingTop() - this.l;
int i2 = (int)(getPaddingLeft() + (0.5F + this.f) - this.l);
int i3 = i2 + this.e + 2 * this.l;
int i4 = i1 + this.c.getIntrinsicHeight() + 2 * this.l;
return (paramFloat1 > i2) && (paramFloat1 < i3) && (paramFloat2 > i1) && (paramFloat2 < i4);
}

[COLOR="Red"] private void b(MotionEvent paramMotionEvent)
{
int i1 = 1;
this.k = 0;
if ((paramMotionEvent.getAction() == i1) && (isEnabled()))
{
int i2 = i1;
a(paramMotionEvent);
if (i2 == 0) {
break label97;
}
this.h.computeCurrentVelocity(1000);
float f1 = this.h.getXVelocity();
if (Math.abs(f1) <= this.m) {
break label89;
}
if (f1 <= 0.0F) {
break label84;
}
}
for (;;)
{
a(i1);
return;
int i3 = 0;
break;
label84:
boolean bool = false;
continue;
label89:
bool = getTargetCheckedState();
}
label97:
a(isChecked());
}[/COLOR]

private boolean getTargetCheckedState()
{
return this.f >= this.g / 2;
}

protected void onDetachedFromWindow()
{
super.onDetachedFromWindow();
if (this.h != null)
{
this.h.recycle();
this.h = null;
}
}

protected void onDraw(Canvas paramCanvas)
{
super.onDraw(paramCanvas);
float f1 = this.f / this.g;
this.b.setAlpha((int)(f1 * 255.0F));
this.a.draw(paramCanvas);
this.b.draw(paramCanvas);
int i1 = (int)(0.5F + this.f);
this.c.setBounds(i1 + getPaddingLeft(), getPaddingTop(), i1 + this.e + getPaddingLeft(), getPaddingTop() + this.c.getIntrinsicHeight());
this.c.draw(paramCanvas);
}

[COLOR="Red"] protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
{
super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
this.d.set(getPaddingLeft(), getPaddingTop(), this.a.getIntrinsicWidth() + getPaddingLeft(), this.a.getIntrinsicHeight() + getPaddingTop());
this.a.setBounds(this.d);
this.b.setBounds(this.d);
if (isChecked()) {}
for (float f1 = this.g;; f1 = 0.0F)
{
this.f = f1;
return;
}
}[/COLOR]

protected void onMeasure(int paramInt1, int paramInt2)
{
setMeasuredDimension(this.b.getIntrinsicWidth() + getPaddingLeft() + getPaddingRight(), this.b.getIntrinsicHeight() + getPaddingTop() + getPaddingBottom());
}

[COLOR="red"] public boolean onTouchEvent(MotionEvent paramMotionEvent)
{
int i1 = 1;
if (this.h == null) {
this.h = VelocityTracker.obtain();
}
this.h.addMovement(paramMotionEvent);
switch (paramMotionEvent.getActionMasked())
{
}
for (;;)
{
i1 = super.onTouchEvent(paramMotionEvent);
float f1;
float f2;
do
{
return i1;
float f5 = paramMotionEvent.getX();
float f6 = paramMotionEvent.getY();
if ((!isEnabled()) || (!a(f5, f6))) {
break;
}
this.k = i1;
this.i = f5;
this.j = f6;
break;
switch (this.k)
{
case 0:
default:
break;
case 1:
float f3 = paramMotionEvent.getX();
float f4 = paramMotionEvent.getY();
if ((Math.abs(f3 - this.i) <= this.l) && (Math.abs(f4 - this.j) <= this.l)) {
break;
}
this.k = 2;
getParent().requestDisallowInterceptTouchEvent(i1);
this.i = f3;
this.j = f4;
return i1;
case 2:
f1 = paramMotionEvent.getX();
f2 = Math.max(0.0F, Math.min(f1 - this.i + this.f, this.g));
}
} while (f2 == this.f);
this.f = f2;
this.i = f1;
invalidate();
return i1;
if (this.k == 2)
{
b(paramMotionEvent);
return i1;
}
this.k = 0;
this.h.clear();
this.h.recycle();
this.h = null;
}
}[/COLOR]

public boolean performClick()
{
this.n = true;
return super.performClick();
}

[COLOR="Red"] public void setChecked(boolean paramBoolean)
{
super.setChecked(paramBoolean);
int i1;
if (paramBoolean)
{
i1 = this.g;
if ((!this.n) || (getWindowToken() == null)) {
break label62;
}
clearAnimation();
startAnimation(new b(this, this.f, i1, null));
}
for (;;)
{
this.n = false;
return;
i1 = 0;
break;
label62:
this.f = i1;
invalidate();
}
}[/COLOR]
}