Thứ Năm, 27 tháng 11, 2014

Android Using External Fonts

http://javatechig.com/android/using-external-fonts-in-android-view

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class CustomTextView extends TextView {
 public CustomTextView(Context context) {
  super(context);
  setFont();
 }
 public CustomTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  setFont();
 }
 public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  setFont();
 }

 private void setFont() {
  Typeface font = Typeface.createFromAsset(getContext().getAssets(),
    "fonts/Maximum.ttf");
  setTypeface(font, Typeface.NORMAL);
 }
}
Now change the following in your android layout xml file
<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:orientation="vertical"
tools:context=".MainActivity"
android:background="#0F0F0F">

<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textSize="18dp"
android:textColor="#AAD4DE" />

<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textColor="#FFDE46"
android:textSize="24dp"/>

<com.javatechig.droid.ui.CustomTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/font_sample_text"
android:textSize="30dp"
android:textColor="#7FBA00"/>

</LinearLayout>

Android Handler

http://javatechig.com/android/android-handler-example

import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.javatechige.handlerexample.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

    private ProgressDialog progressDialog;
    private ImageView imageView;
    private String url = "http://www.9ori.com/store/media/images/8ab579a656.jpg";
    private Bitmap bitmap = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);

        Button start = (Button) findViewById(R.id.button1);
        start.setOnClickListener(new OnClickListener() {

         @Override
            public void onClick(View arg0) {
          progressDialog = ProgressDialog.show(MainActivity.this,
            "", "Loading..");
          new Thread() {
           public void run() {
            bitmap = downloadBitmap(url);
            messageHandler.sendEmptyMessage(0);
           }
          }.start();

            }
        });
    }

    private Handler messageHandler = new Handler() {
        public void handleMessage(Message msg) {
         super.handleMessage(msg);
         imageView.setImageBitmap(bitmap);
            progressDialog.dismiss();
        }
    };

    private Bitmap downloadBitmap(String url) {
        // Initialize the default HTTP client object
        final DefaultHttpClient client = new DefaultHttpClient();

        //forming a HttoGet request
        final HttpGet getRequest = new HttpGet(url);
        try {

            HttpResponse response = client.execute(getRequest);

            //check 200 OK for success
            final int statusCode = response.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                Log.w("ImageDownloader", "Error " + statusCode +
                  " while retrieving bitmap from " + url);
                return null;
            }

            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    // getting contents from the stream
                    inputStream = entity.getContent();

                    // decoding stream data back into image Bitmap
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                    return bitmap;
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        } catch (Exception e) {
            getRequest.abort();
            Log.e(getString(R.string.app_name), "Error "+ e.toString());
        }

        return null;
    }
}

Android Toast

http://javatechig.com/android/android-toast-example
Toast toast = Toast.makeText(getApplicationContext(), "Your toast message.",
                      Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);
toast.show();
Custom Toast:
//get the LayoutInflater and inflate the custom_toast layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.toast_layout_root));

//get the TextView from the custom_toast layout
TextView text = (TextView) layout.findViewById(R.id.toastText);
text.setText("This is my custom toast");

//create the toast object, set display duration,
//set the view as layout that's inflated above and then call show()
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_LONG);
t.setView(layout);
t.show();

Thứ Hai, 3 tháng 11, 2014

For Framework

====Dau phay:

String dichvu="";

boolean fr=subDtResp.isUseFunring();
boolean mca=subDtResp.isUseMca();
boolean cb=subDtResp.isUseCb();

if(fr)
dichvu+="FR";

if(mca){
if(fr)
dichvu+=", MCA";
else
dichvu+="MCA";

}

if(cb){
if(!mca && !fr)
dichvu+="CB";
else
dichvu+=", CB";
}
===============================

public static String toFormatAccounting(long number){
    String pattern = "###,###.###";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String format = decimalFormat.format(number);
return format;
    }

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

public static String returnedChuaCapNhat(String triedText){
    String text = "";
    String returnedStr = "";

    if(triedText!=null){
    text=String.valueOf(triedText);
    if(text.trim().equalsIgnoreCase("")){
    returnedStr="Chưa cập nhật.";
    }else{
    returnedStr=text;
    }
    }else{
    returnedStr="Chưa cập nhật.";
    }
return returnedStr;
    }