Search Tutorials

Monday, 11 November 2013

Perform Action on Hardware Buttons in Android

In this application, we will learn how to perform action when we click on any hardware button, for example menu button, home button, any numbers, power button, etc. Here we are giving a simple android code example of a menu button; we created a custom menu which is initially invisible. It becomes visible when we click on the menu button. We used many buttons in horizontal scroll view, placed at the bottom of the parent layout. We can create a simple example also. For instance, we could display the hardware code on text view whenever we click on any hardware button. To understand how it is done, create a new project and use the XML code given below in your main android XML file:

Perform Action on any Hardware Button in Android
Perform Action on any Hardware Button in Android

 <?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/r/android"
  android:id="@+id/horizontalScrollView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:scrollbars="none"
  android:layout_gravity="bottom"
  android:visibility="invisible">
<LinearLayout
  android:id="@+id/linearLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
>
<Button
  android:id="@+id/one"
  android:layout_width="80dp"
  android:layout_height="56dp"
  android:text="One"
  android:textColor="#fff"
  android:background="#555"
  android:textSize="18sp"/>
<Button
  android:id="@+id/two"
  android:layout_width="80dp"
  android:layout_height="56dp"
  android:text="Two"
  android:textColor="#fff"
  android:background="#777"
  android:textSize="18sp"/>
<Button
  android:id="@+id/three"
  android:layout_width="80dp"
  android:layout_height="56dp"
  android:text="Three"
  android:textColor="#fff"
  android:background="#555"
  android:textSize="18sp"/>
<Button
  android:id="@+id/four"
  android:layout_width="80dp"
  android:layout_height="56dp"
  android:text="Four"
  android:textColor="#fff"
  android:background="#777"
  android:textSize="18sp"/>
<Button
  android:id="@+id/five"
  android:layout_width="80dp"
  android:layout_height="56dp"
  android:text="Five"
  android:textColor="#fff"
  android:background="#555"
  android:textSize="18sp"/>
<Button
  android:id="@+id/six"
  android:layout_width="80dp"
  android:layout_height="56dp"
  android:text="Six"
  android:textColor="#fff"
  android:background="#777"
  android:textSize="18sp"/>
<Button
  android:id="@+id/seven"
  android:layout_width="80dp"
  android:layout_height="56dp"
  android:text="Seven"
  android:textColor="#fff"
  android:background="#555"
  android:textSize="18sp"/>
</LinearLayout>
</HorizontalScrollView>

Now open your Java file and use the code below. To enable action on the click of a hardware button, onKeyDown is a method which is invoked whenever we click on any hardware button. This method overrides the original method and it is invoked first. We can display every code of the hardware button which is clicked by user by simply displaying the keycode value in a given method before the 'if' condition. The code of the android Java file is given below with explanation:


package selecom.smarty; // your package name 
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.HorizontalScrollView;

public class SmartyActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  }
  //The onKeyDown method is Called when any hardware button is clicked
  //This method takes two parameters, first hardware button code
  //Which is clicked and key event object which will have all
  //hardware button codes
  public boolean onKeyDown(int keycode, KeyEvent event)
  {
   //if clicked button code is equal to menu button code
   //then the if condition will turn true
   //To perform action on another button just write keyEvent
   //followed by dot(.) and press CTRL+Space and you will get all
   //the code options. Select anyone and perform your action.
   if(keycode==KeyEvent.KEYCODE_MENU)
   {
    HorizontalScrollView h=(HorizontalScrollView)findViewById(R.id.horizontalScrollView1);
     h.setVisibility(1);
    }
   return super.onKeyDown(keycode, event);
  }
}

Now run your project and test application. If you have any doubts please comment. Share and help all android developers.

Related Tutorials:-

Enable and Disable WiFi

Enable and Disable Bluetooth

Get Latitude and Longitude of the current Location

Use Sensor | Motion detection

Convert Text to Speech

Use Shared Preferences

4 comments:

  1. Sir I hv tried this code..
    its not showing any error but program is unexpectedly terminating

    ReplyDelete
    Replies
    1. Because of the mis text color # 777
      And the right to carry a value of seven boxes Example
      # 777777

      Delete
  2. Sir I need your help can you provide your contact detail

    ReplyDelete
  3. i want my app to start on by clicking three times by power button can anyone tell me how i can do it ?

    ReplyDelete

Back to Top