Android SeekBar


1.Set TextView TextSize using SeekBar. 
2.Discrete method using SeekBar. 
3.SeekBar with TextView in AlertBox. 
4.Vertical SeekBar Creation in Android. 
5.Capture SeekBar Value in Android. 
6.SeekBar with own Value in Android.

1.Set TextView TextSize using SeekBar. 

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code:

 <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" >   
   <SeekBar   
     android:id="@+id/seekBar1"   
     android:layout_width="match_parent"   
     android:layout_height="wrap_content"   
     android:layout_alignParentLeft="true"   
     android:layout_alignParentTop="true"   
     android:layout_marginTop="43dp" />   
   <TextView   
     android:id="@+id/textView1"   
     android:layout_width="wrap_content"   
     android:layout_height="wrap_content"   
     android:layout_below="@+id/seekBar1"   
     android:layout_centerHorizontal="true"   
     android:layout_marginTop="60dp"   
     android:text="Hemanth Somaraju" />   
 </RelativeLayout>    

Step 3 : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.widget.SeekBar;   
 import android.widget.SeekBar.OnSeekBarChangeListener;   
 import android.widget.TextView;   
 public class MainActivity extends Activity {   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     final TextView t1=(TextView) findViewById(R.id.textView1);   
     final SeekBar sk=(SeekBar) findViewById(R.id.seekBar1);   
     sk.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   
      int p=0;   
 @Override   
 public void onStopTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 if(p<30)   
 {   
 p=30;   
 sk.setProgress(p);   
 }   
 }   
 @Override   
 public void onStartTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 }   
 @Override   
 public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {   
 // TODO Auto-generated method stub   
 p=progress;   
 t1.setTextSize(p);   
 }   
  });    
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }    
 }   

Step 4 : Open AndroidManifest.xml and add following code :

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.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>   
   </application>   
 </manifest>   

Step : Our output will be like this : 



2.Discrete method using SeekBar. 

Step : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step : Open res -> layout -> activity_main.xml (or) main.xml and add following code: 

 <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" >   
   <SeekBar   
     android:id="@+id/seekBar1"   
     android:layout_width="match_parent"   
     android:layout_height="wrap_content"   
     android:layout_alignParentLeft="true"   
     android:layout_alignParentTop="true"   
     android:layout_marginTop="130dp" />   
 </RelativeLayout>   

Step : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.widget.SeekBar;   
 import android.widget.Toast;   
 import android.widget.SeekBar.OnSeekBarChangeListener;   
 public class MainActivity extends Activity {   
   float discrete=0;   
   float start=0;   
   float end=100;   
   float start_pos=0;   
   int start_position=0;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     start=-10; //you need to give starting value of SeekBar   
 end=10; //you need to give end value of SeekBar   
     start_pos=5; //you need to give starting position value of SeekBar   
     start_position=(int) (((start_pos-start)/(end-start))*100);   
 discrete=start_pos;   
     SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);   
     seek.setProgress(start_position);   
     seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   
 @Override   
 public void onStopTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "discrete = "   
 +String.valueOf(discrete), Toast.LENGTH_SHORT).show();   
 }   
 @Override   
 public void onStartTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 }   
 @Override   
 public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {   
 // TODO Auto-generated method stub   
 // To convert it as discrete value   
 float temp=progress;   
 float dis=end-start;   
 discrete=(start+((temp/100)*dis));   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }    
 }   

Step : Open AndroidManifest.xml and add following code :

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.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>   
   </application>   
 </manifest>   

Step : Our output will be like this : 

 





3.SeekBar with TextView in AlertBox. 

Step : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

 <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" >   
 </RelativeLayout>   

Step : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.app.AlertDialog;   
 import android.content.DialogInterface;   
 import android.view.Menu;   
 import android.widget.LinearLayout;   
 import android.widget.SeekBar;   
 import android.widget.TextView;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     final AlertDialog.Builder alert = new AlertDialog.Builder(this);   
 alert.setTitle("Alert Box");   
 alert.setMessage("Edit Text");   
 LinearLayout linear=new LinearLayout(this);   
 linear.setOrientation(1);   
 TextView text=new TextView(this);   
 text.setText("Hemanth Somaraju");   
 text.setPadding(10, 10, 10, 10);   
 SeekBar seek=new SeekBar(this);   
 linear.addView(seek);   
 linear.addView(text);   
 alert.setView(linear);   
 alert.setPositiveButton("Ok",new DialogInterface.OnClickListener()   
 {   
 public void onClick(DialogInterface dialog,int id)   
 {   
 Toast.makeText(getApplicationContext(), "OK Pressed",   
 Toast.LENGTH_LONG).show();   
 finish();   
 }   
 });   
 alert.setNegativeButton("Cancel",new DialogInterface.OnClickListener()   
 {   
 public void onClick(DialogInterface dialog,int id)   
 {   
 Toast.makeText(getApplicationContext(), "Cancel Pressed",   
 Toast.LENGTH_LONG).show();   
 finish();   
 }   
 });   
 alert.show();      
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }    
 }   

Step : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.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>   
   </application>   
 </manifest>   

Step : Our output will be like this : 

 



4.Vertical SeekBar Creation in Android. 

Step : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

 <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" >   
   <SeekBar   
     android:id="@+id/seekBar1"   
     android:layout_width="match_parent"   
     android:layout_height="wrap_content"   
     android:layout_alignParentLeft="true"   
     android:layout_alignParentTop="true"   
     android:layout_marginTop="149dp"   
     android:rotation="270" />   
 </RelativeLayout>  

Step : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 public class MainActivity extends Activity {   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }   
 }   

Step : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.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>   
   </application>   
 </manifest>   

Step : Our output will be like this : 

 



5.Capture SeekBar Value in Android. 

Step : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step : Open res -> layout -> activity_main.xml (or) main.xml and add following code  :

 <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" >   
   <SeekBar   
     android:id="@+id/seekBar1"   
     android:layout_width="match_parent"   
     android:layout_height="wrap_content"   
     android:layout_alignParentLeft="true"   
     android:layout_alignParentTop="true"   
     android:layout_marginTop="80dp" />   
 </RelativeLayout>   

Step : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.widget.SeekBar;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
   int pro = 0;   
   @Override   
   public void onCreate(Bundle savedInstanceState) {   
     super.onCreate(savedInstanceState);   
     setContentView(R.layout.activity_main);   
     SeekBar sk = (SeekBar) findViewById(R.id.seekBar1);   
     sk.setMax(50);   
     sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {   
 @Override   
 public void onStopTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 }   
 @Override   
 public void onStartTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 }   
 @Override   
 public void onProgressChanged(SeekBar seekBar, int progress,   
 boolean fromUser) {   
 // TODO Auto-generated method stub   
 pro=progress; //we can use the progress value of pro as anywhere   
 Toast.makeText(getBaseContext(), String.valueOf(progress),   
 Toast.LENGTH_SHORT).show();   
 }   
 });   
   }   
   @Override   
   public boolean onCreateOptionsMenu(Menu menu) {   
     getMenuInflater().inflate(R.menu.main, menu);   
     return true;   
   }    
 }   

Step : Open AndroidManifest.xml and add following code : 

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.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>   
   </application>   
 </manifest>   

Step : Our output will be like this : 

 


6.SeekBar with own Value in Android. 

Step : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here. 

Step : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

 <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" >   
   <SeekBar   
     android:id="@+id/seekBar1"   
     android:layout_width="match_parent"   
     android:layout_height="wrap_content"   
     android:layout_alignParentLeft="true"   
     android:layout_alignParentTop="true"   
     android:layout_marginTop="90dp" />   
 </RelativeLayout>   

Step : Open src -> package -> MainActivity.java and add following code : 

 package com.gudivada.hemanthsomaraju;   
 import android.os.Bundle;   
 import android.app.Activity;   
 import android.view.Menu;   
 import android.widget.SeekBar;   
 import android.widget.SeekBar.OnSeekBarChangeListener;   
 import android.widget.Toast;   
 public class MainActivity extends Activity {   
 float discrete=0;   
 float start=0;   
 float end=100;   
 float start_pos=0;   
 int start_position=0;   
 @Override   
 public void onCreate(Bundle savedInstanceState) {   
 super.onCreate(savedInstanceState);   
 setContentView(R.layout.activity_main);   
 start=-10; //you need to give starting value of SeekBar   
 end=10; //you need to give end value of SeekBar   
 SeekBar seek=(SeekBar) findViewById(R.id.seekBar1);   
 seek.setProgress(start_position);   
 seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {   
 @Override   
 public void onStopTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 Toast.makeText(getBaseContext(), "discrete = "+String.valueOf(discrete),   
 Toast.LENGTH_SHORT).show();   
 }   
 @Override   
 public void onStartTrackingTouch(SeekBar seekBar) {   
 // TODO Auto-generated method stub   
 }   
 @Override   
 public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {   
 // TODO Auto-generated method stub   
 // To convert it as discrete value   
 float temp=progress;   
 float dis=end-start;   
 discrete=(start+((temp/100)*dis));   
 }   
 });   
 }   
 @Override   
 public boolean onCreateOptionsMenu(Menu menu) {   
 getMenuInflater().inflate(R.menu.main, menu);   
 return true;   
 }    
 }   

Step : Open AndroidManifest.xml and add following code :

 <?xml version="1.0" encoding="utf-8"?>   
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"   
   package="com.gudivada.hemanthsomaraju"   
   android:versionCode="1"   
   android:versionName="1.0" >   
   <uses-sdk   
     android:minSdkVersion="8"   
     android:targetSdkVersion="19" />   
   <application   
     android:allowBackup="true"   
     android:icon="@drawable/ic_launcher"   
     android:label="@string/app_name"   
     android:theme="@style/AppTheme" >   
     <activity   
       android:name="com.gudivada.hemanthsomaraju.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>   
   </application>   
 </manifest>   

Step : Our output will be like this :