How to programmatically hide Android soft keyboard

We will learn how to hide the keyboard programmatically. The keyboard generally hides but there are certain when it does not hide. Keyboard is hidden programmatically in mobile application.




Step-By-Step Implementation
Step 1: Create a new project in Android or you can use an already created project. 



or



Step 2: Create an XML file named Acitivity_main.xml in 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/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

 <TextView

android:id="@+id/text_view_result"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="32dp"

android:text="Candidroot solutions"

android:textColor="#061E98"

android:textSize="22sp"

android:textStyle="bold"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

 <EditText

android:id="@+id/edit_text_input"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="76dp"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintHorizontal_bias="1.0"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/text_view_result" />

 <Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:layout_marginTop="124dp"

android:onClick="setText"

android:text="Submit"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/edit_text_input" />

 </androidx.constraintlayout.widget.ConstraintLayout>

 

Step 3: Create ActivityMain.java in your project inside (com.my application).

 import android.content.Context;

 import android.os.Bundle;

 import android.view.View;

 import android.view.inputmethod.InputMethodManager;

 import android.widget.EditText;

 import android.widget.TextView;

 import androidx.activity.EdgeToEdge;

 import androidx.appcompat.app.AppCompatActivity;

 public class MainActivity extends AppCompatActivity {

private TextView textViewResult;

private EditText editTextInput;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

EdgeToEdge.enable(this);

setContentView(R.layout.activity_main);

 

textViewResult

= findViewById(

R.id.text_view_result);

editTextInput

= findViewById(

R.id.edit_text_input);

}

public void setText(View v)

{

String newText

= editTextInput.getText().toString();

textViewResult.setText(newText);

 

closeKeyboard();

editTextInput.setText("");

}

 

private void closeKeyboard()

{

 

// in this layout

View view = this.getCurrentFocus();

// the app from crash

if (view != null) {

 

// service to InputMethodManager

InputMethodManager manager

= (InputMethodManager)

getSystemService(

Context.INPUT_METHOD_SERVICE);

manager

.hideSoftInputFromWindow(

view.getWindowToken(), 0);

}

}

 }


Step 4: Output above example


    


happy coding!  


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


Archive