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
Related
I'm trying to make a set of views (that include several textviews and buttons - all in different parent layouts, but in the same activity) invisible if a particular condition is evaluated to false.
The conventional way to do that would be:
findViewById(R.id.myview).setVisibility(View.INVISIBLE);
My question is, will I have to do this for all the views one by one that I want to be invisible, And then toggle them back when I want them visible?
Or, is there a way to avoid the code-repetition?
If the Views are in different parents , you can't do it directly, but you can implement a method to change the visibility of a bunch of Views if you want to keep your code clean:
List<View> relatedViews = new ArrayList<>();
// ...
relatedViews.add(view1);
relatedViews.add(view2);
relatedViews.add(view3);
// ...
changeVisibility(relatedViews, View.INVISIBLE);
// ...
private void changeVisibility(List<View> views, int visibility) {
for (View view : views) {
view.setVisibility(visibility);
}
}
As a side note, you may want to change the visibility to View.GONE instead of View.INVISIBLE so it doesn't take any space in the layout.
you can use something like this. It's not the most elegant solution but works.
The idea is give to each view that you want to hide a same content description, because in the same layout you can not use same id for multiple view. With the same content description you can find all views in your layout and hide them.
That's an example considering the first layout as Linear. You can change obviously ;)
public class TestActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout rootLayout = (LinearLayout)findViewById(R.id.rootLayout);
int childcount = rootLayout.getChildCount();
for (int i=0; i < childcount; i++){
View v = rootLayout.getChildAt(i);
if(v.getContentDescription() != null && v.getContentDescription().equals("invisibleView")){
v.setVisibility(View.INVISIBLE);
//I suggest you to use GONE instead of INVISIBLE to remove the space of the view
}
}
}
}
in your xml give to the object that you want to hide this property
android:contentDescription="invisibleView"
Use varargs method for show or hide multiple views.
for example if you have views like view1, view2.....etc
then just call setVisibility(View.VISIBLE,view1,view2)
public static void setVisibility(int visibility, View... views){
for (View view : views){
view.setVisibility(visibility);
}
}
I'm developing an Android app in which I use LayoutInflator to generate new layout. What the app does basically is - Its has one imageview. When I click on a button, I change the layout to display multiple ImageViews (like 2x2 or 4x4). I am successful in displaying these layouts using the LayoutInflator. However, the previous Layout stays intact and the new Layout is displayed over the old Layout which, kind of, messes the whole layout. My question is - is there anyway to destroy the old layout before displaying the new one?
Edit:
Here is the code (on the onClick event that I'm using)
public void oneby1onClick(View view){
RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout);
view = getLayoutInflater().inflate(R.layout.activity_main, main,false);
main.removeView(main);
main.addView(view);
}
public void twoby2onClick(View view){
RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout);
view = getLayoutInflater().inflate(R.layout.twoby2, main,false);
main.removeView(main);
main.addView(view);
}
public void fourby4onClick(View view){
RelativeLayout main = (RelativeLayout)findViewById(R.id.main_layout);
view = getLayoutInflater().inflate(R.layout.fourby4, main,false);
main.removeView(main);
main.addView(view);
}
Maybe you could just make your layout invisible if that is the case.
This can be done quite easily programmaticaly:
layout.setVisibility(View.GONE);
According to #ookami.kb's suggestion I should use main.removeAllViews(); instead of main.removeView(main);, that helped me.
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
I am trying to add a View to a RelativeLayout in my OnClickListener.
montrolButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// myParent is a relative layout
// newChild is an ImageView
myParent.addView(newChild);
requestLayout();
}
});
I have looked at the HierarchyViewer, I don' see my new child being added.
Can you please tell me if I miss anything?
I just tried the very same code and it works as it should. There could be an issue with your variable myParent which is not the element you expect it to be.
Also I did not have to call requestLayout() for the added view to appear on the screen.
Possibly try to just explicitly get another part of your view and add it there to see what is happening. Also just to try, you may do this:
RelativeLayout rv = (RelativeLayout) this.findViewById(R.id.right3);
ProgressBar iv = new ProgressBar(this);
rv.addView(iv);
to see if there is anything wrong with your image view instead of myParent
In any case it works if both elements are OK - there is nothing else to do in an activity.
Do you set the layout attributes of the new view (image view)?
I'm trying to insert a View behind another view that is taking up the full screen and then later removing the view in the front to reveal the only remaining view. Functionally, everything is working as expected but the problem is that when I call View.addView() to add the second view, specifying to add it at index 0 so it is behind the first view, the screen flickers. It's almost as if the view is actually getting added in front of the first view for a fraction of a second and then it is hidden again as it is moved behind it.
Here's what I'm doing:
When the Activity is created I add an ImageView to a RelativeLayout and make the RelativeLayout instance the Activity's content view:
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
m_layout = new RelativeLayout(this);
m_layout.setBackgroundColor(Color.BLACK);
m_splashImage = new ImageView(this);
m_splashImage.setImageResource(R.drawable.splash);
m_splashImage.setScaleType(ScaleType.FIT_XY);
m_layout.addView(m_splashImage,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
setContentView(m_layout);
}
When the Activity is started, I created and add the GLSurfaceView to the RelativeLayout at index 0, so it is behind the ImageView:
protected void onStart() {
super.onStart();
m_layout.addView(new MyGLSurfaceView(), 0,
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
}
Later, after all of the loading is done and the GLSurfaceView is ready to continuously render,
the splash ImageView is removed and cleaned up.
public void hideSplashScreen() {
if (m_splashImage != null) {
m_layout.removeView(m_splashImage);
m_splashImage = null;
}
}
Is there a better way to do this that doesn't require creating the GLSurfaceView before the onStart() is called?
Have you tried using view.setVisibility(View.GONE) on of the view that you adding behind? Of course before you are adding it.