Sunday, June 15, 2014
ANDROID DRAG AND DROP EXAMPLE
ANDROID DRAG AND DROP EXAMPLE
STRINGS-
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">DragNDropDemo</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</string> <string name="drag_drop">Click on the image to drag and drop</string> </resources>
LAYOUT-
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/iv_logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/logo" android:contentDescription="@string/drag_drop" /> </RelativeLayout>
JAVA-
package com.example.dragndropdemo; import android.os.Bundle; import android.app.Activity; import android.content.ClipData; import android.content.ClipDescription; import android.util.Log; import android.view.DragEvent; import android.view.View; import android.view.View.DragShadowBuilder; import android.view.View.OnDragListener; import android.widget.*; public class MainActivity extends Activity{ ImageView ima; private static final String IMAGEVIEW_TAG = "Android Logo"; String msg; private android.widget.RelativeLayout.LayoutParams layoutParams; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ima = (ImageView)findViewById(R.id.iv_logo); // Sets the tag ima.setTag(IMAGEVIEW_TAG); ima.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { ClipData.Item item = new ClipData.Item((CharSequence)v.getTag()); String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN}; ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item); // Instantiates the drag shadow builder. View.DragShadowBuilder myShadow = new DragShadowBuilder(ima); // Starts the drag v.startDrag(dragData, // the data to be dragged myShadow, // the drag shadow builder null, // no need to use local data 0 // flags (not currently used, set to 0) ); return true; } }); // Create and set the drag event listener for the View ima.setOnDragListener( new OnDragListener(){ @Override public boolean onDrag(View v, DragEvent event){ switch(event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams(); Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED"); // Do nothing break; case DragEvent.ACTION_DRAG_ENTERED: Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED"); int x_cord = (int) event.getX(); int y_cord = (int) event.getY(); break; case DragEvent.ACTION_DRAG_EXITED : Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED"); x_cord = (int) event.getX(); y_cord = (int) event.getY(); layoutParams.leftMargin = x_cord; layoutParams.topMargin = y_cord; v.setLayoutParams(layoutParams); break; case DragEvent.ACTION_DRAG_LOCATION : Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION"); x_cord = (int) event.getX(); y_cord = (int) event.getY(); break; case DragEvent.ACTION_DRAG_ENDED : Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED"); // Do nothing break; case DragEvent.ACTION_DROP: Log.d(msg, "ACTION_DROP event"); // Do nothing break; default: break; } return true; } }); } }
VELOCITY TRACKER EXAMPLE
VELOCITY TRACKER EXAMPLE
LAYOUT-
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<TextView
android:id="@+id/version"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/action"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="pointer 0"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/velocityx0"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/velocityy0"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="pointer 1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/velocityx1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/velocityy1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
JAVA-
package com.example.androidvelocitytracker;
import android.os.Build;
import android.os.Bundle;
import android.os.Build.VERSION_CODES;
import android.support.v4.view.VelocityTrackerCompat;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
TextView textAvtion, textVelocityX0, textVelocityY0,
textVelocityX1, textVelocityY1;
TextView textVersion;
VelocityTracker velocityTracker = null;
static SparseArray<String> arrayVC = new SparseArray<String>();
static {
arrayVC.append(VERSION_CODES.BASE, "The original, first, version of Android.");
arrayVC.append(VERSION_CODES.BASE_1_1, "First Android update, officially called 1.1");
arrayVC.append(VERSION_CODES.CUPCAKE, "Android 1.5");
arrayVC.append(VERSION_CODES.CUR_DEVELOPMENT, "Magic version number...");
arrayVC.append(VERSION_CODES.DONUT, "Android 1.6");
arrayVC.append(VERSION_CODES.ECLAIR, "Android 2.0");
arrayVC.append(VERSION_CODES.ECLAIR_0_1, "Android 2.0.1");
arrayVC.append(VERSION_CODES.ECLAIR_MR1, "Android 2.1");
arrayVC.append(VERSION_CODES.FROYO, "Android 2.2");
arrayVC.append(VERSION_CODES.GINGERBREAD,"Android 2.3");
arrayVC.append(VERSION_CODES.GINGERBREAD_MR1, "Android 2.3.3");
arrayVC.append(VERSION_CODES.HONEYCOMB, "Android 3.0");
arrayVC.append(VERSION_CODES.HONEYCOMB_MR1, "Android 3.1");
arrayVC.append(VERSION_CODES.HONEYCOMB_MR2, "Android 3.2");
arrayVC.append(VERSION_CODES.ICE_CREAM_SANDWICH,"Android 4.0");
arrayVC.append(VERSION_CODES.ICE_CREAM_SANDWICH_MR1,"Android 4.0.3");
arrayVC.append(VERSION_CODES.JELLY_BEAN, "Android 4.1");
arrayVC.append(VERSION_CODES.JELLY_BEAN_MR1, "Android 4.2");
arrayVC.append(VERSION_CODES.JELLY_BEAN_MR2, "Android 4.3");
arrayVC.append(VERSION_CODES.KITKAT, "Android 4.4");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textAvtion = (TextView) findViewById(R.id.action);
textVelocityX0 = (TextView) findViewById(R.id.velocityx0);
textVelocityY0 = (TextView) findViewById(R.id.velocityy0);
textVelocityX1 = (TextView) findViewById(R.id.velocityx1);
textVelocityY1 = (TextView) findViewById(R.id.velocityy1);
textVersion = (TextView)findViewById(R.id.version);
int version = Build.VERSION.SDK_INT;
String BuildVersion = arrayVC.get(version, "unknown");
textVersion.setText(BuildVersion);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
int index = event.getActionIndex();
int pointerId = event.getPointerId(index);
switch (action) {
case MotionEvent.ACTION_DOWN:
if (velocityTracker == null) {
velocityTracker = VelocityTracker.obtain();
} else {
velocityTracker.clear();
}
velocityTracker.addMovement(event);
if(pointerId == 0){
textVelocityX0.setText("X-velocity (pixel/s): 0");
textVelocityY0.setText("Y-velocity (pixel/s): 0");
}else if(pointerId == 1){
textVelocityX1.setText("X-velocity (pixel/s): 0");
textVelocityY1.setText("Y-velocity (pixel/s): 0");
}
break;
case MotionEvent.ACTION_MOVE:
velocityTracker.addMovement(event);
velocityTracker.computeCurrentVelocity(1000);
//1000 provides pixels per second
float xVelocity = VelocityTrackerCompat.getXVelocity(
velocityTracker, pointerId);
float yVelocity = VelocityTrackerCompat.getYVelocity(
velocityTracker, pointerId);
if(pointerId == 0){
textVelocityX0.setText("X-velocity (pixel/s): " + xVelocity);
textVelocityY0.setText("Y-velocity (pixel/s): " + yVelocity);
}else if(pointerId == 1){
textVelocityX1.setText("X-velocity (pixel/s): " + xVelocity);
textVelocityY1.setText("Y-velocity (pixel/s): " + yVelocity);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
velocityTracker.recycle();
break;
}
return true;
}
}
TIMER TASK ANDROID APP EXAMPLE
TIMER TASK ANDROID APP EXAMPLE
LAYOUT-
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />
<CheckBox
android:id="@+id/singleshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Single Shot"/>
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start"/>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"/>
<TextView
android:id="@+id/counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textStyle="bold"/>
</LinearLayout>
JAVA-
package com.example.androidtimer;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
CheckBox optSingleShot;
Button btnStart, btnCancel;
TextView textCounter;
Timer timer;
MyTimerTask myTimerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
optSingleShot = (CheckBox)findViewById(R.id.singleshot);
btnStart = (Button)findViewById(R.id.start);
btnCancel = (Button)findViewById(R.id.cancel);
textCounter = (TextView)findViewById(R.id.counter);
btnStart.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(timer != null){
timer.cancel();
}
//re-schedule timer here
//otherwise, IllegalStateException of
//"TimerTask is scheduled already"
//will be thrown
timer = new Timer();
myTimerTask = new MyTimerTask();
if(optSingleShot.isChecked()){
//singleshot delay 1000 ms
timer.schedule(myTimerTask, 1000);
}else{
//delay 1000ms, repeat in 5000ms
timer.schedule(myTimerTask, 1000, 5000);
}
}});
btnCancel.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (timer!=null){
timer.cancel();
timer = null;
}
}
});
}
class MyTimerTask extends TimerTask {
@Override
public void run() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat =
new SimpleDateFormat("dd:MMMM:yyyy HH:mm:ss a");
final String strDate = simpleDateFormat.format(calendar.getTime());
runOnUiThread(new Runnable(){
@Override
public void run() {
textCounter.setText(strDate);
}});
}
}
}
WIFI STATUS IN ANDROID PROMATICALLY
WIFI STATUS IN ANDROID PROMATICALLY
UI Layout(check_wifi_connection.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"
>
<TextView
android:id="@+id/aboutapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="This Example Expains Check wifi connection status"
android:textSize="15sp" />
<Button
android:id="@+id/checkstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="44dp"
android:text="Click me i will tell you" />
</RelativeLayout>
<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"
>
<TextView
android:id="@+id/aboutapp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="This Example Expains Check wifi connection status"
android:textSize="15sp" />
<Button
android:id="@+id/checkstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="44dp"
android:text="Click me i will tell you" />
</RelativeLayout>
MAINACTIVITY
package com.yeahultra.checkwificonnection;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CheckWifiConnection extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_wifi_connection);
Button check = (Button) findViewById(R.id.checkstatus);
check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (isWifiAvailable()) {
Toast.makeText(getApplicationContext(),
"You have WIFI connection", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getApplicationContext(),
"You don't have WIFI connection", Toast.LENGTH_LONG)
.show();
}
}
});
}
public Boolean isWifiAvailable() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.isConnected()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.check_internet_connection, menu);
return true;
}
}
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class CheckWifiConnection extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_wifi_connection);
Button check = (Button) findViewById(R.id.checkstatus);
check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (isWifiAvailable()) {
Toast.makeText(getApplicationContext(),
"You have WIFI connection", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getApplicationContext(),
"You don't have WIFI connection", Toast.LENGTH_LONG)
.show();
}
}
});
}
public Boolean isWifiAvailable() {
try {
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifiInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.isConnected()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.check_internet_connection, menu);
return true;
}
}
Subscribe to:
Posts (Atom)