In this pull request for Hocktoberfest I contributed to a small android app project InTalks. For this issue the project author wanted the search var to be added to the main page and results displayed in the second activity.
The Issue:
Add the search box to the First Activity and display results on the second activity
The issue is to add the search box to the main activity and search results put onto the second activity.
The Pull Request:
resolve #3 – add search box to MainActivity
First I needed to add the EditText to the main activity layout:
<EditText
android:id="@+id/main_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="21sp"
android:padding="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="Search by Topic or Genre"
android:background="@color/white"
app:layout_constraintBottom_toTopOf="@id/genreButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
I kept the styling of this as basic as possible since the preferred styling was not specified in the issue, so i strictly added the search functionality.
Next I added the code in the MainActivity to grab the query result and pass it into the next activity:
searchBoxEditText = findViewById(R.id.main_search);
Button topicBtn = findViewById(R.id.topicButton);
Button genr = findViewById(R.id.genreButton);
topicBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String githubQuery = searchBoxEditText.getText().toString();
Intent toTopicActivity = new Intent(MainActivity.this, TopicActivity.class);
toTopicActivity.putExtra("query", githubQuery);
startActivity(toTopicActivity);
}
});
genr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String githubQuery = searchBoxEditText.getText().toString();
Intent toTTopicActivity = new Intent(MainActivity.this, NewMainActivity.class);
toTTopicActivity.putExtra("query", githubQuery);
startActivity(toTTopicActivity);
}
});
Next I grabbed the result from the main activity to use in the second activity to query for the results:
Intent intent = getIntent();
String queryMessage = intent.getStringExtra("query");
Finally I use this value to query for results:
URL githubSearchUrl = NetworkUtils.buildUrl(queryMessage);
mUrlDisplayTextView.setText(githubSearchUrl.toString());
Bundle queryBundle = new Bundle();
queryBundle.putString(SEARCH_QUERY_URL_EXTRA, githubSearchUrl.toString());
LoaderManager loaderManager = getSupportLoaderManager();
Loader<String> githubSearchLoader = loaderManager.getLoader(GITHUB_SEARCH_LOADER);
if (githubSearchLoader == null) {
loaderManager.initLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
} else {
loaderManager.restartLoader(GITHUB_SEARCH_LOADER, queryBundle, this);
}
The issue was not the most detailed in explanations, so I left my initial pull request as basic as possible just to address the request in the issue. For now I will wait for feedback before moving forward with changes.