Saturday, January 26, 2013

HTML 5 FORM ELEMENTS


Label & Text Elements


These elements are common elements for any web page. Label is used to title the text field and Text element provides input area to user.

Example:
<label for="textinput">Normal input box :</label><input required id="textinput" type="text" value="a 'normal' input box" />

Attributes:
Required: used to popup message when user clicks on submit button without entering text inside the text field.
Value: sets/returns value of text field.


Checkbox Element


While feature is having more than one value in that case, checkbox element is used on the web page.

<fieldset>
<legend>Interest</legend>
<ul>
<li>
<input id="chksports" type="checkbox" checked/><label for="chksports">Sports</label>
</li>
<li>
<input id="chkentertainment" type="checkbox"/><label for="chkentertainment">Entertainment</label>
</li>
<li>
<input id="chkpolitics" type="checkbox"/><label for="chkpolitics">Politics</label>
</li>
</ul>
</fieldset>

Attributes:
While using checkbox, it will be in unchecked mode but if user want to mark checked then there are attributes as checked / unchecked. In above example, we can see the sports checkbox is marked as checked.

Radio Element


Radio element allows user to select one of the options from option list. E.g. sex.

<fieldset>
<legend>Sex</legend>
<ul>
<li>
<input name="sex" id="radiomale" type="radio" /><label for="radio">Male</label>
</li>
<li>
<input name="sex" id="radiofemale" type="radio"/><label for="radio">Female</label>
</li>
</ul>
</fieldset>

While executing above code, user can select either male or female from the available options. No element will be selected first time, but if user wants to keep Male option selected then        user can apply checked attribute. There is checked /unchecked attribute. By default, it is unchecked.

File Upload element


It is used while uploading file from client machine to server.

<fieldset>
<legend>Upload</legend>
<ul>
<li>
<label for="file">Upload</label><input type="file" id="file" />
</li>
</ul>
</fieldset>

Using above code, user will be able to select a file from local machine to upload. By default, user is allowed to upload single file. If user wants to upload more than one files then user needs to use “multiple” attribute in input tag. It will automatically change the caption of button from “Choose file” to “Choose files” and then user can select more than one file by using control or shift keys from keyboard.


Password Element


It is used when user wants to enter password. It replaces each character in black dot. Even user can not copy the content of this element.

<fieldset>
<legend>Password</legend>
<ul>
<li><label for="password">Password</label> <input id="password"
type="password" value="mypassword" /></li>
</ul>
</fieldset>


TextArea Element


Text element is only used to enter single line text but if user wants to enter multiple lines then you can use TextArea element.

<fieldset>
<legend>TextArea</legend>
<ul>
<li><label for="address">Address</label><textarea id="textarea" rows="4" cols="40">This is a textarea</textarea></li>
</ul>
</fieldset>

Attributes:
Rows” attribute used for textarea would be displayed in how many rows and “Cols” attribute used for characters in single row.

 Select Element (Dropdownlist)


Select element is used to select one item from the list.

<fieldset>
<legend>TextArea</legend>
<ul>
<li><label for="select">Select</label>
<select id="select" >
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select></li>
</ul>
</fieldset>

Select Element (ListBox)


If user wants to display values in list format then the same select element is used with “Multiple” attribute.

<fieldset>
<legend>ListBox</legend>
<ul>
<li><label for="select">Select</label>
<select id="select" multiple >
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
<option>Option 4</option>
</select></li>
</ul>
</fieldset>

From above list, user can select multiple values from the list by using shift key or control key. Shift key is used to select range and control key is used to select random values from the list.

Button Element


There are three types of button, (1) normal button (2) submit button while clicking on this button entered data will be posted and (3) reset button, to clear the values of entered elements.

<fieldset>
<legend>Button</legend>
<ul>
<li><input type="submit" value="Submit" /></li>
<li><input type="reset" value="Reset" /></li>
<li><input type="button" value="Ok" /></li>
</ul>
</fieldset>


Email Element


HTML5 introduces new element for email address. Browser itself validates value of element. In old days, we need to write script to mark element mandatory but HTML5 provides us “Required” attribute, it will check the value of element when user submits data and provide appropriate message (refer below image).

<fieldset>
<legend>Email</legend>
<ul>
<li>
<label for="email">Email</label>
<input required type="email" id="email" name="email" />
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>



Tel Element


To enter telephone number, there is element called “tel”. It is actually a free text field like text element. It allows entering sign, number and character in text field.

<fieldset>
<legend>Telephone number</legend>
<ul>
<li>
<label for="tel">Telephone number</label>
<input required type="tel" id="tel" name="tel" />
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>

 

URL element


It allows user to enter only URL of web site in the text field. “Required” attribute will validate entered values against the pattern of URL as well.

<fieldset>
<legend>Website</legend>
<ul>
<li>
<label for="url">Website</label>
<input required type="url" id="url" name="url" />
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>


Search Element


It allow to user to provide search interface.  “Results” attribute will assist user to select items from the number which was mentioned for results attribute.

<fieldset>
<legend>Search</legend>
<ul>
<label for="search">Search term</label>
<input required type="search" results="4" id="search" name="search" />
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>


DateTime Element


Chrome version 24.xx.xxx does not support this element. You can verify the functionality of it by using latest version of Opera.

It will provide the readymade calendar control and also request time in UTC format. Time format will in 24 hours.

<fieldset>
<legend>DateTime</legend>
<ul>
<li>
<label for="datetime">Select Date Time: </label><input type="datetime" id="datetime" min=
"2013-01-01" max="2014-01-01"/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>


Datetime-local Element


It you want date & time in local format then you can use datetime-local element of HTML5.
In which UTC will not be appear on right side of element.

<fieldset>
<legend>DateTime</legend>
<ul>
<li>
<label for="datetime">Select Date Time: </label><input type="datetime" id="datetime"
/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>
<fieldset>
<legend>DateTime-local</legend>
<ul>
<li>
<label for="datetime">Select Date Time: </label><input type="datetime-local" id="datetime"
/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>


Date Element


It provides only date. This will work on latest version of chrome and Opera. User can set min and max date range as well.

<fieldset>
<legend>Date</legend>
<ul>
<li>
<label for="date">Select Date : </label><input type="date" id="date" min=
"2013-01-01" max="2014-01-01"/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>

Time Element


It provides only time. This will work on latest version of chrome and Opera.
<fieldset>
<legend>Time</legend>
<ul>
<li>
<label for="time">Select Time : </label><input type="time" id="time"
/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>

Month Element


To select particular week of year, HTML5 provides week element. This will not work on chrome 24.x.xxx version. Please try below code with latest version of Opera. Value will be in YYYY-MM format.

<fieldset>
<legend>Month</legend>
<ul>
<li>
<label for="month">Select Month : </label><input type="month" id="month"
/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>


Week Element


To select particular week of year, HTML5 provides week element. This will not work on chrome 24.x.xxx version. Please try below code with latest version of Opera. Value will be in YYYY-W00 format.

<fieldset>
<legend>Week</legend>
<ul>
<li>
<label for="week">Select Week : </label><input type="week" id="week"
/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>

Number Element


The number input type is used to enter a number. It accepts only numbers otherwise returns validation error. It is having attribute of min, max and step. Step allows you to specify the increment value. Chrome allows decimal incremental value in step whereas Opera does not.

<fieldset>
<legend>Number Picker</legend>
<ul>
<li>
<label for="Number">Select Number : </label><input required type="number" id="number" min="1" max="10" step=".1"/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>


Range & Output Element


The range input type generates slider control which can be used for volume control, it is having min, max and step attributes. Output element will display the current value of slider.

<fieldset>
<legend>Slider</legend>
<ul>
<li>
<label for="slider">Select value : </label><input required type="range" id="range" min="1" max="10" step=".5"/> <output onforminput="value=range.value"/>
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>



Color Element


Excellent element provided by HTML 5 is color. User does not need to write code for this, just user needs to use color element in input tag. You can run below code in latest version of chrome and opera. Both will display color picker differently.

<fieldset>
<legend>Color</legend>
<ul>
<li>
<label for="color">Select color : </label><input required type="color" id="color"  />
</li>
<li><input type="submit" value="Submit" /></li>
</ul>
</fieldset>






Monday, September 10, 2012

Android | TabView

Below is source code of how to implement TabView in Android.

Note: It has been verified in Android emulator 4.0.3.

activity_tab_example.xml


<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost" >
   
    <LinearLayout
        android:id="@+id/tablinearlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
       
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TabWidget>

       
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            ></FrameLayout>
    </LinearLayout>


   
</TabHost>

strings.xml

<resources>

    <string name="app_name">TabExample</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_tab_example">Tab Example</string>
    <string name="title_activity_plus_tab">PlusTabActivity</string>
    <string name="title_activity_minus_tab">MinusTabActivity</string>
    <string name="title_activity_multiplication_tab">MultiplicationTabActivity</string>
    <string name="title_activity_division_tab">DivisionTabActivity</string>
    <string name="plus">You clicked on Plus.</string>
    <string name="minus">You clicked on Minus.</string>
    <string name="division">You clicked on Division.</string>
    <string name="multiplication">You clicked on Multiplication.</string>

</resources>

TabExample.java

package com.example.tabexample;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.util.Log;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

@SuppressWarnings("deprecation")
public class TabExample extends TabActivity {

private TabHost oTabHost;
    @SuppressWarnings("null")
@Override
    public void onCreate(Bundle savedInstanceState) {
        
    try
    {
    super.onCreate(savedInstanceState);
       
      
       setContentView(R.layout.activity_tab_example);
       
       oTabHost = (TabHost) findViewById(android.R.id.tabhost);
       
       oTabHost = getTabHost();
       
       TabHost.TabSpec oTabSpec1 ;
       oTabSpec1 = oTabHost.newTabSpec("+");
       oTabSpec1.setIndicator("+", getResources().getDrawable(R.drawable.sq_plus));
       Intent oPlusIntent = new Intent(this, PlusTabActivity.class);
       oTabSpec1.setContent(oPlusIntent);
       oTabHost.addTab(oTabSpec1);
       
       TabHost.TabSpec oTabSpec2 = oTabHost.newTabSpec("-");
       oTabSpec2.setIndicator("-", getResources().getDrawable(R.drawable.sq_minus));        
       Intent oMinusIntent = new Intent(this, MinusTabActivity.class);
       oTabSpec2.setContent(oMinusIntent);
       oTabHost.addTab(oTabSpec2);
       
       TabHost.TabSpec oTabSpec3 = oTabHost.newTabSpec("*");
       oTabSpec3.setIndicator("*", getResources().getDrawable(R.drawable.multiply));        
       Intent oMultiplicationIntent = new Intent(this, MultiplicationTabActivity.class);
       oTabSpec3.setContent(oMultiplicationIntent);
       oTabHost.addTab(oTabSpec3);
       
       TabHost.TabSpec oTabSpec4 = oTabHost.newTabSpec("/");
       oTabSpec4.setIndicator("/", getResources().getDrawable(R.drawable.sign_direction));
       Intent oDivisionIntent = new Intent(this, DivisionTabActivity.class);
       oTabSpec4.setContent(oDivisionIntent);
       oTabHost.addTab(oTabSpec4);
    }
    catch(Exception e){
    Log.d("TabExample", e.getMessage());
    }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tab_example, menu);
        return true;
    }
}


Snapshot of application structure. I have added 4 layout files and 4 images files in drawable folder.





Result:

In below example, you will get only text in each tab. if you want images then you need to keep first parameter of setIndicator method of each tab blank.


Tuesday, September 4, 2012

Android | DialogBox Example

Here, i would like to show you how we can show confirmation dialog box.

Note: below code has been verified on Android Emulator 4.0.3.

Code: activity_dialog_example.xml




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


    <Button
        android:id="@+id/btnyesno"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnyesno"/>

    <Button
        android:id="@+id/btnitems"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnitems"/>
 
    <Button
        android:id="@+id/btnsingleitem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnsingleitem"/>
     
</LinearLayout>


Code: strings.xml


<resources>

    <string name="app_name">DialogExample</string>

    <string name="menu_settings">Settings</string>
    <string name="title_activity_dialog_example">Dialog Example</string>

    <string name="btnyesno">Yes or No</string>
    <string name="deletedialog">Confirmation</string>
    <string name="deletemessage">Are you sure, Do you want to exit ?</string>
 
    <string name="btnitems">Items</string>
    <string name="btnsingleitem">Single Item Selection</string>
</resources>

Code:DialogExample.java


package com.example.dialogexample;

import javax.crypto.spec.OAEPParameterSpec;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;



public class DialogExample extends Activity implements OnClickListener {

static final int YESORNO_DIALOG = 1;
static final int IITEMS_DIALOG = 2;
static final int SINGLEITEMSELECTION_DIALOG = 3;

Button btnYesNo, btnItems, btnSingleItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_example);
     
        btnYesNo = (Button) findViewById(R.id.btnyesno);
        btnYesNo.setOnClickListener(this);
     
        btnItems = (Button) findViewById(R.id.btnitems);
        btnItems.setOnClickListener(this);
     
        btnSingleItem = (Button) findViewById(R.id.btnsingleitem);
        btnSingleItem.setOnClickListener(this);
     
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_dialog_example, menu);
        return true;
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
AlertDialog.Builder oAlert = new Builder(this);     //Here "this" is must if we use "getApplicationContext" then shows an error.
AlertDialog oDialog = (AlertDialog)  super.onCreateDialog(id);

    try
    {

    switch (id){
    case DialogExample.YESORNO_DIALOG:

    oAlert.setMessage(R.string.deletemessage);
    oAlert.setTitle(R.string.deletedialog);
    oAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
   
    public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub
    DialogExample.this.finish();
    }
    });
   
    oAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
   
    public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub
    arg0.cancel();
    }
    });
   
    oDialog = oAlert.create();
    break;
    case IITEMS_DIALOG:
final String[] strColor = {"Red","Green","Blue"};
oAlert.setTitle("Colors");

oAlert.setItems(strColor, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), strColor[arg1].toString(), Toast.LENGTH_LONG).show();
}
});
oDialog = oAlert.create();
break;

    case SINGLEITEMSELECTION_DIALOG:
    final String[] strFruit = {"Apple","Grapes","Mango"};
    oAlert.setTitle("Fruits");
    oAlert.setSingleChoiceItems(strFruit, -1, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), strFruit[arg1].toString(), Toast.LENGTH_LONG).show();

}
});
    oDialog = oAlert.create();
    break;
    }

   }
    catch(Exception e){
    Log.d("Dialogbox",e.getMessage().toString());
   
    }
    return oDialog;
    }

public void onClick(View arg0) {
// TODO Auto-generated method stub
try{

switch(arg0.getId()){
case R.id.btnyesno:
showDialog(YESORNO_DIALOG);
break;
case R.id.btnitems:
showDialog(IITEMS_DIALOG);
break;
case R.id.btnsingleitem:
showDialog(SINGLEITEMSELECTION_DIALOG);
break;

}
}
catch(Exception e){
Log.d("Dialogbox",e.getMessage().toString());
}
}
}



Result of Clicking on "Yes or No" button :




Note: 
As per the code, when user selects "Yes" then application will be closed and nothing will happen when user selects "No".
Result of while user clicks on "Items" button


Result of while user clicks on "Select Single item".