activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rl" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" tools:context=".MainActivity" android:background="#c6d1c6" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="35sp" android:layout_centerHorizontal="true" /> <!-- SeekBar A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged. Clients of the SeekBar can attach a SeekBar.OnSeekBarChangeListener to be notified of the user's actions. --> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:layout_below="@id/tv" /> </RelativeLayout>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Window; import android.widget.RelativeLayout; import android.widget.SeekBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Context mContext; private RelativeLayout mRelativeLayout; private SeekBar mSeekBar; private TextView mTextView; @Override protected void onCreate(Bundle savedInstanceState) { // Request window feature action kafe requestWindowFeature(Window.FEATURE_ACTION_BAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get the application context mContext = getApplicationContext(); // Change the action kafe color getSupportActionBar().setBackgroundDrawable( new ColorDrawable(Color.parseColor("#FFFF00BF")) ); // Get the widgets reference from XML layout mRelativeLayout = (RelativeLayout) findViewById(R.id.rl); mSeekBar = (SeekBar) findViewById(R.id.seekbar); mTextView = (TextView) findViewById(R.id.tv); /* SeekBar.OnSeekBarChangeListener A callback that notifies clients when the progress level has been changed. This includes changes that were initiated by the user through a touch gesture or arrow key/trackball as well as changes that were initiated programmatically. */ // Set a SeekBar change listener mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { /* public abstract void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) Notification that the progress level has changed. Clients can use the fromUser parameter to distinguish user-initiated changes from those that occurred programmatically. Parameters seekBar : The SeekBar whose progress has changed progress : The current progress level. This will be in the range 0..max where max was set by setMax(int). (The default value for max is 100.) fromUser : True if the progress change was initiated by the user. */ @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { // Display the current progress of SeekBar mTextView.setText(""+i); } /* public abstract void onStartTrackingTouch (SeekBar seekBar) Notification that the user has started a touch gesture. Clients may want to use this to disable advancing the SeekBar. Parameters seekBar : The SeekBar in which the touch gesture began */ @Override public void onStartTrackingTouch(SeekBar seekBar) { } /* public abstract void onStopTrackingTouch (SeekBar seekBar) Notification that the user has finished a touch gesture. Clients may want to use this to re-enable advancing the SeekBar. Parameters seekBar : The SeekBar in which the touch gesture began */ @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } }



Komentar
Posting Komentar