Showing posts with label Layout. Show all posts
Showing posts with label Layout. Show all posts

Monday, August 27, 2012

Android | Seekbar Example

Seekbar is widget and we can use to select the value from selected range.

max property will set the max value for seekbar and progress property will give the current value of where seekbar positioned. ensure, seekbar starts with "0".

below is example of Seekbar which i have verified on Android 4.0.3 emulator.

Code of Layout File:


<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" >

    <TextView
        android:id="@+id/tvvalue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvvalue"
/>
   
    <SeekBar
        android:id="@+id/sbvalue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="99"
        android:progress="0"/>

</LinearLayout>

Code of Strings.xml File:


<resources>

    <string name="app_name">SeekBarExample</string>
    <string name="tvvalue"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_seek_bar_example">SeekBar Example</string>

</resources>


Source code of .JAVA file:


package com.example.seekbarexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class SeekBarExample extends Activity implements OnSeekBarChangeListener {

private TextView tvValue;
private SeekBar sbValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seek_bar_example);
       
        tvValue = (TextView) findViewById(R.id.tvvalue);
        sbValue = (SeekBar) findViewById(R.id.sbvalue);
       
        sbValue.setOnSeekBarChangeListener(this);
    }

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

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
// TODO Auto-generated method stub

tvValue.setText(String.valueOf(arg1));


}

public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}

public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub

}
}

Result:




Sunday, August 19, 2012

Android - Checkbox Example


Below is example of Checkbox element, it can be used to provide features to user to select multiple items.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are lot of resources are used to develop android app. but for this example, we are using three basis components like code base file, layout file and string file.

Layout file (.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">
    <CheckBox 
        android:id="@+id/chkbanana"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkbanana"
        />
    <CheckBox 
        android:id="@+id/chkorange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkorange"
        />
    
    <CheckBox 
        android:id="@+id/chkapple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chkapple"
        />
        
<Button 
   android:id="@+id/btnok"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/btnok"
   />
    
</LinearLayout>


String file (.XML):
<resources>

    <string name="app_name">CheckBoxExample</string>
    <string name="tvmessage"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_checkbox_example">CheckboxExample</string>
<string name="chkbanana">Banana</string>
<string name="chkapple">Apple</string>
<string name="chkorange">Orange</string>
<string name="btnok">OK</string>
</resources>


Code base file(.JAVA):

package com.example.checkboxexample;

import android.os.Bundle;
import android.app.Activity;
import android.text.InputFilter.LengthFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class CheckboxExample extends Activity {

private CheckBox chkBanana, chkOrange, chkApple;
private Button btnOk;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox_example);
        
        chkBanana = (CheckBox) findViewById(R.id.chkbanana);
        chkOrange = (CheckBox) findViewById(R.id.chkorange);
        chkApple = (CheckBox) findViewById(R.id.chkapple);
        btnOk = (Button) findViewById(R.id.btnok);
        onClickOkButton();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_checkbox_example, menu);
        return true;
    }
    
    public void onClickOkButton(){
   
    btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String sStr = new String();
if (chkBanana.isChecked()){
sStr += "Banana";
}
if (chkOrange.isChecked()){
if (sStr.length()>0)
sStr +=  ", ";
sStr += "Orange";
}
if (chkApple.isChecked()){
if (sStr.length()>0)
sStr += ", ";
sStr += "Apple";
}
if (sStr.length()>0){
sStr += " selected.";
}
Toast.makeText(getApplicationContext(), sStr, Toast.LENGTH_LONG).show();
}
});
    }
    
    
}


Screenshot:


Android - Radio Button Example


Below is example of RadioButton, it is used to select option from list of options.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are lot of resources are used to develop android app. but for this example, we are using three basis components like code base file, layout file and string file.

Layout file (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" >

    <RadioGroup 
        android:id="@+id/rdogroupsex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
        <RadioButton 
            android:id="@+id/rdomale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rdomale"
            />
        <RadioButton 
            android:id="@+id/rdofemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rdofemale"
            />
 
    </RadioGroup>
<Button
   android:id="@+id/btnok"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:text="@string/btnok"
   />

</LinearLayout>

String file(XML file):

<resources>

    <string name="app_name">RadioButtonExample</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_radio_button_example">Radio Button Example</string>
<string name="rdomale">Male</string>
<string name="rdofemale">Female</string>
<string name="btnok">OK</string>
</resources>

Code base file (.JAVA):

package com.example.radiobuttonexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioButtonExample extends Activity {

private RadioGroup rdoGroup;
private RadioButton rdoMale, rdoFemale;
private Button btnOk;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button_example);
        
        rdoGroup = (RadioGroup) findViewById(R.id.rdogroupsex);
        rdoFemale = (RadioButton) findViewById(R.id.rdofemale);
        rdoMale = (RadioButton) findViewById(R.id.rdomale);
        btnOk = (Button) findViewById(R.id.btnok);
        
        onButtonOkClick();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_radio_button_example, menu);
        return true;
    }
    
    
    public void onButtonOkClick(){
    btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if ( rdoGroup.getCheckedRadioButtonId() == R.id.rdomale){
Toast.makeText(getApplicationContext(), "Male Selected", Toast.LENGTH_LONG).show();
}
if (rdoGroup.getCheckedRadioButtonId() == R.id.rdofemale){
Toast.makeText(getApplicationContext(), "Female Selected", Toast.LENGTH_LONG).show();
}
}
});
    }
}

Note: By clicking on "Ok" button, it will show selected option.


Android - TextView Example

In Android, There are UI components which are used to develop more interactive interface. To develop user friendly interface we need to explore each component.

Below is example for Textview, it can be used to display static text in the application. It is having more features and through which we can develop interface like text editor.

I used Eclipse as development tool and tested the sample in Emulator for Android 4.0.3.

There are lot of resources are used to develop android app. but for this example, we are using three basis components like code base file, layout file and string file.

Below is code of Layout file.


<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" >

    <TextView
android:id="@+id/tvtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/tvtitle"
        tools:context=".TextViewExample" />

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

Code of Strings.xml file:


<resources>

    <string name="app_name">TextViewExample</string>
    <string name="tvtitle"></string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_text_view_example">Text View Example</string>
    <string name="btnok">Click Me</string>

</resources>


Code of Java file (Source):


package com.example.textviewexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class TextViewExample extends Activity {

Button btnOk;
TextView tvTitle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_view_example);
        
        btnOk = (Button) findViewById(R.id.btnok);
        tvTitle = (TextView) findViewById(R.id.tvtitle);
        
        onButtonClickListener();
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_text_view_example, menu);
        return true;
    }
    
    public void onButtonClickListener(){
    btnOk.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {
// TODO Auto-generated method stub
tvTitle.setText("Hi Friends!!!");
}
});
    }
}



Note: click on button and it will show you "Hi Friends!!!" text in textview.