android:animateLayoutChanges won't work in parent layout - java

Suppose I have a layout file structured like this:
<LinearLayout android:id="#+id/main" android:orientation="vertical" android:animateLayoutChanges="true">
<EditText>
<TextView android:id="#+id/header1">
<LinearLayout android:id="#+id/insertionPoint" android:orientation="vertical" android:animateLayoutChanges="true">
</LinearLayout>
<TextView android:id="#+id/header2">
</LinearLayout>
I want to dynamically add text fields to the Layout insertionPoint and I would like to see an animation of the elements below it (in this case, header2) sliding down.
Using android:animateLayoutChanges only animates the elements in the layout insertionPoint, so there is no animation for header2. What should I do?
Just to be more clear: what I would like to do here is something like the animations that we can see in the People app in ICS when we add more fields, like telephone numbers, to a contact.
Thank you!

API 16 added the LayoutTransition.CHANGING animation type, however it is disabled by default.
To enable:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.main);
linearLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
Nice DevBytes video: https://www.youtube.com/watch?v=55wLsaWpQ4g

Related

Android Studio - How to programmatically add layout_below param to CardViews inside a PercentRelativeLayout

I am here because I am trying to set programmatically the layout_below parameter of some cardViews without success.
These cardViews are located inside a PercentRelativeLayout (I do not know if it is relevant but these cardviews have been added programmatically too).
I show you the code to make it clear.
...
<android.support.percent.PercentRelativeLayout
android:id="#+id/prl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv0"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv1"
// here I would like to add android:layout_below="#+id/cv0"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:percent="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/cv2"
// here I would like to add android:layout_below="#+id/cv1"
android:layout_width="match_parent"
percent:layout_heightPercent="65%"
percent:layout_marginTopPercent="2%"
percent:layout_marginLeftPercent="2%"
percent:layout_marginRightPercent="2%"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="4dp">
</android.support.v7.widget.CardView>
</android.support.percent.PercentRelativeLayout>
...
I tried to do something like this in my Java code but without success.
PercentRelativeLayout prl = (PercentRelativeLayout) view.findViewById(R.id.prl);
CardView cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);
prl.addView(cv);
cv = (CardView) LayoutInflater.from(getActivity()).inflate(R.layout.cv_block, prl, false);
cv.setId(currentId++);
RelativeLayout.LayoutParams cv_lp = (RelativeLayout.LayoutParams) cv.getLayoutParams();
cv_lp.addRule(RelativeLayout.BELOW,currentId-2);
/* I have also tried to call cv.requestLayout() but nothing changed */
prl.addView(cv);
As you can see, I am trying to generate cardviews and insert them one below the other.
Thanks in advance for your help.
PercentRelativeLayout is depreciated, so you are encouraged to use other type of layout.
This class was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:
ConstraintLayout gives lots of opportunities.
More about PercentRelativeLayout deprecation here.
More about ConstraintLayout and how to use it here or video tutorial.

Android: How to cover the whole layout by a view programatically while the layout contains buttons already?

I've a linear layout filled by buttons(view1 - view5).
I would like to add a view(red star) to this layout and I want this red star to cover the whole layout and its buttons (without setting the visibility of them)
in the attached image:
1 - the layout I've, filled by buttons
2- the layout I want to achieve by adding my red star programmatically
How can I do so?
First switch the root layout to a RelativeLayout, something like
<RelativeLayout ...>
<View android:id="#+id/view1"
... />
<View android:id="#+id/view2"
...
android:layout_below="#id/view1" />
<View android:id="#+id/view3"
...
android:layout_toRightOf="#id/view2"
android:layout_below="#id/view1" />
...
Then at the bottom of that layout add your overlay which ignores all the other views (so can draw over them)
<View android:id="#+id/overlay"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>

Create ListView on right side of Activity

According to the following picture:
I want something like this
the ListView is not a NavigationDrawer, it's a part of the Activity.
Items the ListView are columns of a database and when user selected Each of them, on the left side(Activity), other fields of that column should be displayed.
How can I do this?
Try it like this
<LinearLayout 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"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0"
android:layout_height="match_parent"
android:layout_weight="1">
//Your UI
</LinearLayout>
<LinearLayout
android:layout_width="0"
android:layout_height="match_parent"
android:layout_weight="1">
//Your ListView
</LinearLayout>
</LinearLayout>
So your layout will have fragments one a left fragment and one a right one
XML
<LinearLayout
orientation:horizontal>
<FrameLayout
id=leftFragment
weight=1/>
<FrameLayout
id=rightFragment
weight=1/>
</LinearLayout>
Java Code
FragmentManager fm=getFragmentManager();
fm.replace(R.id.leftFragment,new LeftFragment());
fm.commit();
Similarly for Right Fragment
You should use a LinearLayout with Horizontal orientation, then have two layout, one for the left side of the screen and one for the right side of the screen (with your ListView). Then you should use the layout_weight attribute for each layout by declaring like 0.7 for the right one (ListView) and 0.3 for the left one, it should looks like your picture

Animate EditText out of a window

I have a RelativeLayout with a "search bar" (EditText) and a ListView under it:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/etSearch"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:clipToPadding="false"
android:listSelector="#drawable/listview_selector" >
</ListView>
</LinearLayout>
After the user "searches" I want the EditText to animate out of the screen and the ListView to push to the top. I put together a very rough GIF of what I need:
Does anyone have an idea on how I can accomplish this? thanks
UPDATE
I found out how to make the EditText animate out of the screen with this:
slide_out_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="600"/>
</set>
and then using it on the EditText like this:
mSlideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_out_top);
et.startAnimation(mSlideOutTop);
but then the ListView stays at its current height. What I want is for the ListView to extend to the top as the EditText animates out.
You can add TranslateAnimation to the ViewGroup( which is LinearLayout for you), the y-axis moving distance is the hight of the editText View. Then trigger this animation when you need. (update: just thought this way may create a blank bar at bottom, bad idea)
I have an another tricky idea.
set the EditText view in the ListView with position 0 , then simply call the smoothScrollToPostion(1) method to scroll.
see this may help you
smoothScrollToPositionFromTop() is not always working like it should

AdMob stealing 3 pixels?

I have this layout
<LinearLayout android:id="#+id/mainlayout" android:orientation="vertical" android:layout_height="fill_parent" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<TableLayout android:id="#+id/tableLayout1" android:layout_width="fill_parent" adroid:layout_height="wrap_content" android:gravity="center_horizontal" >
...
...
</TableLayout>
</ScrollView>
Ad is added as follow
LinearLayout mainLayout = (LinearLayout)findViewById( R.id.mainlayout );
AdView adView = new AdView(...);
LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,nearLayout.LayoutParams.WRAP_CONTENT);
llParam.gravity = Gravity.CENTER_HORIZONTAL ;
mainLayout.addView(adView, llParam);
I really can't figure out why there 3 pixels below the ad (shown as thiny white checkers in the image). I think tehre is no doubt about that, but I added the black and white pattern in the image to highlight the region I'm talking about. In the original it is all black.
IIRC, a number of ad companies add these "pixels" to their image to keep ad distributors honest, as it were.
A number of companies sell this service (example): they give you one-pixel images to add to your advertisements, and then they can track (independently of AdMob) the number of people who loaded your ad by counting the number of times their 1x1 pixel was requested. So this way, an advertiser can make sure that AdMob isn't inflating the number of views of the ad to get more money out of the advertiser.
This might not be the reason in your particular case -- but it sounds like a reasonable explanation.

Categories

Resources