Android Seekbar for volume adjustment

Source: Internet
Author: User

Seekbar can identify values by the position of the sliders----and the drag bar allows the user to drag the slider to change the value, so the drag bar is often used to adjust some of the system's values, such as adjusting the volume.

Seekbar allows the user to change the slider appearance of the drag bar, changing the appearance of the slider is usually specified by the following properties: Android:thumb: Specifies a drawable object that will customize the slider.

In order for the program to respond to changes in the position of the drag bar slider, the program can consider binding a onseekbarchangelistener listener to it.

Here's an example of using Seekbar to adjust the system volume:

XML code:

<SeekBar Android:id="@+id/sound"Android:layout_width="150px"Android:layout_height="10px"Android:max=" -"  //set drag bar maximum valueandroid:progress="Ten"   //set the current value of the drag barandroid:progressdrawable="@layout/seekbar_style"  //Drag Bar Styleandroid:thumb="@layout/thumb"/>//Slider Style

Seekbar_style.xml:

<?xml version="1.0"encoding="UTF-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android: Id/background"> <shape> <corners android:radius="10dip"/> <gradient android:startcolor="#ffffffff"Android:centercolor="#ff000000"Android:endcolor="#ff808A87"Android:centery="1"android:angle=" the"/> </shape> </item> <item android:id="@android: Id/progress"> <clip> <shape> <corners android:radius="10dip"/> <gradient android:startcolor="#ffffffff"Android:centercolor="#ffFFFF00"Android:endcolor="#ffAABD00"Android:centery="1"android:angle=" the"/> </shape> </clip> </item> </layer-list>

Thumb.xml:

<?xml version="1.0"encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!--Press Status--<Item android:state_pressed="true"android:drawable="@drawable/thumb_normal"/> <!--Normal no-focus state-<Item android:state_focused="false"android:state_pressed="false"android:drawable="@drawable/thumb_normal"/> </selector>

Bacon_seekbar.xml:

<layer-List Xmlns:android="http://schemas.android.com/apk/res/android"> <Item Android:id="@+android:id/background"android:drawable="@drawable/thumb_normal"/> <Item Android:id="@+android:id/secondaryprogress"android:drawable="@drawable/thumb_normal"/> <Item Android:id="@+android:id/progress"android:drawable="@drawable/thumb_normal"/></layer-list>

Java code:

 Public classPianoactivity extends Activity {/** Called when the activity is first created.*/     PrivateImageButton imagebutton_white1; PrivateMediaPlayer MediaPlayer01;  PublicAudiomanager Audiomanage; PrivateTextView Mvolume;//Show Current Volume      PublicSeekBar SoundBar; Private intMaxvolume, Currentvolume; Private intVolume=0;//Initialize Sound@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.main); MediaPlayer01=NewMediaPlayer (); Imagebutton_white1=(ImageButton) Findviewbyid (r.id.white1); Final SeekBar SoundBar= (SeekBar) Findviewbyid (R.id.sound);//Volume SettingsMvolume =(TextView) Findviewbyid (R.id.mvolume); Audiomanage=(Audiomanager) Getsystemservice (Context.audio_service); Maxvolume= Audiomanage.getstreammaxvolume (Audiomanager.stream_music);//get system Maximum volumeSoundbar.setmax (Maxvolume);//drag bar highest value to match system maximum soundCurrentvolume = Audiomanage.getstreamvolume (Audiomanager.stream_music);//Get Current valuesoundbar.setprogress (Currentvolume); Mvolume.settext (Currentvolume* -/maxvolume +" %"); Soundbar.setonseekbarchangelistener (NewOnseekbarchangelistener ()//Tuning Listener        {             Public voidOnprogresschanged (SeekBar arg0,intProgress,boolean Fromuser) {Audiomanage.setstreamvolume (Audiomanager.stream_music, Progress,0); Currentvolume= Audiomanage.getstreamvolume (Audiomanager.stream_music);//Get Current valuesoundbar.setprogress (Currentvolume); Mvolume.settext (Currentvolume* -/maxvolume +" %"); } @Override Public voidOnstarttrackingtouch (SeekBar SeekBar) {//TODO auto-generated Method Stub} @Override Public voidOnstoptrackingtouch (SeekBar SeekBar) {//TODO auto-generated Method Stub                            }        });

TextView XML is not given and needs to be added by itself.

Finish the effect:

Android Seekbar for volume adjustment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.