banner



how to create menu in android studio

  • Updated date Feb 16, 2020
  • 73.9k
  • 0

Android is one of the most popular operating systems for mobile. In this article, I will show you how to create Bluetooth android applications using Android studio.

Introduction

Android is one of the most popular operating systems for mobile. In this article, I will show you how to create Bluetooth Android applications using Android Studio.

Requirements

  • Android Studio version 2.3.3
  • Little bit XML and JAVA knowledge.
  • Android Emulator (or) Android mobile
  • Download link (Android Studio)

Steps to be followed

Follow these steps to create a Bluetooth Android application. I have included the source code in the attachment.

Step 1

Open Android Studio and start a new Android Studio Project.

Android

Step 2

You can choose your application name and choose where your project is to be stored. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.

Android

Now, select the version of Android and select the target Android devices.

Android

Step 3

Now, add the activity and click the "Next" button.

Android

Add activity name and click "Finish".

Android

Step 4

Go to activity_main.xml. This XML file contains the designing code for your Android app.

Android

The XML code is given below.

  1. <?xml version= "1.0"  encoding= "utf-8" ?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="abu.bluetooth.MainActivity"  >
  8.     <RelativeLayout
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:orientation="vertical" >
  12.         <TextView
  13.             android:text=""
  14.             android:id="@+id/out"
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content" ></TextView>
  17.         <Button
  18.             android:id="@+id/button1"
  19.             android:layout_width="wrap_content"
  20.             android:layout_height="wrap_content"
  21.             android:layout_alignParentLeft="true"
  22.             android:layout_alignParentTop="true"
  23.             android:layout_marginLeft="30dp"
  24.             android:layout_marginTop="49dp"
  25.             android:text="ON"  />
  26.         <Button
  27.             android:id="@+id/button2"
  28.             android:layout_width="wrap_content"
  29.             android:layout_height="wrap_content"
  30.             android:layout_alignLeft="@+id/button1"
  31.             android:layout_below="@+id/button1"
  32.             android:layout_marginTop="27dp"
  33.             android:text="COVERAGE"  />
  34.         <Button
  35.             android:id="@+id/button3"
  36.             android:layout_width="wrap_content"
  37.             android:layout_height="wrap_content"
  38.             android:layout_alignLeft="@+id/button2"
  39.             android:layout_below="@+id/button2"
  40.             android:layout_marginTop="28dp"
  41.             android:text="OFF"  />
  42.     </RelativeLayout>
  43. </android.support.constraint.ConstraintLayout>

Step 5

Go to  Main Activity.java. This Java program is the backend language for your app.

Android

The Java code is given below.

  1. package  abu.bluetooth;
  2. import  android.os.Bundle;
  3. import  android.app.Activity;
  4. import  android.view.Menu;
  5. import  android.app.Activity;
  6. import  android.bluetooth.BluetoothAdapter;
  7. import  android.content.Context;
  8. import  android.content.Intent;
  9. import  android.os.Bundle;
  10. import  android.util.Log;
  11. import  android.view.View;
  12. import  android.widget.Button;
  13. import  android.widget.TextView;
  14. import  android.widget.Toast;
  15. public class  MainActivity extends  Activity {
  16. private static final int  REQUEST_ENABLE_BT = 0;
  17. private static final int  REQUEST_DISCOVERABLE_BT = 0;
  18.     @Override
  19. protected void  onCreate(Bundle savedInstanceState) {
  20. super .onCreate(savedInstanceState);
  21.         setContentView(R.layout.activity_main);
  22. final  TextView out = (TextView) findViewById(R.id.out);
  23. final  Button button1 = (Button) findViewById(R.id.button1);
  24. final  Button button2 = (Button) findViewById(R.id.button2);
  25. final  Button button3 = (Button) findViewById(R.id.button3);
  26. final  BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.
  27.                 getDefaultAdapter();
  28. if  (mBluetoothAdapter == null ) {
  29.             out.append("device not supported" );
  30.         }
  31.         button1.setOnClickListener(new  View.OnClickListener() {
  32. public void  onClick(View v) {
  33. if  (!mBluetoothAdapter.isEnabled()) {
  34.                     Intent enableBtIntent =new  Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  35.                     startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  36.                 }
  37.             }
  38.         });
  39.         button2.setOnClickListener(new  View.OnClickListener() {
  40.             @Override
  41. public void  onClick(View arg0) {
  42. if  (!mBluetoothAdapter.isDiscovering()) {
  43.                     Context context = getApplicationContext();
  44.                     CharSequence text ="MAKING YOUR DEVICE DISCOVERABLE" ;
  45. int  duration = Toast.LENGTH_SHORT;
  46.                     Toast toast = Toast.makeText(context, text, duration);
  47.                     toast.show();
  48.                     Intent enableBtIntent =new  Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  49.                     startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);
  50.                 }
  51.             }
  52.         });
  53.         button3.setOnClickListener(new  View.OnClickListener() {
  54.             @Override
  55. public void  onClick(View arg0) {
  56.                 mBluetoothAdapter.disable();
  57.                 Context context = getApplicationContext();
  58.                 CharSequence text ="TURNING_OFF BLUETOOTH" ;
  59. int  duration = Toast.LENGTH_LONG;
  60.                 Toast toast = Toast.makeText(context, text, 15);
  61.                 toast.show();
  62.             }
  63.         });
  64.     }
  65.     @Override
  66. public boolean  onCreateOptionsMenu(Menu menu) {
  67.         getMenuInflater().inflate(R.menu.activity_main, menu);
  68. return true ;
  69.     }
  70. }

Step 6

We need to make a Bluetooth request. So, add the Bluetooth permission in AndroidManifest.xml.

Android

The AndroidManifest.xml code is given below.

  1. <?xml version= "1.0"  encoding= "utf-8" ?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package = "abu.bluetooth" >
  4.     <uses-permission android:name="android.permission.BLUETOOTH"  />
  5.     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"  />
  6.     <application
  7.         android:allowBackup="true"
  8.         android:icon="@mipmap/ic_launcher"
  9.         android:label="@string/app_name"
  10.         android:roundIcon="@mipmap/ic_launcher_round"
  11.         android:supportsRtl="true"
  12.         android:theme="@style/AppTheme" >
  13.         <activity android:name=".MainActivity" >
  14.             <intent-filter>
  15.                 <action android:name="android.intent.action.MAIN"  />
  16.                 <category android:name="android.intent.category.LAUNCHER"  />
  17.             </intent-filter>
  18.         </activity>
  19.     </application>
  20. </manifest>

Step 7

Now, go to the menu bar and click "Make Project" or press ctrl+f9 to debug the app from errors.

Android

Step 8

Then, click the "Run" button or press shift+f10 to finally run the project. And, choose the "virtual machine" option and click OK.

Android

Conclusion

We have successfully created a Bluetooth Android application using Android Studio.

Android

Android

Android

how to create menu in android studio

Source: https://www.c-sharpcorner.com/article/create-bluetooth-android-application-using-android-studio/

Posted by: peterscappire.blogspot.com

0 Response to "how to create menu in android studio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel