Skip to content

Commit

Permalink
Merge pull request firebase#1096 from rosariopfernandes/rpf-implement…
Browse files Browse the repository at this point in the history
…-viewbinding

refactor: use viewbinding instead of findViewById() or Synthetic Binding
  • Loading branch information
samtstern authored Apr 22, 2020
2 parents b3414ab + 33ff989 commit 4f98729
Show file tree
Hide file tree
Showing 115 changed files with 1,492 additions and 1,719 deletions.
5 changes: 4 additions & 1 deletion admob/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
check.dependsOn 'assembleDebugAndroidTest'

android {
Expand All @@ -27,6 +26,10 @@ android {
packagingOptions {
exclude 'LICENSE.txt'
}

viewBinding {
enabled = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.samples.quickstart.admobexample.R;
import com.google.samples.quickstart.admobexample.databinding.ActivityMainBinding;

// [SNIPPET load_banner_ad]
// Load an ad into the AdView.
Expand All @@ -43,12 +44,19 @@ public class MainActivity extends AppCompatActivity {
// [START_EXCLUDE]
private InterstitialAd mInterstitialAd;
private Button mLoadInterstitialButton;
private ActivityMainBinding binding;
// [END_EXCLUDE]

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// [START_EXCLUDE]
binding = ActivityMainBinding.inflate(getLayoutInflater());
mAdView = binding.adView;
mLoadInterstitialButton = binding.loadInterstitialButton;
View layout = binding.getRoot();
// [END_EXCLUDE]
setContentView(layout);
checkIds();

// Initialize the Google Mobile Ads SDK
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
package com.google.samples.quickstart.admobexample.kotlin


import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.InterstitialAd
import com.google.android.gms.ads.MobileAds
import com.google.samples.quickstart.admobexample.R
import kotlinx.android.synthetic.main.activity_main.adView
import kotlinx.android.synthetic.main.activity_main.loadInterstitialButton
import com.google.samples.quickstart.admobexample.databinding.ActivityMainBinding

// [SNIPPET load_banner_ad]
// Load an ad into the AdView.
// [START load_banner_ad]
class MainActivity : AppCompatActivity() {

// [START_EXCLUDE]
private lateinit var binding: ActivityMainBinding
private lateinit var interstitialAd: InterstitialAd
private lateinit var adView: AdView
private lateinit var loadInterstitialButton: Button
// [END_EXCLUDE]

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// [START_EXCLUDE]
binding = ActivityMainBinding.inflate(layoutInflater)
adView = binding.adView
loadInterstitialButton = binding.loadInterstitialButton
val layout = binding.root
// [END_EXCLUDE]
setContentView(layout)
checkIds()

// Initialize the Google Mobile Ads SDK
Expand Down Expand Up @@ -130,6 +139,6 @@ class MainActivity : AppCompatActivity() {

companion object {
private const val TAG = "MainActivity"
private const val TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713";
private const val TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713"
}
}
1 change: 1 addition & 0 deletions admob/app/src/main/res/layout/second_activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:viewBindingIgnore="true"
tools:context="com.google.samples.quickstart.admobexample.java.SecondActivity">

<ImageView
Expand Down
5 changes: 4 additions & 1 deletion analytics/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
check.dependsOn 'assembleDebugAndroidTest'

android {
Expand All @@ -21,6 +20,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

viewBinding {
enabled = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.quickstart.analytics.R;
import com.google.firebase.quickstart.analytics.databinding.ActivityMainBinding;

import java.util.Locale;

Expand All @@ -58,6 +59,8 @@ public class MainActivity extends AppCompatActivity {
new ImageInfo(R.drawable.whitebalance, R.string.pattern4_title, R.string.pattern4_id),
};

private ActivityMainBinding binding;

/**
* The {@link androidx.viewpager.widget.PagerAdapter} that will provide fragments for each image.
* This uses a {@link FragmentPagerAdapter}, which keeps every loaded fragment in memory.
Expand All @@ -84,7 +87,8 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

// [START shared_app_measurement]
// Obtain the FirebaseAnalytics instance.
Expand All @@ -105,12 +109,12 @@ protected void onCreate(Bundle savedInstanceState) {
mImagePagerAdapter = new ImagePagerAdapter(getSupportFragmentManager(), IMAGE_INFOS);

// Set up the ViewPager with the pattern adapter.
mViewPager = findViewById(R.id.viewPager);
mViewPager = binding.viewPager;
mViewPager.setAdapter(mImagePagerAdapter);

// Workaround for AppCompat issue not showing ViewPager titles
ViewPager.LayoutParams params = (ViewPager.LayoutParams)
findViewById(R.id.pagerTabStrip).getLayoutParams();
binding.pagerTabStrip.getLayoutParams();
params.isDecor = true;

// When the visible image changes, send a screen view hit.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import androidx.preference.PreferenceManager
import androidx.viewpager.widget.ViewPager
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.quickstart.analytics.R
import kotlinx.android.synthetic.main.activity_main.pagerTabStrip
import kotlinx.android.synthetic.main.activity_main.viewPager
import com.google.firebase.quickstart.analytics.databinding.ActivityMainBinding
import java.util.Locale

/**
Expand All @@ -36,6 +35,8 @@ class MainActivity : AppCompatActivity() {
)
}

private lateinit var binding: ActivityMainBinding

/**
* The [androidx.viewpager.widget.PagerAdapter] that will provide fragments for each image.
* This uses a [FragmentPagerAdapter], which keeps every loaded fragment in memory.
Expand All @@ -51,7 +52,8 @@ class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

// [START shared_app_measurement]
// Obtain the FirebaseAnalytics instance.
Expand All @@ -71,14 +73,14 @@ class MainActivity : AppCompatActivity() {
imagePagerAdapter = ImagePagerAdapter(supportFragmentManager, IMAGE_INFOS)

// Set up the ViewPager with the pattern adapter.
viewPager.adapter = imagePagerAdapter
binding.viewPager.adapter = imagePagerAdapter

// Workaround for AppCompat issue not showing ViewPager titles
val params = pagerTabStrip.layoutParams as ViewPager.LayoutParams
val params = binding.pagerTabStrip.layoutParams as ViewPager.LayoutParams
params.isDecor = true

// When the visible image changes, send a screen view hit.
viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
binding.viewPager.addOnPageChangeListener(object : ViewPager.SimpleOnPageChangeListener() {
override fun onPageSelected(position: Int) {
recordImageView()
recordScreenView()
Expand Down Expand Up @@ -169,7 +171,7 @@ class MainActivity : AppCompatActivity() {
* @return title of image
*/
private fun getCurrentImageTitle(): String {
val position = viewPager.currentItem
val position = binding.viewPager.currentItem
val info = IMAGE_INFOS[position]
return getString(info.title)
}
Expand All @@ -180,7 +182,7 @@ class MainActivity : AppCompatActivity() {
* @return id of image
*/
private fun getCurrentImageId(): String {
val position = viewPager.currentItem
val position = binding.viewPager.currentItem
val info = IMAGE_INFOS[position]
return getString(info.id)
}
Expand Down
1 change: 1 addition & 0 deletions analytics/app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ specific language governing permissions and limitations under the License.
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:viewBindingIgnore="true"
tools:context="com.google.firebase.quickstart.analytics.java.ImageFragment">

<ImageView
Expand Down
4 changes: 3 additions & 1 deletion app-indexing/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
apply plugin: 'com.android.application'
check.dependsOn 'assembleDebugAndroidTest'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
Expand All @@ -21,6 +20,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
viewBinding {
enabled = true
}
}

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
Expand All @@ -37,6 +36,7 @@
import com.google.firebase.appindexing.builders.Actions;
// [END import_classes]
import com.google.samples.quickstart.appindexing.R;
import com.google.samples.quickstart.appindexing.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

Expand All @@ -50,19 +50,18 @@ public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
// [START_EXCLUDE]
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

final FirebaseAppIndex firebaseAppIndex = FirebaseAppIndex.getInstance();

Button addStickersBtn = findViewById(R.id.addStickersBtn);
addStickersBtn.setOnClickListener(new View.OnClickListener() {
binding.addStickersBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, AppIndexingService.class));
}
});
Button clearStickersBtn = findViewById(R.id.clearStickersBtn);
clearStickersBtn.setOnClickListener(new View.OnClickListener() {
binding.clearStickersBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppIndexingUtil.clearStickers(MainActivity.this, firebaseAppIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,38 @@ import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
// [START import_classes]
import com.google.firebase.appindexing.FirebaseAppIndex
import com.google.firebase.appindexing.FirebaseUserActions
import com.google.firebase.appindexing.Indexable
import com.google.firebase.appindexing.builders.Actions
// [END import_classes]
import com.google.samples.quickstart.appindexing.R
import kotlinx.android.synthetic.main.activity_main.addStickersBtn
import kotlinx.android.synthetic.main.activity_main.clearStickersBtn
import kotlinx.android.synthetic.main.activity_main.link
import com.google.samples.quickstart.appindexing.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

private var articleId: String? = null
private lateinit var binding: ActivityMainBinding
private lateinit var linkText: TextView

// [START handle_intent]
override fun onCreate(savedInstanceState: Bundle?) {
// [START_EXCLUDE]
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

val firebaseAppIndex = FirebaseAppIndex.getInstance()

addStickersBtn.setOnClickListener { startService(Intent(baseContext, AppIndexingService::class.java)) }
with(binding) {
addStickersBtn.setOnClickListener { startService(Intent(baseContext, AppIndexingService::class.java)) }

clearStickersBtn.setOnClickListener { AppIndexingUtil.clearStickers(baseContext, firebaseAppIndex) }
}
linkText = binding.link

clearStickersBtn.setOnClickListener { AppIndexingUtil.clearStickers(baseContext, firebaseAppIndex) }
// [END_EXCLUDE]
onNewIntent(intent)
}
Expand All @@ -41,7 +46,7 @@ class MainActivity : AppCompatActivity() {
val data = intent.data
if (Intent.ACTION_VIEW == action && data != null) {
articleId = data.lastPathSegment
link.text = data.toString()
linkText.text = data.toString()
}
}
// [END handle_intent]
Expand Down
5 changes: 4 additions & 1 deletion auth/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
check.dependsOn 'assembleDebugAndroidTest'

android {
Expand Down Expand Up @@ -28,6 +27,10 @@ android {
sourceCompatibility 1.8
targetCompatibility 1.8
}

viewBinding {
enabled = true
}
}

dependencies {
Expand Down
Loading

0 comments on commit 4f98729

Please sign in to comment.