I have an app that for some reason, on the latest Android SDK (4.4) launches to a black screen.
I can still hear the activity behind it (for example if I click on somewhere in the black, a button sound is clicked, so Its almost like the screen is loaded behind the black screen).
It works fine on my device which is a Nexus 4 running 4.4, it appears to be an issue on the later version (Nexus 5) devices.
It also shows the banner ad (admob) that I have, so it literally is a case that it seems to be an issue with the background image file.
//#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mGLSurfaceView = new CCGLSurfaceView(this);
CCDirector.sharedDirector().setScreenSize(CCDirector.sharedDirector().winSize().width,
CCDirector.sharedDirector().winSize().height);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
CCDirector.sharedDirector().getActivity().setContentView(mGLSurfaceView, createLayoutParams());
InitParam();
getAdmob();
}
Activity game xml is;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</RelativeLayout>
width & height of the imageview is "wrap content" however the scaletype is "fitXY".
thats kind of saying 2 different things to the device.
if that image should be full screen change it's width & height to match/fill parent.
Check onCreate(), onStart() and onResume() methods in your code, you might be doing some time consuming, blocking tasks. If so, load heavy background tasks on a separate thread.
Profile your code with Traceview and dmtracedump to check what is taking more time.
Related
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);
}
}
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.
I'm development a app with a m3u8 transmission using exoplayer, and some times my transmission is offline, so a need to show a background image with it happenes, and i now find no options to solve this. The only thing with i can find is a shutter_background_color propertieswho define a color of the background player, but this not solve my problem. I need to show a bitmap image.
Please help!!!
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/pl_aovivo"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:resize_mode="fit"
app:use_artwork="true"
app:shutter_background_color="#color/colorPrimary" />
Don't use artwork for this. The best you can do is to add an invisible ImageView inside the layout that host your PlayerView and just turn it visible when onPlayerError occurs.
If you have to set place holder image on to exo player than use below code:
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/video_player"
android:layout_width="match_parent"
android:layout_height="#dimen/_150sdp"
android:layout_centerInParent="true"
android:visibility="visible"
app:default_artwork="#drawable/default_media_artwork"
app:use_artwork="true"
app:resize_mode="fixed_width"
app:show_buffering="when_playing" />
I want show image in ImageView, but don't show it. I use XML code and Java code but don't show. show in simulator but in real devices don't show. tested on LG G2, HTC One X, Samsung Galaxy S3.
my xml code :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:background="#drawable/show_sms_header" />
my java code :
Dialog_Header_Img = (ImageView) findViewById(R.id.sms_dialog_header);
Dialog_Header_Img.setImageResource(R.drawable.show_sms_header);
I used to be separated from each.
Please tell me the solution
First of all you should set image in xml by code:
android:src="#drawable/show_sms_header"
not by android:background, well you can set background to color or other image but your main image you should set by android:src
If you changing something about your image, leave first line in your code that you show, and delete second, if setting image is only thing you set, then you can delete both, because you set source of image in xml.
The "src" attribute should be set to your drawable if you want it to be "centerCrop" correctly.
Check this out :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/show_sms_header" />
Make sure that your file, called "show_sms_header", is in your drawable ( /res/drawable) directory.
I'm actually making an android app which uses a SurfaceView to draw the Camera.
I want to add some views over it.
In fact, i already done it. It works on my android 4.1.1. All my views are drawn over the Camera.
But when i try it on a Android 2.3, it don't works anymore.
Depends of what i'm doing, it shows a part of the view in the left-top corner (As if the parent layout were too small)
Here is the xml file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<com.example.calculatrice.CustomCameraView
android:id="#+id/cameraView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/poiGroup">
<Button
android:id="#+id/bBack"
android:layout_width="150dp"
android:layout_height="80dp">
</Button>
</LinearLayout>
</FrameLayout>
I have a button, wich is perfectly shown over the camera.
I put the views i want to draw under that button (with "layout(x,x,x,x)"). They appear cuted (or don't appear, depend).
Here is some usefull code
//In oncreate method
customCamera = (CustomCameraView)this.findViewById(R.id.cameraView);
//Create view and add it to the layout
public void initObjects()
{
listPoiMarker = new LinkedList<POIMarker>();
int i = 0;
while (i < names.length && names[i] != null)
{
Location locTmp = new Location(LocationManager.NETWORK_PROVIDER);
locTmp.setLatitude(coordonates[0][i]);
locTmp.setLongitude(coordonates[1][i]);
POIMarker tmp = new POIMarker(getApplicationContext());
tmp.setPoiId(id[i]);
tmp.setId(i);
tmp.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tmp.setName(names[i]);
tmp.setLocation(locTmp);
tmp.posScreenY = 100 + (i) * 60;
listPoiMarker.add(tmp);
tmp.setOnTouchListener(onTouchListener);
((LinearLayout) findViewById(R.id.poiGroup)).addView(tmp);
tmp.layout(50, 50, 250, 250);
i++;
}
}
Not exactly what i do. In fact the layout function is somewhere else.
But it's the same.
That works fine in Android 4.1.1, but not in 2.2 or 2.3
I looking for a solution for about 2 days. I tried 999 things, none worked.
That's why i come here to ask some help.
(Here is an example of what it does : http://drawsave.com/19Z
I don't want to add a picture.. Take too long
The red thing is the view i add. It's supposed to be a rectangle. But it's cuted, on 2.2 | 2.3 ..
)
Thanks for help
EDIT:
I found a way to fix it.
How ?
I added a transparent background to the layout containing the views. So it don't resize anymore, it keeps the right size.
(Yep, that's not really logical, but it works)