Created
November 27, 2020 03:41
-
-
Save edward1986/16e00358b6f7ab0574c270ae47f9a9e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hello World!" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
> | |
<TextView | |
android:id="@+id/textViewSearchResult" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.searchview"> | |
<application | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultActivity" /> | |
<activity android:name=".MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".SearchResultActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.SEARCH" /> | |
</intent-filter> | |
<meta-data android:name="android.app.searchable" | |
android:resource="@xml/searchable" /> | |
</activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.searchview; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.app.SearchManager; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.widget.SearchView; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
MenuInflater inflater = getMenuInflater(); | |
inflater.inflate(R.menu.menu_search, menu); | |
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); | |
MenuItem searchItem = menu.findItem(R.id.menu_search); | |
SearchView searchView = (SearchView) searchItem.getActionView(); | |
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.searchview; | |
import android.app.SearchManager; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import androidx.appcompat.app.AppCompatActivity; | |
public class SearchResultActivity extends AppCompatActivity { | |
TextView mTextViewSearchResult; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_search_result); | |
mTextViewSearchResult = findViewById(R.id.textViewSearchResult); | |
if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) { | |
handleSearch(getIntent().getStringExtra(SearchManager.QUERY)); | |
} | |
} | |
private void handleSearch(String searchQuery) { | |
Toast.makeText(getApplicationContext(), searchQuery, Toast.LENGTH_SHORT).show(); | |
mTextViewSearchResult.setText(searchQuery); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment