In my previous post i explained how to capture photos using Android camera today in this post i am going to
explain how to crop image loading from gallery for this task following are my source codes
CropActivity.java
package com.example.crop;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CropActivity extends Activity {
protected static final int PICK_FROM_GALLERY = 1;
Button btcrop;
ImageView imgview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
btcrop= (Button) findViewById(R.id.btcrop);
imgview=(ImageView)findViewById(R.id.quickContactBadge1);
btcrop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_GALLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap photo = extras2.getParcelable("data");
imgview.setImageBitmap(photo);
}
}
}
}
Above code is my source code of crop project when ever i press crop button it will open image from gallery it show preview here i gave fixed size of length of image after that output show on ImageView
activity_crop.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btcrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="124dp"
android:layout_marginTop="128dp"
android:text="Crop" />
<QuickContactBadge
android:id="@+id/quickContactBadge1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btcrop"
android:layout_centerVertical="true" />
</RelativeLayout>
Above code is my layout file it have two fields one is button and one-more is QuickContactBadge it like image view only
when ever i press button it will open image from gallery after that output show's on image view
flipkart
Saturday, April 11, 2015
Monday, March 23, 2015
Android Compass Code Example
Today i am going explain how to design compass in android application example, why we develop
our own compass means suppose i went one as part my work but i don't know where West and North
. For finding position we are using compass . This post i am going explain compass example
CompasActivity.java
package com.vamsi.compas;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
public class CompasActivity extends Activity implements SensorEventListener {
// define the display assembly compass picture
private ImageView image;
// record the compass picture angle turned
private float currentDegree = 0f;
// device sensor manager
private SensorManager mSensorManager;
TextView tvHeading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_compas);
//
image = (ImageView) findViewById(R.id.imageViewCompass);
// TextView that will tell the user what degree is he heading
tvHeading = (TextView) findViewById(R.id.tvHeading);
// initialize your android device sensor capabilities
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
}
@Override
protected void onResume() {
super.onResume();
// for the system's orientation sensor registered listeners
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onPause() {
super.onPause();
// to stop the listener and save battery
mSensorManager.unregisterListener(this);
}
@Override
public void onSensorChanged(SensorEvent event) {
// get the angle around the z-axis rotated
float degree = Math.round(event.values[0]);
tvHeading.setText("Heading: " + Float.toString(degree) + " degrees");
// create a rotation animation (reverse turn degree degrees)
RotateAnimation ra = new RotateAnimation(
currentDegree,
-degree,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f);
// how long the animation will take place
ra.setDuration(210);
// set the animation after the end of the reservation status
ra.setFillAfter(true);
// Start the animation
image.startAnimation(ra);
currentDegree = -degree;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// not in use
}
}
The above source code is my java source code Here i am use TextView for displaying heading, ImageView for displaying the compass and android will provide SensorManager i am using this finding heading
activity_compas.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<TextView
android:id="@+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:layout_marginTop="20dp"
android:text="Heading: 0.0" />
<ImageView
android:id="@+id/imageViewCompass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvHeading"
android:layout_centerHorizontal="true"
android:src="@drawable/compass" />
</RelativeLayout>
Above is my layout file and following is my out video
For this we don't need any permissions in Android Manifest file
Saturday, March 21, 2015
Capture photos Using in Android pragmatically
Most Android devices have at least one camera. Some devices have a front and a back facing camera.
Using the camera on the Android device can be done via the integration of existing camera application. In this case you would start the existing Camera application via an intent and use the return data of the application to access the result .
One more we can do it same work using Camera API this process little bit difficult. Today i am going to explain using Intent before going start i have few questions
what is intent ?
what is return type?
where it will return?
Once are you above questions you can easily understand my post
Following is my source codes of Androidphoto project it works successfully
PhotoActivity.java
package com.vamsi.androidphoto;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class PhotoActivity extends Activity {
private Button photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
photo =(Button)findViewById(R.id.photo);
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// we will handle the returned data in onActivityResult
startActivityForResult(captureIntent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("PhotoActivity", "onActivityResult");
}
}
Using the camera on the Android device can be done via the integration of existing camera application. In this case you would start the existing Camera application via an intent and use the return data of the application to access the result .
One more we can do it same work using Camera API this process little bit difficult. Today i am going to explain using Intent before going start i have few questions
what is intent ?
what is return type?
where it will return?
Once are you above questions you can easily understand my post
Following is my source codes of Androidphoto project it works successfully
PhotoActivity.java
package com.vamsi.androidphoto;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class PhotoActivity extends Activity {
private Button photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
photo =(Button)findViewById(R.id.photo);
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// we will handle the returned data in onActivityResult
startActivityForResult(captureIntent,1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("PhotoActivity", "onActivityResult");
}
}
The above class when they press button it will open camera activity next after taking photo it will come back onActivityResult . This onActivityResult have three parameters first one requestcode this code will sent when ever we start intent currently this code is 1 and resultCode is photo capture is successfully or not and data is for parsing photo like we can convert as bitmap or we can store a file in sdcard but currently i printing log message that place what ever you want do operation.
activity_photo.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"
>
<Button
android:id="@+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="144dp"
android:text="Photo" />
</RelativeLayout>
This my layoutfile of my project this layout file i used one button when ever user press it will open capture activity.
For doing above operation we need some permissions in manifest file
<uses-permission android:name="android.permission.CAMERA"/>
Thank you studying my blog-spot you have any doubts please post comment section .
Thursday, November 20, 2014
ViewFlipper in Android
Today I am going this in this post ViewFlipper in Android before going code we know what is ViewFlipper and what is use first thing is ViewFlipper is a some kind Viewer we can use Any business kind of applications we can use first of all if suppose 5 products is there we can show using viewflipper it will show same place all images with span of time today i am going present viewflipper with example code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ViewFlipper
android:id="@+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/images" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image4" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image5" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout03"
android:layout_below="@+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/Previous"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous" >
</Button>
<Button
android:id="@+id/Next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" >
</Button>
</RelativeLayout>
</LinearLayout>
This layout file main activity and it consists of ViewFlipper,Buttons and ImageView inside RelativeLayout and LinearLayout
This are my images
MainActivity.java
package com.example.viewflipper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
Button Next, Previous;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
Next = (Button) findViewById(R.id.Next);
Previous = (Button) findViewById(R.id.Previous);
viewFlipper.setAutoStart(true);
viewFlipper.setFlipInterval(1000);
viewFlipper.startFlipping();
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewFlipper.showNext();
}
});
Previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewFlipper.showPrevious();
}
});
}
}
This application two buttons one is for when ever user press it will show previous image and one for next image it will show if user still show different images based on viewflipper configuration following video is finally my application output
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ViewFlipper
android:id="@+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/images" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image3" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image4" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/image5" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout03"
android:layout_below="@+id/RelativeLayout02"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/Previous"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous" >
</Button>
<Button
android:id="@+id/Next"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" >
</Button>
</RelativeLayout>
</LinearLayout>
This layout file main activity and it consists of ViewFlipper,Buttons and ImageView inside RelativeLayout and LinearLayout
![]() |
This are my images
MainActivity.java
package com.example.viewflipper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
Button Next, Previous;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
Next = (Button) findViewById(R.id.Next);
Previous = (Button) findViewById(R.id.Previous);
viewFlipper.setAutoStart(true);
viewFlipper.setFlipInterval(1000);
viewFlipper.startFlipping();
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewFlipper.showNext();
}
});
Previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
viewFlipper.showPrevious();
}
});
}
}
This application two buttons one is for when ever user press it will show previous image and one for next image it will show if user still show different images based on viewflipper configuration following video is finally my application output
If you any doubts and comments are always welcome
Tuesday, November 18, 2014
SMS application using Android mobile
Today every body using SMS sent to other means friends and business purpose etc.. This post i will decided to explain SMS based application for that i will with example and my outputs. Following code my SMS project that user enter number and message after press button it will retrieve information using EditText and it will send destination number
activity_sms_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"
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:background="@drawable/buttonshape"
tools:context="com.example.smssent.SmsMainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TO"
android:textSize="24dp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:hint="Enter 10 digits only"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:text="Message"
android:textSize="24dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:ems="10"
android:hint="Enter message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="15dp"
android:text="SENT" />
</RelativeLayout>
activity_sms_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"
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:background="@drawable/buttonshape"
tools:context="com.example.smssent.SmsMainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="TO"
android:textSize="24dp" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:ems="10"
android:hint="Enter 10 digits only"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:text="Message"
android:textSize="24dp" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:ems="10"
android:hint="Enter message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginTop="15dp"
android:text="SENT" />
</RelativeLayout>
This my main layout file it under resource folder it consists of 2 TextView, 2 EditText and Button when ever user press button it will send message and background is "buttonshape" for source code press following link
SmsMainActivity,java
package com.example.smssent;
import android.support.v7.app.ActionBarActivity;
import android.telephony.SmsManager;
import android.util.Log;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SmsMainActivity extends ActionBarActivity {
EditText ephoneno,emessage;
Button sent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms_main);
ephoneno=(EditText) findViewById(R.id.editText1);
emessage=(EditText) findViewById(R.id.editText2);
sent=(Button) findViewById(R.id.button1);
sent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String num=ephoneno.getText().toString();
String message=emessage.getText().toString();
Log.d("SmsMainActivity","phone number:"+num+"message:"+message);
try{
SmsManager smsM=SmsManager.getDefault();
smsM.sendTextMessage(num,null, message, null,null);
Toast.makeText(getApplicationContext(), "message sent", Toast.LENGTH_LONG).show();
}catch(Exception ex){
Toast.makeText(getApplicationContext(), ex.getMessage().toString(),Toast.LENGTH_LONG).show();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sms_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This one having Button when ever user press button it will sent message to destination number following is my screen shots
This is main screen of project and user have to enter phone number and message after press button it will sent to destination number
After sent will it will show Toast message for indicating successfully sent
This is main screen of project and user have to enter phone number and message after press button it will sent to destination number
After sent will it will show Toast message for indicating successfully sent
This is SmsMainActivity.java
Accelerometer using Android mobile phone
Hi friends ,
Today I am going to explain Accelerometer using existing hardware inside Android mobile first you have to know why we have to learn Accelerometer answer is i think all most android apps they using indirectly some sensors preexisting mobile hardware and especially Accelerometer is used swipe application and touch based application so Accelerometer is very use-full sensor one of existing.
Today this i will discuss with Accelerometer sensor following is my source code of project.
activity_accelerometer.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"
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="com.example.accelerometer.AccelerometerActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="64dp"
android:layout_marginTop="52dp"
android:textSize="24dp"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:textSize="24dp"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:textSize="24dp"
/>
</RelativeLayout>
AccelerometerActivity.java
package com.example.accelerometer;
import android.support.v7.app.ActionBarActivity;
import android.annotation.SuppressLint;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class AccelerometerActivity extends ActionBarActivity implements SensorEventListener {
TextView tvx,tvy,tvz;
SensorManager sensormanager;
@SuppressLint("ServiceCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accelerometer);
tvx=(TextView) findViewById(R.id.textView1);
tvy=(TextView) findViewById(R.id.textView2);
tvz=(TextView) findViewById(R.id.textView3);
sensormanager=(SensorManager) getSystemService(SENSOR_SERVICE);
sensormanager.registerListener(this, sensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.accelerometer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
// assign directions
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
tvx.setText("X: "+x);
tvy.setText("Y: "+y);
tvz.setText("Z: "+z);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
}
This java file inside first sensor initialization with type of Accelerometer sensor and implements SensorEventListener when values are changed it will onSensorChanged subroutine (function) you can see following video is my out of above code
This is video of my Accelerometer project any it comes to end you any doubts and comments will be accepted
Saturday, November 15, 2014
Find out battery level of android mobile
Today i am going explain how to calculate battery level of android mobile first of all are know with out battery we can't use mobile phone so battery is very important. Today i will display battery percentage on screen using android API any now i will explain battery level with example following code is my battery level finder.
I am using broadcast-receiver it is used when battery level changed it will update
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"
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="com.example.battery.MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/textView1" />
</RelativeLayout>
MainActivity.java
package com.example.battery;
import android.support.v7.app.ActionBarActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView battery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
battery =(TextView) findViewById(R.id.textView2);
batterylevelfinder();
}
private void batterylevelfinder() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale;
}
battery.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This main java file and i register broadcast-receiver for finding battery level when ever it changed it will display on screen using text-view following is my screenshot of project
This output of battery level
I am using broadcast-receiver it is used when battery level changed it will update
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"
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="com.example.battery.MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/textView1" />
</RelativeLayout>
MainActivity.java
package com.example.battery;
import android.support.v7.app.ActionBarActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView battery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
battery =(TextView) findViewById(R.id.textView2);
batterylevelfinder();
}
private void batterylevelfinder() {
BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale;
}
battery.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
This main java file and i register broadcast-receiver for finding battery level when ever it changed it will display on screen using text-view following is my screenshot of project
This output of battery level
Friday, November 14, 2014
Create custom button in Android using xml file
Today i am going to explain customized buttons in android using xml file. In our daily life we are developing number of new applications we are using number of buttons also but may some body knows how to customize button in android, this will new in android programming , I will explain taking example code with proof screenshots of concept.
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"
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="com.example.custmisedbuttons.MainActivity" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginTop="36dp"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_below="@+id/button3"
android:layout_marginTop="22dp"
android:layout_toLeftOf="@+id/button3"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_toRightOf="@+id/button3"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignLeft="@+id/button5"
android:text="Button" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_toRightOf="@+id/button2"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Button" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button5"
android:layout_marginTop="34dp"
android:layout_toLeftOf="@+id/button5"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Button" />
</RelativeLayout>
This is my layout of main activity and this one having buttons with relativelayout
buttonshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#8240A8"
android:startColor="#58E862"
android:endColor="#D6FFF1"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="30dp"
/>
<stroke
android:width="3dp"
android:color="#75AD3E"
/>
</shape>
This main file for customizing button above code will generate following screenshot
Inside layout file i am used for above screen shot is buttonshape.xml if customize colors according you simple change red co-lour digits it will customize according you. following are some of screenshots of my project
This are screenshots of after changing colors please give feedback about my blog it is very useful for me.
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"
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="com.example.custmisedbuttons.MainActivity" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_marginTop="36dp"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_below="@+id/button3"
android:layout_marginTop="22dp"
android:layout_toLeftOf="@+id/button3"
android:text="Button" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_toRightOf="@+id/button3"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_alignLeft="@+id/button5"
android:text="Button" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginLeft="22dp"
android:layout_marginTop="28dp"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_toRightOf="@+id/button2"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Button" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_below="@+id/button5"
android:layout_marginTop="34dp"
android:layout_toLeftOf="@+id/button5"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Button" />
</RelativeLayout>
This is my layout of main activity and this one having buttons with relativelayout
buttonshape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="14dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#8240A8"
android:startColor="#58E862"
android:endColor="#D6FFF1"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="100dp"
android:height="30dp"
/>
<stroke
android:width="3dp"
android:color="#75AD3E"
/>
</shape>
This main file for customizing button above code will generate following screenshot
Inside layout file i am used for above screen shot is buttonshape.xml if customize colors according you simple change red co-lour digits it will customize according you. following are some of screenshots of my project
Subscribe to:
Posts (Atom)











