Toggle click button in from java android application — Java Android

5Ssoftwaresafety
5S JAVA AND KOTLIN LEARNING
3 min readOct 15, 2020

This is how you can attach an Android onClickListener to ToggleButtonMain Activity in Java Android Studio:

Android Options Menu in JAVA ANDROID

This example demonstrates how to use the Learn how to create a “toggle switch” (on/off button) with java. Also you are going to make onToggleClick(View view) practice.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Create Empty Project

How to use toggle button in android?

Step 2 — “Add the following code to res/layout/activity_main.xml.“

I worked in simple relative layout method for only give basic example.

*In the below code, we have declared ToggleButton as ”@+id/toggleButton”

RELATIVE LAYOUT FILE CODE IN 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.muratmete.templatecodefordesign.MainActivity"
android:padding="16dp"
android:id="@+id/relLayout"
android:orientation="vertical"
>

<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="onToggleClick"
/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/toggleButton"
android:layout_centerInParent="true"
android:layout_marginBottom="100dp"
android:textColor="#5E35B1"
android:textStyle="bold"
/>

<TextView
android:id="@+id/textViewJavaLearn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="550dp"
android:gravity="center"
android:text="5S JAVA AND KOTLIN LEARNING"
/>


</RelativeLayout>

Step 3: MainActivity.java” java file − Add the following code to src/MainActivity.java

import android.widget.ToggleButton;

JAVA CODE IN MAIN ACTIVITY:

package com.yourname.yourpackagename;import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


ToggleButton toggleButton;
TextView textView;

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

toggleButton = (ToggleButton) findViewById(R.id.toggleButton);

textView = (TextView) findViewById(R.id.textView);

}

public void onToggleClick(View view){
if(toggleButton.isChecked()){
textView.setText("Toggle is ON");
}
else{
textView.setText("Toggle is OFF");
}
}
}

Review Application

A Toggle Button is a two state button. It is either pressed or not pressed. Following example showcase how to modify the default appearance of a Button in a Java application.

We are using the following APIs.

  • JToggleButton() − To create a toggle button.
  • In android, Toggle Button is a user interface control that is used to display ON (Checked) or OFF (Unchecked) states as a button with a light indicator. The ToggleButton is useful for the users to change the settings between two states either ON or OFF.

Step 4: Results and How to Work OnCreateOptionsMenu !

Click the off button and then change on button at the same time you see Toggle is On

public void onToggleClick(View view){
if(toggleButton.isChecked()){
textView.setText(“Toggle is ON”); }
else{
textView.setText(“Toggle is OFF”); }
}

In this lesson, you learnt How do I implement a simple click onToggleClick(View view) for a ToggleButton in java.

How to use ToggleButton in Android !

What is ToggleButton

ToggleButton: A toggle button allows the user to change a setting between two states.

If you have any question write comments I will reply.

Thanks for reading my blog.

If you need Google AdMob Alternatives you can check out Start Ad Platfrom.

https://portal.start.io/#/signup?referredby=69e578e9-ec0c-24b3-a74e-f58373fac2ef&preferredsite=pub&source=directURL

Have a nice day.

Regards

5S JAVA AND KOTLIN LEARNING

--

--