More UI Components - Spinner Action Bar Adapters
More UI Components - Spinner Action Bar Adapters
More UI Components - Spinner Action Bar Adapters
Prepared by
Smita Mande
Adapters
Adapter is a bridge between UI component and data source that helps us to fill
data in UI component.
It holds the data and send the data to an Adapter view then view can takes the data
from the adapter view and shows the data on different views like as ListView,
GridView, Spinner etc
Adapter View in Android
An Adapter View displays the set of data in the form of List or Grid provided by the
Adapter.
It has the capability to display a large number of items on the User Interface efficiently. An
Android Adapter is responsible for taking the data from the source and put it in the
AdapterView. And it is the responsibility of AdapterView to display the data.
An android adapter view can display the data on the Display screen in 3 forms that are:
Spinner
In Android, Spinner provides a quick way to select one value from
a set of values.
Android spinners are nothing but the drop down-list seen in other
programming languages.
In a default state, a spinner shows its currently selected value.
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
Example of Spinner :open app-> java -> package -> MainActivity.java
package example.abhiandriod.spinnerexample; import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import
android.widget.AdapterView;
import android.widget.ArrayAdapter;import android.widget.Spinner; import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the bank name list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,bankNames);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);}
//Performing action onItemSelected and onNothing selected
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
Toast.makeText(getApplicationContext(), bankNames[position], Toast.LENGTH_LONG).show();}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
Android DatePickerDialog
and TimePickerDialog
The DatePickerDialog class consists of a 5 argument constructor with the parameters listed
below.
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
if (v == btnTimePicker) {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Method Description
public AlertDialog.Builder setTitle(CharSequence) This method is used to set the title of
AlertDialog.
public AlertDialog.Builder This method is used to set the message for
setMessage(CharSequence) AlertDialog.
public AlertDialog.Builder setIcon(int) This method is used to set the icon over
AlertDialog.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// Declare the onBackPressed method when the back button is pressed this method will call
@Override
public void onBackPressed() {
// Create the object of AlertDialog Builder class
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
// Set the message show for the Alert time
builder.setMessage("Do you want to exit ?");
// Set Alert Title
builder.setTitle("Alert !");
// Set Cancelable false for when the user clicks on the outside the Dialog Box then it will
remain on the screen
builder.setCancelable(false);
// Set the positive button with yes name