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

How to Get a Default Instance of SmsManager

public static SmsManager getDefault () was added in API level 4
Get the default instance of the SmsManager
Returns: the default instance of the SmsManager

1. In the MainActivity.java file, Declare a SmsManager object named mySmsManager. 

2. In the onCreate method, add the below line. This will get the default instance of the SmsManager and save it to a SmsManager object named mySmsManager. 

mySmsManager = SmsManager.getDefault();

How to Declare a SmsManager object

SmsManager was added in API Level 1
Manages SMS operations such as sending data, text, and pdu SMS messages.

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

import android.telephony.SmsManager

2. In the onCreate method, add the below line. This will create a SmsManager object named mySmsManager.

SmsManager mySmsManager;

3. Compile and run!

4. Next, Get a Default Instance of SmsManager

Resources:
- Professional Android 4 Application Development by Reto Meier, pg 714
- http://developer.android.com/reference/android/telephony/SmsManager.html

android:onClick - Add an Action to a TextView Click

How to Add an Action to a TextView Click

android:onClick was added in API Level 4

Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View.

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

 2. Ensure to change the minSDKVersion to 4, or greater.

 3. Add these imports to the import section.

            import android.view.View;

 4. In the MainActivity.java file, add a new method called onClickmyTextView.

public void onClickmyTextView(View v) {
}

 5. Add your action to the newly created onClickmyTextView.
    For testing purposes, let's add a Toast inside the onClickmyTextView method.
    First, add this import to the imports section.

              import android.widget.Toast;

 6. Add the toast to the onClickmyTextView method.

Toast.makeText(this, "You clicked myTextView", Toast.LENGTH_SHORT).show();

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

  <TextView
        ...
       android:onClick = "onClickmyTextView"
  />

 8. And lastly, we need to make the TextView clickable.

9. Compile and run! Click on the TextView to test!

Related Articles:
Resources:

android:contentDescription - Set the Content Description of a Button

How to Set the Content Description of a Button

android:contentDescription was added in API Level 4

Defines text that briefly describes content of the view. This property is used primary for accessibility. Since some views do not have textual representation this attribute can be used for providing such.
(TextView/EditText Since a textview/edittext has a textual representation, this would probably not be used in a TextView/EditText, but could be.)

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

2. Ensure to change the minSDKVersion to 4, or greater.

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

  <Button
        ...
       android:contentDescription = "Save Button"
  />

4. Compile and run!

Related Articles:

Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:contentDescription
http://developer.android.com/reference/android/R.attr.html#contentDescription
http://developer.android.com/training/accessibility/accessible-app.html
http://www.grokkingandroid.com/steps-to-making-android-app-accessible/
http://developer.android.com/guide/topics/ui/accessibility/apps.html
developer.android.com/guide/topics/ui/accessibility/checklist.html

android:onClick - Add an OnClick Action to a Button

How to Add an OnClick Action to a Button

android:onClick was added in API Level 4

Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View.

 1. Add a button to your layout.

 2. Ensure to change the minSDKVersion to 4, or greater. If you are targeting API Level 3 and under, you can use setOnClickListener for creating a Click Handler for a Button, using a method instead.

 3. In the MainActivity.java file, add this import to the import section.

            import android.view.View;

 4. Add a new method called onClickmyButton. (Must be set to public and not private.)

public void onClickmyButton(View v) {
// your onClick code goes here //Add a toast to test it.
}

5. In the main.xml file, add the highlighted code between the <Button> tags.

  <Button
        ...
       android:onClick = "onClickmyButton"
  />

6. Compile and run! Press the Button to test!

Next Recommended Article: Name a Button

Related Articles:
Resources: