Showing posts with label API Level 3. Show all posts
Showing posts with label API Level 3. Show all posts

How to Set ProgressBar Progress on Light Sensor Change

API Level 3 or greater needed.


1. Set the Upper Range of a ProgressBar to the Maximum Range of a Light Sensor named myProgressBar.

2. Determine if Sensor Light Changed

3. In the onSensorChanged method, add the following line after the float myEventValues is created. This convert the float value of myEventValues to an integer and set it to the Progress on the myProgressBar.

myProgressBar.setProgress((int)myEventValues);

4. Compile and run!

Resources:

How to Determine if Sensor Light Changed

Added in API Level 3
Sensor.TYPE_LIGHT:
values[0]: Ambient light level in SI lux units

1. Register a SensorEventListener named mySensorEventListener.

2. In the method named onSensorChanged in the SensorEventListener, add the following code. This will test to determine if the sensor type that changed is a Light Sensor (TYPE_LIGHT), then if it is, it declares and sets a new float variable named myEventValue to the saves the event values of the Light Sensor. The next line will display a toast with the current reading of the Light Sensor, which converts the float to a string of float myEventValue.

if(event.sensor.getType()==Sensor.TYPE_LIGHT){
    float myEventValue = event.values[0];
    Toast.makeText(getBaseContext(), "Current Reading: " + String.valueOf(myEventValue), Toast.LENGTH_LONG).show();
}

Resources:


How to Register a SensorEventListener

public boolean registerListener (SensorEventListener listener, Sensor sensor, int rateUs) was added in API level 3
Registers a SensorEventListener for the given sensor.
Note: Don't use this method with a one shot trigger sensor such as TYPE_SIGNIFICANT_MOTION. Use requestTriggerSensor(TriggerEventListener, Sensor) instead.
Parameters:
listener - A SensorEventListener object.
sensor - The Sensor to register to.
rateUs - The rate sensor events are delivered at. This is only a hint to the system. Events may be received faster or slower than the specified rate. Usually events are received faster. The value must be one of SENSOR_DELAY_NORMAL, SENSOR_DELAY_UI, SENSOR_DELAY_GAME, or SENSOR_DELAY_FASTEST or, the desired delay between events in microseconds. Specifying the delay in microseconds only works from Android 2.3 (API level 9) onwards. For earlier releases, you must use one of theSENSOR_DELAY_* constants.
Returns: true if the sensor is supported and successfully enabled.

1. Determine if Android Device has Light Sensor, using SensorManager with SensorManager named mySensorManager, a Sensor named mySensor. 

2. Add a SensorEventListener named mySensorEventListener

3. Add the line below to the onCreate method. This will register the SensorEventListener named mySensorEventListener to the Sensor named mySensor, at a rate of Normal Delay.

mySensorManager.registerListener(mySensorEventListener, mySensor,  SensorManager.SENSOR_DELAY_NORMAL);

4. Compile and run!

5. You can use the below options to change the sensor delay.

SENSOR_DEALY_NORMAL - rate (default) suitable for screen orientation changes
SENSOR_DELAY_UI - rate suitable for the user interface
SENSOR_DELAY_GAMES - rate suitable for games
SENSOR_DELAY_FASTEST - get sensor data as fast as possible

Resources:http://developer.android.com/reference/android/hardware/SensorManager.html#registerListener(android.hardware.SensorEventListener, android.hardware.Sensor, int)

How to Add a SensorEventListener

SensorEventListener was added in API Level 3
Used for receiving notifications from the SensorManager when sensor values have changed.

1. In the MainActivity.java file, add the lines below to the import section.

import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;

2. Add the below code to the onCreate method. This will add a SensorEventListener named mySensorEventListenerToasts were added for testing.

SensorEventListener mySensorEventListener = new SensorEventListener(){

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
Toast.makeText(getBaseContext(), "onAccuracyChanged", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
Toast.makeText(getBaseContext(), "onSensorChanged", Toast.LENGTH_LONG).show();
    }
};

3. Compile and run!

Resources:
http://developer.android.com/reference/android/hardware/SensorEventListener.html
http://developer.android.com/reference/android/hardware/SensorEvent.html

How to Set the Upper Range of a ProgressBar to the Maximum Range of a Light Sensor

For API Level 3 and greater

1. Get the Maximum Range of a Light Sensor and save it so a float named myMaxRange.

2. Set the Maximum Upper Range of a ProgressBar. Instead of setting the max range to 100, we would like to set it to the float named myMaxRange. However, since it is a float, you cannot save a float to an integer. You will need to convert it to an integer first. So, just set the max upper range to (int) myFloat

3. Compile and run!

How to get the Maximum Range of a Light Sensor

public float getMaximumRange () was added in API level 3
Returns: maximum range of the sensor in the sensor's unit.

1. Determine if Android Device has Light Sensor, using SensorManager with a Sensor named mySensor and SensorManager named mySensorManager.

2. In the else of the if/else in step #1,

       a. Declare a Float variable named myMaxRange

       b. Add the below line the float declaration. This will get the maximum range of the light sensor
           and save it to a float variable named myMaxRange

How to Determine if Android Device has Light Sensor, using SensorManager

Needs API Level 3, or greater.
For API Level 7 and above you may use, How to Determine if Android Device has a Light Sensor, using PackageManager.

1. Get the Default Light Sensor with a Sensor named mySensor.

2. Add the below code to the onCreate method. This will test the mySensor to determine if light sensor is available. It will display a toast as determined.

if (mySensor == null){
Toast.makeText(this,  "No Light Sensor Available", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this,  "Light Sensor Available", Toast.LENGTH_LONG).show();
}

3. Compile and run!

Resources:

How to Get the Default Light Sensor

public Sensor getDefaultSensor (int type) was added in API level 3
Use this method to get the default sensor for a given type. Note that the returned sensor could be a composite sensor, and its data could be averaged or filtered. If you need to access the raw sensors use getSensorList.
Parameters: type - of sensors requested
Returns: the default sensors matching the asked type.

1. Declare a Sensor object named mySensor.

2. Get a Reference to SensorManager named mySensorManager.

3. Save Sensor TYPE_LIGHT to integer named intMySensor.

4. Add the below line to the onCreate method. This will get the default light sensor and save it to mySensor.

mySensor = mySensorManager.getDefaultSensor(intMySensor);

5. Compile and run!

Resources:
http://developer.android.com/reference/android/hardware/SensorManager.html#getDefaultSensor(int)
http://android-coding.blogspot.com/2011/10/using-android-device-build-in-light.html

How to Save Sensor TYPE_LIGHT to integer

public static final int TYPE_LIGHT was added in API level 3
A constant describing a light sensor type.

1. Set the Minimum and Target SDK version to 3, or greater. 

2. Declare an integer named intMySensor

3. Add the below line to the onCreate method. This will save the light sensor type(TYPE_LIGHT) to an integer named intMySensor.

intMySensor = Sensor.TYPE_LIGHT;

How to Declare a Sensor object

Sensor was added in API Level 3
Class representing a sensor. Use getSensorList(int) to get the list of available Sensors.

1. Set the Minimum and Target SDK version to 3, or greater. 

2. In the MainActivity.java file, add the below line to the imports section. 

import android.hardware.Sensor;
3. Add the below line to the onCreate method. This will declare a Sensor object named mySensor.

Sensor mySensor; 

4. Compile and run

setRawInputType - How to Display Soft Keyboard on EditText, in Java

public void setRawInputType (int type) was added in API level 3
Directly change the content type integer of the text view, without modifying any other state.
Related XML Attributes: android:inputType

1. Add a EditText in the main.xml file or Instantiate a new EditText object named myEditText.

2. Add the below line to the import section.

import android.text.InputType;

2. In the MainActivity.java file, add the code below. This will set the soft keyboard displayed to be numeric.

myEditText.setRawInputType(InputType.TYPE_CLASS_NUMBER);

3. Compile and run!

4. Other InputType options are list on this page: developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER

Resources:
http://developer.android.com/reference/android/widget/TextView.html#setRawInputType(int)
developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER
http://stackoverflow.com/questions/9933574/edittext-setinputtype-vs-setrawinputtype

setInputExtras

setInputExtras was added in API Level 3
Related XML Attribute: android:editorExtras

setInputExtras (int xmlResId)
Set the extra input data of the text, which is the TextBoxAttribute.extras Bundle that will be filled in when creating an input connection. The given integer is the resource ID of an XML resource holding an <input-extras>XML tree.
Ensure to change the minSDKVersion to 3, or greater.

Related Articles:

Resources:

android:editorExtras

android:editorExtras was added in API Level 3
Related Method: setInputExtras

Reference to an <input-extras> XML resource containing additional data to supply to an input method, which is private to the implementation of the input method. This simply fills in the EditorInfo.extras field when the input method is connected.

Ensure to change the minSDKVersion to 3, or greater.

Related Articles:

Resources:

setCompoundDrawablesWithIntrinsicBounds/android:drawableTop - Add a Drawable Above the Text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable Above the Text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableTop

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

The drawable (image, picture, etc) will be displayed above the text in a TextView.

1. Ensure to change the minSDKVersion to 3, or greater.

2. Add a drawable named myImage

3. Add the following text to the TextView"This is an example with a drawable set above the text in a TextView."

6. Add the code below to the onCreate method.

myTextView.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.myImage,0,0);

7. At this point, the code will work. But to easier see the defines perimeter of the TextView, let's change the background color of the TextView to Gray. Hex: #808080

8. Compile and run!

This is using android:layout = "wrap_content"


9. You can also change the android:layout_width to match_parent, and you get the result below.

This is using android:layout = "match_parent"

Related Articles:

setCompoundDrawablesWithIntrinsicBounds/android:drawableRight - Add a Drawable to the Right of the text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable to the Right of the text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableRight

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

The drawable (image, picture, etc) will be displayed to the right of the text

1. Ensure to change the minSDKVersion to 3, or greater.

2. Add a drawable named myImage

3. Add the following text to the TextView: "This is an example with a drawable set to the right in a TextView."

6. Add the code below to the onCreate method.

myTextView.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.myImage,0);

7. Compile and run!

This is using android:layout_width = "wrap_content"

8. You can also change the android:layout_width to match_parent, and you get the result below.

This is using android:layout_width = "match_parent"


Related Articles:

setCompoundDrawablesWithIntrinsicBounds/android:drawableLeft - Add a Drawable to the Left of the text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable to the Left of the text in a TextView, in Java

setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableLeft 

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

The drawable (image, picture, etc) will be displayed to the left of the text

1. Ensure to change the minSDKVersion to 3, or greater.

2. Add the following text to the TextView: "This is an example with a drawable set to the left in a TextView."


4. Add a drawable named myImage

5. Add the code below to the onCreate method.

myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.myImage,0,0,0);

6. Compile and run!



Related Articles:

setCompoundDrawablesWithIntrinsicBounds/android:drawableBottom - Add a Drawable Below a TextView

setCompoundDrawablesWithIntrinsicBounds - How to Add a Drawable Below a TextView

setCompoundDrawablesWithIntrinsicBounds was added in API Level 3
Related XML Attribute: android:drawableBottom

setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use 0 if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

The drawable (image, picture, etc) will be displayed centered, below the text

1. Ensure to change the minSDKVersion to 3, or greater.


3. Add a drawable named myImage

4. Add the code below to the onCreate method.

myTextView.setCompoundDrawablesWithIntrinsicBounds(0,0,0,R.drawable.myImage);

5. Compile and run!

Related Articles:

android:isScrollContainer

added in API Level 3

Set this if the view will serve as a scrolling container, meaning that it can be resized to to shrink its overall window so that there will be space for an input method.
If not set, the default value will be true if "scrollbars" has the vertical scrollbar set, else it will be false.
(ScrollView, ListView, GridView are all examples of a scrolling container.)

       android:isScrollContainer = "true"
       android:isScrollContainer = "false"

Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:isScrollContainer
http://developer.android.com/reference/android/R.attr.html#isScrollContainer
http://stackoverflow.com/questions/5308247/what-does-androidisscrollcontainer-do


Other Resources:

android:hapticFeedbackEnabled

added in API Level 3

Boolean that controls whether a view should have haptic feedback enabled for events such as long presses.

       android:hapticFeedbackEnabled = "true"
       android:hapticFeedbackEnabled = "false"

Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:hapticFeedbackEnabled
http://developer.android.com/reference/android/R.attr.html#hapticFeedbackEnabled


HorizontalScrollView - Scroll Text with finger in a TextView

How to Scroll Text with finger in a TextView

added in API Level 3

Text wider than the TextView can be scrolled horizontally. Put your finger on the text and move text from side to side to read.

1. Create an Android project, if you don't already have one.

2. Add an TextView in the main.xml file.

3. Ensure the minSDKVersion is, 3 or greater.

4. In the main.xml file, add the code below.

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
        <TextView
                  ...
android:text="This is a very, very, very long line of text to test the HorizontalScrollView in a TextView."
/>
</HorizontalScrollView>

5. Compile and run!
http://developer.android.com/reference/android/widget/HorizontalScrollView.html