Search code examples
javaandroidlayoutsearchview

How to make Searchview Widget input go to specific screen regardless of user input


Beginner here looking to make a proof of concept App to demo. I have a simple app laid out with 2 screens (XML layouts and Activities are already created). The first screen has a SearchView Widget in the center of the screen. When a user performs a search I would simply like the app to go to the 2nd screen regardless of what the user inputs. Any help would be greatly appreciated.

 <SearchView
    android:id="@+id/searchView1"
    android:layout_width="fill_parent"
    android:layout_height="55dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:background="@drawable/ic_blanksearch"
    android:clickable="true"
    android:hint="@string/search_hint"
    android:textColorHint= "#000000"
    android:padding="10dp" >
</SearchView>

Here is all that is in my MainActivity.java

    package com.tcw.gametime;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Solution

  • You need to start the second activity from the first one (the one containing the SearchWidget), for example if there is a button called 'search' in your activity and if the second activity (the one you want to launch) is called ResultActivity, you should do something like that:

    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent act = new Intent(getApplicationContext(), ResultActivity.class);
            startActivity(act);
        }
    });
    

    Edit for the SearchView, you should do

    private SearchView search;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        search = (SearchView) findViewById(R.id.searchView1);
    
        search.setOnSearchClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Intent act = new Intent(getApplicationContext(), ResultActivity.class);
                startActivity(act);
            }
        });
    }