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

findViewbyID - How to Find a RatingBar using ID

findViewbyId was added in API Level 1
findViewById: Look for a child RatingBar with the given id. If this view has a given id, return this RatingBar. The findViewById() method is available to all activities in Android. findViewById allows you to find a view inside an activity layout.

1. Name a RatingBar, in XML named myRatingBar.

2. Declare a RatingBar named myRatingBar.

3. In MainActivity, java file, add the below code to the onCreate method. Where R.id.myRatingBar is the same as the android:id name in your main.xml file, and myRatingBar is the RatingBar name referred to in Java. This returns a RatingBar class and is casted to a RatingBar type so you can work with it.

myRatingBar = (RatingBar) findViewById(R.id.myRatingBar); 

4. Compile and run!

Resources:

How to Declare a RatingBar

RatingBar was added in API Level 1
A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar.

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

import android.widget.RatingBar;

2. Add the below line to the onCreate method. This will declare a new RatingBar named myRatingBar.

RatingBar myRatingBar;

3. Compile and run!

Next Recommended Article: findViewbyID - How to Find a RatingBar using ID

Resources:
http://developer.android.com/reference/android/widget/RatingBar.html

android:id - How to Name a Rating Bar, in XML

android:id was added in API Level 1
This is a unique identifier name for the RatingBar so you can refer to the RatingBar later in your project.

1. Add a Rating Bar in your main.xml file.

2. In the main.xml file, add the code below between the RatingBar tags.This will name the RatingBar myRatingBar.

  <RatingBar
        ...
       android:id = "@+id/myRatingBar"
  />

3. The "+" character is required if you have not yet defined that id, if perhaps the element you reference is lower in the .xml file.

Otherwise you if it's already defined above (or included) then you can omit the "+" char:

4. Compile and run!

Next Recommended Article: How to Declare a RatingBar

Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:id


How to Add Rating Bar, in XML

RatingBar was added in API Level 1
A RatingBar is an extension of SeekBar and ProgressBar that shows a rating in stars. The user can touch/drag or use arrow keys to set the rating when using the default size RatingBar.




1. In the main.xml file, add the below code after your layout tags.

<RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
 />

2. Compile and run!

Next Recommended Article: android:id - How to Name a Rating Bar, in XML

Resources:
http://developer.android.com/reference/android/widget/RatingBar.html

How to Close an Activity

public void finish () was added in API level 1
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

In this example, let's say you would like to close the activity when a button is pushed.
1. Add an OnClick Action to a Button

2. In the onClickmyButton method, add the below line. This will close the currently displayed activity when the user presses the button.

finish();
3. Compile and run!

Resources:
http://developer.android.com/reference/android/app/Activity.html#finish()
http://stackoverflow.com/questions/15393899/how-to-close-activity-and-go-back-to-previous-activity-in-android


How to Align an ImageView to the Right in a View

android:layout_alignParentRight was added in API Level 1
If true, makes the right edge of this view match the right edge of the parent. Accommodates right margin.
Must be a boolean value, either "true" or "false".

1. In your main.xml file, add a RelativeLayout.

2. Add an ImageView  to the main.xml file.

3. Add the below line between the <ImageView> tags. This will move the ImageView to the Right of the view within its parent.

android:layout_alignParentRight = "true"

4. Compile and run!

Next Recommended Article: android:gravity - Move Text inside a EditText (center, top, bottom, right, left, and more)

Resources:
developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignParentRight

How to Align an EditText to the Bottom in a View

android:layout_alignBottom was added in API Level 1
Makes the bottom edge of this view match the bottom edge of the given anchor view ID. Accommodates bottom margin.

1. In your main.xml file, add a RelativeLayout.

2. Add an EditText to the main.xml file.

3. Add the below line between the <EditText> tags. This will move the EditText to the bottom of the view within its parent.

android:layout_alignParentBottom = "true"

4. Compile and run!

Next Recommended Article: android:gravity - Move Text inside a EditText (center, top, bottom, right, left, and more)

Resources:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#attr_android:layout_alignBottom

android:padding - How to Set the Padding on a ProgressBar

android:padding was added in API Level 1
Sets the padding, in pixels, of all four edges. Padding is defined as space between the edges of the view and the view's content. A view size will include it's padding.

1. Add a ProgressBar, in XML in the main.xml file.

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

  <ProgressBar
        ...
       android:padding = "20dp"
  />

3. Compile and run!

Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:padding
http://developer.android.com/reference/android/R.attr.html#padding
http://android-coding.blogspot.com/2011/10/using-android-device-build-in-light.html

android:progress - How to Set the Default Progress Value of a ProgressBar

public static final int progress was added in API level 1
Defines the default progress value, between 0 and max.
Must be an integer value, such as "50".

1. Add a ProgressBar, in XML

2. Add the below line between the ProgressBar tags. This will set the progress on the ProgressBar to 20.

<ProgressBar
    android:progress="20"
/>

3. Compile and run!

Resources:

android:max - How to Set the Maximum Value for a ProgressBar, in XML

public static final int max was added in API level 1
Defines the maximum value the progress can take.
Must be an integer value, such as "100".
By default, the progress bar is full when it reaches 100, a max value is not set.

1. Add a ProgressBar, in XML

2. Add the below line between the ProgressBar tags. This will set the maximum value of the ProgressBar to 300.

<ProgressBar
    android:max="300"
/>

3. Compile and run!

Resources:
http://developer.android.com/reference/android/widget/ProgressBar.html
http://developer.android.com/reference/android/R.attr.html#max
http://android-coding.blogspot.com/2011/10/using-android-device-build-in-light.html

How to Change the ProgressBar to a Horizontal Progress Bar

By default, the progress bar is a spinning wheel (an indeterminate indicator).
To change to a horizontal progress bar, apply one of the styles below.



2. Add the below style between the ProgressBar tags. This will give the bar a thick line, with grey background and orange progress meter.

<ProgressBar
    style="@android:style/Widget.ProgressBar.Horizontal"
    ...
 />


3. Compile and run!

4. To have a thin progress bar, use this style below instead.

style="?android:attr/progressBarStyleHorizontal"

How to Set a Float Variable


1. Declare a Float Variable named myFloat.

2. Add the below line to the onCreate method. This will set the float variable named myFloat to 3.8644f.

myFloat = 3.8644f;

3. Compile and run!

Resources:
http://developer.android.com/reference/java/lang/Float.html


How to Set the Maximum Upper Range of a ProgressBar

public synchronized void setMax (int max) was added in API level 1
Set the range of the progress bar to 0...max.
Parameters: max the upper range of this progress bar

1. Find a ProgressBar using ID named myProgressBar.

2. Add the line below to the onCreate method. This will set the maximum upper range of the myProgressBar to 100.

How to Declare a Float Variable

The Float class wraps a value of the primitive type float in an object. An object of type Float contains a single field whose type is float.

Float data type is a single-precision 32-bit IEEE 754 floating point.
Float is mainly used to save memory in large arrays of floating point numbers.
Default value is 0.0f.
Float data type is never used for precise values such as currency.
Example: float myFloat = 234.5f

1. In the MainActivity.java file, add the below to the onCreate method. This will create a Float variable named myFloat.

float myFloat; 

2. Compile and run!

Resources:
http://developer.android.com/reference/java/lang/Float.html
http://www.tutorialspoint.com/java/java_basic_datatypes.htm

How to Get a Reference to SensorManager

public abstract Object getSystemService (String name) was added in API level 1
Return the handle to a system-level service by name. The class of the returned object varies by the requested name.

1. In the MainActivity.java file, Get the Context.SENSOR_SERVICE and save to a string named svcName.

2. In the onCreate method, Declare a SensorManager named mySensorManager.

3. In the onCreate method, add the following line. This will get a reference to the SensorManager saved to mySensorManager.

mySensorManager = (SensorManager)getSystemService(svcName);

4. Compile and run!

Next Recommended Article:

Resources:
Professional Android 4 Application Development by Reto Meier , pg 482
http://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.String)

How to Get the the Context.SENSOR_SERVICE

public static final String SENSOR_SERVICE was added in API level 1
Use with getSystemService(String) to retrieve a SensorManager for accessing sensors.

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

 import android.content.Context;

2. In the MainActivity.java file, add the below line to the onCreate method. This will save the SENSOR_SERVICE to a string named svcName. 

String svcName = Context.SENSOR_SERVICE;

3. Compile and run!

Next Recommended Article: How to Declare a SensorManager

Resources:
Professional Android 4 Application Development by Reto Meier, pg 482
http://developer.android.com/reference/android/content/Context.html#SENSOR_SERVICE

How to Declare a SensorManager

SensorManager was added in API Level 1
SensorManager lets you access the device's sensors

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

import android.hardware.SensorManager;

2. Add the below line to the onCreate method. 

findViewbyID - How to Find a ProgressBar using ID

findViewbyId was added in API Level 1
findViewById: Look for a child ProgressBar with the given id. If this view has a given id, return this ProgressBar. The findViewById() method is available to all activities in Android. findViewById allows you to find a view inside an activity layout.

1. Name a ProgressBar, in XML named myProgressBar.

2. Declare a ProgressBar named myProgressBar.

3. In MainActivity, java file, add the below code to the onCreate method. Where R.id.myProgressBar is the same as the android:id name in your main.xml file, and myProgressBar is the ProgressBar name referred to in Java. This returns a ProgressBar class and is casted to a ProgressBar type so you can work with it.

myProgressBar = (ProgressBar) findViewById(R.id.myProgressBar); 

4. Compile and run!

Resources:

android:id - How to Name a ProgressBar, in XML

android:id was added in API Level 1
This is a unique identifier name for the Progressbar so you can refer to the Progressbar later in your project.

1. Add a ProgressBar in your main.xml file.

2. In the main.xml file, add the code below between the ProgressBar tags.This will name the ProgressBar myProgressBar.

  <Progressbar
        ...
       android:id = "@+id/myProgressBar"
  />

3. The "+" character is required if you have not yet defined that id, if perhaps the element you reference is lower in the .xml file.

Otherwise you if it's already defined above (or included) then you can omit the "+" char:

4. Compile and run!

Next Recommended Article: How to Declare a ProgressBar

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

How to Add a ProgressBar, in XML

Progressbar was added in API Level 1
Visual indicator of progress in some operation.
By default, the progress bar is a spinning wheel (an indeterminate indicator)

Default ProgressBar
1. In the main.xml file, add the below code after your layout tags.

<ProgressBar
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
/>

2. Compile and run!

Next Recommended Article: android:id - How to Name a ProgressBar, in XML

Resources:
http://developer.android.com/reference/android/widget/ProgressBar.html