<?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
Sir I hv tried this code..
ReplyDeleteits not showing any error but program is unexpectedly terminating
Because of the mis text color # 777
DeleteAnd the right to carry a value of seven boxes Example
# 777777
Sir I need your help can you provide your contact detail
ReplyDeletei want my app to start on by clicking three times by power button can anyone tell me how i can do it ?
ReplyDelete