Using AppCompat
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 31 Aug 2016 19:30:21 +0000 (21:30 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Wed, 31 Aug 2016 19:30:21 +0000 (21:30 +0200)
app/build.gradle
app/src/main/java/name/gumartinm/weather/information/activity/AboutActivity.java
app/src/main/java/name/gumartinm/weather/information/activity/LicensesActivity.java
app/src/main/java/name/gumartinm/weather/information/activity/MainTabsActivity.java
app/src/main/java/name/gumartinm/weather/information/activity/MapActivity.java
app/src/main/java/name/gumartinm/weather/information/activity/PreferencesActivity.java
app/src/main/java/name/gumartinm/weather/information/activity/SpecificActivity.java
app/src/main/res/layout/weather_main_tabs.xml
app/src/main/res/values-v11/styles.xml
app/src/main/res/values-v14/styles.xml
app/src/main/res/values/styles.xml

index dfd6749..f22eb2e 100644 (file)
@@ -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'
 }
index 12d19c0..35fd93e 100644 (file)
  */
 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));
     }
 
index 992dc29..8aac5d3 100644 (file)
@@ -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());
index 05a81a3..ccc70a4 100644 (file)
  */
 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];
+        }
     }
 }
index 0f2b59e..69c8eed 100644 (file)
@@ -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;
index 84d1e8f..4d31f15 100644 (file)
  */
 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));
     }
 }
index a52c00c..caa0ffe 100644 (file)
  */
 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();
index 272fc03..03bffef 100644 (file)
     limitations under the License.
 -->
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-        android:orientation="vertical" android:padding="4dip"
-        android:gravity="center_horizontal"
-        android:layout_width="match_parent" android:layout_height="match_parent">
+    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">
+
+    <android.support.design.widget.TabLayout
+        android:id="@+id/sliding_tabs"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        app:tabMode="scrollable"
+        android:fillViewport="false" />
 
     <android.support.v4.view.ViewPager
             android:id="@+id/pager"
index bf303e9..5e8e931 100644 (file)
         Base application theme for API 11+. This theme completely replaces
         AppBaseTheme from res/values/styles.xml on API 11+ devices.
     -->
-    <style name="AppBaseTheme" parent="android:Theme.Holo">
+    <style name="AppBaseTheme" parent="Theme.AppCompat">
         <!-- API 11 theme customizations can go here. -->
     </style>
 
-    <!-- Application theme. -->
-    <style name="AppTheme" parent="android:Theme.Holo">
-        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
-    </style>
-
     <style name="Divider">
         <item name="android:layout_width">match_parent</item>
         <item name="android:layout_height">1dp</item>
index f225f5a..2e89c9b 100644 (file)
         AppBaseTheme from BOTH res/values/styles.xml and
         res/values-v11/styles.xml on API 14+ devices.
     -->
-    <style name="AppBaseTheme" parent="android:Theme.Holo">
+    <style name="AppBaseTheme" parent="Theme.AppCompat">
         <!-- API 14 theme customizations can go here. -->
     </style>
 
-    <!-- Application theme. -->
-    <style name="AppTheme" parent="android:Theme.Holo">
-        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
-    </style>
-
     <style name="Divider">
         <item name="android:layout_width">match_parent</item>
         <item name="android:layout_height">1dp</item>
index bb8eddc..9a32ea4 100644 (file)
@@ -20,7 +20,7 @@
         Base application theme, dependent on API level. This theme is replaced
         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
     -->
-    <style name="AppBaseTheme" parent="android:Theme.Holo">
+    <style name="AppBaseTheme" parent="Theme.AppCompat">
         <!--
             Theme customizations available in newer API levels can go in
             res/values-vXX/styles.xml, while customizations related to
@@ -29,7 +29,7 @@
     </style>
 
     <!-- Application theme. -->
-    <style name="AppTheme" parent="android:Theme.Holo">
+    <style name="AppTheme" parent="AppBaseTheme">
         <!-- All customizations that are NOT specific to a particular API-level can go here. -->
     </style>