Tag Archive for user interface

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

User Interface: Layouts

Layouts are visual elements designed to control the distribution, position and dimensions of the controls that are inserted inside. These components extend to the base class ViewGroup, like many other container components, ie, able to contain other controls. In the next post we will see what kinds of offers android layout. The types are as follows.

FrameLayout

This is the simplest of all layouts of Android. A FrameLayout places all its child in line with its upper left corner, so that each control will be hidden by the next control (unless the latter have transparency). Therefore, commonly used to display a single control on the inside, as a container (placeholder) simple one replaceable element, such as a picture.
The components included in a FrameLayout may establish their propiedadesandroid: layout_width and android: layout_height, which may take the values ​​”fill_parent” (so that the child takes control the size of its layout container) or “wrap_content” (so that the child takes control the size of its content).
example:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
< EditText android:id= "@+id/TxtName"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"/>
 </ FrameLayout>

LinearLayout

The next Android layout in the level of complexity is the LinearLayout. This layout stacked one after the other child elements either horizontally or vertically as property set android: orientation.
As a FrameLayout, the elements contained in a can set its properties LinearLayout android: layout_width and android: layout_height to determine its size in the layout. But in the case of a LinearLayout, we have another parameter to play with, property android: layout_weight.

Example:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>
   < Button android:id = "@+id/BtnAccept"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"/>
 </ LinearLayout >
This property will allow us to give the elements in the layout dimensions proportional between them. This is harder to explain than to understand with an example. If we include in a vertical LinearLayout two text boxes (EditText) and one of them will establish a layout_weight = “1″ and the other one layout_weight = “2″ will get the effect that the entire layout area was occupied by the two boxes text and also the second is double (ratio of weight properties) as high as the first.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1"/>
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1"/>
 </ LinearLayout >

 

Thus, despite the apparent simplicity of this layout proves to be versatile enough to be useful to us on many occasions.

TableLayout

A TableLayout allow us to distribute child elements in tabular form, defining the rows and columns needed, and the position of each component within the table.

The table structure is defined in a similar way as in HTML, ie, indicating the rows that make up the table (TableRow objects), and within each row of columns required, except that there is no object especially to define a column (something like unTableColumn) but directly insert the necessary controls within each component of TableRow and inserted (which can be a simple or even another ViewGroup) corresponds to a column of the table. Thus, the final number of rows in the table correspond to the number of items TableRowinsertados, and the total number of columns will be determined by the number of components of the row that contains more components.

Generally, the width of each column will correspond to the width of the largest component of that column, but there are a number of properties that will help us to change this behavior:

  • android: stretchColumns. Indicate the columns that can expand to absorb the space left by the other columns to the right of the screen.
  • android: shrinkColumns. Indicate the columns can be collapsed to make room for the rest of the columns that you can exit on the right of the palntalla.
  • android: collapseColumns. Indicate the columns of the table that you want to completely hide.

All these properties TableLayout can receive a list of indices of columns separated by commas (example: android: stretchColumns = “1,2,3″) or an asterisk to indicate that it should apply to all columns (example: android: stretchColumns = “*”). Another important feature is the possibility that a given cell can occupy the space of several columns of the table (colspan attribute similar to HTML). This property is indicated by android: layout_span specific component to be taken that space. Consider an example with several of these elements:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:stretchColumns="1">
   <TableRow>
      <TextView android:text= "Cell 1.1" />
      <TextView android:text= "Cell 1.2" />
   </TableRow>
   <TableRow>
      <TextView android:text= "Cell 2.1" />
      <TextView android:text= "Cell 2.2" />
   </TableRow>
   <TableRow>
      <TextView android:text= "Cell 3"
       android:layout_span= "2" />
   </TableRow>
</TableLayout>
RelativeLayout

A TableLayout to distribute child elements in tabular form, defining the rows and columns needed, and the position of each component within the table. The table structure is defined in a similar way as in HTML, ie, indicating the rows that make up the table (TableRow objects), and within each row of columns required, except that there is no object especially to define a column (something like unTableColumn) but directly insert the necessary controls within each component of TableRow and inserted (which can be a simple or even another ViewGroup) corresponds to a column of the table. Thus, the final number of rows in the table correspond to the number of items TableRowinsertados, and The last type of layout that we will see is the RelativeLayout. This layout allows you to specify the position of each element relative to its parent or any other element in the layout itself. Thus, by including a new element X can indicate for example that should be placed under the element Y and right-aligned layout father.

Let’s see this in the following example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   < EditText android:id= "@+id/TxtName"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"/>
   < Button android:id = "@+id/BtnAccept"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@id/TxtName"
     android:layout_alignParentRight="True"/>
 </ RelativeLayout >

 

In the example, the button is placed BtnAccept text box below TxtName (android: layout_below = “@ id / TxtName”) and right-aligned layout parent (android: layout_alignParentRight = “true”), and left a margin to the left of 10 pixels (android: layout_marginLeft = “10px”). Like these three properties, we have an endless RelativeLayout properties for each control placed right where we want. Here are the main [I think their names perfectly explain the function of each]. Position relative to other control:

  • android:layout_above.
  • android:layout_below.
  • android:layout_toLeftOf.
  • android:layout_toRightOf.
  • android:layout_alignLeft.
  • android:layout_alignRight.
  • android:layout_alignTop.
  • android:layout_alignBottom.
  • android:layout_alignBaseline.

Position relative to parent layout:

  • android:layout_alignParentLeft.
  • android:layout_alignParentRight.
  • android:layout_alignParentTop.
  • android:layout_alignParentBottom.
  • android:layout_centerHorizontal.
  • android:layout_centerVertical.
  • android:layout_centerInParent.

Options range (also available for other layouts):

  • android:layout_margin.
  • android:layout_marginBottom.
  • android:layout_marginTop.
  • android:layout_marginLeft.
  • android:layout_marginRight.

Spacing or padding options (also available for other layouts):

  • android:padding.
  • android:paddingBottom.
  • android:paddingTop.
  • android:paddingLeft.
  • android:paddingRight.

 

Seo Packages