0% found this document useful (0 votes)
13 views

DatePicker,spinner,list view.docx

The document provides an overview of using DatePicker, ListView, and Spinner in Android applications, detailing their functionalities and implementation. It includes code snippets for creating a DatePicker dialog, populating a ListView with data using an ArrayAdapter, and implementing a Spinner for selecting items. Additionally, it explains the XML layout configurations and Java class implementations necessary for these components.

Uploaded by

nayanbiradar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

DatePicker,spinner,list view.docx

The document provides an overview of using DatePicker, ListView, and Spinner in Android applications, detailing their functionalities and implementation. It includes code snippets for creating a DatePicker dialog, populating a ListView with data using an ArrayAdapter, and implementing a Spinner for selecting items. Additionally, it explains the XML layout configurations and Java class implementations necessary for these components.

Uploaded by

nayanbiradar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DatePicker

DatePicker dialog is seen used in many android applications where we have


to select the date. This widget is mostly seen in the hotel reservation
applications for travel booking applications. With the help of this widget, we
can simply pick the date from the DatePicker dialog.

1 getDayOfMonth()

This method gets the selected day of month

2 getMonth()

This method gets the selected month

3 getYear()

This method gets the selected year

4 setMaxDate(long maxDate)

This method sets the maximal date supported by this DatePicker in

milliseconds since January 1, 1970 00:00:00 in getDefault() time zone

5 setMinDate(long minDate)

This method sets the minimal date supported by this NumberPicker in

milliseconds since January 1, 1970 00:00:00 in getDefault() time zone

6 setSpinnersShown(boolean shown)
This method sets whether the spinners are shown

7 updateDate(int year, int month, int dayOfMonth)

This method updates the current date

8 getCalendarView()

This method returns calendar view

9 getFirstDayOfWeek()

This Method returns first day of the week

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.datepicker.MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="102dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:text="" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:text="Change Date" />

<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />

</RelativeLayout>

Activity class
File: MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


DatePicker picker;
Button displayDate;
TextView textview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textview1=(TextView)findViewById(R.id.textView1);
picker=(DatePicker)findViewById(R.id.datePicker);
displayDate=(Button)findViewById(R.id.button1);

textview1.setText("Current Date: "+getCurrentDate());

displayDate.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {

textview1.setText("Change Date: "+getCurrentDate());


}

});

}
public String getCurrentDate(){
StringBuilder builder=new StringBuilder();;
builder.append((picker.getMonth() + 1)+"/");//month is 0 based
builder.append(picker.getDayOfMonth()+"/");
builder.append(picker.getYear());
return builder.toString();
}
}

Output:
Android ListView
List View
An adapter actually bridges between UI components and the data source that fill

data into UI Component. Adapter holds the data and send the data to adapter view,

the view can takes the data from adapter view and shows the data on different

views like as spinner, list view, grid view etc.

The ListView and GridView are subclasses of AdapterView and they can be

populated by binding them to an Adapter, which retrieves data from an external

source and creates a View that represents each data entry.

Android provides several subclasses of Adapter that are useful for retrieving

different kinds of data and building views for an AdapterView ( i.e. ListView or

GridView). The common adapters are ArrayAdapter,Base Adapter, CursorAdapter,

SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter.

1
android:id

This is the ID which uniquely identifies the layout.


2
android:divider

This is drawable or color to draw between list items.

3
android:dividerHeight

This specifies height of the divider. This could be in px, dp, sp, in, or mm.

4
android:entries

Specifies the reference to an array resource that will populate the ListView.

5
android:footerDividersEnabled

When set to false, the ListView will not draw the divider before each footer

view. The default value is true.

6
android:headerDividersEnabled

When set to false, the ListView will not draw the divider after each header

view. The default value is true.

Example of ListView
Let's implement a simple listview example.
activity_main.xml
First we need to drag and drop ListView component from palette to activity_main.xml file.
File: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="listview.example.com.listview.MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</android.support.constraint.ConstraintLayout>
Create an additional mylist.xml file in layout folder which contains view components
displayed in listview.
mylist.xml
File: mylist.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d"
/>

Now place the list of data in strings.xml file by creating string-array.


strings.xml
File:strings.xml

<resources>
<string name="app_name">ListView</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
</resources>

Activity class
In java class we need to add adapter to listview using setAdapter() method of listview.
File: MainActivity.java

package listview.example.com.listview;
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.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


ListView listView;
TextView textView;
String[] listItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView=(ListView)findViewById(R.id.listView);
textView=(TextView)findViewById(R.id.textView);
listItem = getResources().getStringArray(R.array.array_technology);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, listItem);
listView.setAdapter(adapter);

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l)
{ String value=adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();

}
});
}
}

Output
ArrayAdapter
You can use this adapter when your data source is an array. By default,

ArrayAdapter creates a view for each array item by calling toString() on each item

and placing the contents in a TextView. Consider you have an array of strings you

want to display in a ListView, initialize a new ArrayAdapter using a constructor to

specify the layout for each string and the string array −

ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);

Here are arguments for this constructor −

● First argument this is the application context. Most of the case, keep it

this.
● Second argument will be layout defined in XML file and having

TextView for each string in the array.

● Final argument is an array of strings which will be populated in the

text view.

Once you have array adapter created, then simply call setAdapter() on your

ListView object as follows −

ListView listView = (ListView) findViewById(R.id.listview);

listView.setAdapter(adapter);

Android Spinner Example


Android Spinner is like the combox box of AWT or Swing. It can be used to display the
multiple options to the user in which only one item can be selected by the user.
Android spinner is like the drop down menu with multiple values from which the end user
can select only one value.
Android spinner is associated with AdapterView. So you need to use one of the adapter
classes with spinner.
Android Spinner class is the subclass of AsbSpinner class.
In this example, we are going to display the country list. You need to use ArrayAdapter class
to store the country list.
Let's see the simple example of spinner in android.
activity_main.xml
Drag the Spinner from the pallete, now the activity_main.xml file will like this:

XML attributes Description

android:id Used to specify the id of the view.


android:textAlignment Used to the text alignment in the dropdown list.

android:background Used to set the background of the view.

android:padding Used to set the padding of the view.

android:visibility Used to set the visibility of the view.

Used to specify the gravity of the view like center,


android:gravity
top, bottom, etc

File: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example. com.spinner.MainActivity">

<Spinner
android:id="@+id/spinner"
android:layout_width="149dp"
android:layout_height="40dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />

</android.support.constraint.ConstraintLayout>

Activity class
Let's write the code to display item on the spinner and perform event handling.
File: MainActivity.java
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[] country = { "India", "USA", "China", "Japan", "Other"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);

ArrayAdapter aa = new
ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spin.setAdapter(aa);

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {
Toast.makeText(getApplicationContext(),country[position] ,
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}

Output:

You might also like