Friday, 22 July 2016

SeekBar Example


Screenshots :





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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.chantisandroid.seekbar.MainActivity">

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="98dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:id="@+id/textView"
        android:layout_below="@+id/seekBar"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp" />
</RelativeLayout>




MainActivity.java :


package com.chantisandroid.seekbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener {

    SeekBar seekBar;
    TextView textView;
    int progress = 0;

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

        textView = (TextView)findViewById(R.id.textView);
        seekBar = (SeekBar)findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(this);

        textView.setText("Count: " + seekBar.getProgress() + "/" + seekBar.getMax());

    }

    @Override

    public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {

        progress = progresValue;

        //here you can write toast message also
        //Toast.makeText(getApplicationContext(), "seekbar's progress", Toast.LENGTH_SHORT).show();

    }

    @Override

    public void onStartTrackingTouch(SeekBar seekBar) {

        //here you can write toast message also
        //Toast.makeText(getApplicationContext(), "Started seekbar", Toast.LENGTH_SHORT).show();

    }

    @Override

    public void onStopTrackingTouch(SeekBar seekBar) {

        textView.setText("Count: " + progress + "/" + seekBar.getMax());

        //here you can write toast message also
        //Toast.makeText(getApplicationContext(), "Stopped seekbar", Toast.LENGTH_SHORT).show();
    }
}




No comments:

Post a Comment

Alarm Manager Example in Android