Tuesday, October 5, 2021

Seek Bar in Android

 

Seek Bar

Xml code:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:orientation="vertical"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity">
    <
RatingBar
       
android:id="@+id/rate"
       
android:layout_width="wrap_content"
       
android:numStars="3"
       
android:layout_height="wrap_content"/>
    <
Button
        
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center"
       
android:id="@+id/btn_rate"/>
    <
TextView
       
android:layout_margin="30dp"
       
android:id="@+id/tv_progress"
        
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:text="Progress is"
       
android:textStyle="bold"
       
android:textSize="20sp"
       
android:gravity="center_horizontal"
       
android:textAlignment="center"/>
    <
SeekBar
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:scrollbarSize="38dp"
       
android:id="@+id/seekbar"/>
</
LinearLayout>

 

Java code:-

package com.example.seekbarandreekbardemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    RatingBar
ratingbar;
    Button
btn;
    TextView
txt;
    SeekBar
bar;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
bar=findViewById(R.id.seekbar);
       
btn=findViewById(R.id.btn_rate);
       
ratingbar=findViewById(R.id.rate);
      
txt=findViewById(R.id.tv_progress);
      
bar.setMin(50);
      
bar.setMax(200);
      
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
          
@Override
          
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
              
txt.setText("Progress is:"+progress);
           }

          
@Override
          
public void onStartTrackingTouch(SeekBar seekBar) {

           }

          
@Override
          
public void onStopTrackingTouch(SeekBar seekBar) {

           }
       });
       
btn.setOnClickListener(new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
                String rate= String.valueOf(
ratingbar.getRating());
                Toast.makeText(MainActivity.
this, "You Gave"+rate, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

 

No comments:

Post a Comment

Activity Navigation One Activity to another Activity

Main Activity :-   <? xml version ="1.0" encoding ="utf-8" ?> < RelativeLayout xmlns: android ="http://sch...