Showing posts with label android tutorial. Show all posts
Showing posts with label android tutorial. Show all posts

Wednesday, November 18, 2015

Play Audio From Asset Directory in Android



Description:

Many applications need small audio file integrated in app only. For Example, Simple 2D bomb blast game, it requires bomb blast sound effect. It is necessary for developer to add sound effect in this type of app.Another reason is make application more attractive. Sound effect shapes your app more attractive and look different than other apps. Here is the code which help you to play audio file from asset file.

We have created this app with very easy way, you can use the same file for different application with copy/paste file.

First, Create AudioPlayer.java file:

AudioPlayer.java


import java.io.IOException;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;

public class AudioPlayer {

String fileName;
   Context contex;
   MediaPlayer mp;

   //Constructor
   public AudioPlayer(String name, Context context) {
       fileName = name;
       contex = context;
      // playAudio();
   }

   //Play Audio
   public void playAudio() {
       mp = new MediaPlayer();
       try {
           AssetFileDescriptor descriptor = contex.getAssets()
                   .openFd(fileName);
           mp.setDataSource(descriptor.getFileDescriptor(),
                   descriptor.getStartOffset(), descriptor.getLength());
           descriptor.close();
           mp.prepare();
           mp.setLooping(true);
           mp.start();
           mp.setVolume(3, 3);

       } catch (IllegalArgumentException e) {
           e.printStackTrace();
       } catch (IllegalStateException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
   //Stop Audio
   public void stop() {
    if(mp != null){
       mp.stop();
    }
   }
   
   //Pause Audio
   public void pause(){
    if(mp != null){
    mp.pause();
    }
   }
   
}

Now, Add 3 Buttons Play,Pause and Stop in activity_main.xml file.

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"
    tools:context="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Button
            android:id="@+id/buttonPlay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Play"
            android:drawableLeft="@android:drawable/ic_media_play" />

        <Button
            android:id="@+id/buttonPause"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pause"
            android:drawableLeft="@android:drawable/ic_media_pause" />

        <Button
            android:id="@+id/buttonStop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:drawableLeft="@drawable/stop" />

    </LinearLayout>

</RelativeLayout>


Copy below code in MainActivity.java file

MainActivity.java


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button btnPlay, btnPause, btnStop;
AudioPlayer audioPlayer;
Context context;

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

btnPlay = (Button) findViewById(R.id.buttonPlay);
btnPause = (Button) findViewById(R.id.buttonPause);
btnStop = (Button) findViewById(R.id.buttonStop);

context = getApplicationContext();
audioPlayer = new AudioPlayer("audio_sample.mp3", context);

btnPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
audioPlayer.playAudio();
btnPlay.setEnabled(false);
}
});

btnPause.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
btnPlay.setEnabled(true);
if (audioPlayer != null) {
audioPlayer.pause();
}
}
});

btnStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
btnPlay.setEnabled(true);
if (audioPlayer != null) {
audioPlayer.stop();
}
}
});
}
}


Download Full Source Code: GitHub



Tuesday, November 17, 2015

Session Time Out Android Demo Application





Description:

Android application which has Login/Logout functionality , They need this type of functionality. In Web / Java Applications Session Time out is given by default but in android there is no default functionality like this.

Here is the example, in which go to next screen and keep screen un-touch for 30 sec. It will show you session time out to alert and when you press "ok" button it will redirect to previous screen.

Here is the full source code:

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp" >

     <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="40dp"
        android:layout_weight="1"
        android:src="@drawable/logo" />
     
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:text="Go To next screen and hold for 30 sec. It will automatically give you session timeout warning and redirect this page again."
        android:layout_marginBottom="10dp"
        android:textSize="22dp" />

     <Button
        android:id="@+id/buttonNextScreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Go To Next Screen" />
   
</LinearLayout>

MainActivity.java


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button btnGoToNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGoToNext = (Button) findViewById(R.id.buttonNextScreen);
btnGoToNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}


Now, Create another Activity called SecondActivity.java


activity_second.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"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="22dp"
        android:padding="10dp"
        android:textAlignment="center"
        android:text="Keep untouch this screen for 30 sec. It will show dialog box." />

</RelativeLayout>


SecondActivity.java


import android.os.Bundle;

public class SecondActivity extends MyBaseActivity {

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


Main thing is, In which screen / Activity you want to add session timeout , you need to implement "MyBaseActivity" instead of "Activity".


MyBaseActivity.java


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

public class MyBaseActivity extends Activity {

public static final long DISCONNECT_TIMEOUT = 30000; // 30 sec = 30 * 1000 ms

private Handler disconnectHandler = new Handler() {
public void handleMessage(Message msg) {
}
};

private Runnable disconnectCallback = new Runnable() {
@Override
public void run() {

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
MyBaseActivity.this);
alertDialog.setCancelable(false);
alertDialog.setTitle("Alert");
alertDialog
.setMessage("Session Timeout, Hit ok to go to previous screen.");
alertDialog.setNegativeButton("OK",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MyBaseActivity.this,
MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

dialog.cancel();
}
});

alertDialog.show();

// Perform any required operation on disconnect
}
};

public void resetDisconnectTimer() {
disconnectHandler.removeCallbacks(disconnectCallback);
disconnectHandler.postDelayed(disconnectCallback, DISCONNECT_TIMEOUT);
}

public void stopDisconnectTimer() {
disconnectHandler.removeCallbacks(disconnectCallback);
}

@Override
public void onUserInteraction() {
resetDisconnectTimer();
}

@Override
public void onResume() {
super.onResume();
resetDisconnectTimer();
}

@Override
public void onStop() {
super.onStop();
stopDisconnectTimer();
}
}


AndroidMenifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="session.time.out"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>

</manifest>


Download Full Source Code: GitHub


Monday, May 5, 2014

Contact List With Checkbox - Part 1




  • Create Project named ContactList.
  • Add following permission in android menifest file.

<uses-permission android:name="android.permission.READ_CONTACTS" />



  • Add Following Code in MainActivity.java file

MainActivity.java



import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

MyCustomAdapter dataAdapter = null;
Context context = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new LongOperation().execute();

checkButtonClick();

}

private class MyCustomAdapter extends ArrayAdapter<Phonebook> {

private ArrayList<Phonebook> phonebookList;

public MyCustomAdapter(Context context, int textViewResourceId,

ArrayList<Phonebook> pbList) {
super(context, textViewResourceId, pbList);
this.phonebookList = new ArrayList<Phonebook>();
this.phonebookList.addAll(pbList);
}

private class ViewHolder {
TextView name, number;
CheckBox selected;
ImageView imgView;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

Log.v("ConvertView", String.valueOf(position));

if (convertView == null) {

LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = vi.inflate(R.layout.blacklist_layout, null);

holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.tvName);
holder.number = (TextView) convertView
.findViewById(R.id.tvNumber);
holder.selected = (CheckBox) convertView
.findViewById(R.id.checkBox1);
holder.imgView = (ImageView) convertView
.findViewById(R.id.imgView);

convertView.setTag(holder);

holder.selected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Phonebook _state = (Phonebook) cb.getTag();
_state.setChecked(cb.isChecked());
}
});

} else {
holder = (ViewHolder) convertView.getTag();
}

Phonebook state = phonebookList.get(position);

holder.name.setText(state.getName());
holder.number.setText(state.getNumber());
holder.selected.setChecked(state.isChecked());
Log.v("getImgUri", "" + state.getImgUri());
holder.imgView.setImageURI(state.getImgUri());
if (holder.imgView.getDrawable() == null) {
holder.imgView.setImageResource(R.drawable.ic_launcher);
}
holder.selected.setTag(state);
return convertView;
}

}

private void checkButtonClick() {

Button myButton = (Button) findViewById(R.id.findSelected);

myButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ArrayList<Phonebook> stateList = dataAdapter.phonebookList;
StringBuffer sb = new StringBuffer();

for (int i = 0; i < stateList.size(); i++) {
Phonebook state = stateList.get(i);
if (state.isChecked()) {
//Get selected contact information
//sb.append("\nImage URI:" + state.getImgUri().toString());
sb.append("\nName:" + state.getName());
sb.append("\nNumber:" + state.getNumber());
sb.append("\n-----");
}
}
Toast.makeText(context, ":" + sb.toString(), Toast.LENGTH_LONG)
.show();
}
});
}

ProgressDialog mProgressDialog;
Intent i;

private class LongOperation extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {

runOnUiThread(new Runnable() {

@Override
public void run() {

ArrayList<Phonebook> phonebookList = new ArrayList<Phonebook>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(
ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);

if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

// get the phone number
Cursor pCur = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { id }, null);
while (pCur.moveToNext()) {
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Uri uri = Utils
.getPhotoUri(
Long.parseLong(Utils
.fetchContactIdFromPhoneNumber(
phone,
context)),
context);

// ---------------------------------------------------------------------------
Phonebook _states = new Phonebook(name,
phone, false, uri);
phonebookList.add(_states);
// ---------------------------------------------------------------------------
}
pCur.close();

}
}
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(MainActivity.this,
R.layout.blacklist_layout, phonebookList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
}
});
}

}
});

return null;
}

@Override
protected void onPostExecute(Void result) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}

}

@Override
protected void onPreExecute() {
ShowLoading();
}

@Override
protected void onProgressUpdate(Void... values) {

}

}

private void ShowLoading() {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading Contacts ....");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
}


  • Add XML code in layout file, activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#000"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            style="@style/titlebar_textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:textSize="18dp"
            android:text="Contacts" />

        <Button
            android:id="@+id/findSelected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:text="Save"
            android:textColor="#ffffff" />
    </LinearLayout>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:divider="#BC0061"
        android:dividerHeight="2dp"
        android:background="@android:color/transparent"
        android:cacheColorHint="@android:color/transparent"
        android:padding="10dp" />

</LinearLayout>

After Added above code you will get error in some code but don't worry.Go to step 2 and add remaining code.




Contact List With Checkbox - Part 2





  • Create new Class file, named Phonebook.java.

Phonebook.java



package com.example.contactlist;

import android.net.Uri;

public class Phonebook {

String number = null,name = null;
boolean isChecked = false;
Uri imgUri = null;
public Phonebook(String name,String number,boolean selected,Uri uri){
super();
this.name = name;
this.imgUri = uri;
this.isChecked = selected;
this.number = number;
}

public String getNumber() {
return number;
}

public void setNumber(String number) {
this.number = number;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public boolean isChecked() {
return isChecked;
}

public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}

public Uri getImgUri() {
return imgUri;
}

public void setImgUri(Uri imgUri) {
this.imgUri = imgUri;
}
}

  • Create another class file,named Utils.java

Utils.java

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;

public class Utils {

public static String fetchContactIdFromPhoneNumber(String phoneNumber,Context context) {
// TODO Auto-generated method stub

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cFetch = context.getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);

String contactId = "";

if (cFetch.moveToFirst()) {

cFetch.moveToFirst();

contactId = cFetch
.getString(cFetch.getColumnIndex(PhoneLookup._ID));

}

System.out.println(contactId);
return contactId;

}

public static Uri getPhotoUri(long contactId,Context context) {
ContentResolver contentResolver = context.getContentResolver();

try {
Cursor cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "

+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);

if (cursor != null) {
if (!cursor.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}

} catch (Exception e) {
e.printStackTrace();
return null;
}

Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
}


  • Create layout xml file in res/layout,named blacklist_layout.xml

blacklist_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal"
    android:paddingLeft="3dip"
    android:paddingRight="3dip"
    android:paddingTop="3dip"
    android:paddingBottom="1dip" >

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:padding="5dp"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="left|center"
        android:orientation="vertical"
        android:padding="6dip" >

        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name"
            android:textStyle="bold"
            android:textSize="15dp"
            android:textColor="#000" />

        <TextView
            android:id="@+id/tvNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Number"
            android:textSize="15dp"
            android:textColor="#000" />
    </LinearLayout>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="right"
        android:textColor="#B40404" />

</LinearLayout>


  • Last but not the least, Add following code in styles.xml file.


<style name="titlebar_textview">
        <item name="android:textColor">#fff</item>
        <item name="android:textStyle">bold</item>
        <item name="android:padding">10dp</item>
    </style>

  • Done ! Now Run Program.

Download Full Source Code:Download Here !

Monday, December 9, 2013

Seekbar in android

Here is the full tutorial for seekbar in android.It is used to set range of value.You can also set background image of seekbar for some value.

Here is the tutorial for seekbar,It displays the value of seekbar in Edittext and also change the color of seekbar.

Source Code:


  • Create one project and copy below code in activity_main.xml file

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center"
    android:padding="20dp" >

    <SeekBar
        android:id="@+id/seek_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.71"
        android:max="100"
        android:progress="0" />

    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.21" />

</LinearLayout>


  • Write below code in MainActivity.java file.

MainActivity.java


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

SeekBar sb;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sb = (SeekBar)findViewById(R.id.seek_bar);
et= (EditText)findViewById(R.id.et);
sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
et.setText(""+progress);
if(progress>=50 && progress<70){
sb.setBackgroundColor(Color.RED);
}else if(progress>=70){
sb.setBackgroundColor(Color.YELLOW);
}else if(progress<50){
sb.setBackgroundColor(Color.TRANSPARENT);
}
}
});
}
}

  • That's It. Now run the project and see the output.