How to create a button in android
- Add a button to the main layout
- Add the button to the activity
- Label the button green
- Add a click listener to the activity
Step 1
- open flash light activity
- click on R. @ the set content view and press f3 (setContentView(R.layout.main);
go to the main view and right under label red put your code : (<Button
android:id="@+id/green_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/green" )
(The “+” means that it’s the first time , I’m defining this id , so give me a new unique number identifier in the R-file. Subsequently the + won’t be added if I need to put another id.)
(string name of green need to be created in string.xml to avoid error after typing the above code. ( <string name="green">Green</string> )
3. Add a quick listener by going to myRedFlashLightActivity and type this code (Button greenButton = (Button)findViewById(R.id.green_button);
greenButton.setOnClickListener(new View.OnClickListener() { ).
4. Run on emulator.
The outcome :
No comments:
Post a Comment