Showing posts with label Button. Show all posts
Showing posts with label Button. Show all posts

How to Set Button Text Size, 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 Button in the main.xml file or Instantiate a new Button object named myButton.

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 48.

myButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 48); 

3. Compile and run!

4. 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

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

How to Instantiate a new Button, in Java

Button was added in API Level 1

1. Declare a Button named myButton.

2. After you declare the Button object, add this line below to instantiate it. This will instantiate a new Button  named myButton.

myButton = new Button(this);

3. Compile and run!

How to Declare a Button, in Java

Button was added in API Level 1
Represents a push-button widget. Push-buttons can be pressed, or clicked, by the user to perform an action.

1. Add the below import to the imports section.

import android.widget.Button;

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

Button myButton;

3. Compile and run!

Next Recommended Article: findViewById - How to Find a Button using ID

Resources:
developer.android.com/reference/android/widget/Button.html

How to Display an AlertDialog from a Button

Note: Some of the items in these steps are deprecated.
API Level 1

When a button is pressed an AlertDialog will display.

1. Add an OnClick Action to a Button

2. Add a Deprecated: showDialog to the onClickmyButton method with a constant of ALERT_DIALOG.

3. Add a New Method with a Return Variable, with a method named showAlertDialog and a return variable named Dialog.

4. Add a Deprecated: onCreateDialog method to your class with a case for ALERT_DIALOG and within the case the line below. 

return showAlertDialog();

5. In the showAlertDialog method, Create an AlertDialog with Supplied Arguments

6. Compile and run!

Resources:

setOnClickListener - How to Create a Click Handler for a Button, using a method

setOnClickListener was added in API Level 1
Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.
Parameters: the callback that will run

1. In the MainActivity.java file, add a new method called myButtonClickListener.

2. In the imports section, add the below imports.

import android.view.View.OnClickListener;
import android.view.View;

3. In the new method myButtonClickListener:

    a. Find a button using ID and name it myButton.

    b. Next add the below code to the myButtonClickListener method.

myButton.setOnClickListener(new View.OnClickListener() {
    @Override

    public void onClick(View v) {
        //add toast here for testing if you like.
    }
});

4. Add the new method to the onCreate method to initialize.

myButtonClickListener();

5. Compile and run!

Resources:
http://developer.android.com/reference/android/widget/Button.html
http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

setOnClickListener - How to Create a Click Handler for a Button, all within onCreate method

setOnClickListener was added in API Level 1
Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.
Parameters: the callback that will run

1. Find a button using ID and name it myButton.

2. In the imports section, add the below imports.

import android.view.View.OnClickListener;
import android.view.View;

3. In the MainActivity.java file, in the onCreate method, add the following code.

myButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        //add toast here for testing if you like.
    }
});

4. Compile and run!

Next Recommended Article: setOnClickListener - Create a Click Handler for a Button, using a method

Resources:
http://developer.android.com/reference/android/widget/Button.html
http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

setVisibility() - Hide a Button, in Java

How to Hide a Button, in Java

Related XML Attribute: android:visibility

setVisibility() was added in API Level 1

1. Find the Button by ID named myButton.

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

import android.view.View;

3. In the MainActivity.java file, add the below line of code to the onCreate method.

myButton.setVisibility(View.GONE);

4. All options:

mybutton.setVisibility(View.VISIBLE);
mybutton.setVisibility(View.INVISIBLE);
mybutton.setVisibility(View.GONE);

5. Compile and run!

Related Articles:
References:
http://developer.android.com/reference/android/view/View.html#setVisibility(int)
stackoverflow.com/questions/7348150/android-why-setvisibilityview-gone-or-setvisibilityview-invisible-do-not

findViewById - How to Find a Button using ID

How to Find a Button using ID

findViewbyId was added in API Level 1

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

1. In the main.xml file, Name the Button, Name it myButton.

2. In the MainActivity.java file, Declare a Button named myButton.

3. Add the below code to the onCreate method. Where R.id.myButton is the same as the android:id name in your main.xml file, and myButton is the button name referred to in Java. This returns a Button View class and is casted to a Button type so you can work with it.

myButton = (Button) findViewById(R.id.myButton); 

4. Compile and run!

Next Recommended Article: android:onClick - Add an OnClick Action to a Button

Resources:

android:id - How to Name a Button

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

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

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

  <Button
        ...
       android:id = "@+id/myButton"
  />

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!

Related Articles:

Resources:
http://developer.android.com/reference/android/view/View.html#attr_android:id
http://developer.android.com/reference/android/R.attr.html#id
http://android-wtf.com/2012/11/difference-between-at-plus-id-and-at-id-in-android/
http://stackoverflow.com/questions/5025910/difference-between-id-and-id-in-android
http://stackoverflow.com/questions/5048586/can-you-set-tab-order-in-xml-layout

android:visibility - Hide a Button, in XML

How to Hide a Button, in XML

android:visibility was added in API Level 1
Controls the initial visibility of the view.

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

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

  <Button
        ...
       android:visibility = "gone"
  />

3. All options:

visibile - Visible on screen; the default value.
invisible - Not displayed, but taken into account during layout (space is left for it).
gone - Completely hidden, as if the view had not been added.

4. Compile and run!

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:textSize - Set the Text Size of a Button

How to Set/Change the Text Size of a Button

added in API Level 1

Recommended dimension type for text is "sp".

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

2. Add a button.

3. Open the main.xml file and add the below highlighted line of code between your <Button> tags.

<Button
  ...

       android:textSize="50sp"
    />

4. Compile and run!

Resources:
http://developer.android.com/reference/android/R.attr.html#textSize

android:textStyle - Bold Text in a Button

How to Bold Text in a Button

added in API Level 1

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

2. Add a Button.

3. Open the layout, main.xml and add the following highlighted code inside the <Button> tag.

<Button
  ...

     android:textStyle="bold"
/>

android:textColor -How to Set the Text Color of a Button

android:textColor was added in API Level 1
Set the Color of text
1. Add a Button.

2. Open the layout, main.xml and add the following highlighted code inside the <Button> tag. This will set the text color to green.

<Button
     ....
    android:textColor="#00CC00"
/>

setText() - How to Set the Text on a Button, in any method, in Java


1. Add a Button to your xml file.

2. Find a button using ID, named myButton, declared as a class variable. 

3. You can now add the setText line of code to any method. For example, in an onClickButton method.

  public void onClickButton(View v) { //needs import android.view.View;

       myButton.setText("This is the new text!");
 }

4. Compile and run!

android:width - Set/Change the Width of a Button

How to Set/Change the Width of a Button

added in API Level 1

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

2. Add a button.

3. Open the main.xml file and add the below highlighted line to set the height to 130 pixels.

<Button
  ......
android:width="130px"
    />

4. Compile and run!

android:height - Set/Change the Height of a Button

How to Set/Change the Height of a Button

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

2. Add a button.

3. Open the main.xml file and add the below highlighted line to set the height to 100 pixels.

<Button
  ......
android:height="100px"
    />

4. Compile and run!

Related Articles

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:

How to Add a Button to an Activity, in XML

Button was added in API level 1

1. In the main.xml file, and add the below code below your layout tags. This will display a button.

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="My Button Text"
    />

</LinearLayout>

2. Compile and run!

3. This will display a button, however nothing will happen when you click on the button yet though! To do that you'll need to add an onClick to your button.

Next Recommended Article: Add an onClick to a Button

Related Articles
Resources: