Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 

ToggleButton Vs Switch In Android:




ToggleButton allows the users to change the setting between two states like turn on/off your wifi, Bluetooth, etc from your phone’s setting menu. Since Android 4.0 version ( API level 14 ) there is another kind of ToggleButton called Switch which provides the user slider control. 



CODE IMPLEMENTATION OF ACTIVITY:-


CODE OF JAVA FILE:-


package com.example.additionapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity18 extends AppCompatActivity {

ToggleButton tgb;
TextView txt;
Switch swtch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main18);
tgb = findViewById(R.id.tgbtn);
txt = findViewById(R.id.txtres22);
swtch = findViewById(R.id.swtch);

swtch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(swtch.isChecked())
{
txt.setText("ON");
}
else
{
txt.setText("OFF");
}
}
});
tgb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(tgb.isChecked())
{
txt.setText("ONN");
}
else
{
txt.setText("OFF");
}
}
});

}
}


CODE OF DESIGN FILE:-

<?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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity18">

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tgbtn"
></ToggleButton>

<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/swtch">

</Switch>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtres22"
></TextView>
</LinearLayout>


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post