From: Gustavo Martin Morcuende Date: Wed, 31 Aug 2016 19:30:21 +0000 (+0200) Subject: Using AppCompat X-Git-Tag: weatherinformation-1.3~10 X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=9ca43164a70403908ac52839a232e052cb0272b9;p=AndroidWeatherInformation Using AppCompat --- diff --git a/app/build.gradle b/app/build.gradle index dfd6749..f22eb2e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -54,6 +54,7 @@ dependencies { compile 'com.android.support:support-v4:+' compile 'com.google.android.gms:play-services-maps:+' compile 'com.google.android.gms:play-services-analytics:+' + compile 'com.android.support:design:+' compile 'com.fasterxml.jackson.core:jackson-core:2.3.3' compile 'com.jakewharton.timber:timber:2.5.0' } diff --git a/app/src/main/java/name/gumartinm/weather/information/activity/AboutActivity.java b/app/src/main/java/name/gumartinm/weather/information/activity/AboutActivity.java index 12d19c0..35fd93e 100644 --- a/app/src/main/java/name/gumartinm/weather/information/activity/AboutActivity.java +++ b/app/src/main/java/name/gumartinm/weather/information/activity/AboutActivity.java @@ -15,16 +15,16 @@ */ package name.gumartinm.weather.information.activity; -import android.app.ActionBar; -import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; import android.view.View; import name.gumartinm.weather.information.R; -public class AboutActivity extends Activity { +public class AboutActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { @@ -36,7 +36,7 @@ public class AboutActivity extends Activity { public void onResume() { super.onResume(); - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); actionBar.setTitle(this.getString(R.string.weather_about_action)); } diff --git a/app/src/main/java/name/gumartinm/weather/information/activity/LicensesActivity.java b/app/src/main/java/name/gumartinm/weather/information/activity/LicensesActivity.java index 992dc29..8aac5d3 100644 --- a/app/src/main/java/name/gumartinm/weather/information/activity/LicensesActivity.java +++ b/app/src/main/java/name/gumartinm/weather/information/activity/LicensesActivity.java @@ -15,9 +15,9 @@ */ package name.gumartinm.weather.information.activity; -import android.app.ActionBar; -import android.app.Activity; import android.os.Bundle; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; import android.webkit.WebView; import com.google.android.gms.common.GooglePlayServicesUtil; @@ -31,7 +31,7 @@ import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.nio.charset.Charset; -public class LicensesActivity extends Activity { +public class LicensesActivity extends AppCompatActivity { private WebView mWebView; @@ -47,7 +47,7 @@ public class LicensesActivity extends Activity { public void onResume() { super.onResume(); - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); actionBar.setTitle(this.getString(R.string.weather_licenses_title)); final String googlePlayServices = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(this.getApplicationContext()); diff --git a/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java b/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java index 05a81a3..ccc70a4 100644 --- a/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java +++ b/app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java @@ -15,22 +15,25 @@ */ package name.gumartinm.weather.information.activity; -import android.app.ActionBar; -import android.app.ActionBar.Tab; -import android.app.FragmentTransaction; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.design.widget.TabLayout; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; +import java.text.MessageFormat; +import java.util.Locale; + import name.gumartinm.weather.information.R; import name.gumartinm.weather.information.fragment.APIKeyNoticeDialogFragment; import name.gumartinm.weather.information.fragment.current.CurrentFragment; @@ -38,11 +41,8 @@ import name.gumartinm.weather.information.fragment.overview.OverviewFragment; import name.gumartinm.weather.information.model.DatabaseQueries; import name.gumartinm.weather.information.model.WeatherLocation; -import java.text.MessageFormat; -import java.util.Locale; - -public class MainTabsActivity extends FragmentActivity { +public class MainTabsActivity extends AppCompatActivity { private static final int NUM_ITEMS = 2; private ViewPager mPager; @@ -52,50 +52,19 @@ public class MainTabsActivity extends FragmentActivity { this.setContentView(R.layout.weather_main_tabs); this.mPager = (ViewPager)this.findViewById(R.id.pager); - this.mPager.setAdapter(new TabsAdapter(this.getSupportFragmentManager())); - + this.mPager.setAdapter(new TabsAdapter(this.getSupportFragmentManager(), this.getApplicationContext())); - this.mPager.setOnPageChangeListener( - new ViewPager.SimpleOnPageChangeListener() { - @Override - public void onPageSelected(final int position) { - MainTabsActivity.this.getActionBar().setSelectedNavigationItem(position); - } - }); + // Give the TabLayout the ViewPager + TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs); + tabLayout.setupWithViewPager(mPager); - - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); PreferenceManager.setDefaultValues(this, R.xml.weather_preferences, false); // Specify that tabs should be displayed in the action bar. - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); actionBar.setDisplayHomeAsUpEnabled(true); - - // Create a tab listener that is called when the user changes tabs. - final ActionBar.TabListener tabListener = new ActionBar.TabListener() { - - @Override - public void onTabSelected(final Tab tab, final FragmentTransaction ft) { - MainTabsActivity.this.mPager.setCurrentItem(tab.getPosition()); - - } - - @Override - public void onTabUnselected(final Tab tab, final FragmentTransaction ft) { - - } - - @Override - public void onTabReselected(final Tab tab, final FragmentTransaction ft) { - - } - - }; - - actionBar.addTab(actionBar.newTab().setText(this.getString(R.string.text_tab_currently)).setTabListener(tabListener)); - actionBar.addTab(actionBar.newTab().setText(this.getString(R.string.text_tab_five_days_forecast)).setTabListener(tabListener)); } @Override @@ -160,7 +129,7 @@ public class MainTabsActivity extends FragmentActivity { } } - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); // 1. Update title. final DatabaseQueries query = new DatabaseQueries(this.getApplicationContext()); @@ -176,18 +145,7 @@ public class MainTabsActivity extends FragmentActivity { actionBar.setTitle(this.getString(R.string.text_field_no_chosen_location)); } - // 2. Update forecast tab text. - final String keyPreference = this.getString(R.string.weather_preferences_day_forecast_key); - final String value = sharedPreferences.getString(keyPreference, ""); - String humanValue = ""; - if (value.equals(this.getString(R.string.weather_preferences_day_forecast_five_day))) { - humanValue = this.getString(R.string.text_tab_five_days_forecast); - } else if (value.equals(this.getString(R.string.weather_preferences_day_forecast_ten_day))) { - humanValue = this.getString(R.string.text_tab_ten_days_forecast); - } else if (value.equals(this.getString(R.string.weather_preferences_day_forecast_fourteen_day))) { - humanValue = this.getString(R.string.text_tab_fourteen_days_forecast); - } - actionBar.getTabAt(1).setText(humanValue); + } @Override @@ -196,8 +154,12 @@ public class MainTabsActivity extends FragmentActivity { } private class TabsAdapter extends FragmentPagerAdapter { - public TabsAdapter(final FragmentManager fm) { + private final String tabTitles[] = new String[NUM_ITEMS]; + private final Context context; + + public TabsAdapter(final FragmentManager fm, Context context) { super(fm); + this.context = context; } @Override @@ -214,5 +176,30 @@ public class MainTabsActivity extends FragmentActivity { } } + + @Override + public CharSequence getPageTitle(int position) { + final SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(context); + + // 1. Set currently tab text. + final String currently = context.getString(R.string.text_tab_currently); + tabTitles[0] = currently; + + // 2. Update forecast tab text. + final String keyPreference = context.getString(R.string.weather_preferences_day_forecast_key); + final String value = sharedPreferences.getString(keyPreference, ""); + String forecast = ""; + if (value.equals(context.getString(R.string.weather_preferences_day_forecast_five_day))) { + forecast = context.getString(R.string.text_tab_five_days_forecast); + } else if (value.equals(context.getString(R.string.weather_preferences_day_forecast_ten_day))) { + forecast = context.getString(R.string.text_tab_ten_days_forecast); + } else if (value.equals(context.getString(R.string.weather_preferences_day_forecast_fourteen_day))) { + forecast = context.getString(R.string.text_tab_fourteen_days_forecast); + } + tabTitles[1] = forecast; + + return tabTitles[position]; + } } } diff --git a/app/src/main/java/name/gumartinm/weather/information/activity/MapActivity.java b/app/src/main/java/name/gumartinm/weather/information/activity/MapActivity.java index 0f2b59e..69c8eed 100644 --- a/app/src/main/java/name/gumartinm/weather/information/activity/MapActivity.java +++ b/app/src/main/java/name/gumartinm/weather/information/activity/MapActivity.java @@ -15,7 +15,6 @@ */ package name.gumartinm.weather.information.activity; -import android.app.ActionBar; import android.content.Context; import android.location.Criteria; import android.location.Geocoder; @@ -25,9 +24,10 @@ import android.location.LocationManager; import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; @@ -48,7 +48,7 @@ import name.gumartinm.weather.information.model.DatabaseQueries; import name.gumartinm.weather.information.model.WeatherLocation; -public class MapActivity extends FragmentActivity implements +public class MapActivity extends AppCompatActivity implements LocationListener, MapProgressFragment.TaskCallbacks { private static final String PROGRESS_FRAGMENT_TAG = "PROGRESS_FRAGMENT"; @@ -100,7 +100,7 @@ public class MapActivity extends FragmentActivity implements public void onResume() { super.onResume(); - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); actionBar.setTitle(this.getString(R.string.weather_map_mark_location)); WeatherLocation weatherLocation; diff --git a/app/src/main/java/name/gumartinm/weather/information/activity/PreferencesActivity.java b/app/src/main/java/name/gumartinm/weather/information/activity/PreferencesActivity.java index 84d1e8f..4d31f15 100644 --- a/app/src/main/java/name/gumartinm/weather/information/activity/PreferencesActivity.java +++ b/app/src/main/java/name/gumartinm/weather/information/activity/PreferencesActivity.java @@ -15,14 +15,14 @@ */ package name.gumartinm.weather.information.activity; -import android.app.ActionBar; -import android.app.Activity; import android.os.Bundle; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; import name.gumartinm.weather.information.R; import name.gumartinm.weather.information.fragment.preferences.PreferencesFragment; -public class PreferencesActivity extends Activity { +public class PreferencesActivity extends AppCompatActivity { @Override protected void onCreate(final Bundle savedInstanceState) { @@ -38,7 +38,7 @@ public class PreferencesActivity extends Activity { public void onResume() { super.onResume(); - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); actionBar.setTitle(this.getString(R.string.weather_preferences_action_settings)); } } diff --git a/app/src/main/java/name/gumartinm/weather/information/activity/SpecificActivity.java b/app/src/main/java/name/gumartinm/weather/information/activity/SpecificActivity.java index a52c00c..caa0ffe 100644 --- a/app/src/main/java/name/gumartinm/weather/information/activity/SpecificActivity.java +++ b/app/src/main/java/name/gumartinm/weather/information/activity/SpecificActivity.java @@ -15,27 +15,26 @@ */ package name.gumartinm.weather.information.activity; -import android.app.ActionBar; import android.os.Bundle; -import android.support.v4.app.FragmentActivity; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; + +import java.text.MessageFormat; +import java.util.Locale; import name.gumartinm.weather.information.R; import name.gumartinm.weather.information.model.DatabaseQueries; import name.gumartinm.weather.information.model.WeatherLocation; -import java.text.MessageFormat; -import java.util.Locale; - -public class SpecificActivity extends FragmentActivity { +public class SpecificActivity extends AppCompatActivity { @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.weather_specific); - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); - actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_SHOW_TITLE); actionBar.setDisplayHomeAsUpEnabled(true); @@ -49,7 +48,7 @@ public class SpecificActivity extends FragmentActivity { final DatabaseQueries query = new DatabaseQueries(this); final WeatherLocation weatherLocation = query.queryDataBase(); if (weatherLocation != null) { - final ActionBar actionBar = this.getActionBar(); + final ActionBar actionBar = this.getSupportActionBar(); final String[] array = new String[2]; array[0] = weatherLocation.getCity(); array[1] = weatherLocation.getCountry(); diff --git a/app/src/main/res/layout/weather_main_tabs.xml b/app/src/main/res/layout/weather_main_tabs.xml index 272fc03..03bffef 100644 --- a/app/src/main/res/layout/weather_main_tabs.xml +++ b/app/src/main/res/layout/weather_main_tabs.xml @@ -15,9 +15,19 @@ limitations under the License. --> + xmlns:app="http://schemas.android.com/apk/res-auto" + android:orientation="vertical" + android:padding="4dip" + android:gravity="center_horizontal" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + - - - - - - - -