What will I Learn?
Requirements
Procedure
- Now open the activity_main.xml file as shown the tree structure here and click on split button to show the code and design at the same time.
- Now replace the code as given here it include a Linear Layout and a Button inside it with the id message_btn
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<Button
android:id="@+id/message_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Message"/>
</LinearLayout>
- Open the MainActivity.java file as shown the RED line in this mockup
- Insert the Button click code after this line
setContentView(R.layout.activity_main);
here is the button click code
//Its a code for a button click that we declare in activity_main.xml file
findViewById(R.id.message_btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Its a code to show the message on the screen
Toast.makeText(MainActivity.this, "Hello Hive", Toast.LENGTH_SHORT).show();
}
});
- Picture of all code setup in MainActivity.java
- Now connect you mobile device and lets test the app make sure the Developer Option is on and the USB Debugging is enabled.
- Click on the play button to run the app
Final Result
| App on Mobile | Button Clicked |
|---|---|
Follow | Upvote | Share
About me
Hello Friends I am Muhammad Faisal Amin. My country is Pakistan. nI live in a city of Pakistan named Faisalabad. I love my country.
I am 21 years old in 2020.
I study in Software Engineering in University of Agriculture Faisalabad Main Campus it also knows as UAF.
Its a very beautiful University and the biggest University in Asia.
I am also having experiences in MObile App Development using Java Android and Flutter also.
Its my passion to develop quality app for both iPhone and Android Mobiles.