Admob(Ads) in Custom Alert Dialog - java

In the market I saw an app is displaying admob in the alert dialog . after I reading this and this and this ,
I still can't figure out how to display the ads in alert dialog same like the one below. (or any others method , that can display the ads in a alert dialog)
I've been scratching my head trying to figure out how
to do that.
Could anyone be kind enough to guide me through how
to go about this?
XML :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#id/tablayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.xxx.xxx">
<TableRow>
<com.admob.android.ads.AdView android:id="#id/myad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#ff000000"
myapp:primaryTextColor="#ffffffff"
myapp:secondaryTextColor="#ffcccccc" />
</TableRow>
</TableLayout>
Java :
public void onBackPressed() {
//set up dialog
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.exitdialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
});
}
Thanks.

I found another solution to do this.
AdMob offers a JavaScript integration solution for Android and iPhone sites/web apps.
Instead of using XML and a custom dialog.
We can use Admob web ads and a HTML that contain the admob code and load it with webview in the dialog.

I believe it's not possible unless you're on landscape mode. The reason is that the ad needs something close to the device width (in portrait mode, e.g. 480) for the ad, so it is only available in landscape mode when you're adding it inside a dialog

Related

AdMob ad doesn't show until I reopen the app

I'm trying to put an AdMob banner into my app but half the time it doesn't show any content.
The AdListener even receives an call to onAdLoaded and I never get any error.
I noticed that if the ad isn't showing and I put the app in the background somehow the ad starts to show. For example if I:
close and reopen the app
start an fullscreen interstitial ad
start the in-app purchase overlay
All these actions trigger an update that somehow redraws/remeasures the views.
I tried many different things to simulate this in onAdLoaded.
Is it possible that AdMob can't find a matching ad even if I don't get any errors? According to the website the match rate is 98%. The number of impressions is about a third of the requests.
I found more posts with similar issues (all from ~7 years ago) but none of their answers worked in my case:
Admob not showing up until phone is locked
AdMob won't show the banner until refresh or sign in to google plus
Android AdMob: addView doesn't show ad until return to activity
Admob ads appear only after first refresh
I tried to get this to work for over two days. Please take a look at my code, I annotated it with explainations:
MainActivity:
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
if(privacyPolicyAcceptedAndHasNoPremium()){ //at first i check the shared preferences
binding.adPlaceholder.setVisibility(View.VISIBLE); //the adPlaceholder starts as GONE when i set it to VISIBLE the rest of the content slides up
List<String> testDevices = Arrays.asList(AdRequest.DEVICE_ID_EMULATOR, "...");
RequestConfiguration requestConfiguration = new RequestConfiguration.Builder().setTestDeviceIds(testDevices).build();
MobileAds.setRequestConfiguration(requestConfiguration);
MobileAds.initialize(MainActivity.this, initializationStatus -> {});
adView = new AdView(this); //i create the adView programmatically but using the xml view element causes the same problem
adView.setAdSize(AdSize.LARGE_BANNER);
adView.setAdUnitId("...");
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() { // this gets called so i think the ad is successfully downloaded from the google server. i put all kinds of code in here to force the adView to show
//setContentView(binding.getRoot());
//binding.adPlaceholder.addView(new View(MainActivity.this));
//binding.adPlaceholder.forceLayout();
//binding.constraintLayout.requestLayout();
//adView.invalidate();
//adView.bringToFront();
//adView.setVisibility(AdView.GONE);
//adView.setVisibility(AdView.VISIBLE);
//adView.setBackgroundColor(Color.TRANSPARENT);
}
}
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
binding.adPlaceholder.addView(adView); //i add the adView into the empty LinearLayout. i tried to add it directly to binding.constraintLayout too.
adView.loadAd(adRequestBuilder.build());
}
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<!-- this is the empty container into which i load the ads. it sits at the bottom of the screen and is not visible before an ad is showing -->
<LinearLayout
android:id="#+id/adPlaceholder"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:orientation="vertical"
android:visibility="gone" />
<!-- this MotionLayout contains the rest of the app (except the toolbar). it slides up when an ad is showing -->
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="#+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layoutDescription="#xml/activity_main_scene"
app:layout_constraintBottom_toTopOf="#+id/adPlaceholder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MainActivity">
...
</androidx.constraintlayout.motion.widget.MotionLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application ...>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="..." />
...
</application>
Edit:
I finally found a way that works for me.
I'm just setting the window background color to the color that it already has. This is really fast and doesn't recreate the activity.
However I don't know why this works and why only this worked for me. I guess it updates the views in a similar way to the many other solutions from the other questions.
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
getWindow().setBackgroundDrawableResource(R.color.background);
}
}

How to show progressbar with blurry background and disabled UI

so I am making an app which has a lot of connections to the database, so there is a "waiting" time everywhere.
I want to put a progress bar everywhere where is a connection to the database. It should look like this:
-The progress bar is shown after clicking the Login button with the blurry background.
In short - Show progress bar, blur the background, deactivate UI controls while progressbar is activated.
I'll try to show you the pseudo code here:
loginBtn.setOnClickListener {
progressBar.visibility = View.VISIBLE
BlurTheBackground()
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
DoTheLoginStuff()
}
And after Login I want to disable progress bar and reactivate fully UI.
PS: After Login the activity changes to another,but after hitting back button on the smartphone it comes back without refresh
You can try https://android-arsenal.com/details/1/4409 this library. I think it can help You.
<RelativeLayout
android:id="#+id/progressBar_blurLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.armboldmind.natalipharm.view.customViews.RealtimeBlurView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:realtimeBlurRadius="15dp"
app:realtimeOverlayColor="#99FFFFFF" />
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
Set it on top of your layout, and on login button click change visibility of progress bar layout.

How to achieve the following Image + Text in button in android app?

I am trying to make a clickable phone number button with an icon in my app. I was checking for some references and found google maps implementation good. How can I achieve this in my app?
I have tried the Image Button view but that does not solve the problem. I have put 'onClick' attribute for text & image views, but the button animation isn't there and both text & image icon does not look together.
Please guide me as to what view/s we have to use to achieve the result as in the image and how to get that animation on click of the button. Or is there any better way to achieve this?
I am aware of intents, so that part is clear.
If you can let me know how to make that phone number copied to the clipboard automatically on hold of that button, that would be really great.
Try this code
Change spacing dimens according to your use and also change icon.
<LinearLayout
android:id="#+id/callButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/spacing_small"
android:padding="#dimen/spacing_small"
android:clickable="false"
app:srcCompat="#drawable/ic_download"
android:tint="#color/black" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/amaranth"
android:gravity="center_vertical"
android:paddingLeft="#dimen/spacing_xxhuge"
android:paddingTop="#dimen/spacing_medium"
android:paddingBottom="#dimen/spacing_medium"
android:text="000 0000 000"
android:textColor="#color/grey_70"
android:textSize="#dimen/textsize_large" />
</LinearLayout>
and set on click listener on callButton. use below code in java code.
And also i have added a code to copy phone number directly on click event. You have to save text in clipboard.
LinearLayout callButton = findViewById("callButton");
callButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
// You have to get text from phoneNumber textview. and set it to clipboard.
clipboard.setPrimaryClip(clip);
}
});
Some questions:
Do you have any experience with Android development?
If so, do you have anything up and running?
I'm gonna assume you do have experience but you're asking before you start coding anything. There are many ways to implement this, the way that would be easiest would be to have a custom listview (here's a simple and easy tutorial for that) and use an item in the listview to display a phone number. Each listview item has a setOnItemLongClickListener which you can use and inside it use the ClipboardManager to copy or use an intent to the phone calling service.
list.setOnItemLongClickListener(new OnItemLongClickListener() { //list is my listView
#Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int pos, long id) {
//Whatever you wanna do
ClipboardManager clipboard = (ClipboardManager)
getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
}
});
I believe this is what you want to achieve
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Your other layouts-->
<TextView
android:background="?selectableItemBackground"
android:focusable="true"
android:clickable="true"
android:padding="#dimen/activity_vertical_margin"
android:drawableStart="#drawable/ic_phone"
android:drawablePadding="16dp"
android:text="The mobile number here"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
android:background="?selectableItemBackground"
This will add the default system animation of ripple(or anything) on click.
Also android:focusable=" and android:clickable="true" is necessary for this to work.
If you want to customize the click events, you better be using selectors in the background of your view.
For the 'Copy to Clipboard' feature you can refer to the other answers.
Happy Coding!

how to display this notification in Android?

Im looking for this library to display this kind of notification that i could display when there is a new update on my list.
Here is the sample like in Facebook or LinkedIn.
You can use this library popup bubble for the above requirement
In your xml
<com.webianks.library.PopupBubble
android:layout_margin="16dp"
android:id="#+id/popup_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
/>
In your activity
PopupBubble popupBubble = (PopupBubble) findViewById(R.id.popup_bubble);
popupBubble.setPopupBubbleListener(new PopupBubble.PopupBubbleClickListener() {
#Override
public void bubbleClicked(Context context) {
//popup_bubble is clicked
}
});
you can also attach this to your recycler view
popupBubble.setRecyclerView(recyclerView);
set an onScrollListener to your RecyclerView. On every scroll call your API which fetches the data.
Use a TextView with Gone/Visible fadein/fadeout animation if new data is added. This can be simply done from the adapter.

Dialog changes the orientation by itself. How to fix the orientation of a dialog to landscape

I am showing a custom dialog in an activity of which the orientation is set to landscape. But the orientation is getting changed to portrait mode from the inital landscape(only the dialog, below activity remains in landscape mode). This is happening without any outside action like tilting the tablet or something.
I could not find any help in fixing the orientation of dialog anywhere.
Any ideas?
Dialog mDialog = new Dialog(MainActivity.this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
mDialog.setContentView(R.layout.alert);
mDialog.show();
This is the code used for showing the dialog.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dip"
android:layout_height="300dip"
android:background="#drawable/alert_bg"
android:orientation="vertical" >
------------------------------
---------------------------
</LinearLayout>
This is the xml element parent tag used.
If you are using an Activity as a dialog then you can set
android:screenOrientation="landscape"
in your manifest file.
If you are using custom dialog then you can use
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
before you use setContentView(); method.

Categories