Skip to content

Commit

Permalink
Added a refresh button and modified the code in forecastfragment.java…
Browse files Browse the repository at this point in the history
… to display the menu options and the refresh button.
  • Loading branch information
himakiran committed Nov 10, 2016
1 parent e6511fd commit 7ef8b88
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
Expand All @@ -32,11 +35,22 @@ public class ForecastFragment extends Fragment {
public ForecastFragment() {
}

// to create the object
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this code makes sure menu gets displayed and further allows onCreateOptionsMenu to function
setHasOptionsMenu(true);

}

//to create the view
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// the below line calls the layout defined in fragment_main
View rootView = inflater.inflate(fragment_main, container, false);

// this is the array values that get filled in the layout
ArrayList<String> dailyWeatherUpdate = new ArrayList<String>();
dailyWeatherUpdate.add("Today - Sunny - 88/76");
Expand All @@ -62,10 +76,33 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
ListView listview = (ListView) rootView.findViewById(R.id.listview_forecast);
listview.setAdapter(adapter);


return rootView;

}

// to create the menu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// this code inflates the menu we detailed in forecastfragment.xml
inflater.inflate(R.menu.forecastfragment, menu);


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
//code to handle each menu item
int id = item.getItemId();
if (id == R.id.action_refresh) {

return true;
}

return super.onOptionsItemSelected(item);
}

// to fetch data from external source
public class FetchWeatherTask extends AsyncTask<String, String, String> {

public FetchWeatherTask() {
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/res/menu/forecastfragment.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_forecast">
<item android:id="@+id/action_refresh"
android:title="Refresh"
android:showAsAction="ifRoom"
tools:ignore="AppCompatResource" />
android:title="@string/action_refresh"
app:showAsAction="ifRoom" />
<item
android:id="@+id/help"
android:title="@string/help" />

</menu>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<resources>
<string name="app_name">Sunshine</string>
<string name="hello_world">Hello World !</string>
<string name="action_refresh" translatable="false">Refresh</string>
<string name="help">Help</string>


</resources>

0 comments on commit 7ef8b88

Please sign in to comment.