Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday 18 February 2016

CircularProgressbar sample

Hello gyes
Today we are learn very interesting component like animated CircularProgressbar so are you ready to learn how to create  ???
So lets start.

  See live sample                                                                                   




Create a new project in android studio and follow give all instructions.

Create a new java class by name CircularProgressBar.java

CircularProgressBar.java


package com.samset.circularprogressbar.custom_views.circularprogressbar;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ProgressBar;

import com.samset.circularprogressbar.R;


public class CircularProgressBar extends ProgressBar{
private static final String TAG = "CircularProgressBar";

private static final int STROKE_WIDTH = 20;

private String mTitle = "";
private String mSubTitle = "";
private int Titlesize = 0;

private int mStrokeWidth = STROKE_WIDTH;

private final RectF mCircleBounds = new RectF();

private final Paint mProgressColorPaint = new Paint();
private final Paint mBackgroundColorPaint = new Paint();
private final Paint mTitlePaint = new Paint();
private final Paint mSubtitlePaint = new Paint();

private boolean mHasShadow = true;
private int mShadowColor = Color.BLACK;

public interface ProgressAnimationListener{
public void onAnimationStart();
public void onAnimationFinish();
public void onAnimationProgress(int progress);
}

public CircularProgressBar(Context context) {
super(context);
init(null, 0);
}

public CircularProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}

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

public void init(AttributeSet attrs, int style){
//so that shadow shows up properly for lines and arcs
setLayerType(View.LAYER_TYPE_SOFTWARE, null);

TypedArray a = getContext().obtainStyledAttributes(attrs,
R.styleable.CircularProgressBar, style, 0);

String color;
Resources res = getResources();

this.mHasShadow = a.getBoolean(R.styleable.CircularProgressBar_cpb_hasShadow, true);

color = a.getString(R.styleable.CircularProgressBar_cpb_progressColor);
if(color==null)
mProgressColorPaint.setColor(res.getColor(R.color.circular_progress_default_progress));
else
mProgressColorPaint
.setColor(Color.parseColor(color));

color = a.getString(R.styleable.CircularProgressBar_cpb_backgroundColor);
if(color==null)
mBackgroundColorPaint.setColor(res.getColor(R.color.circular_progress_default_background));
else
mBackgroundColorPaint
.setColor(Color.parseColor(color));

color = a.getString(R.styleable.CircularProgressBar_cpb_titleColor);
if(color==null)
mTitlePaint.setColor(res.getColor(R.color.circular_progress_default_title));
else
mTitlePaint
.setColor(Color.parseColor(color));

color = a.getString(R.styleable.CircularProgressBar_cpb_subtitleColor);
if(color==null)
mSubtitlePaint.setColor(res.getColor(R.color.circular_progress_default_subtitle));
else
mSubtitlePaint
.setColor(Color.parseColor(color));


String t = a.getString(R.styleable.CircularProgressBar_cpb_title);
if(t!=null)
mTitle = t;

t = a.getString(R.styleable.CircularProgressBar_cpb_subtitle);
if(t!=null)
mSubTitle = t;

mStrokeWidth = a.getInt(R.styleable.CircularProgressBar_cpb_strokeWidth, STROKE_WIDTH);

a.recycle();


mProgressColorPaint.setAntiAlias(true);
mProgressColorPaint.setStyle(Style.STROKE);
mProgressColorPaint.setStrokeWidth(mStrokeWidth);

mBackgroundColorPaint.setAntiAlias(true);
mBackgroundColorPaint.setStyle(Style.STROKE);
mBackgroundColorPaint.setStrokeWidth(mStrokeWidth);

if (Titlesize==0)
{
mTitlePaint.setTextSize(40);
}else
{
mTitlePaint.setTextSize(Titlesize);
}
mTitlePaint.setStyle(Style.FILL);
mTitlePaint.setAntiAlias(true);
mTitlePaint.setTypeface(Typeface.create("Roboto-Thin", Typeface.NORMAL));
mTitlePaint.setShadowLayer(0.1f, 0, 1, Color.GRAY);

mSubtitlePaint.setTextSize(20);
mSubtitlePaint.setStyle(Style.FILL);
mSubtitlePaint.setAntiAlias(true);
mSubtitlePaint.setTypeface(Typeface.create("Roboto-Thin", Typeface.BOLD));
// mSubtitlePaint.setShadowLayer(0.1f, 0, 1, Color.GRAY);
}

@Override
protected synchronized void onDraw(Canvas canvas) {
canvas.drawArc(mCircleBounds, 0, 360 , false, mBackgroundColorPaint);

int prog = getProgress();
float scale = getMax() > 0 ? (float)prog/getMax() *360: 0;

if(mHasShadow)
mProgressColorPaint.setShadowLayer( 3, 0, 1, mShadowColor);
canvas.drawArc(mCircleBounds, 270, scale, false, mProgressColorPaint);


if(!TextUtils.isEmpty(mTitle)){
int xPos =  (int)(getMeasuredWidth()/2 - mTitlePaint.measureText(mTitle) / 2);
int yPos = (int) (getMeasuredHeight()/2);

float titleHeight = Math.abs(mTitlePaint.descent() + mTitlePaint.ascent());
if(TextUtils.isEmpty(mSubTitle)){
yPos += titleHeight/2;
}
canvas.drawText(mTitle, xPos, yPos, mTitlePaint);

yPos += titleHeight;
xPos = (int)(getMeasuredWidth()/2 - mSubtitlePaint.measureText(mSubTitle) / 2);

canvas.drawText(mSubTitle, xPos, yPos, mSubtitlePaint);
}

super.onDraw(canvas);
}

@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
final int min = Math.min(width, height);
setMeasuredDimension(min + 2 * STROKE_WIDTH, min + 2 * STROKE_WIDTH);

mCircleBounds.set(STROKE_WIDTH, STROKE_WIDTH, min + STROKE_WIDTH, min + STROKE_WIDTH);
}

@Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);

// the setProgress super will not change the details of the progress bar
// anymore so we need to force an update to redraw the progress bar
invalidate();
}

public void animateProgressTo(final int start, final int end, final ProgressAnimationListener listener){
if(start!=0)
setProgress(start);

final ObjectAnimator progressBarAnimator = ObjectAnimator.ofFloat(this, "animateProgress", start, end);
progressBarAnimator.setDuration(1500);
// progressBarAnimator.setInterpolator(new AnticipateOvershootInterpolator(2f, 1.5f));
progressBarAnimator.setInterpolator(new LinearInterpolator());

progressBarAnimator.addListener(new AnimatorListener() {
@Override
public void onAnimationCancel(final Animator animation) {
}

@Override
public void onAnimationEnd(final Animator animation) {
CircularProgressBar.this.setProgress(end);
if (listener != null)
listener.onAnimationFinish();
}

@Override
public void onAnimationRepeat(final Animator animation) {
}

@Override
public void onAnimationStart(final Animator animation) {
if (listener != null)
listener.onAnimationStart();
}
});

progressBarAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(final ValueAnimator animation) {
int progress = ((Float) animation.getAnimatedValue()).intValue();
if(progress!=CircularProgressBar.this.getProgress()){
Log.d(TAG, progress + "");
CircularProgressBar.this.setProgress(progress);
if(listener!=null)
listener.onAnimationProgress(progress);
}
}
});
progressBarAnimator.start();
}

public synchronized void setTitle(String title){
this.mTitle = title;
invalidate();
}
public synchronized void setTitleSize(int size){
this.Titlesize = size;
invalidate();
}


public synchronized void setSubTitle(String subtitle){
this.mSubTitle = subtitle;
invalidate();
}

public synchronized void setSubTitleColor(int color){
mSubtitlePaint.setColor(color);
invalidate();
}

public synchronized void setTitleColor(int color){
mTitlePaint.setColor(color);
invalidate();
}
public synchronized void setProgressbarColor(int color){
mProgressColorPaint.setColor(color);
invalidate();
}
public synchronized void setProgressbarBackgroundColor(int color){
mBackgroundColorPaint.setColor(color);
invalidate();
}

public synchronized void setHasShadow(boolean flag){
this.mHasShadow = flag;
invalidate();
}

public synchronized void setShadow(int color){
this.mShadowColor = color;
invalidate();
}



public String getTitle(){
return mTitle;
}

public boolean getHasShadow(){
return mHasShadow;
}
}


Go to res folder and go to values folder and create a resource file by name attrs.xml

attrs.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CircularProgressBar">
        <attr name="cpb_hasShadow" format="boolean"/>
        <attr name="cpb_progressColor" format="string"/>
        <attr name="cpb_backgroundColor" format="string"/>
        <attr name="cpb_title" format="string"/>
        <attr name="cpb_titleColor" format="string"/>
        <attr name="cpb_subtitle" format="string"/>
        <attr name="cpb_subtitleColor" format="string"/>
        <attr name="cpb_strokeWidth" format="integer"/>
    </declare-styleable>
</resources>

Create a color file a same folder

color.xml


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="pb_background">#09cbd0</color>

    <!--Circularprogressbar start-->
   
<color name="circular_progress_default_progress">#157e78</color>
    <color name="circular_progress_default_background">#787878</color>
    <color name="circular_progress_default_title">#09cbd0</color>
    <color name="circular_progress_default_subtitle">#D1D1D1</color>
    <!--Circularprogressbar end-->
</resources>


Now you open style resource file and paste below code carefully

style.xml


<resources>
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
   
</style>
    <!-- Base application theme. -->
   
<style name="Theme.AppCompat" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
       
<item name="android:windowContentOverlay">@null</item>
        <item name="android:windowActionBarOverlay">true</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowIsTranslucent">true</item>
    </style>
    <!-- Circularprogressbar start-->
   
<style name="Widget"></style>
    <style name="Widget.ProgressBar.CircularProgressBar" parent="Widget">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:adjustViewBounds">true</item>
        <item name="cpb_progressColor">@color/circular_progress_default_progress</item>
        <item name="cpb_hasShadow">true</item>
        <item name="cpb_strokeWidth">20</item>
        <item name="cpb_titleColor">@color/circular_progress_default_title</item>
        <item name="cpb_subtitleColor">@color/circular_progress_default_subtitle</item>
    </style>

    <style name="Widget.ProgressBar.Holo.CircularProgressBar" parent="Widget.ProgressBar.CircularProgressBar">
        <item name="cpb_hasShadow">true</item>
        <item name="cpb_strokeWidth">20</item>
    </style>
    <!-- for your custom use item-->
   
<style name="Widget.ProgressBar.Holo.CircularPro1" parent="Widget.ProgressBar.CircularProgressBar">
        <item name="cpb_hasShadow">false</item>
        <item name="cpb_strokeWidth">5</item>
    </style>
    <style name="Widget.ProgressBar.Holo.CircularPro2" parent="Widget.ProgressBar.CircularProgressBar">
        <item name="cpb_hasShadow">true</item>
        <item name="cpb_strokeWidth">10</item>
    </style>
    <!-- Circularprogressbar end-->
</resources>


Now you create drwable file in drawable folder by name

timer_shape.xml


<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- "background shadow" -->
   
<item>
        <shape  android:shape="oval" >
            <solid android:color="#e5e5e5" />

            <size
                android:height="95dp"
                android:width="95dp"
/>
        </shape>
    </item>
    <item android:bottom="3px"
        android:left="3px"
        android:right="3px"
        android:top="3px"
>
        <shape  android:shape="oval" >
            <solid android:color="#fff" />

            <size
                android:height="50dp"
                android:width="50dp"
/>
        </shape>
    </item>

</layer-list>

And last you go to activity_main.xml and paste code

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:circular="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3d3d3d"
    android:orientation="vertical"
    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"
>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
>

        <com.samset.circularprogressbar.custom_views.circularprogressbar.CircularProgressBar
            android:id="@+id/circularprogressbar1"
            style="@style/Widget.ProgressBar.Holo.CircularProgressBar"
            android:layout_width="120dip"
            android:layout_height="120dip"
            android:layout_marginTop="10dip"
            circular:cpb_subtitle="subtitle"
            circular:cpb_title="Title"
/>

        <com.samset.circularprogressbar.custom_views.circularprogressbar.CircularProgressBar
            android:id="@+id/circularprogressbar2"
            style="@style/Widget.ProgressBar.Holo.CircularProgressBar"
            android:layout_width="110dip"
            android:layout_height="110dip"
            android:layout_marginTop="10dip"
            circular:cpb_subtitle="subtitle"
            circular:cpb_title="Title"
/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
>

        <com.samset.circularprogressbar.custom_views.circularprogressbar.CircularProgressBar
            android:id="@+id/circularprogressbar3"
            style="@style/Widget.ProgressBar.CircularProgressBar"
            android:layout_width="90dip"
            android:layout_height="90dip"
            android:layout_marginTop="10dip"
            circular:cpb_subtitle="subtitle"
            circular:cpb_title="Title"
/>

        <com.samset.circularprogressbar.custom_views.circularprogressbar.CircularProgressBar
            android:id="@+id/circularprogressbar4"
            style="@style/Widget.ProgressBar.CircularProgressBar"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginTop="10dip"
            circular:cpb_subtitle="subtitle"
            circular:cpb_title="Title"
/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
>

        <com.samset.circularprogressbar.custom_views.circularprogressbar.CircularProgressBar
            android:id="@+id/circularprogressbar5"
            style="@style/Widget.ProgressBar.Holo.CircularPro1"
            android:layout_width="90dip"
            android:layout_height="90dip"
            android:layout_marginTop="10dip"
            android:layout_weight="1"
            circular:cpb_subtitle="subtitle"
            circular:cpb_title="Title"
/>


    </LinearLayout>

    <!-- If you want to create some custom views then see code-->
   
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
>

        <com.samset.circularprogressbar.custom_views.circularprogressbar.CircularProgressBar
            android:id="@+id/circularprogressbar6"
            style="@style/Widget.ProgressBar.Holo.CircularPro2"
            android:layout_width="100dip"
            android:layout_height="100dip"
            android:layout_marginTop="10dip"
            android:layout_centerInParent="true"
            android:layout_weight="1"
            circular:cpb_subtitle="subtitle"
            circular:cpb_title="Title"
/>

        <RelativeLayout
            android:id="@+id/layoutHQ"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@drawable/timer_shape"
            android:gravity="center"
>

            <TextView
                android:id="@+id/txtresult"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="5dp"
                android:text="0"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/colorAccent"
                android:textStyle="bold"
/>

        </RelativeLayout>
    </RelativeLayout>
</LinearLayout>



MainActivity.java



/*
 * Copyright (C) 2016 Sanjay singh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.samset.circularprogressbar;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.samset.circularprogressbar.custom_views.circularprogressbar.CircularProgressBar;

public class MainActivity extends AppCompatActivity {
    private CircularProgressBar c1;
    private CircularProgressBar c2;
    private CircularProgressBar c3;
    private CircularProgressBar c4;
    private CircularProgressBar c5;
    private CircularProgressBar c6;

    private TextView tv_result;
    private MyCountDownTimer countDownTimer;
    private int progress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_result= (TextView) findViewById(R.id.txtresult);
        c1();
        c2();
        c3();
        c4();
        c5();
        c6();

        countDownTimer = new MyCountDownTimer(10000, 100);
        countDownTimer.start();

    }

    private void c1() {
        c1 = (CircularProgressBar) findViewById(R.id.circularprogressbar1);
        c1.setProgress(45);

    }

    private void c2() {
        c2 = (CircularProgressBar) findViewById(R.id.circularprogressbar2);
        c2.animateProgressTo(0, 77, new CircularProgressBar.ProgressAnimationListener() {
            @Override
            public void onAnimationStart() {

            }

            @Override
            public void onAnimationFinish() {
                c2.setSubTitle("done");
            }

            @Override
            public void onAnimationProgress(int progress) {
                c2.setTitle(progress + "%");
            }
        });

    }

    private void c3() {
        c3 = (CircularProgressBar) findViewById(R.id.circularprogressbar3);
        c3.setTitle("Sam");
        c3.setSubTitle("set");
        c2.setProgress(34);
    }

    private void c4() {
        c4 = (CircularProgressBar) findViewById(R.id.circularprogressbar4);
        c4.setProgress(58);
    }

    private void c5() {
        c5 = (CircularProgressBar) findViewById(R.id.circularprogressbar5);
        c4.setProgress(15);
    }

    private void c6() {
        c6 = (CircularProgressBar) findViewById(R.id.circularprogressbar6);
    }

    public class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {


            progress = (int) (millisUntilFinished / 100);
            //You can change progressbar background color and front color
           
c6.setProgressbarBackgroundColor(getResources().getColor(R.color.pb_background));
            c6.setProgressbarColor(getResources().getColor(R.color.colorAccent));

            c6.setProgress(progress);
            tv_result.setText("" + progress);
        }

        @Override
        public void onFinish() {
            //onfinish you write your code
       
}
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        countDownTimer.cancel();
    }
}

See build.gradle file preview


apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

   
defaultConfig {
        applicationId "com.samset.circularprogressbar"
       
minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
   
}
    buildTypes {
        release {
            minifyEnabled false
           
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       
}
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
   
compile 'com.android.support:appcompat-v7:23.1.1'
}


Done
I hope this blog helps you.
If this blog helps you then do not forget  to like and comment.

You want to download full source code go below link...

https://github.com/SamsetDev/CircularProgressbar_Example/tree/master


Enjoy and play with code.


DialogBox Animation

Hello friends
Today we are studying how to set animation in dialog box in android.So lets start and enjoying.....

Create a anim directory(folder) in res folder and then create 4 file name of below given.


slide-up.xml

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

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="100%"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="0" 
/>

</set>


slide_down.xml



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

    <translate
        android:duration="@android:integer/config_mediumAnimTime"
        android:fromYDelta="0%p"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toYDelta="100%p"
/>

</set>

zoom_in.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:fillAfter="false"
        android:startOffset="200"
        android:duration="200"
        android:pivotX = "50%"
        android:pivotY = "-90%"
       
/>
    <translate
        android:fromYDelta="50%"
        android:toYDelta="0"
        android:startOffset="200"
        android:duration="200"
       
/>
</set>


zoom_out.xml


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:fillAfter="false"
        android:duration="200"
        android:pivotX = "50%"
        android:pivotY = "-90%"
       
/>
    <translate
        android:fromYDelta="0"
        android:toYDelta="50%"
        android:duration="200"
       
/>
</set>


Now you entry this animation  in style resource file ..

style.xml


<resources>

    <!-- Base application theme. -->
   
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
       
<item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="SamAnimation.Window" parent="@android:style/Animation.Activity">
        <item name="android:windowEnterAnimation">@anim/zoom_in</item>
        <item name="android:windowExitAnimation">@anim/zoom_out</item>
    </style>

    <style name="SetDialogAnimation">
        <item name="android:windowEnterAnimation">@anim/slide_up</item>
        <item name="android:windowExitAnimation">@anim/slide_down</item>
    </style>
</resources>

Now you create custom dialogbox view

dialog_viewss.xml


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

    <TextView
        android:padding="10dp"
        android:gravity="center"
        android:textColor="#990000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Testing"
/>

    <TextView
        android:padding="10dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Testing"
/>

    <TextView
        android:padding="10dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Testing"
/>
</LinearLayout>

main_activity.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#e5e5e5"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
>

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="26dp"
        android:paddingBottom="5dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:text="Dialog-1"
/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="26dp"
        android:paddingBottom="5dp"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:text="Dialog-2"
/>

</LinearLayout>


Now you go MainActivity java class and paste below code carefully

MainActivity.java


package com.samset.dialoganimation;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button button1,button2;
    AlertDialog alertDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1= (Button) findViewById(R.id.btn1);
        button2= (Button) findViewById(R.id.btn2);


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Alert(0);

            }
        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Alert(1);

            }
        });
    }
    private void Alert(int i)
    {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        View view = layoutInflater.inflate(R.layout.dialog_viewss, null);
        getDialogBox(view,i);
    }
    private AlertDialog.Builder getDialogBox(View view,int i) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view);
        builder.setCancelable(true);
        alertDialog=builder.create();
        if (i==0)
        {
                alertDialog.getWindow().getAttributes().windowAnimations=R.style.SamAnimation_Window;
        }else
       
{
            alertDialog.getWindow().getAttributes().windowAnimations=R.style.SetDialogAnimation;
        }

        alertDialog.show();
        return builder;

    }
}


Thanks
I hope this blog helps you

See live sample








Download full source code

https://github.com/SamsetDev/DialogboxAnimation/tree/master




Wednesday 17 February 2016

How to image blur

Hello gyes
If you want to your imageview image blur then follow my blog sample..

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background=“#e5e5e5”
    android:orientation="vertical">

 
                <ImageView
                    android:id="@+id/holder"
                    android:layout_width="match_parent"
                    android:layout_height="300dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/img_header"
                    android:tint="#11000000" />


</LinearLayout>


MainActivity.java

/*
*
*Samset created 17/02/2016
*/
public class MainActivity extends AppCompatActivity {
private ImageView placeholder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
 placeholder= (ImageView) findViewById(R.id.holder);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img_header);
Bitmap blurredBitmap = blur(bitmap);
placeholder.setImageBitmap(blurredBitmap);


       
    }
//by samset
public Bitmap blur(Bitmap image) {
    if (null == image) return null;

    Bitmap outputBitmap = Bitmap.createBitmap(image);
    final RenderScript renderScript = RenderScript.create(this);
    Allocation tmpIn = Allocation.createFromBitmap(renderScript, image);
    Allocation tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap);

   
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
        theIntrinsic.setRadius(BLUR_RADIUS);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);
    }

    return outputBitmap;
}


}

I hope this sample helps you

Thank you 





Monday 1 February 2016

FragmentBackHandle

Hi friends

Today we are learn most challenging task approximately max developer face this problem so i am resolve this problem i hope this post help you. 


 Are you ready to learn about how to handle fragment back stack....

Step 1-> Create a new sample project in android studio

Step 2->Create multiple fragments

Step 3->And now you go to  MainActivity.java this class is most powerful so please concentrate.

public class MainActivity extends AppCompatActivity implements OnItemClicks{
    public Stack<String> mFragmentStack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentStack = new Stack<String>();
        FragmentManager fragmentManager = 
getSupportFragmentManager();
        FragmentTransaction transaction = 
fragmentManager.beginTransaction();
        Fragment fragment = new FragmentFirst();
        String tag = fragment.toString();
        mFragmentStack.add(tag);
        transaction.add(R.id.containers, fragment, tag);
        transaction.addToBackStack(tag);
        transaction.commit();
  }
    @Override
    public void itemClick(String input)
    {
        if (input.equalsIgnoreCase("first"))
        {
            Fragment newFragment = new SecondFragment();
            beginTransction(newFragment);
        }else if (input.equalsIgnoreCase("second"))
        {
          Fragment newFragment = new ThirdFragment();
            beginTransction(newFragment);
        }
        else if (input.equalsIgnoreCase("third"))
        {
            Fragment fragment = new FourthFragment();
            beginTransction(fragment);
        }else if (input.equalsIgnoreCase("fourth"))
        {     Fragment fragment = new FifthFragment();
            beginTransction(fragment);
        }else if (input.equalsIgnoreCase("fifth"))
        {
            Fragment fragment = new SixFragment();
            beginTransction(fragment);
        }
    }
    private void beginTransction(Fragment fragment)
    {
        Bundle args = new Bundle();
        String tag = fragment.toString();
        fragment.setArguments(args);
        FragmentTransaction transaction = 
getSupportFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.anim.fragment_animation_fade_in, R.anim.fragment_animation_fade_out);
        // I find what the current fragment from the stack is, take it and hide it
        // using transaction.hide(currentFragment);
        Fragment currentFragment = 
getSupportFragmentManager().findFragmentByTag(mFragmentStack.peek());
        transaction.hide(currentFragment);
        transaction.add(R.id.containers, fragment,fragment.toString());
        // This is important, we must addToBackStack so we can pull it out later.
        transaction.addToBackStack(tag);
        // Instead of using replace we use add. Why? If we use replace, then the previous
        // fragment will always have to be re-created. What if you don't want to do that. In
        // my case, I didn't want it to be re-created all the time because I had a position
        // set and by re-creating it, I would have lost the position or had to use static flag.
        mFragmentStack.add(tag);
        transaction.commit();
    }
    @Override
    public void onBackPressed(){
        // from the stack we can get the latest fragment
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(mFragmentStack.peek());
        // If its an instance of Fragment1 I don't want to finish my activity, so I launch a Toast instead.
        if (fragment instanceof FragmentFirst){
            Toast.makeText(getApplicationContext(), "YOU ARE AT THE TOP FRAGMENT", Toast.LENGTH_SHORT).show();
        }
        else{
            // Remove the framg
            removeFragment();
            super.onBackPressed();
        }
    }
    private void removeFragment(){
        // remove the current fragment from the stack.
        mFragmentStack.pop();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        // get fragment that is to be shown (in our case fragment1).
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(mFragmentStack.peek());
        // This time I set an animation with no fade in, so the user doesn't wait for the animation in back press
        transaction.setCustomAnimations(R.anim.zoom_in, R.anim.zoom_out);
        // We must use the show() method.
        transaction.show(fragment);
        transaction.commit();
    }
}
Step 4-> Now you create a listener interface

public interface OnItemClicks {
    public void itemClick(String tag);
}

if you want full code source code click this link
https://github.com/SamsetDev/FragmentbackStackHandle

If this sample helps you please like this page