Thursday, December 4, 2014

Android Using custom progress Bar Horizontal


Source Code:
==========

Xml File:
========
 <Button
          android:id="@+id/button1"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_below="@+id/textView1"
          android:layout_marginTop="31dp"
          android:background="@drawable/button"
          android:onClick="startProgressDialog"
          android:text="Start Downloading File" />

Java file:
========

package com.example.customizedprogressbar;



import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;

public class MainActivity extends ActionBarActivity
{

                     ProgressDialog progressBar;
                     private int progressBarStatus = 0;
                     private Handler progressBarHandler = new Handler();
                     Button click_circle;
                     private long fileSize = 0;
 
                    @Override
                    public void onCreate(Bundle savedInstanceState)
                    {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                                click_circle = (Button)findViewById(R.id.button2);
                                click_circle.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent fadein = new Intent(MainActivity.this, CircleProgress.class);
startActivity(fadein);

}
});
                               
                     }

                    public void startProgressDialog(View V)
                  {
       

                 
           
                                  // prepare for a progress bar dialog
           
                                  progressBar = new ProgressDialog(V.getContext());
                                  progressBar.setCancelable(true);
                                  progressBar.show();
                                  progressBar.setMessage("Downloading File...");
                                  progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                                  progressBar.setProgress(0);
                                  progressBar.setMax(100);
         
                                  // Get the Drawable custom_progressbar
                                 
                           
                                // Drawable customDrawable= getResources().getDrawable(R.drawable.progressbar2);
                         

                                  // set the drawable as progress drawable

                                  progressBar.setProgressDrawable(customDrawable);

                                  progressBar.show();

                                 //reset progress bar status
                                  progressBarStatus = 0;

                                 //reset filesize
                                  fileSize = 0;

                                  new Thread(new Runnable() {
                                  public void run() {
                                         while (progressBarStatus < 100) {

                                    // process some tasks
                                progressBarStatus = fileDownloadStatus();

                                //  sleep 1 second to show the progress
                                try {
                                        Thread.sleep(1000);
                                    }
                                catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }

                                // Update the progress bar
                                progressBarHandler.post(new Runnable() {
                                    public void run() {
                                        progressBar.setProgress(progressBarStatus);
                                    }
                                });
                            }

                            // when, file is downloaded 100%,
                            if (progressBarStatus >= 100) {

                                // sleep for  2 seconds, so that you can see the 100% of file download
                                try {
                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }

                                // close the progress bar dialog
                                progressBar.dismiss();
                             
                            }
                        }
                    }).start();

      }


     
//method returns the % of file downloaded
    public int fileDownloadStatus()
    {

        while (fileSize <= 10000000) {

            fileSize++;

            if (fileSize == 100000) {
                return 10;
            } else if (fileSize == 200000) {
                return 20;
            } else if (fileSize == 300000) {
                return 30;
            }
            else if (fileSize == 400000) {
                return 40;
            } else if (fileSize == 500000) {
                return 50;
            } else if (fileSize == 600000) {
                return 60;
            }
            else if (fileSize == 700000) {
                return 70;
            }
            else if (fileSize == 800000) {
                return 80;
            }
            else if (fileSize == 900000) {
                return 90;
            }
            else if (fileSize == 1000000) {
           
                return 100;
            }
         
            // write your code here

        }

        return 100;

    }
   
    /*protected void onProgressUpdate(Integer... progress) {
    progressBar.setProgress(progress[0]);
     
   }*/
}

Drawable file :
===========
this xml file include to  res folder

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:angle="270"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.5"
                android:endColor="#ff747674"
                android:startColor="#ff9d9e9d" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="0"
                    android:endColor="#ff009900"
                    android:startColor="#ff000099" />
            </shape>
        </clip>
    </item>
</layer-list>


Screenshot:
============






Tuesday, December 2, 2014

Android animation using override pending transitions

Source code:
=========

Slide  right screen

==============

U can create two Activity:

Animation file:
=============

Note :  you can store to anim folder

Slide in right
=============

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="100%" android:toXDelta="0%" />
    <alpha android:duration="5000" android:fromAlpha="0.0" android:toAlpha="1.0" />
</set>

Slide Out right:
==============

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <translate android:duration="5000" android:fromXDelta="0%" android:toXDelta="100%"/>
    <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

java file:
=======

Intent a = new Intent(MainActivity.this,
Pushupin.class);
startActivity(a);
overridePendingTransition(R.anim.slideinright, R.anim.slideoutright);


Screenshot:
============
screen one:

screen change progress:




Monday, December 1, 2014

Android using text binking with color interval

Android  using text blinking  and color change for every 20 mins......using timetcount

source code:

xml file:

<TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />


java file:

package com.example.blinking;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
TextView text;
Animation animBlink;
private static final int RED = 0xffFF8080;
private static final int BLUE = 0xff8080FF;

private CountDownTimer countDownTimer;
private final long startTime = 30 * 1000;
private final long interval = 1 * 1000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.textview);


countDownTimer = new MyCountDownTimer(startTime, interval);
countDownTimer.start();
Log.d("timecounter", ""+String.valueOf(startTime / 1000));

Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); // You can manage the blinking time with this//
// parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
text.startAnimation(anim);
text.setTextColor(RED);


}

public class MyCountDownTimer extends CountDownTimer {

public MyCountDownTimer(long startTime, long interval) {

super(startTime, interval);

}

@Override
public void onFinish() {

//text.setText("Time's up!");
text.setTextColor(RED);
countDownTimer.cancel();
countDownTimer.start();
}

@Override
public void onTick(long millisUntilFinished) {

//text.setText("" + millisUntilFinished / 1000);

long tir =millisUntilFinished / 1000;
if(tir <=20)
{
text.setTextColor(BLUE);
}
}

}


}