Payday Loan Payday Loan

Archive for the ‘Time and Date’ Category

In this tutorial i want to show you some common usefull convertings.
Convert Drawable to bitmap:

Bitmap icon= BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.icon_resource);

Convert Bitmap to drawable:

Drawable d =new BitmapDrawable(bitmap);

Convert imageview to bitmap:

ImageView image=new ImageView(context);
 Bitmap viewBitmap = Bitmap.createBitmap(i.getWidth(),i.getHeight(),Bitmap.Config.ARGB_8888);//i is imageview which u want to convert in bitmap
Canvas canvas = new Canvas(viewBitmap);
 
image.draw(canvas);

Convert List to String[]:

 static String[] convert(List<String[]> from) {
        ArrayList<String> list = new ArrayList<String>();
        for (String[] strings : from) {
            Collections.addAll(list, strings);
        }
        return list.toArray(new String[list.size()]);
    }

Example of using the function:

  public static void main(String[] args) {
        List<String[]> list = new ArrayList<String[]>();
        list.add(new String[] { "a", "b" });
        list.add(new String[] { "c", "d", "e" });
        list.add(new String[] { "f", "g" });
        String[] converted = convert(list);
        System.out.print(converted.toString());
    }

Convert seconds to date time string:

SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMMM d, yyyy HH:mm");
String dateString = formatter.format(new Date(seconds * 1000L));
||||| 0 I Like It! |||||

How to make a countdown Timer in Android.

The following code will show you how to make a countdown Timer in Android:
We create class MyCount in The Main class called Counting.
We instantiate MyCount class and tell it how many seconds to countdown.
In the method onThick of the MyCount class we update a TextView that is
in the layout counting.xml with id TextView01.
The layout looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"  android:background="#000000" android:layout_height="fill_parent">
<TextView android:id="@+id/TextView01" android:layout_width="wrap_content"
 android:layout_height="wrap_content" android:text="0" android:textColor="#ffff00" 
android:textStyle="bold" android:textSize="15dip"></TextView> </RelativeLayout>


Main class called Counting.java looks like this:

public class Counting extends Activity{
final MyCount counter = new MyCount(12000,1000);//this means that we count from 12.000 to 0 with 1000ms
per second...so it will be displayed...12...11...10...etc
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.counting);
MyCount counter = new MyCount(12000,1000);//instantiate counter
counter.start();//counter start as soon as app is opened...you can put this in a OnClickListener also.
}
 
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onFinish() {
TextView scor = (TextView)findViewById(R.id.TextView01);
 
scor.setText("0");//this is the message displayed at the finish of the counting...can be Game Over ..etc..
 
}
@Override
public void onTick(long millisUntilFinished) {
TextView scor = (TextView)findViewById(R.id.TextView01);
scor.setText(String.valueOf(millisUntilFinished/1000));
//here we update the text with seconds until 0.
}}

 

||||| 0 I Like It! |||||

How to get month, year and day and use them for different functions on Android.
In the following example we will display different text on different days. This can be used for an app like
Bible Verse of the Day.

 public class Main extends Activity {
	private int mYear;
    private int mMonth;
    private int mDay;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView w=(TextView)findViewById(R.id.TextView01);
 
        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);   
        if(mMonth==8&&mDay==20)
        {w.setText(" John 3:16: For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life.");}
        else if(mMonth==8&&mDay==20){w.setText(" John 3:16: For God so loved the world that he gave his one and only Son, that whoever believes in him shall not perish but have eternal life.");}
 
}
}

Month is always one number behind. So for January you will put month 0, and for december month 11.

||||| 0 I Like It! |||||

Search

Popular

Sponsors