Showing posts with label listview. Show all posts
Showing posts with label listview. Show all posts

May 11, 2011

List of available sensors on Android

You can obtain the list of available sensors on your device by calling sensonManager.getSensorList() and providing the type Sensor.TYPE_ALL as a parameter. Find below a full example.




main.xml


    

main.java
package com.blog.sensorslist;

import java.util.List;

import android.app.ListActivity;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class main extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       // setContentView(R.layout.main);
        
        // create new SensorManager
        SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);
        // get all sensors of all types
        List sensorList = sensorManager.getSensorList(Sensor.TYPE_ALL);
        // list of all sensor types
        String[] sensorTypes = new String[]{"","ACCELEROMETER", "MAGNETIC_FIELD", 
          "ORIENTATION", "GYROSCOPE", "LIGHT", "PRESSURE","TEMPERATURE", 
          "PROXIMITY", "GRAVITY", "LINEAR_ACCELERATION", "ROTATION_VECTOR"};
        
        // array for ListAdapter
        String[] infoList = new String[sensorList.size()];
        
        for (int i =0; i < sensorList.size(); i++){
         Sensor currentSensor = sensorList.get(i);
         infoList[i] = "Sensor Name: "+ currentSensor.getName() +"\n" +
             "Vendor: "+ currentSensor.getVendor() + "\n" +
             "Version: "+ currentSensor.getVersion()+ "\n"+
             "Type: "+ currentSensor.getType() + " - "+sensorTypes[currentSensor.getType()]+ "\n"+
             "Maximum Range: "+ currentSensor.getMaximumRange()+ "\n"+
             "Power (mA):"+ currentSensor.getPower()+ "\n"+
             "Resolution: "+ currentSensor.getResolution();              
        }
        // setting the ListAddapter
        setListAdapter(new ArrayAdapter(this, R.layout.main, infoList));
    }
}

April 29, 2011

How to create a listview with custom adapter on Android

To create a ListView with your custom layout you need to create a custom adapter, override the getView method, and set the list adapter.
Find below an example with a TextView, ImageView and Button in the layout.
















main.xml

    
    
    
    
    




main.java
package com.blog.listviewwithcustomadapter;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class main extends ListActivity {

 public final String[] Colors = { "Blue", "Green", "Black" };

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // setContentView(R.layout.main);
  // instantiate our custom adapter
  MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.main, Colors);
  //setting the adapter
  setListAdapter(adapter);
 }

 public class MyCustomAdapter extends ArrayAdapter {

  public MyCustomAdapter(Context context, int textViewResourceId, String[] list) {
   super(context, textViewResourceId, list);
  }

  @Override
     public View getView(int position, View convertView, ViewGroup parent) {
            
      View row = convertView;

      if (row == null) {
       LayoutInflater inflater = getLayoutInflater();
       row = inflater.inflate(R.layout.main, parent, false);
      }
      final int itemPosition = position;
      
      TextView listItem = (TextView) row.findViewById(R.id.colors );
      listItem.setText(Colors[position]);
      
      ImageView image = (ImageView) row.findViewById(R.id.imgAndroid);
      image.setImageResource(R.drawable.icon);
      
      Button btn = (Button) row.findViewById(R.id.btnColor);
      btn.setOnClickListener( new View.OnClickListener() {
    
    @Override
    public void onClick(View v) {
     Toast.makeText(getApplicationContext(), "Toast from button"+ Colors[itemPosition], Toast.LENGTH_SHORT).show();     
    }
   });
            
      row.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
        Toast.makeText(getApplicationContext(), Colors[itemPosition], Toast.LENGTH_SHORT).show();
       }     
      });
      return row;
     }
 }
}