Image will not display in contentview - android

In Xamarin, I have a simple Activity that loads up with a five second wait before another Activity is loaded.
The first Activity has an image as part of the Layout, yet this image is not being displayed.
Here is my Activity code:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.SplashActivity);
Thread.Sleep(5000);
var testActivity = new Intent (this, typeof(TestStartup));
StartActivity (testActivity);
}
Here is my SplashActivity layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/background_color">
<ImageView
android:src="#drawable/Splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/SplashImage"
android:layout_gravity="center_vertical" />
</LinearLayout>
The application waits five seconds, and then the TestStartup Activity loads, however, the image is not displayed during the five second wait. How can I display this image while the Activity waits five seconds?
Can I please have some help with this code?
Thanks in advance

It is not shown because you are blocking the UI Thread with
Thread.Sleep(5000);
You should get rid of this kind of logic. Relying on x secs it is always wrong. The computation you are waiting for could be ready before, making the remaining waiting period a wast, or could require more the x secs. To "fix" your issue you can use Handler.postDelayed(Runnable, 5000), and put the logic you need to be executed inside the run method. This way, at least, the UI Thread will not be blocked

You might want to take a look at Xamarin's tutorial for creating splash screens in Android:
http://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/
It looks like you were on the right track, but they use a theme rather than setting the content view. As previously mentioned, your layout isn't being displayed because your sleep method is blocking the UI Thread. Using a theme is a good way to get around this, and you can reuse a lot of what you already have.

Related

Cardview wont turn invisible after set visible

I really didn't want to post it here because this problem sounds really stupid and is difficult to describe, but after hiting my head 2 hours for a stupid layout problem i decided to try
I've one activity with several layout components...
During on create all components are set to be invisible just one keeps visible.
When user presses the button, all components turn visible
when presses button again, all components SHOULD turn invisible again
ALL COMPONENTS VISIBILITY IS ADJUST IN ONLY ONE METHOD
so the activity looks like:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_giveaway, R.id.mainView);
/*lots of stuff*/
//last thing
makeVisible(View.INVISIBLE);
}
private void makeVisible(int visi) {
findViewById(R.id.cardView).setVisibility(visi);
((ViewGroup) findViewById(R.id.influencerLayout)).setVisibility(visi);
this.recyclerView.setVisibility(visi);
}
the problem: is on second click all components get invisible but one keeps on screen
the component which stays on is a cardview
Mainlayout:
<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="com.tomatedigital.giveawaymaster.activity.NewGiveawayActivity">
//lots of stuff//
<include layout="#layout/giveaway" />
layout/giveaway is:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="#dimen/giveawayCardHeight"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:cardUseCompatPadding="true">
//lots of other stuf
</cardview>
It's the first thing i set visible on controller method but the only which doesn't go back invisible
REPEATING: there is no other calls to setVisibility other than these, all visibitilities are controled just under that method
I didn't post the whole activity code here because is way long
==========UPDATE==========
Just to clarify:
1- the cardview is one separated layout file re-used several places
2- there is only one cardview in the mainlayout
3
if i remove makeVisible(View.INVISIBLE)from onCreate(), all stuff stays visible,
if i call makeVisible(View.INVISIBLE) and never call makeVisible(View.VISIBLE) all stuff stays invisible
but if I invisble->visible->invisible everything goes invisible but cardview keeps visible
When you want to set the whole layout to Invisibility state, you need to do it in your include #layout/giveaway.xml. Becouse it is a view too.
Like we talk in comments...

How to avoid total view-hierarchy re-layout when using EditText?

Background
I have a rather complex layout being shown to the user in an activity.
One of the views is an EditText.
Since I had to make one of the views stay behind the soft-keyboard, yet the rest above it, I had to listen to view-layout changes (written about it here).
The problem
I've noticed that whenever the EditText has focus and shows its caret, the entire view-hierarchy gets re-layout.
You can see it by either looking at the log of the listener I've created, or by enabling "show surface updates" via the developers settings.
This causes bad performance on some devices, especially if the layout of the activity is complex or have fragments that have complex layouts.
The code
I'm not going to show the original code, but there is a simple way to reproduce the issue:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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="com.example.user.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="just some text"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:text="write here"
android:textSize="18dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="just some text 2"/>
</LinearLayout>
</FrameLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(android.R.id.content).getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
Log.d("AppLog", "onPreDraw");
return true;
}
});
}
}
What I've tried
When disabling the caret (using "cursorVisible", which for some reason is called a "cursor" instead) , I can see that the problem doesn't exist.
I've tried to find an alternative to the built-in caret behavior, but I can't find. Only thing I've found is this post, but it seems to make it static and I'm not sure as to how well it performs (performance and compatibility vs normal caret).
I've tried to set the size of the EditText forcefully, so that it won't need to cause invalidation of the layout that contains it. It didn't work.
I've also noticed that on the original app, the logs can (for some reason) continue being written even when the app goes to the background.
I've reported about this issue (including sample and video) here, hoping that Google will show what's wrong or a fix for this.
The question
Is there a way to avoid the re-layout of the entire view hierarchy ? A way that will still let the EditText have the same look&feel of normal EditText?
Maybe a way to customize how the EditText behaves with the caret?
I've noticed that whenever the EditText has focus and shows its caret,
the entire view-hierarchy gets re-layout.
This is not true. Size and position of EditText is constant - there is no re-layouting. You can check it by using code below.
findViewById(android.R.id.content).getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.d("AppLog", "Layout phase");
}
});
Because of blinking caret - EditText constatly calls invalidate(). This forces the GPU to draw EditText again.
On my nexus 5 (marshmallow) I see that only EditText beeing redrawn (Show GPU view updates - enabled).
How about overriding dispatchOnPreDraw() all the vies you use in activity and having flag to check whether that specific view needs to redraw ?
As you need to disable redraw of all other views only when a text view is on focus. So when a text view is on focus have a flag to disable the redrawing of other views.
if dispatchOnPreDraw() method returns false then refreshing of view will be continues else not. I don't know how complex is your layout and how many views are used, but here a separate class should extent a view used and override that method, and also need a mechanism/variable to distinguish the object in current focus.
Hope this method helps!

Android TextView text is showing up as black squares?

I have a TextView in my application that works fine until I pause the application (by pressing home) and launch it again. It should just resume the activity, but for some reason the TextView doesn't display the text properly after it resumes the activity. The text is showing up as black squares, like this:
http://i.stack.imgur.com/5mtvy.png
Does anyone know how to fix this?
EDIT: Here's my code:
The TextView is created in a layout xml that has a GLSurfaceView (called GameView) and on top of that a TextView. Here's the xml code:
<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="MainActivity" >
<GameView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gv"
/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:textColor="#android:color/black"
android:id="#+id/tv" />
</RelativeLayout>
And here is the code in the MainActivity class onCreate method:
setContentView(R.layout.activity_main);
GameView gameView = (GameView) findViewById(R.id.gv);
TextView textView = (TextView) findViewById(R.id.tv);
The onPause, onResume, onStart, onStop methods have nothing related to the TextView.
The TextView is saved into an instance variable for MainActivity and later, TextView.setText() is called to set the text of the TextView.
EDIT: I was able to fix this problem by commenting out one line: GLES20.glDeleteTextures(...) that was being called onPause() I have NO idea how this is even related or why calling this function caused that issue, but when I comment it out the problem goes away and when I bring this line back the problem comes back with it. If anyone can explain how one relates to the other, it would be greatly appreciated.
Check what u have in on start and on stop methods, it is for sure painting what u have typed earlier.
Try putting
#Override
public void onResume() {
super.onResume(); // Always call the superclass method first
TextView textView = (TextView) findViewById(R.id.tv);
textView.forceLayout();
}
Let me know if this solves your problem or not?
I encountered the same situation.
Just make sure your deleting view(which uses opengl) texture action in the thread you created it.
In your case, GameView is the key.

Getting fragment width 0 in onStart()

We are using fragment compatibility library, and are trying to get width of the fragment, added to FrameLayout. According to fragment lifecycle, until onCreateView is executed, getView().getMeasuredWidth() should return 0. However, we are getting 0 in onStart(), which comes after onCreateView, and therefore, the returned value should not be 0.
This is fragment which width we want to know:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/carbon_fiber"/>
Why this anomaly is happening? Any help will be appreciated.
Similar to Activities, the views in the view hierarchy haven't been measured at Fragment::onStart(). To get the size of the view, you need to create an OnGlobalLayoutListener and use it in the following manner:
Android get Fragment width
It's just an assumption, but according to Android Developers:
onStart() makes the fragment visible to the user (based on its containing activity being started).
So width and height will be calculated only after your fragment will become visible.
But, maybe, I'm not right.
I ran into the same problem with an Activity. I needed one of my images to maintain a 4:6 ratio. So as simple as I could find for fragments I put the code in its OnStart() for Activities it was the OnWindowFocusChanged()
From researching this it turns out it's because the layout has not actually "drawn" yet. So therefore no components have been measured. There are various other work around but this seemed to me to be the quickest implementations.
In a similar situation I used the post() method of the View class:
getView().post( new Runnable(){
#Override
public void run(){
//do your measurements here
}
});
I think you need to define the android:class with the class of your fragment.

Android: Increase call stack size

I have an application with very complex UI, containing many layouts nested one in another. While Creating another layout, I've caught a StackOverflowError
Wondering, I've created two test examples:
1) Hello world application with following xml for main Activity
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!-- ...So on 30 times... -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<!-- ...So on 30 times... -->
</FrameLayout>
</FrameLayout>
</FrameLayout>
causes StackOverflowError while drawing the layout (cause every layout recursively draws it's children)
2) The following test case
public class TestOverflowActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overflow(0);
}
private void overflow(int i){
android.util.Log.i("Stack depth:", " = " + i);
overflow(i+1);
}
}
causes StackOverflowError on depth about 260-270 calls.
Every call of stack element for second test case takes 4 bytes for return address + 4 bytes for parameter = 8 bytes. It's possible that Dalvik's VM keeps a bit more info in every element, but even 16 bytes per element * 260 calls = about 4Kbytes for maximum overall stack size. This does not seem enough.
Is there any way to increase maximum stack size?
You might not be able to increase call stack size in the main UI Thread(which is understandable because you're supposed to do as few things as possible here), but you can do it in a separate thread by making use of the Thread object's constructor parameters:
ThreadGroup group = new ThreadGroup("threadGroup");
new Thread(group, runnableObject, "YourThreadName", 2000000).start();
With this example I increased my stack size from 8k (around 260 calls) to 2M (enough for not to get the StackOverFlowException, of course you can add as much as you want as long as the memory can take it), so in the end, for further readers, this is the way you can increase your stack size,although is not recommended, in some cases it's actually necessary for example an algorithm with extensive recursive calls and of course by doing all the hard work in a worker thread(as you're supposed to) with your specified stack size and just "posting" changes in UIs using the main UI Thread with Handlers or whatever mechanism you would like to use to interact with it...
Hope this Helps...
Regards!
You definitely don't want to nest layouts inside each other like that. If you find yourself nesting three or four times then the construction of the view becomes less and less efficient, which can cause activity transitions to start and probably in your case finish before the view is created. Which will look weird, or be a complete waste of an activity transition.
You should make your root layout a relative layout and have all your frame layouts as children of that root relative layout.
If you need this complex layout (as many will doubt) you can still draw it programticaly in onCreate method.

Categories

Resources