Sunday 23 February 2014

Place Marker On Static Image Example

Hi Friends,

Once i was working on a project where in app i have to place marker on the static ImageView programmatically onTouchEvent, I Google it but not getting any informative inform from Google then i decide to try this myself, its take my 2 day to complete this task, i know if anyone struck when he/she are at the end of any project that is really frustrating, that's why i am going to share this information with you people hope this will solve your problems:


Example:












Codes:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

   <in.androiddevelopmentanddiscussion.markeronimageview.PaintSurface
       android:id="@+id/paint_surface"
       android:layout_width="300dp"
       android:layout_height="300dp"
       android:layout_alignParentTop="true"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="50dp" />

   <TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:layout_marginBottom="20dp"
       android:layout_marginLeft="10dp"
       android:layout_marginRight="10dp"
       android:layout_marginTop="10dp"
       android:gravity="center"
       android:text="Where is California (State)"
       android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.markeronimageview;

import in.androiddevelopmentanddiscussion.drawandcanvasexample.R;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.view.SurfaceHolder;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView iv;
PaintSurface mPaintSurface;
final Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mPaintSurface = (PaintSurface)findViewById(R.id.paint_surface);

mPaintSurface.setZOrderOnTop(true);
SurfaceHolder sh = mPaintSurface.getHolder();
sh.setFormat(PixelFormat.TRANSLUCENT);

mPaintSurface.setOnTouchListener(mPaintSurface);

}

@Override
protected void onResume() {
super.onResume();
mPaintSurface.resume();
}

@Override
protected void onPause() {
super.onPause();
mPaintSurface.pause();
}


public static void alertMsg(final Context context,String msg){

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Result");
alertDialogBuilder
.setMessage(msg)
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {

Intent intent = ((Activity) context).getIntent();
((Activity) context).overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
((Activity) context).finish();
((Activity) context).overridePendingTransition(0, 0);
((Activity) context).startActivity(intent);

}
});

AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();

}



}


PaintSurface.java

package in.androiddevelopmentanddiscussion.markeronimageview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import in.androiddevelopmentanddiscussion.drawandcanvasexample.R;
import in.androiddevelopmentanddiscussion.markeronimageview.MainActivity;


public class PaintSurface extends SurfaceView implements Runnable, OnTouchListener{
private SurfaceHolder mHolder;
private Thread mThread;
private boolean mFlag = false;
private float mX;
private float mY;
private Paint mPaint,nPaint,lPaint;
Canvas canvas;
float distance;


public PaintSurface(Context context, AttributeSet attrs) {
super(context, attrs);
setBackgroundResource(R.drawable.usa);
mHolder = getHolder();
mPaint = new Paint();
mPaint.setColor(Color.GREEN);
nPaint = new Paint();
nPaint.setColor(Color.RED);
lPaint = new Paint();
lPaint.setColor(Color.BLUE);
}
public void resume(){
// Instantiating the thread
mThread = new Thread(this);
mFlag = true;
mThread.start();
}
public void pause(){
mFlag = false;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
         case MotionEvent.ACTION_DOWN:

                 mX = event.getX();
                 mY = event.getY();
              
                 distance = (float) Math.sqrt(Math.pow(101 - mX, 2) + Math.pow(280 - mY, 2));
                 
                 String msg = "X axis: "+mX+"\nY axis: "+mY+"\nDistance :"+distance;
                 
                 MainActivity.alertMsg(getContext(),msg);
                 
                 
                 break;
}
return true;
}

@Override
public void run() {
while(mFlag){

if(!mHolder.getSurface().isValid())
continue;
canvas = mHolder.lockCanvas();
canvas.drawARGB(0, 0, 0, 0);
if(distance<=0){

}else if(distance<50){
Bitmap bmp3 = BitmapFactory.decodeResource(getResources(),R.drawable.green_flag);
canvas.drawBitmap(bmp3, 101- (bmp3.getWidth() / 3), 280- (bmp3.getHeight() / 3), null);
Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.green_flag);
canvas.drawBitmap(bmp2, mX- (bmp2.getWidth() / 3), mY- (bmp2.getHeight() / 3), null);
}else{
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),R.drawable.green_flag);
canvas.drawBitmap(bmp1, 101- (bmp1.getWidth() / 3), 280- (bmp1.getHeight() / 3), null);

Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.red_flag);
canvas.drawBitmap(bmp, mX- (bmp.getWidth() / 3), mY- (bmp.getHeight() / 3), null);
canvas.drawLine(101 , 280,mX, mY, lPaint);
}

mHolder.unlockCanvasAndPost(canvas);
}
}
}


Manifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.androiddevelopmentanddiscussion.drawandcanvasexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="in.androiddevelopmentanddiscussion.markeronimageview.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>


Download Full Project From Here : 



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en

 

Friday 21 February 2014

Controlling the Android device through Laptop/Desktop

Hi Friends,

Again i am here to explain you a fantastic trick in android, have ever think to control your Android device with your laptop/Desktop, i am sure you are not so, i am here going to explain you how to control the Android device through Laptop/Desktop, find the details mention below:

For android phones, you could use the Android Screen Monitor download and extract.

http://code.google.com/p/android-screen-monitor/

Android Screen Monitor is a tool to display the Android phone screen on your PC. However, in order to use it you will need to download the following:





After installing android SDK & JDK follow the mention below steps:

Navigate to platform-tools in android sdk folder and paste the ASM_2_50 folder there which you download and extract before :

E:\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\platform-tools



Now connect your Android device with laptop/desktop and follow the step:

Android Device Setup

  • Enable USB Debugging (Settings > Apps > Development > USB Debugging)
  • Enable Unknown Source (Settings > Apps > Unknown sources)

    


After completing the above steps in android device start command prompt and follow the steps:

cd E:\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\platform-tools

Press Enter






java -jar E:\adt-bundle-windows-x86_64-20131030\adt-bundle-windows-x86_64-20131030\sdk\platform-tools\ASM_2_50\ASM_2_50\asm.jar

Press Enter



Click on OK










That's all now your device connected with laptop/desktop, now you can control your android device through laptop/desktop.



Wednesday 12 February 2014

Count Down Timer

Hi Friends,

Today i am going to explain you about Count Down Timer in android. Schedule a countdown until a time in the future, with regular notifications on intervals along the way. below is the example of showing a 10 second countdown in a text field:


Example:




Code:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>


MainActivity.java


package in.androiddevelopmentanddiscussion.countdowntimer1;


import android.os.Bundle;

import android.os.CountDownTimer;
import android.app.Activity;
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 {


TextView tv;

Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tv = (TextView)findViewById(R.id.textView1);
btn = (Button)findViewById(R.id.button1);

btn.setOnClickListener(new OnClickListener() {

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

new CountDownTimer(11000, 1000) {

    public void onTick(long millisUntilFinished) {

        tv.setText("Time left : " + millisUntilFinished / 1000+"sec");
    }

    public void onFinish() {

        tv.setText("done!");
    }
 }.start();
}
});


}

@Override

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


}


Download full project from here: