UI component: Spinner, DatePicker, TimePicker, SeekBar

Here in this post I’m gonna teach you to create an application with the following UI component:

  1. Spinner
  2. DatePicker
  3. TimePicker
  4. SeekBar

Read This Post –> UI component: ImageButton, Togglebutton, ProgressBar

Steps Description
1 create a project in android studio with any name.
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml
4 create src/Main2Activity.java
5 create res/layout/activity_main2.xml
6  Modify src/MainActivity2.java and add the code
7  Modify layout XML file res/layout/activity_main2.xml

Content of src/MainActivity.java 

package com.example.kenilvavaliya.module23;

import android.content.Intent;
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.Button;
import android.widget.DatePicker;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

Spinner spinner;
SeekBar seekBar;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//spinner = (Spinner) findViewById(R.id.spinner);
seekBar = (SeekBar) findViewById(R.id.seekBar);
String[] country = {"India", "Pakistan", "France", "Australia", "China", "Srilanka", "Brazil", "Germany"};
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);

//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);


//seekbar
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
Toast.makeText(getApplicationContext(), "seekbar progress: " + progress, Toast.LENGTH_SHORT).show();
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "seekbar touch started!", Toast.LENGTH_SHORT).show();
}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(getApplicationContext(), "seekbar touch stopped!", Toast.LENGTH_SHORT).show();
}
});

button = (Button) findViewById(R.id.button);

}
public void onClick(View view)
{
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
//Performing action onItemSelected and onNothing selected

@Override
public void onItemSelected (AdapterView < ? > adapterView, View view,int i, long l){
//Toast.makeText(getApplicationContext(), friends[position], Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected (AdapterView < ? > adapterView){

}


}

Content of res/layout/activity_main.xml

<?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=".MainActivity">

<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp" />

<SeekBar
android:id="@+id/seekBar"
android:layout_width="214dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="196dp" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="217dp"
android:text="Next"
android:onClick="onClick"/>

</RelativeLayout>

After that left click on java folder go New > Activity > Empty Activity

It will create a new java and XML file.

Content of src/Main2Activity.java

package com.example.kenilvavaliya.module23;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;

import android.view.View;
import android.widget.Button;


public class Main2Activity extends AppCompatActivity {

DatePicker datePicker;
TimePicker timePicker;
TextView tv1;
TextView tv2;
Button button;
Button button1;
Button button2;
Button button3;
Button date;
Button time;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

datePicker = (DatePicker)findViewById(R.id.datePicker1);
timePicker = (TimePicker) findViewById(R.id.timePicker);
tv1 = (TextView) findViewById(R.id.textView);
tv2 = (TextView) findViewById(R.id.textView2);
button = (Button) findViewById(R.id.button);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button4);
button3 = (Button) findViewById(R.id.button5);
date = (Button) findViewById(R.id.button6);
time = (Button) findViewById(R.id.button7);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
datePicker.setVisibility(View.VISIBLE);
tv1.setVisibility(View.INVISIBLE);
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

tv1.setText(getCurrentDate());
datePicker.setVisibility(View.INVISIBLE);
tv1.setVisibility(View.VISIBLE);
}
//Date Picker
public String getCurrentDate(){
StringBuilder builder=new StringBuilder();;
builder.append(datePicker.getDayOfMonth()+"/");//month is 0 based
builder.append((datePicker.getMonth() + 1)+"/");
builder.append(datePicker.getYear());
return builder.toString();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timePicker.setVisibility(View.VISIBLE);
tv2.setVisibility(View.INVISIBLE);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timePicker.setVisibility(View.INVISIBLE);
tv2.setVisibility(View.VISIBLE);

}
});
//TimePicker
timePicker = (TimePicker) findViewById(R.id.timePicker);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
tv2.setText(hourOfDay+":"+minute);
}
});

date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button.setVisibility(View.VISIBLE);
button1.setVisibility(View.VISIBLE);
button2.setVisibility(View.INVISIBLE);
button3.setVisibility(View.INVISIBLE);
timePicker.setVisibility(View.INVISIBLE);
}
});

time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
button2.setVisibility(View.VISIBLE);
button3.setVisibility(View.VISIBLE);
button.setVisibility(View.INVISIBLE);
button1.setVisibility(View.INVISIBLE);
datePicker.setVisibility(View.INVISIBLE);
}
});
}

}

Content of res/layout/activity_main2.xml

<?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=".Main2Activity">

<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:layout_below="@+id/button"/>

<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:visibility="invisible">

</TimePicker>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="63dp"
android:layout_marginTop="96dp"
android:text="Get Date"
android:visibility="invisible" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/datePicker1"
android:layout_alignParentEnd="true"
android:layout_marginEnd="54dp"
android:text="Set Date"
android:visibility="invisible" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/datePicker1"
android:layout_centerHorizontal="true" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/datePicker1"
android:layout_alignStart="@+id/button"
android:text="Get Time"
android:visibility="invisible" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/button1"
android:layout_alignTop="@+id/button4"
android:text="Set Time"
android:visibility="invisible"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="240dp" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/button"
android:layout_marginTop="22dp"
android:text="select Date" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button6"
android:layout_alignStart="@+id/button1"
android:text="select time" />

</RelativeLayout>

Output:

Stay Connect with our app: – 

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

About easyengineering 37 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

Be the first to comment

Leave a Reply

Your email address will not be published.


*