How to create button in android

 Activity_main_xml : coding for layout

<androidx.constraintlayout.widget.ConstraintLayout 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">

<Button
android:id="@+id/button"
android:layout_width="120dp"
android:layout_height="120dp"
android:text="Simple Button"
tools:ignore="MissingConstraints"
/>

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java: Coding For MainActivity
package com.example.howtocreatebuttoninandroid;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button
button; //Comments: Import Button Here

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button); //Comments: Call button from layout activitymain

// Commnents: Set onclicklistener on your button
// when you call something do by clicking the button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//This is Toast when you run your project and click on your button
// it will call the toast and show you the "Hello Text"

Toast.makeText(MainActivity.this,"Hello",Toast.LENGTH_SHORT).show();
}
})
;
}
}



OutPut



Comments

Popular posts from this blog

Tips to lose belly fat without dieting or even exercise

Top Drones Cameras

iPhone 13 prices