Sunday 29 June 2014

Custom EditText With change hint font style ,text font style and password character change.


This  is my CustomEditText Class and its attr.xml

public class CustomEditText extends EditText implements TextWatcher {
private Typeface tf = null, tfhint = null;
private String customhintfont, customFont;
boolean inputtypepassword;
private CharSequence chartype;

// private CharSequence mSource;

public CustomEditText(Context context) {
super(context);
// TODO Auto-generated constructor stub
// initViews();
}

public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
// initViews();
setCustomFontEdittext(context, attrs);

}

public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
// initViews();
setCustomFontEdittext(context, attrs);
}

private void setCustomFontEdittext(Context ctx, AttributeSet attrs) {
final TypedArray a = ctx.obtainStyledAttributes(attrs,
R.styleable.CustomEditText);
customFont = a.getString(R.styleable.CustomEditText_edittextfont);
customhintfont = a
.getString(R.styleable.CustomEditText_edittextfontHint);

// custompwd = a.getString(R.styleable.CustomEditText_edittextpwd);
inputtypepassword = a.getBoolean(
R.styleable.CustomEditText_edittextpwd, false);
setCustomFontEdittext(ctx, customFont);
setCustomFontEdittextHint(ctx, customhintfont);

chartype = (CharSequence) a
.getText(R.styleable.CustomEditText_editcharpwd);
setCustompwd(inputtypepassword);
a.recycle();
}

public boolean setCustompwd(boolean pwd) {
if (pwd) {
this.setTransformationMethod(new PasswordCharacterChange());
}
return pwd;
}

public boolean setCustomFontEdittext(Context ctx, String asset) {
try {
tf = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
return false;
}
setTypeface(tf);
return true;
}

public boolean setCustomFontEdittextHint(Context ctx, String asset) {
try {
tfhint = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
return false;
}
setTypeface(tfhint);

return true;
}

@Override
public void onTextChanged(CharSequence text, int start, int lengthBefore,
int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
if (text.length() < 0) {
setCustomFontEdittextHint(getContext(), customhintfont);
} else {
setCustomFontEdittextHint(getContext(), customFont);
}
if (TextUtils.isEmpty(text)) {
setCustomFontEdittextHint(getContext(), customhintfont);
}
// this.setText("*");
}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

public class PasswordCharacterChange extends PasswordTransformationMethod {

@Override
public CharSequence getTransformation(CharSequence source, View view) {
// TODO Auto-generated method stub
return new PasswordCharSequence(source);
}

private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;

public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}

public char charAt(int index) {
return chartype.charAt(0); // This is the important part
}

public int length() {
return mSource.length(); // Return default
}

public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
}

}


2) attrs.xml


 <declare-styleable name="CustomEditText">
        <attr name="edittextfont" format="string" />
        <attr name="edittextfontHint" format="string" />
        <attr name="edittextpwd" format="string" />
        <attr name="editcharpwd" format="string" />
    </declare-styleable>

3) activity_main.xml




<com.customdemo.CustomEditText
        android:id="@+id/activity_main_edit_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       app:edittextfont="Calibri Bold Italic.ttf"
        app:edittextfontHint="beatmygu.ttf"/>




<com.customdemo.CustomEditText
        android:id="@+id/activity_main_edit_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:editcharpwd="$"
        app:edittextpwd="true"
       />




Wednesday 25 June 2014

Fragment Transaction above android 4.0 using ObjectAnimator




1) First create Custom Class for container.

public class SlidingRelativeLayout extends RelativeLayout {
    private float xFraction = 0;

    public SlidingRelativeLayout(Context context) {
        super(context);
    }

    public SlidingRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SlidingRelativeLayout(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    private ViewTreeObserver.OnPreDrawListener preDrawListener = null;


    public void setXFraction(float fraction) {
        this.xFraction = fraction;
        if (getWidth() == 0) {
            if (preDrawListener == null) {
                preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
                    @Override
                    public boolean onPreDraw() {
                        getViewTreeObserver().removeOnPreDrawListener(
                                preDrawListener);
                        setXFraction(xFraction);
                        return true;
                    }
                };
                getViewTreeObserver().addOnPreDrawListener(preDrawListener);
            }
            return;
        }
        float translationX = getWidth() * fraction;
        setTranslationX(translationX);
    }
}

2) Four animator files  require.

 1) animator/slide_fragment_left_in.xml

         <objectAnimator
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                 android:propertyName="XFraction"
                 android:valueType="floatType"
                 android:valueFrom="-1"
                 android:valueTo="0"
                 android:duration="@android:integer/config_mediumAnimTime"/>

 2)animator/slide_fragment_left_out.xml
          <objectAnimator
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                 android:propertyName="XFraction"
                 android:valueType="floatType"
                 android:valueFrom="0"
                 android:valueTo="-1"
                 android:duration="@android:integer/config_mediumAnimTime"/>

 3)animator/slide_fragment_right_in.xml
            <objectAnimator
                      xmlns:android="http://schemas.android.com/apk/res/android"
                      android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                      android:propertyName="XFraction"
                      android:valueType="floatType"
                      android:valueFrom="1"
                      android:valueTo="0"
                      android:duration="@android:integer/config_mediumAnimTime"/>

 4)animator/slide_fragment_right_out.xml
              <objectAnimator
                     xmlns:android="http://schemas.android.com/apk/res/android"
                     android:interpolator="@android:anim/accelerate_decelerate_interpolator"
                     android:propertyName="XFraction"
                     android:valueType="floatType"
                     android:valueFrom="0"
                     android:valueTo="1"
                     android:duration="@android:integer/config_mediumAnimTime"/>
            

3) Container, activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <com.example.slidingdemo.SlidingRelativeLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </com.example.slidingdemo.SlidingRelativeLayout>

</RelativeLayout>

4) fragment_first.xml.


<com.example.slidingdemo.SlidingRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_next"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="NEXT" />

</com.example.slidingdemo.SlidingRelativeLayout>


5) fragment_second.xml

<com.example.slidingdemo.SlidingRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_back"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Back" />

</com.example.slidingdemo.SlidingRelativeLayout>


6) Now Only setCustomAnimation use in fragmenttransaction and set above animator xml file. 


Like:-

final FragmentManager fragmentManager = getFragmentManager();
                        final FragmentTransaction fragmentTransaction = fragmentManager
                                .beginTransaction();
                        fragmentTransaction.setCustomAnimations(
                                R.animator.slide_fragment_right_in,
                                R.animator.slide_fragment_left_out,
                                R.animator.slide_fragment_left_in,
                                R.animator.slide_fragment_right_out);
                        final SecondFragment firstFragment = new SecondFragment();
                        fragmentTransaction.replace(R.id.container,
                                firstFragment, firstFragment.getClass()
                                        .getSimpleName());
                        fragmentTransaction.addToBackStack(firstFragment
                                .getClass().getSimpleName());
                        fragmentTransaction.commit();