0% found this document useful (0 votes)
2 views19 pages

ProgressBar View, AutoCompleteTextView, TimePicker View, DatePicker View, ListView View,Spinner View

The document provides examples of various Android UI components including ProgressBar, AutoCompleteTextView, TimePicker, and DatePicker. It includes code snippets demonstrating how to implement these components in an Android application, along with their associated methods and XML layouts. The document serves as a guide for developers to create user interfaces with these elements in Android apps.

Uploaded by

nayanbiradar14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views19 pages

ProgressBar View, AutoCompleteTextView, TimePicker View, DatePicker View, ListView View,Spinner View

The document provides examples of various Android UI components including ProgressBar, AutoCompleteTextView, TimePicker, and DatePicker. It includes code snippets demonstrating how to implement these components in an Android application, along with their associated methods and XML layouts. The document serves as a guide for developers to create user interfaces with these elements in Android apps.

Uploaded by

nayanbiradar14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 19

1

ProgressBar View, AutoCompleteTextView, TimePicker View, DatePicker View, ListView View,Spinner View

Android ProgressBar Example


We can display the android progress bar dialog box to display the status of work being done e.g. downloading file,
analyzing status of work etc.

In this example, we are displaying the progress dialog for dummy file download operation.

Here we are using android.app.ProgressDialog class to show the progress bar. Android ProgressDialog is the
subclass of AlertDialog class.

The ProgressDialog class provides methods to work on progress bar like setProgress(), setMessage(),
setProgressStyle(), setMax(), show() etc. The progress range of Progress Dialog is 0 to 10000.

1. ProgressDialog progressBar = new ProgressDialog(this);


2. progressBar.setCancelable(true);//you can cancel it by pressing back button
3. progressBar.setMessage("File downloading ...");
4. progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
5. progressBar.setProgress(0);//initially progress is 0
6. progressBar.setMax(100);//sets the maximum value 100
7. progressBar.show();//displays the progress bar

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

<RelativeLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/idRLContainer"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">
2

<!--on below line we are creating

a text for our app-->

<TextView

android:id="@+id/idTVHeading"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_above="@id/idPBLoading"

android:layout_centerInParent="true"

android:layout_margin="20dp"

android:gravity="center"

android:padding="10dp"

android:text="Progress Bar in Android"

android:textAlignment="center"

android:textColor="@color/black"

android:textSize="20sp"

android:textStyle="bold" />

<!--on below line we are creating a progress bar-->

<ProgressBar

android:id="@+id/idPBLoading"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_above="@id/idBtnDisplayProgress"

android:layout_centerHorizontal="true"

android:layout_margin="20dp"

android:visibility="gone" />
3

<!--on below line we are creating a button-->

<Button

android:id="@+id/idBtnDisplayProgress"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:layout_margin="20dp"

android:text="Show Progress Bar"

android:textAllCaps="false" />

</RelativeLayout>

package com.gtappdevelopers.kotlingfgproject;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ProgressBar;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

// on below line we are creating a variable.

private Button showProgressBtn;


4

private ProgressBar loadingPB;

boolean isProgressVisible = false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// on below line we are initializing variables with ids.

showProgressBtn = findViewById(R.id.idBtnDisplayProgress);

loadingPB = findViewById(R.id.idPBLoading);

// on below line we are adding click listener for our button

showProgressBtn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// on below line we are checking if

// progress bar is already visible.

if (isProgressVisible) {

// if it is visible we are

// updating text for our button.

showProgressBtn.setText("Show Progress Bar");

// on below line we are changing

// its visibility

loadingPB.setVisibility(View.GONE);
5

// on below line we are updating

// is progress visible to false

isProgressVisible = false;

} else {

// this condition will be called if

// progress bar visibility is gone

isProgressVisible = true;

// on below line we are updating

// text for our button.

showProgressBtn.setText("Hide Progress Bar");

// on below line we are changing

// visibility for our progress bar.

loadingPB.setVisibility(View.VISIBLE);

});

}
6

AutoCompleteTextView

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

String[] language
={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
7

setContentView(R.layout.activity_main);
//Creating the instance of ArrayAdapter containing list
of language names
ArrayAdapter<String> adapter = new ArrayAdapter<String>

(this,android.R.layout.select_dialog_item,language);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first
character
actv.setAdapter(adapter);//setting the adapter data into
the AutoCompleteTextView
actv.setTextColor(Color.RED);
}
}

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


<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is your favourite programming
language?"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.032" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
8

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="188dp"
android:layout_height="93dp"
android:layout_weight="1"
android:text="AutoCompleteTextView" />
</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

timePicker

import android.widget.TimePicker;
private TimePicker timePicker1;
timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
9

In order to get the time selected by the user on the screen, you will use

getCurrentHour() and getCurrentMinute() method of the TimePicker Class. Their

syntax is given below.

int hour = timePicker1.getCurrentHour();


int min = timePicker1.getCurrentMinute();
Apart form these methods, there are other methods in the API that gives more control

over TimePicker Component. They are listed below.

Sr.N Method & description


o

1
is24HourView()

This method returns true if this is in 24 hour view else false

2
isEnabled()

This method returns the enabled status for this view

3
setCurrentHour(Integer currentHour)

This method sets the current hour

4
setCurrentMinute(Integer currentMinute)

This method sets the current minute


10

5
setEnabled(boolean enabled)

This method set the enabled state of this view

6
setIs24HourView(Boolean is24HourView)

This method set whether in 24 hour or AM/PM mode

7
setOnTimeChangedListener(TimePicker.OnTimeChangedListener

onTimeChangedListener)

This method Set the callback that indicates the time has been adjusted by

the user

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
11

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class MainActivity extends AppCompatActivity {

TextView textview1;
TimePicker timepicker;
Button changetime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textview1=(TextView)findViewById(R.id.textView1);
timepicker=(TimePicker)findViewById(R.id.timePicker);
//Uncomment the below line of code for 24 hour view
timepicker.setIs24HourView(true);
changetime=(Button)findViewById(R.id.button1);

textview1.setText(getCurrentTime());

changetime.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
textview1.setText(getCurrentTime());
}
});

public String getCurrentTime(){


String currentTime="Current Time:
"+timepicker.getCurrentHour()+":"+timepicker.getCurrentMinute();
return currentTime;
}

}
12

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

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".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 Time" />

<TimePicker
android:id="@+id/timePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
13

android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp" />
</RelativeLayout>

DatePicker

Sr.N Method & description


o

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


14

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

android:layout_height="match_parent"

tools:context="example.javatpoint.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"
16

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

package example.javatpoint.com.datepicker;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;
17

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(){
18

@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:
19

You might also like