Posts

Showing posts from December, 2017

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…

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;
    }

  …