Skip to content

Commit

Permalink
Update 11-08-2024
Browse files Browse the repository at this point in the history
  • Loading branch information
samyak2403 committed Aug 11, 2024
1 parent 4cb875b commit d8eb37d
Show file tree
Hide file tree
Showing 24 changed files with 413 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .idea/gradle.xml

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

1 change: 1 addition & 0 deletions OpenAds/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
42 changes: 42 additions & 0 deletions OpenAds/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.samyak2403.banner'
compileSdk 34

defaultConfig {
minSdk 21

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

implementation 'com.google.android.gms:play-services-ads:21.2.0'
}
Empty file added OpenAds/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions OpenAds/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.samyak2403.banner

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.samyak2403.banner.test", appContext.packageName)
}
}
4 changes: 4 additions & 0 deletions OpenAds/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

</manifest>
17 changes: 17 additions & 0 deletions OpenAds/src/test/java/com/samyak2403/banner/ExampleUnitTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.samyak2403.banner

import org.junit.Test

import org.junit.Assert.*

/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

implementation project(path: ':nativetemplates')
implementation project(path: ':bannerads')
//cardview
implementation 'androidx.cardview:cardview:1.0.0'
//ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.samyak2403.nativerecyclerview
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.com.samyak2403.nativerecyclerview.R
import com.com.samyak2403.nativerecyclerview.databinding.ActivityMainBinding
import com.google.rvadapter.AdmobNativeAdAdapter
import com.samyak2403.bannerads.Control
import com.samyak2403.nativerecyclerview.adapter.MyAdapter
import com.samyak2403.nativerecyclerview.model.UserModel

Expand All @@ -30,6 +32,12 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)



val control = Control(this)
val adUnitId = getString(R.string.admob_banner_id) // Fetch the AdMob banner ID from resources
control.loadBannerAd(R.id.bannerAdContainer, adUnitId)

binding.apply {
recyclerView.layoutManager = LinearLayoutManager(this@MainActivity)
val admobNativeAdAdapter: AdmobNativeAdAdapter = AdmobNativeAdAdapter.Builder
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
tools:context="com.samyak2403.nativerecyclerview.MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


<!-- LinearLayout to contain the Banner Ad -->
<LinearLayout
android:id="@+id/bannerAdContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="@+id/recyclerView" />

</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<resources>
<string name="app_name">Native Recyclerview</string>

<string name="admob_banner_id">ca-app-pub-3940256099942544/9214589741</string> <!-- Replace with your actual AdMob Banner ID -->
</resources>
1 change: 1 addition & 0 deletions bannerads/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
42 changes: 42 additions & 0 deletions bannerads/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}

android {
namespace 'com.samyak2403.bannerads'
compileSdk 34

defaultConfig {
minSdk 21

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}

dependencies {

implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.android.material:material:1.12.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

implementation 'com.google.android.gms:play-services-ads:21.2.0'
}
Empty file added bannerads/consumer-rules.pro
Empty file.
21 changes: 21 additions & 0 deletions bannerads/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.samyak2403.bannerads

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.samyak2403.bannerads.test", appContext.packageName)
}
}
5 changes: 5 additions & 0 deletions bannerads/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />
</manifest>
76 changes: 76 additions & 0 deletions bannerads/src/main/java/com/samyak2403/bannerads/Control.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Created by Samyak kamble on 8/11/24, 8:29 PM
* Copyright (c) 2024 . All rights reserved.
* Last modified 8/11/24, 8:29 PM
*/

package com.samyak2403.bannerads



import android.app.Activity
import android.util.DisplayMetrics
import android.widget.LinearLayout
import android.widget.Toast
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.LoadAdError

class Control(private val activity: Activity) {

fun loadBannerAd(linearLayoutId: Int, adUnitId: String) {
val linearLayout: LinearLayout? = activity.findViewById(linearLayoutId)
if (linearLayout != null) {
val adView = AdView(activity)
adView.adUnitId = adUnitId

val adSize = getAdSize()
adView.setAdSize(adSize)

linearLayout.addView(adView)
adView.adListener = object : AdListener() {
override fun onAdClicked() {
super.onAdClicked()
}

override fun onAdClosed() {
super.onAdClosed()
}

override fun onAdFailedToLoad(loadAdError: LoadAdError) {
super.onAdFailedToLoad(loadAdError)
// Toast.makeText(activity, "Ad Failed", Toast.LENGTH_SHORT).show()
}

override fun onAdImpression() {
super.onAdImpression()
}

override fun onAdLoaded() {
super.onAdLoaded()
}

override fun onAdOpened() {
super.onAdOpened()
}
}
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
} else {
Toast.makeText(activity, "LinearLayout not found", Toast.LENGTH_SHORT).show()
}
}

private fun getAdSize(): AdSize {
val outMetrics = DisplayMetrics()
activity.windowManager.defaultDisplay.getMetrics(outMetrics)
val density = outMetrics.density

val adWidthPixels = outMetrics.widthPixels.toFloat()
val adWidth = (adWidthPixels / density).toInt()

return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, adWidth)
}
}
Loading

0 comments on commit d8eb37d

Please sign in to comment.