You are on page 1of 3

package com.example.

myapplication;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.Button;

import android.widget.CompoundButton;

import android.widget.EditText;

import android.widget.RadioGroup;

import android.widget.Spinner;

import android.widget.Toast;

import android.widget.ToggleButton;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

ToggleButton toggleButton;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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


toggleButton.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if(isChecked){

Toast.makeText(MainActivity.this,"TRUE",Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(MainActivity.this, "FALSE",Toast.LENGTH_SHORT).show();

});

@Override

public void onClick(View v) {

-----------------------------------------------

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">

<ToggleButton

android:id="@+id/toggle"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textOn="Seleccionado"

android:textOff="No seleccionado"/>

</RelativeLayout> }

});

You might also like