Tuesday, 12 March 2013

Handler Example which shows ProgressDialogue untill UI gets Updated

Java Class
----------------

package com.example.handleexample;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sample = (Button) findViewById(R.id.button1);

        sample.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                fetchData();
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    private void fetchData() {
        // TODO Auto-generated method stub
        progressDialog = ProgressDialog.show(this, "", "Doing...");

        new Thread() {

            public void run() {
                String text = "hello world";
                Message msg = Message.obtain();
                msg.what = 2;
                try {
                    Thread.sleep(800);

                } catch (InterruptedException e) {

                }

                Bundle b = new Bundle();
                b.putString("text", text);
                msg.setData(b);
                messageHandler.sendMessage(msg);
            }

        }.start();

    }

    private Handler messageHandler = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
            case 1:
                // ImageView img = (ImageView) findViewById(R.id.imageview01);
                // img.setImageBitmap((Bitmap)(msg.getData().getParcelable("bitmap")));
                break;
            case 2:
                TextView text = (TextView) findViewById(R.id.textView2);
                System.out.println("Test here "
                        + msg.getData().getString("text"));
                text.setText(msg.getData().getString("text"));
                break;
            }
            progressDialog.dismiss();
        }
    };

}


activity_main.xml
----------------------
<RelativeLayout 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"
    tools:context=".MainActivity" >

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:layout_toLeftOf="@+id/textView1"
        android:text="Button" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="22dp"
        android:text="TextView" />

</RelativeLayout>

SampleOutPut
------------------

Thursday, 7 March 2013

Generate SHA-1 Key for Android Map Version-2

To generate this key:
1)Go to Java---JRE---Bin and the type the following command
keytool -list -v keystore "path of your android keystore(It will be in users account)"
2)Type Enter
3)Type the password android
4)Key gets generated
5)Place this key in googleAPI console (https://code.google.com/apis/console/#project:429176074131:access in this go to API access)
6)Press create button key gets generated with respect to the project

Monday, 4 March 2013

Android Thermometer example using canvas

package com.example.canvassample;

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.View;

public class DrawView extends View {
    Paint paint = new Paint();
    Heightwidth sample = new Heightwidth();

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {

        int devicewidth = sample.getWidth();
        int deviceheight = sample.getHeight();
        int boxheight = deviceheight / 10;

        int Mwidht = devicewidth / 2;
        float rdistance = (float) (Mwidht / 1.2);
        float rdistancearc = (float) (Mwidht / 1.2);
        float ldistanve = (float) (1.3 * (rdistance));

        float rdistancesecond = rdistance + (Mwidht / 4);

        int top = deviceheight - boxheight;
        System.out.println("top is " + top);
        int toppos = top / 12;
        int botpos = 2 * (toppos);
        int botposarc = (toppos) + boxheight;

        // bottom arc
        int botarc = top / 5;
        int lastpara = boxheight + top;
        // canvas.drawLine(160, 500, 160, 160, paint);
        // canvas.drawLine(180, 500, 180, 160, paint);

        // canvas.drawLine(rdistance, 80, rdistance, botpos, paint);

        Paint p = new Paint();
        // smooths
        p.setAntiAlias(true);
        p.setColor(Color.RED);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(1);
        RectF rect2 = new RectF(rdistance, toppos, ldistanve, botposarc);
        canvas.drawArc(rect2, 0, -180, false, p);

        // RectF rect3 = new RectF(rdistance,botarc,ldistanve,lastpara);
        // canvas.drawArc(rect3,0, 180, false, p);

        float center_x, center_y;
        center_x = devicewidth / 2;
        center_y = deviceheight / 4;
        float radius = deviceheight / 10;
        final RectF oval = new RectF();
       
        float parameter = boxheight;
       
        oval.set(center_x - radius, center_y - radius, center_x + radius,
                center_y + radius);

        paint.setStyle(Paint.Style.STROKE);
        center_x = devicewidth / 2;
        center_y = deviceheight * 3 / 4;
        oval.set(center_x - (radius+(radius/8)),
                (float) (center_y - (radius + (1.3 * boxheight))), center_x
                        + (radius-(radius/8)), center_y + (radius - (top / 5)));

       
        paint.setTextSize(28);
        //canvas.drawText("TemperatureLevel", Mwidht, 20, paint);

        canvas.drawArc(oval, -67, 315, false, paint);

        canvas.drawText("Hello", Mwidht - 20, (float) (deviceheight / 1.7), p);

        // canvas.drawPath(drawCurve(canvas, paint, rdistance, ldistanve),
        // paint);

        // canvas.drawArc(rect2, 270, 0, false, p);

        canvas.drawLine(rdistance, (float) (1.1 * boxheight), rdistance,
                (float) (5.25 * boxheight), paint);

        canvas.drawLine(rdistancesecond, (float) (1.1*boxheight),
                rdistancesecond, (float) (5.25 * boxheight), paint);
        // canvas.draw

        // right distance,length,right distance,todistance
        // canvas.drawLine(20, 0, 0, 20, paint);
    }

    private Path drawCurve(Canvas canvas, Paint paint, float rdistance,
            float ldistanve) {

        Path myPath = new Path();
        myPath.moveTo(rdistance, sample.getHeight() / 10);
        myPath.quadTo(rdistance, sample.getHeight() / 10, rdistance,
                sample.getHeight() / 10);
        return myPath;
    }
}



package com.example.canvassample;

public class Heightwidth {

    static int height;
    static int width;

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }
}


package com.example.canvassample;

import android.app.Activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;

public class MainActivity extends Activity {
    DrawView drawView;
    Heightwidth sample = new Heightwidth();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        String str_ScreenSize = "The Android Screen is: " + dm.widthPixels
                + " x " + dm.heightPixels;
        sample.setHeight(dm.heightPixels);
        sample.setWidth(dm.widthPixels);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);

    }
}


activity_main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

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

</RelativeLayout>


sampleoutput
----------------------

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...