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.
Let's see a simple example to display progress bar in android.


  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  

Android DatePicker Example

Android DatePicker is a widget to select date. It allows you to select date by day, month and year. Like DatePicker, android also provides TimePicker to select time.
The android.widget.DatePicker is the subclass of FrameLayout class.

Android DatePicker Example

Let's see the simple example of datepicker widget in android.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/textView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:layout_marginLeft="50dp"  
  14.         android:layout_marginTop="36dp"  
  15.         android:text="Current Date:" />  
  16.   
  17.     <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_alignParentBottom="true"  
  22.         android:layout_centerHorizontal="true"  
  23.         android:layout_marginBottom="140dp"  
  24.         android:text="Change Date" />  
  25.   
  26.     <DatePicker  
  27.         android:id="@+id/datePicker1"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_above="@+id/button1"  
  31.         android:layout_centerHorizontal="true"  
  32.         android:layout_marginBottom="30dp" />  
  33.   
  34. </RelativeLayout>  


Activity class

File: MainActivity.java
  1. package com.example.datepicker2;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.DatePicker;  
  10. import android.widget.TextView;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     DatePicker picker;  
  15.     Button displayDate;  
  16.     TextView textview1;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.           
  22.         textview1=(TextView)findViewById(R.id.textView1);  
  23.         picker=(DatePicker)findViewById(R.id.datePicker1);  
  24.         displayDate=(Button)findViewById(R.id.button1);  
  25.           
  26.         textview1.setText(getCurrentDate());  
  27.           
  28.         displayDate.setOnClickListener(new OnClickListener(){  
  29.             @Override  
  30.             public void onClick(View view) {  
  31.                 textview1.setText(getCurrentDate());  
  32.             }  
  33.               
  34.         });  
  35.     }  
  36.     public String getCurrentDate(){  
  37.         StringBuilder builder=new StringBuilder();  
  38.         builder.append("Current Date: ");  
  39.         builder.append((picker.getMonth() + 1)+"/");//month is 0 based  
  40.         builder.append(picker.getDayOfMonth()+"/");  
  41.         builder.append(picker.getYear());  
  42.         return builder.toString();  
  43.     }  
  44.     @Override  
  45.     public boolean onCreateOptionsMenu(Menu menu) {  
  46.         // Inflate the menu; this adds items to the action bar if it is present.  
  47.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  48.         return true;  
  49.     }  
  50.   
  51. }  

Android SeekBar

Android SeekBar 

it is a kind of ProgressBar with draggable thumb. The end user can drag the thum left and right to move the progress of song, file download etc.
The SeekBar.OnSeekBarChangeListener interface provides methods to perform even handling for seek bar.
Android SeekBar and RatingBar classes are the sub classes of AbsSeekBar.

Android SeekBar Example

activity_main.xml

Drag the seek bar from the pallete, now activity_main.xml will look like this:
File: activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <SeekBar  
  12.         android:id="@+id/seekBar1"  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_centerHorizontal="true"  
  17.         android:layout_marginTop="39dp" />  
  18.   
  19. </RelativeLayout>  

Activity class
Let's see the Activity class displaying seek bar and performing event handling.
File: MainActivity.java
  1. package com.example.seekbar;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.widget.SeekBar;  
  6. import android.widget.SeekBar.OnSeekBarChangeListener;  
  7. import android.widget.Toast;  
  8. public class MainActivity extends Activity implements OnSeekBarChangeListener{  
  9.     SeekBar seekBar1;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.           
  15.         seekBar1=(SeekBar)findViewById(R.id.seekBar1);  
  16.         seekBar1.setOnSeekBarChangeListener(this);  
  17.     }  
  18.     @Override  
  19.     public void onProgressChanged(SeekBar seekBar, int progress,  
  20.             boolean fromUser) {  
  21.         Toast.makeText(getApplicationContext(),"seekbar progress: "+progress, Toast.LENGTH_SHORT).show();  
  22.     }  
  23.     @Override  
  24.     public void onStartTrackingTouch(SeekBar seekBar) {  
  25.         Toast.makeText(getApplicationContext(),"seekbar touch started!", Toast.LENGTH_SHORT).show();  
  26.     }  
  27.     @Override  
  28.     public void onStopTrackingTouch(SeekBar seekBar) {  
  29.         Toast.makeText(getApplicationContext(),"seekbar touch stopped!", Toast.LENGTH_SHORT).show();  
  30.     }  
  31.     @Override  
  32.     public boolean onCreateOptionsMenu(Menu menu) {  
  33.         // Inflate the menu; this adds items to the action bar if it is present.  
  34.         getMenuInflater().inflate(R.menu.main, menu);  
  35.         return true;  
  36.     }  
  37. }  

Android WebView

Android WebView 

it is used to display web page in android. The web page can be loaded from same application or URL. It is used to display online content in android activity.
Android WebView uses webkit engine to display web page.
The android.webkit.WebView is the subclass of AbsoluteLayout class.
The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.


Let's see the simple code to display javatpoint.com web page using web view.
  1. WebView mywebview = (WebView) findViewById(R.id.webView1);  
  2. mywebview.loadUrl("http://www.javatpoint.com/");  
Let's see the simple code to display HTML web page using web view. In this case, html file must be located inside the asset directory.
  1. WebView mywebview = (WebView) findViewById(R.id.webView1);  
  2. mywebview.loadUrl("file:///android_asset/myresource.html");  
Let's see another code to display HTML code of a string.
  1. String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>";  
  2. mywebview.loadData(data, "text/html""UTF-8");  

Android WebView Example

Let's see a simple example of android webview.

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <WebView  
  8.         android:id="@+id/webView1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:layout_marginTop="42dp" />  
  14.   
  15. </RelativeLayout>  

Activity class

File: MainActivity.java
  1. package com.example.webview;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.webkit.WebView;  
  7.   
  8. public class MainActivity extends Activity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.           
  15.         WebView mywebview = (WebView) findViewById(R.id.webView1);  
  16.          //mywebview.loadUrl("http://www.javatpoint.com/");  
  17.           
  18.         /*String data = "<html><body><h1>Hello, Javatpoint!</h1></body></html>"; 
  19.         mywebview.loadData(data, "text/html", "UTF-8"); */  
  20.           
  21.         mywebview.loadUrl("file:///android_asset/myresource.html");  
  22.     }  
  23.   
  24.     @Override  
  25.     public boolean onCreateOptionsMenu(Menu menu) {  
  26.         // Inflate the menu; this adds items to the action bar if it is present.  
  27.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  28.         return true;  
  29.     }  
  30.   
  31. }  

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...