UI components: Switch, RatingBar

Here in this post, I am teaching you to C\create an application having the following UI components:

  1. Switch
  2. RatingBar

Read This Post –> UI component: Spinner, DatePicker, TimePicker, SeekBar

Steps Description
1 create a project in android studio with any name.
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml

Content of src/MainActivity.java 

package com.example.kenilvavaliya.module24;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Switch;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Switch sw;
    private RatingBar rb;
    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sw = (Switch)findViewById(R.id.switch1);
        rb = (RatingBar)findViewById(R.id.ratingBar);
        btn = (Button)findViewById(R.id.button);

    }
    public void onSwitchClick(View v)
    {
        if (sw.isChecked())
        {
            Toast.makeText(this,"Switch is ON. You can allow Rating",Toast.LENGTH_SHORT).show();
            rb.setVisibility(v.VISIBLE);
            btn.setVisibility(v.VISIBLE);
        }
        else
        {
            Toast.makeText(this,"Switch is OFF. You cannot allow Rating",Toast.LENGTH_SHORT).show();
            rb.setVisibility(v.INVISIBLE);
            btn.setVisibility(v.INVISIBLE);
        }
    }
    public void onButtonClick(View v)
    {
        float ratingvalue = rb.getRating();
        Toast.makeText(getApplicationContext(),"Rating is : "+ratingvalue,Toast.LENGTH_SHORT).show();
    }
}

Content of res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_height="match_parent"
    tools:context=".MainActivity">

    <Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="125dp"
        android:text="Switch"
        android:onClick="onSwitchClick"/>

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="221dp"
        android:visibility="invisible"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="151dp"
        android:text="Submit"
        android:onClick="onButtonClick"
        android:visibility="invisible"/>
</RelativeLayout>

Output:

Stay Connect with our app: – 

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

About easyengineering 38 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

Be the first to comment

Leave a Reply

Your email address will not be published.


*