Skip to main content

Posts

Using TypeFace in Android

In android, when you using the custom fonts. You need to create the assets folder in the main, then paste your .ttf or .otf format fonts.

Here some methods will be very useful for you,


Set the typeface

    public Typeface setTypeFace(Context context) {
        Typeface typeface = Typeface.createFromAsset(context.getAssets(),
                "fonts/MyriadPro_Regular.otf");
        return typeface;
    }

txt_username.setTypeface(setTypeFace(getActivity()));

Like you can set the EditText, Spinner and other widgets.


Using the spannable text view in android,

Spannable span = new SpannableString("Hello Android, Have a great day!");        
span.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 13, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    span.setSpan(new ForegroundColorSpan(Color.RED), 13, 25, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(span);

Using Custom TypeFace:
import android.content.res.Resources;
import android.graphics.Paint;
import android.graphics.Typeface;
import android…
Recent posts

Date conversion into different data type

Most of times, we've busy with converting one data type into another data type and validations. That's a developer's life. Actually it's makes life interesting.

This is old, anyhow I post here. Just try it.

Convert current time to long Timestamp.
    public static long getCurrentTimeStamp(Context context) {
        long timestamp = 0;
        try {
            Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMMM-yyyy hh:mm ss a");
            timestamp = c.getTime().getTime();
        } catch (Exception e) {
            //handle exception
        }
        return timestamp;
    }


Convert Date into String

    public static String convertDate(Date date) {
        String format = "dd-MMM-yyyy hh:mm a";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String stringDate = "";
        try {
            stringDate = sdf.format(date)…

Convert the Model class or List into JSON string.

Sometimes making the json structure is so hard, but this way could be very easier to do any kind of object model into json structure.


Add the following dependency in app.gradle.
compile 'com.google.code.gson:gson:2.5'


Example:
User.Java
public class User {
    String userId;
    String userName;
    String userAge;
    String userAddress;
    String userCity;

    public User() {
    }

    public User(String userId, String userName, String userAge, String userAddress, String userCity) {
        this.userId = userId;
        this.userName = userName;
        this.userAge = userAge;
        this.userAddress = userAddress;
        this.userCity = userCity;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserAge() {
        return userAge;
    }

  …

Detecting shut down and reboot in android

Hi, Have a great day frnds..

Now we going to look up the detecting shut down and reboot events in android.

Here is the code:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


public class ShutDownReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
            Log.i("event", "shutdown");
        } else if (intent.getAction().equals(Intent.ACTION_REBOOT)) {
            Log.i("event", "reboot");
        }
    }
}

Then apply in your activity,

shutDownReceiver = new ShutDownReceiver();
filter.addAction(Intent.ACTION_REBOOT);
filter.addAction(Intent.ACTION_SHUTDOWN);

@Override
    protected void onPause() {
        super.onPause();
unregisterReceiver(shutDownReceiver);
}

@Override
    protected void onResume() {
        super.onResume();
registerReceiver(shutDownReceiver, fil…

Detecting Screen on and Screen Off via services

Hi buddies, have a happy Sunday.

Today I'll explore about detecting screen on and screen off in android devices.

I got your mind, can use also by Broad cast Receivers. Yes, you can.

But how do you know, if app goes offline and screen off.

First write the Broad cast receiver class for receiving results.

ScreenReceiver.Java
--------------------------
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class ScreenReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
            Log.i("screenLog", "screen off");

        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
            Log.i("screenLog", "screen on");
        }
    }
}

Then write the service class:

UpdateScreen.Java

import a…

Print the large string values in android log cat.

Hi,

Have a happy Monday.

In Android, sometimes we need to print the large numbers of strings value in log. But log can't accept whole value of the string.

In this case, we need to split the logs. It's very simple android code, I attached below this blog.

public static void logLargeString(String TAG, String msg) {         if (str.length() > 3000) {             Log.i(TAG, msg.substring(0, 3000));             logLargeString(TAG, msg.substring(3000));         } else {             Log.i(TAG, msg);         } }
Just use it and enjoy...

Backward and Forward Asterisks

Hi, Have a happy Sunday. Today I'm going to explain, how to deal with asterisks problems.

Just before the day, I've been attend the interview.

Question:
To print out Asterisk (*) in system console in specific order.
*****
****
***
**
*

This is backward asterisks, I'm failed to do that, because of less time, no test cases and unfortunate system freezes. Yeah, this is not accept by recruiters. It's very simple problem, I know.

Anyway 15 minutes passes by. No clues.

I'm going to give the answer by two ways here now.
1. Backward Asterisks
2. Forward Asterisks

Solution:

public class Asterisks{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the number of rows : ");
        int n = scan.nextInt();
        System.out.println("Backward Asterisks");
        for (int i = n; i >=0; i--) {
 …