Android - LayoutTransition hides views inside the animated view before closing it - android

I have a LinearLayout that contains several views - when I add or remove it from my view I used the default LayoutTransition.
I'm adding the view to my AppbarLayout - and I added the animations programmatically by setting a new LayoutAnimation on the appbar before adding the view and setting it to null after the view is added - I don't do it using the tag in the xml due to https://code.google.com/p/android/issues/detail?id=191170
The problem is that when i remove it from the view the default animation is done in two parts
all the views inside immediately disappear
the closing of the view is animated from bottom to top
Which change is needed to the LayoutTransition in order to have only the 2nd animation where the view is closed from bottom to top?

CommonsWare answer
Modify its LayoutParams to move it to the end position. Use
getLayoutParams() on the View, cast it to the appropriate type based
on its container, modify the LayoutParams object, then call
setLayoutParams() on the View to commit the changes.

Try this:
final LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutTransition transition = container.getLayoutTransition();
transition.disableTransitionType(LayoutTransition.APPEARING);
transition.disableTransitionType(LayoutTransition.DISAPPEARING);

Related

Android FlexboxLayout : android:animateLayoutChanges not working when child view resized

I added this option to my FlexboxLayout for had animation when children move :
android:animateLayoutChanges="true"
Unfortunately child view are animated only when I add child, but not when one child is resized!
Have you got any idea to always have an animation?
To animate children when they change their size you need to enable CHANGING transition type of the container's layoutTransition:
LayoutTransition layoutTransition = new LayoutTransition();
layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
parentView.setLayoutTransition(layoutTransition);
LayoutTransition

How to move text fields or buttons or any content in a fragment in android?

I want to know how to move any content like edittext, button etc on a fragment.
I searched a lot but what I found is how to move a fragment from left to right or right to left. I don't want to move fragment, I want to move content in fragments.
When I press next button the edittext field I would like to move it to the right and then an other field appears.
All this should be done on a fragment. In a simple activity I did it with animation but in fragment I can't do the same effect.
You can change any view's parent by following code :
To move a view from one fragment to another, first remove it from original parent :
ViewGroup parent = (ViewGroup) yourChildView.getParent();
if (parent != null) {
// detach the child from parent or you get an exception if you try
// to add it to another one
parent.removeView(yourChildView);
}
Now add it to required fragment or View :
// you might have to update the layout parameters on your child
// for example if your child was attached to a RelativeLayout before
// and you add it to a LinearLayout now
// this seems very odd coming from iOS but iOS doesn't support layout
// management, so...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(//required params);
yourChildView.setLayoutParams(params);
yourNewParent.addView(yourChildView);
Animation should work inside fragment also. Use object animator to move the view components from one place to another.E.g
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view,"translationX", 0, 750); // start , end position
objectAnimator.setDuration(1000);
objectAnimator.start();

How to add view in custom layout in runtime

I am creating custom layout (extends FrameLayout). I have a bunch of view defined in xml(it doesn't matter now).
What I need to do. My custom layout has custom defined attributes, let's assume that it named footer_banner_type.
I have different Banners classes some of them I quite different from one another, so I cannot place some base banner in xml. So I have to add some banner based on attribute value.
I am extending FrameLayout . I am newbie and this is my first custom layout.
I don't know how to improve performance.
As I understand Layout iterating and inflating all child views. But if I need to add view in runtime.I don't want to make layout reiterate view hierarchy, because it will be performance issue.
My question is how to implement my task in better way.
//First create your view:
View wonderfulView = new View(this.getApplicationContext());
//Then, create its LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
//Set those layout params in your view
wonderfulView.setLayoutParams(params);
//finaly, add the view to your ViewGroup
yourLayout.addView(wonderfulView);
That's it.
If you want to change the view container, you'll have to remove it form the previous parent like this:
View movingThing = ((YourLayoutClass)findViewById(R.id.currentContainer)).getChildAt(WhereYourViewis);
((YourLayoutClass)findViewById(R.id.currentContainer)).removeView(movingThing);
((YourLayoutClass)findViewById(R.id.newContainer)).addView(movingThing);

How to disable effect when addView in Android

I have a problem : When I add a CustomView into FrameLayout with some params to set CustomView position at bottom of FrameLayout. The first, CustomView is created with some params, and I used addView() function to add it to FrameLayout. But first CustomView is displayed at top of FrameLayout, and after that It moving(very fast) to bottom of FrameLayout.
How to disable this effect?
You need to disable the LayoutTransition for your view.
LayoutTransition layoutTransition = yourFrameLayout.getLayoutTransition();
layoutTransition.disableTransitionType(LayoutTransition.APPEARING);
Read more about LayoutTransition on the android developers website.

How to Programmatically Add Views to Views

Let's say I have a LinearLayout, and I want to add a View to it, in my program from the Java code. What method is used for this? I'm not asking how it's done in XML, which I do know, but rather, how can I do something along the lines of this sample code?
(One View).add(Another View)
Like one can do in Swing.
Calling addView is the correct answer, but you need to do a little more than that to get it to work.
If you create a View via a constructor (e.g., Button myButton = new Button();), you'll need to call setLayoutParams on the newly constructed view, passing in an instance of the parent view's LayoutParams inner class, before you add your newly constructed child to the parent view.
For example, you might have the following code in your onCreate() function assuming your LinearLayout has id R.id.main:
LinearLayout myLayout = findViewById(R.id.main);
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
myLayout.addView(myButton);
Making sure to set the LayoutParams is important. Every view needs at least a layout_width and a layout_height parameter. Also getting the right inner class is important. I struggled with getting Views added to a TableRow to display properly until I figured out that I wasn't passing an instance of TableRow.LayoutParams to the child view's setLayoutParams.
The best way I found is to use the inflate static method of View.
View inflatedView = View.inflate(context, yourViewXML, yourLinearLayout);
where yourViewXML is something like R.layout.myView
please notice that you need a ViewGroup in order to add a view (which is any layout you can think of)
so as an example lets say you have a fragment which it view already been inflated and you know that the root view is a layout, and you want to add a view to it:
View view = getView(); // returns base view of the fragment
if (view == null)
return;
if (!(view instanceof ViewGroup))
return;
ViewGroup viewGroup = (ViewGroup) view;
View popup = View.inflate(viewGroup.getContext(), R.layout.someView, viewGroup);
EDIT:
Kotlin code for the example above (view is the getView() of a fragment)
(view as? ViewGroup)?.let {
View.inflate(context, R.layout.add_credit_card, it)
}
To add the view programmatically, you can do:
LinearLayout rlmain = new LinearLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout ll1 = new LinearLayout (this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.logo);
LinearLayout .LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
iv.setLayoutParams(lp);
ll1.addView(iv);
rlmain.addView(ll1);
setContentView(rlmain, llp);
You can also add any number of views.
LinearLayout is a subclass of ViewGroup, which has a method called addView. The addView method should be what you are after.
The idea of programmatically setting constraints can be tiresome. This solution below will work for any layout whether constraint, linear, etc. Best way would be to set a placeholder i.e. a FrameLayout with proper constraints (or proper placing in other layout such as linear) at position where you would expect the programmatically created view to have.
All you need to do is inflate the view programmatically and it as a child to the FrameLayout by using addChild() method. Then during runtime your view would be inflated and placed in right position. Per Android recommendation, you should add only one childView to FrameLayout [link].
Here is what your code would look like, supposing you wish to create TextView programmatically at a particular position:
Step 1:
In your layout which would contain the view to be inflated, place a FrameLayout at the correct position and give it an id, say, "container".
Step 2
Create a layout with root element as the view you want to inflate during runtime, call the layout file as "textview.xml" :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
</TextView>
BTW, set the layout-params of your frameLayout to wrap_content always else the frame layout will become as big as the parent i.e. the activity i.e the phone screen.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
If not set, because a child view of the frame, by default, goes to left-top of the frame layout, hence your view will simply fly to left top of the screen.
Step 3
In your onCreate method, do this :
FrameLayout frameLayout = findViewById(R.id.container);
TextView textView = (TextView) View.inflate(this, R.layout.textview, null);
frameLayout.addView(textView);
(Note that setting last parameter of findViewById to null and adding view by calling addView() on container view (frameLayout) is same as simply attaching the inflated view by passing true in 3rd parameter of findViewById(). For more, see this.)
One more way to add view from Activity
ViewGroup rootLayout = findViewById(android.R.id.content);
rootLayout.addView(view);
You guys should also make sure that when you override onLayout you HAVE to call super.onLayout with all of the properties, or the view will not be inflated!

Categories

Resources