Tag Archive for Buttons

Basic Controls (III)

In the previous article (Basic Controls II)  of Android developer tutorial we saw different types of image and text controls. In the next article we will do a review of the various controls available for android to select information.

In this post we will focus on the next user interface controls:

  •  CheckBox
  •  RadioButton

Control CheckBox

A checkbox control is often used to check or uncheck options in an application and Android is represented by the class of the same name, CheckBox. The way to define it in our interface and methods available to manipulate from our code are similar to those already mentioned for the ToggleButton control.

Thus, to define a control of this type in our layout we can use the following code, which defines a checkbox with the text “Mark me”:

< ImageView android:id= "@+id/ChkExample"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Mark me"/>

As for the customization of the control we can say that it extends [indirectly] TextView control, so all the formatting options already discussed in previous articles are also valid for this control.

In the application code we can use the methods isChecked () for the control state and setChecked ( state ) to establish a state to control.

if (checkBox.isChecked()){
   checkBox.setChecked(false);
}

As for the possible events that can throw this control, the most interesting is undoubtedly the reporting that has changed the status of control, which is called OnCheckedChanged. To implement the actions of this event could therefore use the following logic:

final CheckBox cb = (CheckBox)findViewById(R.id.chkExample);
cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(
{
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
   {  if(isChecked)
      {
          cb.setText("Checkbox marcked!");
      }else{
          cb.setText("Checkbox unmarcked!");
      }
   }
});

For more information you can visite the next page.

 

Control RadioButton

As controls checkbox, a radiobutton can be marked or unmarked, but in this case are often used within a group of options to one, and only one of them must be marked mandatory, meaning that if you mark one of they will automatically be unchecked which was inactive. Android, a radiobutton group is defined by RadioGroup element, which in turn contains all elements necessary RadioButton. An example of how to define a group of two radiobutton in our interface:

 

 < RadioGroup android:id="@+id/gruporb"
           android:orientation="vertical"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
      < RadioButton android:id= "@+id/ChkExample1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="button A"/>
      < RadioButton android:id= "@+id/ChkExample2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="button B"/>
 </RadioGroup >

 

First, here’s how we define the control group indicating their orientation (vertical or horizontal) as was the case for example with a LinearLayout. After that, add all objects needed RadioButton ID indicating your property by android: id and text using android: text.

Having defined the interface we can manipulate the control from our java code using different control methods RadioGroup, the most important: check (id) to make a choice determined by its ID, clearCheck () to deselect all options ygetCheckedRadioButtonId () which as its name suggests returns the ID of the highlighted option (or the value -1 if no checked). Here’s an example:

     final RadioGroup rg = (RadioGroup)findViewById(R.id.gruporb);
     rg.clearCheck();
     rg.check(R.id.radio1);
     int idSeleccionado = rg.getCheckedRadioButtonId();

 

As for the events sent, as in the case of checkboxes, the more important the reporting of changes in the selected element, also called in this case onCheckedChange. We see how to handle this event RadioGroup object:

     final RadioGroup rg = (RadioGroup)findViewById(R.id.gruporb);

rg.setOnCheckedChangeListener(

    new RadioGroup.OnCheckedChangeListener() {

        public void onCheckedChanged(RadioGroup group, int checkedId) {

            lblMensaje.setText("ID opcion seleccionada: " + checkedid);

        } 
    }
)

For more information you can visite the next page.

Basic Controls (II)

In the previous article (Buttons)  of Android developer tutorial we saw different types of buttons available. In the next articles on Android we will do a review of the various controls available for android, Images, labels and for last text boxes.

In this post we will focus on the next user interface controls:

  •  ImageView
  •  TextView
  •  EditText
  •  ScrollView

ImageView control

ImageView control is capable of displaying the application. The most interesting property is android: src, which lets you specify the image to display. Again, the normal will indicate how the image source of a resource identifier of our portfolio /res/drawable for example android: src = “@drawable/imageexample”. In addition to this property, there are some other useful sometimes those designed to set the maximum size that can take the picture, android:maxWidth and android:maxHeight.

< ImageView android:id= "@+id/ImgPhoto"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/icon"/>

 

In the application logic, we could establish the image by setImageResorce (…), passing the ID of the resource to use as the content of the image.

ImageView img= (ImageView)findViewById(R.id.ImgFoto);
   img.setImageResource(R.drawable.icon);

 

For more information you can visite the next page.

TextView control

The TextView control is another classic in programming GUIs, text labels, and is used to display a given text to the user. As in the case of buttons, the control’s text property is set using the android: text. A part of this property, the nature of control makes the most interesting are those that establish the format of the displayed text, which as in the case of the buttons are as follows: android: background (background color), android: textColor (text color), android: textsize (font size) android: typeface (text style: bold, italic, …).

< TextView android:id= "@+id/Label1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="text label"
   android:background="#AA44FF"
   android:typeface="monospace"/>

 

Similarly, we can manipulate these properties from our code. For example, in the following example recover the text of a label with getText (), and then you concatenate a few numbers, update your content via setText () and change its background color with setBackgroundColor ().

< TextView  label1= (TextView)findViewById(R.id.Label1);
   String text1 = label1.getText().toString();
   texto +=  "este es el texto"
   android:text="text label"
   label1.setText(text1);

 

For more information you can visite the next page.

EditText control

EditText control is the text editing component that provides the Android platform. It allows text input and editing by the user, so that design-time most interesting property also establish their position, size and format, the text to display, attribute android: text.

< EditTextandroid:id= "@+id/TxtText"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@id/Label1"/>

Similarly, from our code can retrieve and set the text using the methods getText() and setText(Text to set), respectively:

 EditText txtText = (EditText)findViewById(R.id.txtText);
      String text1 = txtText.getText().toString();
      txtText.setText("Text to set");

For more information you can visite the next page.

ScrollView control

Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.

The TextView class also takes care of its own scrolling, so does not require a ScrollView, but using the two together is possible to achieve the effect of a text view within a larger container.

ScrollView only supports vertical scrolling.

<?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
          android:orientation="vertical"    
          android:layout_width="fill_parent"    
          android:layout_height="fill_parent">

         <ScrollView  android:id="@+id/SCROLLER_ID"    
              android:layout_width="fill_parent"    
              android:layout_height="wrap_content"    
              android:scrollbars="vertical"    
              android:fillViewport="true">

              <TextView android:id="@+id/TEXT_STATUS_ID"        
                  android:layout_width="fill_parent"        
                  android:layout_height="fill_parent"        
                  android:layout_weight="1.0"/>    
          </ScrollView>
</LinearLayout>

For more information you can visite the next page.

 

User interface: Basic controls (Buttons)

In the previous article (Layouts)  of Android developer tutorial we saw different types layouts available to distribute Android interface controls. In the next articles on Android we will do a review of the various controls available for android.

In this first post on the topic we will focus on different types of buttons and how we can customize them. The Android SDK provides three types of buttons:

  • classic (Button)
  • on / off  type (ToggleButton)
  • Image (ImageButton)

Control Button

This Button control type is the most basic button that we can use. The following example defines a button with the text “Click Me” assigning his property android: text. In addition to this property could use many others such as background color (android: background), font style (android: typeface), font color (android: textcolor), font size (android: textsize).

< Button android:id= "@+id/BtnButton"
   android:text="Click Me"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

 

For more information you can visite the next page.

Control ToggleButton 

A ToggleButton control type is a type of button you can stay in two states, pressed / no_pressed. In this case, instead of defining a control text only define two, depending on your state. Thus, we assign the properties android: texton and android: textOff to define both texts. Here’s an example below.

< ToggleButton android:id= "@+id/BtnButton"
   android:textOn="ON"
   android:textOff="OFF"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

 

For more information you can visite the next page.

Control ImageButton

In an ImageButton control type we can define an image to show instead of a text, for which we assign ownership android: src. Normally assign this property to a resource descriptor that we included in the folder / res / drawable. For example, in our case we have included a picture called “ example.png ” so we will refer to the resource “@drawable/example”.

< Button android:id= "@+id/BtnButton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/ok"/>

For more information you can visite the next page

Button events

As you can imagine, but these controls can throw a lot of other events, the most common of them all and we will want to capture in most cases is the onClick event. To define the logic of this event we will have to implement it by defining a new objetoView.OnClickListener () and associating it with the button using the method setOnClickListener ().

The most common way to do this is:

Button btnBoton1 = (Button)findViewById(R.id.BtnBoton1);
      btnBoton1.setOnClickListener( new View.OnClickListener() {
         @Override
         public void onClick(View arg0){
            lblMensaje.setText("Botón 1 pulsado!");
         }
      });

In the case of a ToggleButton type button is often useful to know what state the button has been pressed after being, for which we may use your isChecked method (). The following example checks the status of the button after being pressed and perform different actions depending on the outcome.

   ToggleButton btnButton2 = (ToggleButton)findViewById(R.id.btnButton2);
        btnButton2.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View arg0)
           {
              if(btnButton2.isChecked())
                lblMesage.setText("Button 2: ON");
              else
                lblMesage.setText("Button 2: OFF");
           }
        });

 

Customize the look of a button

In the picture we saw the way they appear by default three types of buttons available. But what if we want to further customize its appearance to change a little the type or font color or background?

To change the shape of a button could simply assign a picture to propiedadandroid: background, but this solution does not help much because we always show the same image even with the button, giving little sense of element “clickable”.

The perfect solution would therefore define different background images depending on the state of the button. Well, Android gives us total freedom to do this by using selectors . A selector is defined by an XML file located in the folder /res/drawable, and in it you can set different values for a given property of a control depending on their condition.

For example, if we give a flat look to a ToggleButton button, we could design the images needed for states, “down” (in the example toggle_on.png ) and “down” (in the example toggle_off.png ) and create a selector like this:

 <?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="false" android:drawable="@drawable/toggle_off" />
   <item android:state_checked="true" android:drawable="@drawable/toggle_on" />
</selector>

 

This selector we keep it for example in a file called toggle_style.xml and place it as a resource in our resource folder / res / drawable. This done, just enough to refer to this new resource on the property we have created android: background of the button:

 <ToggleButton android:id="@+id/BtnButton4"
         android:textOn="ON"
         android:textOff="OFF"
         android:padding="10dip"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:background="@drawable/toggle_style"/>

 

Thank you for visit me… and remember if you like my blog and my posts please click on ads it will motivate me to write more

Seo Packages