Showing posts with label CheckBox. Show all posts
Showing posts with label CheckBox. Show all posts

How to Set the CheckBox TextSize, in Java

public void setTextSize (int unit, float size) was added in API Level 1
Set the default text size to a given unit and value. See TypedValue for the possible dimension units.
Related XML Attributes: android:textSize
Parameters:
unit - The desired dimension unit.
size - The desired size in the given units.
Size of the text. Recommended dimension type for text is "COMPLEX_UNIT_SP".

1. Add a CheckBox in the main.xml file or Instantiate a new CheckBox object named myCheckBox.

2. Add the below line to the import section.

import android.util.TypedValue;

2. In the MainActivity.java file, add the code below. This will set the TypedValue to SP, and the size to 18.

myCheckBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); 

3. Other TypedValue options are:

COMPLEX_UNIT_DIP - value is Device Independent Pixels
COMPLEX_UNIT_IN - value is in inches
COMPLEX_UNIT_MM - value is in millimeters
COMPLEX_UNIT_PT - value is in points
COMPLEX_UNIT_PX - value is raw pixels
COMPLEX_UNIT_SP - value is a scaled pixel

4. Compile and run!

Resources:
http://developer.android.com/reference/android/widget/TextView.html#setTextSize(int, float)
http://developer.android.com/reference/android/R.attr.html#textSize
http://developer.android.com/reference/android/util/TypedValue.html
http://alvinalexander.com/java/jwarehouse/android/core/java/android/content/res/Resources.java.shtml
http://stackoverflow.com/questions/9494037/how-to-set-text-size-of-textview-dynamically-for-diffrent-screens

findViewById - Find a CheckBox using ID

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

1. Name a CheckBox myCheckBox.

2. Declare a CheckBox named myCheckBox..

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

myCheckBox = (CheckBox) findViewById(R.id.myCheckBox); 

4. Compile and run!

Resources:

android:id - How to Name a CheckBox

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

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

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

  <CheckBox
        ...
       android:id = "@+id/myCheckBox"
  />

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: Find a CheckBox using ID

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 CheckBox to an Activity, using XML

CheckBox was added in API Level 1

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

2. Open the main.xml file and add the below code to add a CheckBox to the XML file between the Layout tags.

<CheckBox   
    android:layout_width=”wrap_content”
    android:layout_height=”wrap_content”
     />

3. Compile and run!

Next Recommended Article: android:id - How to Name a CheckBox

How to Set the CheckBox Text, in Java

setText was added in API Level 1
Sets the text that this CheckBox is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.

1. Find a TextView using ID if using XML, or Instantiate a new CheckBox, in Java named myCheckBox.

2. In the MainActivity.java file, add the code below to the onCreate method 

myCheckBox.setText("This is new text from setText"); 

3. Compile and run!

Resources:

How to Instantiate a new CheckBox object


1. Delcare a New CheckBox, in Java named myCheckBox.

2. Add the below line to the onCreate method.

myCheckBox = new CheckBox(this);

3. Compile and run!

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


How to Declare a New CheckBox, in Java

CheckBox was added in API Level 1
A checkbox is a specific type of two-states button that can be either checked or unchecked.

1. Add the below import to the imports section.

import android.widget.CheckBox;

2. Add the below line to the onCreate method. This will create a new CheckBox object named myCheckBox.

CheckBox myCheckBox;

3. Compile and run!

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