Android Basics Getting Started tutorial--2.3.8 SeekBar (drag bar)

Source: Internet
Author: User

Android Basics Getting Started tutorial--2.3.8 SeekBar (drag bar)

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

In this section we continue to learn about the drag bar--seekbar in the basic UI controls of Android, and believe that everyone is not unfamiliar with him, the most common
The place is music player or video player, volume control or playback progress control, all use this seekbar, we
Let's take a look at SeekBar's class structure and come to the official document: SeekBar

Hey, this thing is ProgressBar of the sub-class yes, that is, ProgressBar properties can be used!
And he has a property of his own:Android:thumb, which allows us to customize the slider ~
OK, start this section!

1.SeekBar Basic Usage

Well, the basic usage is actually very simple, the commonly used properties are just the following several commonly used properties, Java code as long as the setxxx can:

Android:max= maximum value of "100"//slider bar
android:progress= current value of "60"//slider bar
android:secondaryprogress= progress of "70"//Level two Sliders
android:thumb = "@mipmap/sb_icon"//Slider drawable

And then we're going to say SeekBar's event,Seekbar.onseekbarchangelistener .
We only need to rewrite the three corresponding methods:

onprogresschanged: Triggers when a change in schedule occurs
Onstarttrackingtouch: triggers when holding Seekbar
Onstoptrackingtouch: Trigger when release Seekbar

A simple code example:

:

Implementation code:

 Public  class mainactivity extends appcompatactivity {    PrivateSeekBar Sb_normal;PrivateTextView txt_cur;PrivateContext Mcontext;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Mcontext = mainactivity. This;    Bindviews (); }Private void bindviews() {sb_normal = (SeekBar) Findviewbyid (r.id.sb_normal);        Txt_cur = (TextView) Findviewbyid (r.id.txt_cur); Sb_normal.setonseekbarchangelistener (NewSeekbar.onseekbarchangelistener () {@Override             Public void onprogresschanged(SeekBar SeekBar,intProgressBooleanFromuser) {Txt_cur.settext ("Current progress value:"+ Progress +"/"); }@Override             Public void Onstarttrackingtouch(SeekBar SeekBar) {Toast.maketext (Mcontext,"Touch Seekbar", Toast.length_short). Show (); }@Override             Public void Onstoptrackingtouch(SeekBar SeekBar) {Toast.maketext (Mcontext,"Let go of Seekbar.", Toast.length_short). Show ();    }        }); }}
2. Simple Seekbar customization:

Originally also want to customize under Seekbar, later think about, or forget, involves custom view of some things, may beginner and
Do not understand, it seems a bit difficult, about the custom view or put on the advanced there, so here is just a simple custom under seekbar!
Custom content includes sliders, as well as tracks!

code example:

Run:

Code implementation:
1. Slider Status Drawable:sb_thumb.xml

<?xml version= "1.0" encoding= "Utf-8"?  Span class= "Hljs-tag" ><selector  xmlns:android  = "http://schemas.android.com/apk/res/android" ; <item  android:state_pressed  =< Span class= "Hljs-value" > "true"  android:drawable  =  "@mipmap/seekbar_thumb_pressed" /> <item  android:state_pressed  =  android:drawable  = "@mipmap/seekbar_thumb_normal" />  </selector ;  

Paste the material:

2. Drawable:sb_bar.xml Bar Bar
Here to use a layer-list of drawable resources! Actually is the cascade picture, in order: background, two level progress bar, the current progress:

<?xml version= "1.0" encoding= "Utf-8"?><layer-listxmlns:android="Http://schemas.android.com/apk/res/android">         <item android:id="@android: Id/background">        <shape>            <solid android:color="#FFFFD042" />        </shape>    </Item>    <item android:id="@android: id/secondaryprogress">        <clip>            <shape>                <solid android:color="#FFFFFFFF" />            </shape>        </clip>    </Item>    <item android:id="@android: id/progress">        <clip>            <shape>                <solid android:color="#FF96E85D" />            </shape>        </clip>    </Item></layer-list>

3. Then after the layout introduces Seekbar, set the next progressdrawable with the thumb!

    <SeekBar        android:id="@+id/sb_normal"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:maxHeight="5.0dp"        android:minHeight="5.0dp"        android:progressDrawable="@drawable/sb_bar"        android:thumb="@drawable/sb_thumb"/>

It's so simple!

This section summarizes:

OK, about Seekbar to here, thank you all ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--2.3.8 SeekBar (drag bar)

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.