I'm using the code below to display AdMob Ads, but unfortunately it's not showing anything. Please let me know if I have missed something
#Composable
fun AdvertView(modifier: Modifier = Modifier) {
AndroidView(
modifier = modifier.fillMaxWidth(),
factory = { context ->
AdView(context).apply {
adSize = AdSize.BANNER
adUnitId = "ca-app-pub-3940256099942544/6300978111"
loadAd(AdRequest.Builder().build())
}
},
update = {
it.apply {
loadAd(AdRequest.Builder().build())
}
}
)
}
I got a similar problelm.. What worked for me was making sure I had internet connection and waiting a bit for the banner ad to load when I run my code..
Also Since Admob 21.0.0, you would get the following error:
Val cannot be reassigned
So, instead of
adSize = AdSize.BANNER
use
setAdSize(AdSize.BANNER)
AdMob in Jetpack Compose (Banner and Interstitial) enter link description here
Related
I'm working on a project that use Firebase Storage and Firestore. I added function that allows to upload images from phone gallery to Storage and storage url of this image is saved in Firestore with some other informations. Everything works fine, even the image url in firestore displays selected image correctly in browser. The problem is when i try to set this image to ImageView using Glide or Picasso, because it only shows a blank image/placeholder. This is how my repository functions looks to add and download data:
override fun addProject(uri: Uri, project: Project) {
val imagesRef = storage.reference.child("images/${uri.lastPathSegment}")
Log.d("firebase ref url", imagesRef.toString())
imagesRef.putFile(uri).addOnCompleteListener {
Log.d("firebase image", "Succesfully uploaded")
imagesRef.downloadUrl.addOnSuccessListener { uri->
project.photoUrl = uri.toString()
db.collection("projects").add(project)
}
}.addOnFailureListener {
Log.d("firebase image", "Unsuccessfully uploaded")
}
}
override suspend fun getProjects(): List<Project> {
val projectList = mutableListOf<Project>()
val projectRef = db.collection("projects")
val snapshot = projectRef.get().await()
val docs = snapshot.documents
docs.forEach {
val project = it.toObject(Project::class.java)
if (project != null) {
projectList.add(project)
Log.d("firebase listen", projectList.toString())
}
}
return projectList
}
firebase storage
firebase firestore
After i retrieve all projects with their photoUrl and other informations i want to display them in recycler view using ListAdapter. Title and description are displayed correctly in every recycler view item but it seems like this url can't be accessed by glide/ picasso . There is also a log line which shows photoUrl and if I click on it from logcat the browser displays correct image.
fun bind(project: Project) {
binding.apply {
Log.d("recycler view url", project.photoUrl)
// Picasso.get().load(project.photoUrl).into(projectIv)
Glide.with(binding.root).load(project.photoUrl).into(projectIv)
titleTv.text = project.name
descriptionTv.text = project.description
}
binding.executePendingBindings()
}
I have found some related topics but still haven't found working solution to that problem.
EDIT
Log result inside recyclerview bind method:
Log resylt
After clicking on link:
Link result
How it looks in my app:
app screenshot 1
app screenshot 2
It looks like it only takes the photo size parameters but can't resolve the photo content, but i'm not sure
Please try to tolerate my inexperience.
I'm practicing Jetpack Compose navigation. I making an app that displays a set of card in a LazyHorizontalGrid format, and when clicked it navigates to a screen (destination) containing details of the card, depending on which card is clicked.
Here's what I mean:
I want to navigate to a screen (destination) that describe a particular "Favorite Collections" when clicked, depending on which is clicked.
I was following a tutorial on youtube, but I got lost when he was using Companion Object on a new activity (i.e having multiple activity in a single project and navigating by intent), since that's not recommended I'm not doing that, so I don't know where to put my Companion Object.
RecyclerView in Jetpack Compose - Jetpack Compose For Beginners #7
As I get errors when I try to put the Companion Object in the file of the Screen I'm trying to navigate to, error; Modifier 'companion' is not applicable inside 'file', or In a Composable in the file, then I get this error; Unresolved reference: companion.
This is the screen I'm trying to navigate to (let's call it; DetailScreen);
#Composable
fun DisplayHomeInfo(){
WelcomeText()
HomeInfoDetails(homeInfo = )
}
#Composable
fun WelcomeText() {
Text(
text = "Welcome, to Home Information",
style = MaterialTheme.typography.h3,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 18.dp)
)
}
#Composable
fun HomeInfoDetails(homeInfo: HomeInfo) {
Card(
modifier = Modifier
.padding(10.dp)
.clip(CircleShape),
elevation = 10.dp,
) {
....
This is the screen I'm trying to navigate from;
#Composable
fun HomeScreen(onHomeCardClick: () -> Unit) {
HomeContentScreen(onHomeCardClick = onHomeCardClick)
}
....
Data class I want to load on the detail screen;
data class HomeInfo(
val id: Int,
val title: String,
val sex: String,
val age: Int,
val description: String,
val homeInfoImageId: Int = 0
)
And this is the Object I want to match with the data;
object HomeInfoModel {
val homeInfoModelList = listOf(
HomeInfo(
id = 1,
title = "Monty",
sex = "Male",
age = 14,
description = "Monty enjoys chicken treats and cuddling while watching Seinfeld.",
homeInfoImageId = R.drawable.ab1_inversions
),
...
)
}
To summarize everything, MY QUESTION is how do I pass the data to the detailScreen, and show each card details, depending on the card clicked.
Please I understand my explanation is not very clear, and I'll be more than happy to clarify anything.
No information is too small, I will appreciate any help. Thanks a lot in advance.
I have a project where we display a graph, this graph is in an Box which is scrollable.
By opening the view, we need to center the root node which causes the problem currently.
Determining the position and setting the values of the states is currently done the following way:
.onGloballyPositioned { coordinates -> scrollBy = coordinates.positionInParent().y - dpstate!!.scrollState.firstVisibleItemScrollOffset
}
.onFocusChanged {
if(it.isFocused ){
print("is focused")
scope.launch { dpstate!!.scrollState.animateScrollBy(scrollBy)
dpstate!!.offsetState.value = Offset(leftX.toFloat(),dpstate!!.offsetState.value.y)
}
}
}
in the modifier of the box.
The state dpstate is an instance of the following:
data class DisplayState(
val scrollState: LazyListState,
val scaleState: MutableState<Float>,
val offsetState: MutableState<Offset>,
val editState: MutableState<Boolean>,
val showInfo:Map<Int, MutableState<Boolean>>,)
Important is, that I need to center by opening it, not by clicking a button.
My try was in the calling code of all of this the following code:
DisposableEffect(Unit){
com.github.tukcps.appel.ui.rendering.focusRequester!!.requestFocus()
onDispose { }
}
There is no exception, it just don't do anything, thanks for your help.
I am learning Android programing, and I can not figure it out how to open settings fragment when button is clicked.
This fragment is not in the navigation map, so it is not possible to connect them in navigation and to use findNavController().navigate(actionFromTo)
As explained on developer guide: https://developer.android.com/guide/topics/ui/settings
I have created fragment:
class PreferencesFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}
What I need to write in click listener to enter settings fragment?
Click listener is:
binding.buttonSettings.setOnClickListener {
}
I have tried with to use code in developers guide:
binding.buttonSettings.setOnClickListener {
val fragmentManager: FragmentManager? = activity?.supportFragmentManager
fragmentManager?.beginTransaction()?.replace(R.id.preferencesFragment, PreferencesFragment())?.commit()
}
But program crashes when button is pressed, with error:
No view found for...
The issue seems to be the content ID you want to replace, therefore you need to pass the current content ID you want to replace
see below code, use android.R.id.content instead of R.id.preferencesFragment
binding.buttonSettings.setOnClickListener {
val fragmentManager: FragmentManager? = activity?.supportFragmentManager
fragmentManager?.beginTransaction()?.replace(android.R.id.content, PreferencesFragment())?.commit()
}
So I'm making an app which needs to paint garland, light bulbs and other decoration. I have a code which will make an imageview on Action_Move but the app crashes. see the code below
fun drawLights(){
val listener = View.OnTouchListener(function = { view, motionEvent ->
val x = motionEvent.getX()
val y = motionEvent.getY()
when (motionEvent.action){
MotionEvent.ACTION_DOWN -> {
Toast.makeText(this,"Action Down",Toast.LENGTH_SHORT).show()
}
MotionEvent.ACTION_MOVE -> {
Toast.makeText(this, "Moving", Toast.LENGTH_SHORT).show()
////Imageview Creation Here using late init var
}
MotionEvent.ACTION_UP -> {
Toast.makeText(this,"Done" ,Toast.LENGTH_SHORT).show()
}
}
true
})
edit_Canvas.setOnTouchListener(listener)
}
Does anyone here know any blog related to this or already resolved this problem? Thanks!
You need to look for topics on drawing on the Android Canvas. There are lots of sample codes out there
This one's from the official documentation in drawing on the Canvas https://developer.android.com/training/custom-views/custom-drawing