Friday 7 February 2014

Gravity Sensor Details

Gravity Sensor:

Gravity Sensor is a unique feature that allows applications to take advantage of the phone's real-world physical position in your hand.Also the Gravity sensor is the ability to play games without using your fingers and u can also move it side to side and it fit to the screen.And programmatically The gravity sensor provides a three dimensional vector indicating the direction and magnitude of gravity.

List of Gravity Sensors and Dialog

gravity Sensor detailsgravity Sensor details



Create new Android Project
Project Name: SensorDetails
//tested from 2.3.3 to current android sdk 
Build Target: Android 2.3.3   //or greater than that
Application Name: Sensor Details
Package Name: com.shaikhhamadali.blogspot.sensordetails
Create Activity file: SensorDetails


1.code of activity:


package com.shaikhhamadali.blogspot.sensordetails;

import java.util.List;

import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;

public class SensorDetails extends ListActivity {
 //declare Variables
 //SensorManager lets you access the device's sensors

 private SensorManager mySensorManager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //create instance of sensor manager and get system service to interact with Sensor
  mySensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
  //Check the availability of sensor
  if (mySensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY) != null){
   //create instance of List and get Gravity Sensor from Sensor Manager 
   List<Sensor> gravitySensorList = mySensorManager.getSensorList(Sensor.TYPE_GRAVITY);
   //Set ListAdapter of Sensor  
   setListAdapter(new ArrayAdapter<Sensor>(this,android.R.layout.simple_list_item_1,gravitySensorList));
  }else{
   //if Sensor Not found show Toast
   Toast.makeText(getApplicationContext(), "No Gravity Sensor!", Toast.LENGTH_LONG).show();
  }
 }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) {
  //Get position of item click and create instance of sensor 
  Sensor sensor = (Sensor)l.getItemAtPosition(position);
  //Create instance of Alert dialog 
  AlertDialog.Builder sensorDialog = new AlertDialog.Builder(SensorDetails.this);
  //set title
  sensorDialog.setTitle(sensor.getName());

  @SuppressWarnings("unchecked")
  //create instance of class and get Sensor class
  Class<Sensor> sensorClass = (Class<Sensor>)sensor.getClass();
  //get details of sensor like Vendor, Version Type etc
  String vendor = sensor.getVendor();
  int version = sensor.getVersion();
  int type = sensor.getType();
  int minDelay = sensor.getMinDelay();
  float power = sensor.getPower();
  float maxRange = sensor.getMaximumRange();
  float resolution = sensor.getResolution();
  //Create instance of LinearLayout for alert dialog
  LinearLayout dialogLayout = new LinearLayout(getApplicationContext());
  dialogLayout.setBackgroundColor(Color.BLACK);
  //Create instance of Layout Params Width match parent and Height wrap content
  LayoutParams dialogLayoutParams = new LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  //Set layout params to LinearLayout
  dialogLayout.setLayoutParams(dialogLayoutParams);
  //Set Orientation of Layout as Vertical
  dialogLayout.setOrientation(LinearLayout.VERTICAL);
  //Create Controls to add in Linear Layout and show details of Sensor  
  TextView textSensorClass = new TextView(getApplicationContext());
  textSensorClass.setText(sensorClass.toString());
  TextView textVendor = new TextView(getApplicationContext());
  textVendor.setText("Vendor: " + vendor);
  TextView textVersion = new TextView(getApplicationContext());
  textVersion.setText("Version: " + String.valueOf(version));
  TextView textType = new TextView(getApplicationContext());
  textType.setText("Type: " + String.valueOf(type));
  TextView textMinDelay = new TextView(getApplicationContext());
  textMinDelay.setText("MinDelay: " + String.valueOf(minDelay) + " microsecond");
  TextView textPower = new TextView(getApplicationContext());
  textPower.setText("Power: " + String.valueOf(power) + " mA");
  TextView textMaxRange = new TextView(getApplicationContext());
  textMaxRange.setText("MaximumRange: " + String.valueOf(maxRange));
  TextView textResolution = new TextView(getApplicationContext());
  textResolution.setText("Resolution: " + String.valueOf(resolution));
  //Add Views in Linear Layout
  dialogLayout.addView(textSensorClass);
  dialogLayout.addView(textVendor);
  dialogLayout.addView(textVersion);
  dialogLayout.addView(textType);
  dialogLayout.addView(textMinDelay);
  dialogLayout.addView(textPower);
  dialogLayout.addView(textMaxRange);
  dialogLayout.addView(textResolution);
  //Create Instance of ScrollView 
  ScrollView scrollView = new ScrollView(getApplicationContext());
  // Set Layout Params of ScrollView width match parent and height warp content
  LayoutParams scrollViewLayoutParams = new LayoutParams(
    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  //Set Layout Params
  scrollView.setLayoutParams(scrollViewLayoutParams);
  //Add LAyout in ScrollView
  scrollView.addView(dialogLayout);
  //Set View on Dialog as ScrollView
  sensorDialog.setView(scrollView);
  //Show Dialog
  sensorDialog.show();
 }

}

2. note that:

  • you can use SensorManager to find out the available sensors in device and also selected type sensors as I use Gravity Sensor.
  • You can get all the details from Sensors as I use Gravity Sensor  
  • Learn more about Sensors and multiple types of sensors.

      3. conclusion:

      • Some information about how to get Description list of device Sensors.
      • know what are Sensors and how to use Sensors.
      • Know how to Create Alert Dialog with Dynamic Layout.

        4. About the post:

        • The code seems to explain itself due to comments, but if you have any questions you can freely ask too!
        •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
        •  Hope you enjoy it!
            5. Source Code:
                    you can download the source code GoogleDrive, Github

            Cheers,
            Hamad Ali Shaikh