TranslateAnimation move tomake other views appear - android

I'm using a TranslateAnimation to make a fragment (GoogleMap) sliding down to give space to an EditText and a TextView to be visible.
so I used this:
text: TextView
edit: EditText
MapLayout: a LinearLayout that contains the Map
Animation animation = new TranslateAnimation(
MapLayout.getX(), MapLayout.getY(),MapLayout.getY(), text.getHeight()+edit.getHeight());
The problem is that I can't make the slide because text.getHeight()+edit.getHeight() returns 0 so there's no slide!
I tried using a number (100 for exemple), the slide is made, but it's different between the devices, I tested on a Galaxy S3 and the slide is not complete, there's still a part of the EditText which is not visible, as for the emulator it worked ok.
When I tried to make the number a bit bigger, so the slide will be longer (200 for exemple), well... the slide was good for the S3, but i was big for the emulator.
So what I want to know is that if there's any way to make the slide move to a point, without depending on the device, I mean without using pixels; so the slide will work perfectly in any device/
I hope that my problem is clear.
Thank you
Update: I don't if this will help, I added a Toast message, show the height of the EditText and the TextView, in the Emulator it says: 85 and in the S3 it says 181
So yeah, I need to make the map slide down in any device like I said
MainActivity:
protected Animation animation;
protected LinearLayout MapLayout;
protected EditText edit;
protected TextView text;
MapLayout = (LinearLayout)findViewById(R.id.MapLayout);
edit = (EditText)findViewById(R.id.Recherche);
text = (TextView)findViewById(R.id.CaptionRecherche);
Toast.makeText(context, "Height: "+(edit.getHeight()+text.getHeight()), 1000).show();
animation = new TranslateAnimation(MapLayout.getX(), MapLayout.getY(), MapLayout.getY(), text.getHeight()+edit.getHeight());
animation.setDuration(1000);
animation.setFillAfter(true);
MapLayout.startAnimation(animation);
Main XML:
------- I'm using a DrawerLayout...I have a slide menu tu show in the application...just for your information-------
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/ContenuPrincipal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/activity_main_relative"
/>
<!-- ListView... La liste des options du menu -->
<ListView
android:id="#+id/Menu"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:background="#333"
android:divider="#666"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
/>
</android.support.v4.widget.DrawerLayout>
Main2 XML (The one I included above):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E8E8E8">
<!-- Champs de saisie pour effectuer la recherche: -->
<TextView
android:id="#+id/CaptionRecherche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer l'emplacement que vous cherchez: "
android:textSize="20sp"
android:layout_marginTop="7dp"
android:layout_marginLeft="20dp"
/>
<EditText
android:id="#+id/Recherche"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:hint="Salle, Deparetement..."
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:maxLength="100"
android:maxLines="1"
android:layout_below="#id/CaptionRecherche"/>
<!-- La map: -->
<LinearLayout
android:id="#+id/MapLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</RelativeLayout>

As a part of my application, I have a status bar which contains some text. This status bar is hidden until the user clicks a button at which point it slides down (hiding the topmost content of the layout below).
The code I use to get the correct height of the hidden status bar:
private int hiddenStatusHeight;
private int currentStatusBarHeight;
private void getStatusBarHeight() {
final ViewTreeObserver observer = hiddenStatus.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#SuppressLint("NewApi") #SuppressWarnings("deprecation") #Override public void onGlobalLayout() {
hiddenStatus.measure(MeasureSpec.UNSPECIFIED,
MeasureSpec.UNSPECIFIED);
hiddenStatusHeight = hiddenStatus.getMeasuredHeight();
currentStatusBarHeight = statusBar.getHeight();
ViewTreeObserver obs = hiddenStatus.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
}
The code that is executed when the button is clicked:
private OnClickListener ExpandClickListener = new OnClickListener() {
#Override public void onClick(View v) {
boolean isExpanded = (Boolean) expandButton
.getTag(R.id.TAG_EXPANDED);
int originalHeight = (Integer) expandButton
.getTag(R.id.TAG_ORIGINAL_HEIGHT);
if (isExpanded) {
expandButton.setTag(R.id.TAG_EXPANDED, false);
expandButton.setImageResource(R.drawable.ic_action_down);
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, originalHeight));
Log.d(TAG, "Collapsing to " + originalHeight);
ValueAnimator va = ValueAnimator.ofInt(currentStatusBarHeight,
originalHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
} else {
expandButton.setTag(R.id.TAG_EXPANDED, true);
expandButton.setImageResource(R.drawable.ic_action_collapse);
currentStatusBarHeight = originalHeight + hiddenStatusHeight;
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, currentStatusBarHeight + 15));
Log.d(TAG, "Expanding to " + originalHeight + "+"
+ hiddenStatusHeight + "=" + currentStatusBarHeight);
ValueAnimator va = ValueAnimator.ofInt(originalHeight,
currentStatusBarHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
}
}
};
And finally my layout XML (it has to be a FrameLayout):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:showDividers="middle" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/displayStatusBar"
style="#style/DisplayStatusBar"
android:layout_width="match_parent"
android:layout_height="65dp" >
<RelativeLayout
android:id="#+id/status_always_visible"
style="#style/StatusBar"
android:layout_width="match_parent"
android:layout_height="20dp" >
<TextView
android:id="#+id/status_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/received" />
<TextView
android:id="#+id/status_time_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/status_received" />
<TextView
android:id="#+id/status_time_delete_relative_text"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/status_time_delete_relative"
android:text="#string/is_deleted" />
<TextView
android:id="#+id/status_time_delete_relative"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/minutes" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/status_hidden"
style="#style/StatusHidden"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/status_always_visible" >
<LinearLayout
android:id="#+id/status_hydrants_near_address_container"
style="#style/StatusHiddenText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:divider="#android:drawable/divider_horizontal_bright"
android:orientation="vertical"
android:paddingLeft="10dp"
android:showDividers="middle" >
<TextView
style="#style/StatusHiddenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_information" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/optionsBar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#999"
android:orientation="horizontal"
android:paddingTop="5dp" >
<ImageButton
android:id="#+id/button_hydrants"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:alpha="50"
android:contentDescription="#string/module_hydrants"
android:src="#drawable/ic_action_place" />
<ImageButton
android:id="#+id/button_route"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:contentDescription="#string/module_directions"
android:src="#drawable/ic_action_directions" />
<ImageButton
android:id="#+id/button_pdf"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:clickable="false"
android:contentDescription="#string/module_accessplan"
android:src="#drawable/ic_action_attachment" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/button_more"
style="#style/android:Widget.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_down" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<!-- The "empty" view to show when there are no items in the "list" view defined above. -->
<TextView
android:id="#android:id/empty"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="32dp"
android:text="#string/no_information"
android:textColor="?android:textColorSecondary" />
</FrameLayout>
Hope some of this may be helpful to you.
Just to mention it, I have asked a similar question where the visible layout is pushed downwards as the menu expands. So have a look at that if you rather want this behaviour: How to animate a slide in notification view that pushes the content view down
Happy coding

Related

ImageView background in not setting multiple times

I am working on an android project, in which I need to set ImageView
background (ascending icon and descending icon) multiple times. to do
this I am doing it with a clicking variable like:
sortBookedOnLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pickUpImage.setBackgroundResource(R.drawable.ascending);
vouchredImage.setBackgroundResource(R.drawable.ascending);
cancledImage.setBackgroundResource(R.drawable.ascending);
if(Constant.bookedOn == 1) {
Log.d(TAG, "FliteronClick:1 ");
bookedonImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ascending_active));
// int ascending_active_b = R.drawable.ascending_active;
// bookedonImage.setBackgroundResource(ascending_active_b);
// bookedonImage.setImageDrawable(mContext.getResources().getDrawable( R.drawable.ascending_active));
// bookedonImage.setBackground(mContext.getResources().getDrawable(R.drawable.ascending_active));
// ivPickUpTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ascending_active, 0);
Constant.bookedOn = 2;
}else if(Constant.bookedOn == 2){
Log.d(TAG, "FliteronClick:2 ");
bookedonImage.setImageDrawable(mContext.getResources().getDrawable(R.drawable.decending_active));
// bookedonImage.setImageResource(android.R.color.transparent);
// int decending_active_b = R.drawable.decending_active;
// bookedonImage.setBackgroundResource(decending_active_b);
// bookedonImage.setImageDrawable(mContext.getResources().getDrawable( R.drawable.decending_active));
// ivPickUpTxt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.decending_active, 0);
Constant.bookedOn = 1;
}
}
}
});
And My Xml layout is just like:
<LinearLayout
android:id="#+id/sortBookedOnLayout"
android:orientation="horizontal"
android:background="#color/white"
android:weightSum="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1.8"
android:layout_width="0dp"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/bookedOntxtLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/ivPickUpTxt"
style="#style/editTextTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edittext_background"
android:drawableLeft="#drawable/cal2"
android:drawablePadding="10dp"
android:hint="#string/booked_on"
android:inputType="text"
android:maxLines="1"
android:padding="10dp"
android:textColor="#000000"
android:textCursorDrawable="#drawable/cursor_drawable"
android:textSize="#dimen/filter_text_size"
/>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_weight=".2"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:id="#+id/booedOnImage"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/ascending"
/>
</LinearLayout>
</LinearLayout>
Screenshot of my layout.
in this image i am going to click on BookedOn layout.
And my targetSdkVersion 25
I have tried it multiple ways as I commented but image icon is not
reflecting on imageView. any help will be appreciable.Thanks.
Try this:
qImageView.setBackgroundResource(R.drawable.thumbs_down);
and add android:scaleType:fitxy
<ImageView
android:id="#+id/booedOnImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitxy"
android:background="#drawable/ascending"/>
(or) Try programmatically
imgview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Here's a thread that talks about the differences between the two methods.
I Understand your problem...You are not set the width of the LinearLayout
<LinearLayout
android:layout_weight=".2"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/booedOnImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/abc_btn_rating_star_off_mtrl_alpha"
/>
</LinearLayout>

Animate a view from 0dp width to MATCH_PARENT

I'm trying to animate my RecyclerView item when its clicked by making a rectangle grow from zero width to 100% (MATCH_PARENT) and become the background of the item.
However I can't see the animation working. I mean, the initial background is white, but the rectangle is gray, so the clicked item would become gray. But this is not happening.
Here's the item xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="0dp"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="72dp"
android:orientation="horizontal">
<View
android:id="#+id/colored_bar"
android:layout_width="3dp"
android:layout_height="match_parent"
android:background="#drawable/colored_bar_bg1"></View>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/option_background_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#e0e0e0"></View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="13dp"
app:srcCompat="#drawable/ic_lock" />
<TextView
android:id="#+id/card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/icon"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="16dp"
android:paddingLeft="8dp"
android:textColor="?android:attr/textColorPrimary"
android:textSize="16sp"
tools:text="#string/option_title_label" />
<TextView
android:id="#+id/card_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/card_title"
android:layout_toEndOf="#+id/icon"
android:layout_toRightOf="#+id/icon"
android:paddingLeft="8dp"
android:textSize="14sp"
tools:text="#string/option_description_label" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And the code to make the animation:
public class OptionListItemHolder extends RecyclerView.ViewHolder {
private TextView cardTitle;
private TextView cardSubtitle;
private ImageView icon;
private View coloredBar;
private View optionBackground;
public OptionListItemHolder(View v) {
super(v);
cardTitle = (TextView)v.findViewById(R.id.card_title);
cardSubtitle = (TextView)v.findViewById(R.id.card_subtitle);
icon = (ImageView)v.findViewById(R.id.icon);
coloredBar = v.findViewById(R.id.colored_bar);
optionBackground = v.findViewById(R.id.option_background_container);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ObjectAnimator animation = ObjectAnimator.ofInt(optionBackground, "width", 0, view.getWidth());
animation.setDuration(600);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
});
}
}
Why it is not working?
I've found that using a ValueAnimator instead works
ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), newWidth);
widthAnimator.setDuration(500);
widthAnimator.setInterpolator(new DecelerateInterpolator());
widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
view.getLayoutParams().width = (int) animation.getAnimatedValue();
view.requestLayout();
}
});
widthAnimator.start();
If the view's width needs to match the parent, you can get the width of the parent by
int parentWidth = ((View)view.getParent()).getMeasuredWidth();

Android views tate changes resets layout

I have a list with Swipe Actions on it using IOnTouchListener, as dragging occurs I am changing the Left of a LinearLayout to reveal a second LinearLayout behind it. That layout has two buttons, one is hidden. If the user does a "quick" swipe then I move the Left of the top view to a specific value and fire some code. This code changes the invisible button to be visible. When the ViewState of that button changes it makes the top view's Left snap back to 0 instead of staying where I place it. Any idea why it would do this or how to work around it.
The XML looks like... (But will change slightly based on the answer to another question I posted)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="fill_vertical"
android:background="#color/black"
android:id="#+id/Swipe"
>
<LinearLayout
android:id="#+id/LeftContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/yellow"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<Button
android:id="#+id/ApproveButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#2796C3"
android:text="Approve"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/ApproveUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#215681"
android:text="Undo"
android:layout_toRightOf="#id/ApproveButton"
/>
</LinearLayout>
<LinearLayout
android:layout_alignParentRight="true"
android:id="#+id/RightContentView"
android:layout_width="175dp"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="horizontal"
>
<Button
android:id="#+id/DenyButton"
android:layout_width="0dp"
android:layout_weight=".72"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="Deny"
/>
<Button
android:id="#+id/DenyUndoButton"
android:layout_width="0dp"
android:layout_weight=".28"
android:layout_height="match_parent"
android:background="#860000"
android:text="Undo"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/TopContentView"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1F1F1F">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="match_parent" >
<ImageView
android:id="#+id/UnreadImage"
android:layout_height="match_parent"
android:layout_width="7dp"
android:src="#drawable/vertical_blue_bar"
android:background="#2796C3"/>
<LinearLayout
android:id="#+id/ListText"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:padding="12dp">
<TextView
android:id="#+id/Text_Line1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="13dip"
/>
<TextView
android:id="#+id/Text_Line3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="11dip"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
I am not using Java, I am using Xamarin with C#. Here are the relevant pieces of code...
public bool OnTouch(View v, MotionEvent e)
{
switch (e.Action)
{
case MotionEventActions.Down:
OriginalTouchX = Math.Ceiling(e.RawX);
index = e.ActionIndex;
pointerId = e.GetPointerId(index);
VelocityTracker.AddMovement(e);
break;
case case MotionEventActions.Move:
VelocityTracker.AddMovement(e);
ChangeX(newX);
VelocityTracker.ComputeCurrentVelocity(1);
float velocity = Math.Abs(VelocityTracker.GetXVelocity(pointerId));
if (velocity > SlowDrag && Math.Abs(distanceMoved) > (DragThreshold / 3))
{
QuickApprove();
}
break;
}
}
void QuickApprove()
{
ToggleUndoButton(true, true);
WaitingForUndo = true;
ChangeX(DragThreshold);
this.ApproveTimer.Start(true);
}
private void ToggleUndoButton(bool ShowUndo, bool LeftSide)
{
this.ApproveUndoButton.Visibility = ViewStates.Visible;
this.ApproveButton.Text = "Approving...";
}
private void ChangeX(int newX)
{
int width = this.TopContentView.Right - this.TopContentView.Left;
this.TopContentView.Left = newX;
this.TopContentView.Right = this.TopContentView.Left + width;
}
If in the ToggleUndoButton method I comment out the line
this.ApproveUndoButton.Visibility = ViewStates.Visible;
Everything works fine. The Left of TopContentView changes to be equal to my DragThreshold, the text changes to Approving... the timer starts, It stays this way for 2 seconds, then the code in my timer tick fires and the TopContentView is moved back to a Left of 0. If I leave in the visibility change then the TopContentView is immediately moved back to 0 instead of waiting until the timer is done.

View width for dynamically added view

I want to show a notification counter like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:alpha=".5"
android:id="#+id/gvIcon"
android:src="#drawable/ic_person_reading"
android:scaleType="centerCrop"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp" />
<LinearLayout
android:id="#+id/llTexts"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="#style/gridItemsText"
android:id="#+id/gvText"
android:text="Guten Morgen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partNoticeTooltip"
android:orientation="vertical"
android:background="#drawable/bg_tooltip_red"
android:layout_width="#dimen/tooltipWH"
android:layout_height="#dimen/tooltipWH">
<TextView
android:id="#+id/tvCounter"
android:text="4"
android:textColor="#color/white"
android:textSize="#dimen/h6"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which has an output like this:
This is my expectation, but with dynamic way. So I wrote this in the setOnItemClickListener of my GridView:
View tooltip = context.getLayoutInflater().inflate(R.layout.part_tooltip, null);
TextView tvCounter = (TextView) tooltip.findViewById(R.id.tvCounter);
tvCounter.setText("" + counter);
LinearLayout llText = (LinearLayout) view.findViewById(R.id.llTexts);
llText.addView(tooltip);
the part_tooltip has exactly the same code but with another layout. And here is the output:
The red layout does not being displayed with full width. What am I missing?
You need to use a global layout listener and also the view tree observer:
ViewTreeObserver mVTO = parentLayoutOfTheViewAddedDynamically.getViewTreeObserver();
mVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(viewAddedDynamically != null){
int viewWidth = viewAddedDynamically.getWidth();
// any operation based on viewWidth is to be done here
}
}
});

Animation in Viewpager tab change fadein / fadeout as like Linkedin introduction screen

I want to implement same kind of animation such as linked in does in android application for its Introduction(Login / register) screen.
I am using view pager for Introduction screen and i want to implement fadein fadeout animation on background image change, As per swipe right to left or vice versa.
I want to implement fadein and fadeout animation on background image change according to swipe of screen.
any help is appreciated.
Please take a look at my layout code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/background_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="7" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:gravity="right"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="5dp"
android:src="#drawable/icon_skip" />
<TextView
android:id="#+id/skip_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Skip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="bottom"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/logo" />
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context="com.xyz.View.IntroductionScreen" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/connection_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:background="#drawable/button"
android:text="CONNEXION"
android:textColor="#android:color/white" />
<Button
android:id="#+id/register_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:background="#drawable/button"
android:text="INSCRIPTION"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
And View pager fragment layout is
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/text_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TextView
android:id="#+id/tagline_tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:text="Laissez votre prochain job"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/details_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="2"
android:text="vous trouver"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>
sample Splashs creen this is what i want to implement.
Thank you
This is a lag free one and also handles the Buttons
Main Idea:
1) first create transparent background for your fragments.
2) Create LayerDrawable and add background image of each fragment as an item. Then add your LayerDrawable as a background of your viewpager.
3) in onCreate method set alpha of each layer correctly so just upper one has alpha value of 255.
4) set for each view of your FragmentStatPagerAdapter a tag that corresponds to drawable index that you declared in the LayerDrawable. for example when you open the app FragmentA is showing so its tag must correspond to upper drawable that is 2 (beginning from 0). last page tag must be 0 corresponds to lowest drawable.
5) change drawable of each view at the function transformPage
6) for adding the button use RelativeLayout.
In order to place buttons on top of all views use RelativeLayout. Later children are placing higher on the Z axis. You can see it in the code:
now lets see code:
MainActivity
public class MainActivity extends FragmentActivity {
ViewPager viewPager=null;
int numberOfViewPagerChildren = 3;
int lastIndexOfViewPagerChildren = numberOfViewPagerChildren - 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MyAdapter(getSupportFragmentManager()));
final LayerDrawable background = (LayerDrawable) viewPager.getBackground();
background.getDrawable(0).setAlpha(0); // this is the lowest drawable
background.getDrawable(1).setAlpha(0);
background.getDrawable(2).setAlpha(255); // this is the upper one
viewPager.setPageTransformer(true, new ViewPager.PageTransformer() {
#Override
public void transformPage(View view, float position) {
int index = (Integer) view.getTag();
Drawable currentDrawableInLayerDrawable;
currentDrawableInLayerDrawable = background.getDrawable(index);
if(position <= -1 || position >= 1) {
currentDrawableInLayerDrawable.setAlpha(0);
} else if( position == 0 ) {
currentDrawableInLayerDrawable.setAlpha(255);
} else {
currentDrawableInLayerDrawable.setAlpha((int)(255 - Math.abs(position*255)));
}
}
});
}
class MyAdapter extends FragmentStatePagerAdapter
{
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
Fragment fragment=null;
if(i==0)
{
fragment=new FragmentA();
}
if(i==1)
{
fragment=new FragmentB();
}
if(i==2)
{
fragment=new FragmentC();
}
return fragment;
}
#Override
public int getCount() {
return numberOfViewPagerChildren;
}
#Override
public boolean isViewFromObject(View view, Object object) {
if(object instanceof FragmentA){
view.setTag(2);
}
if(object instanceof FragmentB){
view.setTag(1);
}
if(object instanceof FragmentC){
view.setTag(0);
}
return super.isViewFromObject(view, object);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/layerdrawable" >
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_marginBottom="48dip" >
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sign in"
android:layout_margin="16dip"
android:background="#2ec6e4"
android:textColor="#FFFFFF" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Join us"
android:background="#2ec6e4"
android:layout_margin="16dip"
android:textColor="#FFFFFF"
/>
</LinearLayout>
</RelativeLayout>
LayerDrawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap
android:id="#+id/Idofbg3"
android:gravity="fill"
android:src="#drawable/bg3" />
</item>
<item>
<bitmap
android:id="#+id/Idofbg2"
android:gravity="fill"
android:src="#drawable/bg2" />
</item>
<item>
<bitmap
android:id="#+id/Idofbg1"
android:gravity="fill"
android:src="#drawable/bg1" />
</item>
</layer-list>
for lazy people who just do not want to declare fragments:
FragmentA
public class FragmentA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_a,container,false);
return v;
}
}
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/FragmentA"
android:background="#android:color/transparent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="This is Fragment A"
android:textColor="#FFFFFF"
android:id="#+id/textView"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Set a ViewPager.PageTransformer to the ViewPager and achieve the desired animation using aplha and translation animation properties.
The most important input is the position parameter passed to transformPage callback. The position value indicates how the view is positioned currently.
Assuming the views in ViewPager are full width, here is how position value need to be interpreted.
------------------------------------------------------------------------------------
position | what does it mean
------------------------------------------------------------------------------------
0 | view is positioned in the center and fully visible to the user.
-1 | view is positioned in the left and not visible to the user.
1 | view is positioned in the right and not visible to the user.
>-1 & <0 | view is being scrolled towards left and is partially visible.
>0 & <1 | view is being scrolled towards right and is partially visible.
------------------------------------------------------------------------------------
mPager.setPageTransformer(true, new ViewPager.PageTransformer() {
#Override
public void transformPage(View view, float position) {
// Ensures the views overlap each other.
view.setTranslationX(view.getWidth() * -position);
// Alpha property is based on the view position.
if(position <= -1.0F || position >= 1.0F) {
view.setAlpha(0.0F);
} else if( position == 0.0F ) {
view.setAlpha(1.0F);
} else { // position is between -1.0F & 0.0F OR 0.0F & 1.0F
view.setAlpha(1.0F - Math.abs(position));
}
// TextView transformation
view.findViewById(R.id.textView).setTranslationX(view.getWidth() * position);
}
});
Here is the layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_alignParentTop="true"
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
</RelativeLayout>
Here is the screen record:

Categories

Resources