More UI Components - Spinner Action Bar Adapters

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

UI Components

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.

• Spinner is associated with


Adapter view so to fill the data
in spinner we need to use one of
the Adapter class.
• By default, ArrayAdapter
expects a Layout with a single
TextView.
Spinner - ArrayAdapter
Syntax:
ArrayAdapter(Context context, int resource, int textViewResourceId, T[]
objects)
 context:
 The first parameter is used to pass the context means the reference of current
class. Here this is a keyword used to show the current class reference. We can
also use getApplicationContext(), getActivity() in the place of this keyword.
getApplicationContext() is used in a Activity and getActivity() is used in a
Fragment.
 ArrayAdapter arrayAdapter = new ArrayAdapter(this, int resource, int
textViewResourceId, T[] objects);
Spinner - ArrayAdapter
 resource:
 The second parameter is resource id used to set the layout(xml file) for list items in which
you have a text view.
 ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items, int
textViewResourceId, T[] objects);
 textViewResourceId:
 The third parameter is textViewResourceId which is used to set the id of TextView where
you want to display the actual text.
 ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items,
R.id.textView, T[] objects);
 objects:
 The fourth parameter is an array of objects, used to set the array of elements in the
textView. We can set the object of array or array list here.
String[] bankNames={"BOI","SBI","HDFC","PNB","OBC"};
 ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_view_items,
Example of Spinner :Open res -> layout -> activity_main.xml

<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

 DatePickerDialog and TimePickerDialog


classes have onDateSetListener() and
onTimeSetListener() callback methods
respectively.
 These callback methods are invoked when
the user is done with filling the date and
time respectively.
DatePickerDialog

 The DatePickerDialog class consists of a 5 argument constructor with the parameters listed
below.

 Context: It requires the application context


 CallBack Function: onDateSet() is invoked when the user sets the date with the following
parameters:
 int year : It will be store the current selected year from the dialog
 int monthOfYear : It will be store the current selected month from the dialog
 int dayOfMonth : It will be store the current selected day from the dialog
 int mYear : It shows the the current year that’s visible when the dialog pops up
 int mMonth : It shows the the current month that’s visible when the dialog pops up
 int mDay : It shows the the current day that’s visible when the dialog pops up
TimePickerDialog
 The TimePickerDialog class consists of a 5 argument constructor with the parameters listed
below.

 Context: It requires the application context


 CallBack Function: onTimeSet() is invoked when the user sets the time with the following
parameters:
 int hourOfDay : It will be store the current selected hour of the day from the dialog
 int minute : It will be store the current selected minute from the dialog
 int mHours : It shows the current Hour that’s visible when the dialog pops up
 int mMinute : It shows the current minute that’s visible when the dialog pops up
 boolean false : If its set to false it will show the time in 24 hour format else not
if (v == btnDatePicker) {

// Get Current Date


final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog datePickerDialog = new DatePickerDialog(this,


new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {

txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

}
}, mYear, mMonth, mDay);
datePickerDialog.show();
if (v == btnTimePicker) {

// Get Current Time


final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

// Launch Time Picker Dialog


TimePickerDialog timePickerDialog = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

txtTime.setText(hourOfDay + ":" + minute);


}
}, mHour, mMinute, false);
timePickerDialog.show();
}
Android AlertDialog

 Android AlertDialog can be used to display the dialog


message with OK and Cancel buttons. It can be used to
interrupt and ask the user about his/her choice to continue or
discontinue.
 Android AlertDialog is composed of three regions: title,
content area and action buttons.
 Android AlertDialog is the subclass of Dialog class.
Methods of AlertDialog class

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

builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog, which) -> {


// When the user click yes button then app will close
finish();
});
// Set the Negative button with No name
builder.setNegativeButton("No", (DialogInterface.OnClickListener) (dialog, which) -> {
// If user click no then dialog box is canceled.
dialog.cancel();
});

// Create the Alert dialog


AlertDialog alertDialog = builder.create();
// Show the Alert Dialog box
alertDialog.show();
}
}

You might also like