waiting for setContentView() method - android

I have problem with getting information about views after calling setContentView(). I'm changing layout of activity after click on button. Here is the example of my code.
public void onClickButton1(View v) {
setContentView(R.layout.activity_main);
setScreen();
}
But durring execution setScreen() method, layout still is not rendered, so I cannot call getWidth() on some view intended in layout (respectively I can but I always get 0). How I can wait until setContentView() is finished? Please notice that using of this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
setScreen();
}
is not sufficient for me. I need to change layout independently on activity cycle.

You need to attach a ViewTreeObserver to your layout:
setContentView(R.layout.yourLayoutName);
View yourLayout = findViewById(R.id.ID_OF_YOUR_LAYOUT);
ViewTreeObserver vto = yourLayout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
//Do your work
vto.removeOnGlobalLayoutListener(this);
}
});

There's no easy way to use setContentView() to get the result you want. You either need to entirely remove all views and then inflate the new layout, use a ViewFlipper to switch between different views, or, which is IMHO the best approach, rework your app to use Fragments

Related

When is layout not null when creating view Android

I'm making my own custom view and having trouble accessing the layout object immediately after initializing it.
I understand the documentation says layout can be null. But is there a certain event which I can override which will tell me when it's available? I've seen answers on SO which recommend adding a ViewTreeObserver (here)
It seems weird to me that I would need a separate class to know when the layout is available. Is there another way?
From what I understood, there exist no such method which you can override to know when the layout is available. If you find ViewTreeObserver difficult, you can try posting a runnable in the UI Thread i.e.,
view.post(new Runnable() {
#Override
public void run() {
int height = view.getHeight();
int width = view.getWidth();
}
});
Basically, the initialization of the layouts occurs in onCreate() so it would be better if you try the below code in onResume() whether the custom layout is available or not.
if the custom layout is custom_layout.xml then try the below code
LayoutInflater inflater=getLayoutInflater();
View view=inflater.inflate(R.layout.custom_layout,null);
if(view!=null)
{
//Place your code over here
}

getHeight in a fragment

im trying to measure a ImageView, after my fragment is displayed, or if possible before.
I heared, that its not yet possible in the onActivityCreated. But somehow it works with the global Layout listener. -But how? -I have a method, wich measures and does some code, i just don't know when to call the method.
Can somebody gice an example?
the beginning of the measure method:
public void skalierung() {
InputStream dots=getResources().openRawResource(R.drawable.amountofdots);
Bitmap dotsBmp = BitmapFactory.decodeStream(dots);
View mainframe=(View)getActivity().findViewById(R.id.mainframe);
int breite=mainframe.getWidth();
Thanks!
To set a GlobalLayoutListener you have to get ViewTreeObserverof your View using the view.getViewTreeObserver() method which :
Returns the ViewTreeObserver for this view's hierarchy. The view tree observer can be used to get notifications when global events, like layout, happen.
After doing that well , you can addOnGlobalLayoutListener on your ViewTreeObserever
OnGlobalLayoutListener : Interface definition for a callback to be invoked when the global layout state or the visibility of views within the view tree changes.
and inside the onGlobalLayoutmethod you can call the getWidth on the your desired view, here's an example :
View mainframe=(View)getActivity().findViewById(R.id.mainframe);
ViewTreeObserver vto = mainframe.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
skalierung(); // here you can call the getWidth and getHeight methods
ViewTreeObserver obs = mainframe.getViewTreeObserver();
// you have to reset the ViewTreeObserver each time to ensure the reuse of the OnGlobalLayoutListener
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
Hope it helps.

Animating a TextView appearing on activity creation

I want to show an animation of a TextView appearing when an activity is created. What I want is to show the activity without the TextView and then the TextView appearing (ideally, flying from outside) in its final position without user interaction.
I've tried to use the transition framework from API level 19 by having the TextView with visibility gone in the XML layout and setting it to visible in onCreate() with this code:
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
TransitionManager.beginDelayedTransition(layout, new ChangeBounds());
textView.setVisibility(View.VISIBLE);
This doesn't work. However, if I don't do this in onCreate() but as a response to a button click, it works. I think that the problem is that the layout is not created yet, so when I set the visibility to visible in the onCreate(), the layout is created with the final state and there aren't two scenes for the TransitionManager to work.
I've tried putting the code in onPause() but the result was the same. Any ideas how this should be done?
Try onWindowFocusChanged() like this
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
// start your animation here
}
}
You should do this in onResume() after the layout has been created. Initially the visibility would be Invisible and then the view will animate.
try like this:
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
layout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TransitionManager.beginDelayedTransition(layout, new ChangeBounds());
textView.setVisibility(View.VISIBLE);
}
});
because your view should be able to animate only after it's creation

Why view width and height 0 in post() runnable?

I'm getting height and width of a view, inflated in the getView() method. It's a grid item.
Usually I use post() on the view to get the size, after it was attached to the layout. But it returns 0.
final View convertViewFinal = convertView;
convertView.post(new Runnable() {
#Override
public void run() {
doSomethingWithConvertView(convertViewFinal);
}
});
...
doSomethingWithConvertView(View v) {v.getWidth(); /*returns 0*/};
How do I get the size?
While using a viewTreeObserver definitely works, I have found that calling measurements requirements on inflated views can be done reliably using a runnable from the activity's view. i.e.
someActivityInstance.getWindow().getDecorView().post(new Runnable() {
#override
public void run() {
// someMeasurements
}
});
The thing is that convertview most likely is not drawn on the phone screen yet, so you have to wait until it is ready.
You need to use a ViewTreeObserver, to know exactly when the view has been drawn.
Check this answer for more info:
When Can I First Measure a View?
You probably is calling this at onCreate or onStart or onResume, methods which runs before layout measure.
But there is a lot of work arounds, this is one good option:
ViewTreeObserver vto = rootView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
v.getWidth();//already measured...
}
});
Where rootView may be any viewGroup in a higher level than the one you want the width.
But be aware that this listner may run more than once.

Using smoothScrollBy method in Android ScrollView class

I have an Android program with a ScrollView that will correctly do an automatic scroll when I call the scrollBy method on the emulator on my computer but will not do it on my Android phone.
Here is the code:
public class RecordGameActivity3 extends Activity {
ScrollView myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordgame3);
myView = (ScrollView)findViewById(R.id.scrollView1);
}
public void addPlayer(View v)
{
//Removed non-related code
myView.smoothScrollBy(0, 50);
}
}
Also, the regular scrollBy method doesn't work or the scrollTo method (although I may just not be using that correctly since that doesn't work on the computer either). Does anyone have an idea what might be wrong?
EDIT:
My problem as Darrell's answer showed me was that I was calling my smoothScrollBy method from within a function where I was making changes to the layout that would make the scroll area big enough to be able to be scrolled. Apparently, by the time I was calling it in the function, the changes weren't actually applied and so it couldn't be scrolled.
Thus, I changed the code using his advice to the following:
public class RecordGameActivity3 extends Activity {
ScrollView myView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recordgame3);
myView = (ScrollView)findViewById(R.id.scrollView1);
// New code that listens for a change in the scrollView and then calls the scrollBy method.
ViewTreeObserver vto = myView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
myView.smoothScrollBy(0, 100);
}});
}
public void addPlayer(View v)
{
//Code that is called on an onClick listener that adds things to the ScrollView making it scrollable.
}
}
Are you calling smoothScrollBy from the onCreate method? Try waiting until after the views are set up, for instance from onResume. (If you must do it from onCreate you could register a ViewTreeObserver for your view with a OnGlobalLayoutListener, and do the scrolling from there.)
In my case, I need to scroll the ScrollView when there is a MotionEvent.ACTION_UP event triggered in the exact ScrollView , scrollTo() works, but smoothScrollTo() doesn't . Thanks to you guys, I figured it out now, use a handler to smoothScroll the view after a while will work.

Categories

Resources