How to Create SearchView Dialog in Android

In this android programming source code example, we are going to explore how to create searchview dialog in android.

Below is a step by step source code to create searchview dialog in android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true">

</RelativeLayout>

MainActivity.java

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.View;

import com.bluapp.androidview.R;

public class SearchViewActivity9 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_view9);
        showSearchDialog();
    }

    public void showSearchDialog(){
        final SearchView taskSearchview = new SearchView(this);
        taskSearchview.setQueryHint("Search Name");
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setTitle("Title");
        alertDialogBuilder.setMessage("What do you want to Search?");
        alertDialogBuilder.setView(taskSearchview);
        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment