Introduction
This blog explains how to create a Toggle button app in an Android Application, using Android Studio.
Requirements
If you want create a Toggle button app, it should follow the steps given below. Step 1 Now, open Android Studio and you can choose the File and subsequently New. Afterwards, choose New Project. Step 2 Here, you can create Your Application name and choose where your project is stored on the location and click Next button. Now, we can select the version of Android, which is Target Android Devices. Step 3 Here, we can add the activity, more activity available, and click Next button. Now, we can write the activity name and click Finish button. Step 4 Now, open your project and you will go to activity_main.xml and afterwards, you will build the design. You should choose toolbox and if you want some other option (Togglebutton, button), the drag and drop method needs to be used. Now, we can see Graphical User Interface design. Step 5 Here, you need to build on the design and write .XML code. activity_mai.xml code. - <?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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="xyz.rvconstructions.www.togglebttnapp.MainActivity">
- <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New ToggleButton" android:id="@+id/toggleButton" android:layout_marginTop="51dp" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/toggleButton2" android:layout_toStartOf="@+id/toggleButton2" />
- <ToggleButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New ToggleButton" android:id="@+id/toggleButton2" android:layout_alignTop="@+id/toggleButton" android:layout_alignRight="@+id/button" android:layout_alignEnd="@+id/button" />
- <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="submit" android:id="@+id/button" android:layout_marginTop="50dp" android:background="#b700ff" android:padding="10dp" android:textColor="#fff" android:textSize="20sp" android:textStyle="bold" android:layout_centerVertical="true" android:layout_centerHorizontal="true" />
- </RelativeLayout>
Step 6 Now, you will go to MainActivity.java page and build Java code. First of all, you will declare a file, which is an extension file. Now, we can see MainActivity.java code. - package xyz.rvconstructions.www.togglebttnapp;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Button;
- import android.widget.Toast;
- import android.widget.ToggleButton;
- public class MainActivity extends AppCompatActivity {
- ToggleButton firstToggleButton, secondToggleButton;
- Button submit;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- firstToggleButton = (ToggleButton) findViewById(R.id.toggleButton);
- secondToggleButton = (ToggleButton) findViewById(R.id.toggleButton2);
- submit = (Button) findViewById(R.id.button);
- submit.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String status = "UPS : " + firstToggleButton.getText() + "\n" + "POWER : " + secondToggleButton.getText();
- Toast.makeText(getApplicationContext(), status, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
Step 7 Here, you will go to run and select Run-> Run app option. Here, you will choose Emulator or the device and it is Nokia Nokia _X. Step 8
Here, you can see the output. Now, you will click any toggle button and you can see the output.