How to Use a Static Method in Android?

Static keyword in part of java language. Static works as a simple variable, method and static block in java. In this article ,we are going to see how  we can implement static methods in android application


Let’s discuss about static keyword or method example 

Step 1:  Create a new project
  • Open a new project
  • We will be working  on empty activity with language as java
  • Name the application at your convenience.
Step 2: Create an XML file named Acitivity_main.xml of the layout folder.
 <?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="80dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTitle"
android:layout_marginStart="30dp"
android:layout_marginTop="104dp"
android:layout_marginEnd="30dp"
android:visibility="gone"
android:hint="Typing...."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

<Button
android:id="@+id/btnsubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:text="Click to show toast"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<Button
android:id="@+id/btnsubmitAlertDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Click to show Alert Dialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnsubmit" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Create ActivityMain.java or java in your project inside (com.my application).
 import android.os.Bundle;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonsend = findViewById(R.id.btnsubmit);
Button btnsubmitAlertDialog = findViewById(R.id.btnsubmitAlertDialog);
buttonsend.setOnClickListener(v -> {
StaticMethod.displayToast(MainActivity.this, getString(R.string.app_name));
});
btnsubmitAlertDialog.setOnClickListener(v -> {
StaticMethod.displayAlertDialog(MainActivity.this, getString(R.string.app_name), "welcome ");
});
}
 }
Step 4: Output of above example 


Happy coding!

365Bloggy May 22, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive