How to float left drawable with (hint) label in TextInputLayout? - android

I am using TextInputLayout and in which i set a drawableLeft and when i click on edittext the label is already floating up but i want to float the left drawable too with the label. How can i achieve , see the attached image...
Right now when i click on "Title" only leble is floating up but i want both should be floating "leftdrawable" and "label" when focused. And before to click it will look a normal line like "Service Date".
My code is below..
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/input_layout_cardnum"
>
<ImageView
android:id="#+id/imgLeft"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:src="#mipmap/ic_launcher"
android:layout_marginBottom="10dp"
/>
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_cardnums"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/cardnumEditTexst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="255"
android:singleLine="true"
android:hint="hello"
android:drawableLeft="#null"
android:paddingLeft="35dp"
/>
</android.support.design.widget.TextInputLayout>
</FrameLayout>
And in my code i'm setting an animation on onFocus so it goes on top with label
final Animation animation = AnimationUtils.loadAnimation(this,R.anim.anims);
final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft);
final EditText et = (EditText) findViewById(R.id.cardnumEditTexst);
et.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
if (et.getText().length() == 0)
{
imgLeft.startAnimation(animation);
}
} else
{
if (et.getText().length() == 0)
imgLeft.clearAnimation();
}
}
});
And my animation will throw the image view on top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:duration="200"
android:interpolator="#android:interpolator/linear"
>
<scale
android:fromXScale="0%"
android:fromYScale="0%"
android:toXScale="60%"
android:toYScale="60%"
android:pivotX="50%"
android:pivotY="50%"
/>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="100"
android:fromYDelta="0%"
android:toYDelta="-80%"/>
</set>
And by setting this i got, now floating is okay but see the paddingLeft="35dp", i want to start it from 0th position see in the first image where "Title" is showing, i mean there should not be padding but if i will remove the paddingLeft the floating functionality loss.

try this...
final Animation animation = AnimationUtils.loadAnimation(this, R.anim.anims);
final ImageView imgLeft = (ImageView) findViewById(R.id.imgLeft);
findViewById(R.id.cardnumEditTexst).setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
imgLeft.startAnimation(animation);
} else {
imgLeft.clearAnimation();
}
}
});
and
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fillAfter="true"
android:interpolator="#android:interpolator/linear">
<scale
android:fromXScale="0%"
android:fromYScale="0%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="60%"
android:toYScale="60%" />
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0%"
android:startOffset="100"
android:toYDelta="-80%" />
</set>
and set your layout like this...
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/input_layout_cardnum">
<ImageView
android:id="#+id/imgLeft"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="bottom"
android:src="#mipmap/ic_launcher"
android:layout_marginBottom="10dp" />
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_cardnums"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/cardnumEditTexst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="255"
android:singleLine="true"
android:hint="hello"
android:drawableLeft="#null"
android:paddingLeft="35dp" />
</android.support.design.widget.TextInputLayout>
</FrameLayout>

Related

Slide down, up animation only works for the first time in fragment

I have implemented a slide_down and slide_up animation for my layout in my fragment. I have implemented that for a click in my MPAndroidChart bar chart. Problem is this works well for the first slide_down and slide_up click. For next clicks animation not working.
slide_down.xml
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
android:toYScale="1.0" />
slide_up.xml
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="1.0"
android:toYScale="0.0" />
Layout
<LinearLayout
android:id="#+id/textViewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/redPrimaryLight"
android:orientation="horizontal"
android:padding="#dimen/_5sdp"
android:weightSum="3">
<TextView
android:id="#+id/txtSelMonth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#color/white" />
<TextView
android:id="#+id/txtMotor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#color/white" />
<TextView
android:id="#+id/txtNonMotor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#color/white" />
</LinearLayout>
code
I made the layout invisible inside onCreateView
textViewLayout.setVisibility(View.GONE);
downAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_down);
upAnimation = AnimationUtils.loadAnimation(context, R.anim.slide_up);
Then barchart click....
gBarChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
#Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
textViewLayout.setVisibility(View.VISIBLE);
textViewLayout.setAnimation(downAnimation);
}
#Override
public void onNothingSelected() {
textViewLayout.setAnimation(upAnimation);
textViewLayout.setVisibility(View.GONE);
}
});
You need to refresh your chart every time whenever you show the chart. For that you need to call following line.
chartView.invalidate();

setAnimation not working in LongClickListener

Why is my setAnimation not working in LongClickListener where as it is working fine in outside the LongClick listener?
This is my java code for adding animation
final Animation shake = AnimationUtils.loadAnimation(NavyashActivity.context, R.anim.shake);
final CardView breakfast = (CardView) rootView.findViewById(R.id.breakfastcard);
breakfast.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Log.e("shake long press","coming ");
// TODO Auto-generated method stub
breakfast.setAnimation(shake);
return true;
}
});
Also my shake.xml is as:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="-5"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="#android:anim/linear_interpolator"
android:toDegrees="5" />
<translate
android:fromXDelta="-10"
android:toXDelta="10"
android:repeatCount="5"
android:repeatMode="reverse"
android:interpolator="#android:anim/linear_interpolator"
android:duration="70" />
</set>
and my layout for cardview for which i need shaking is:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:foreground="?android:attr/selectableItemBackground"
android:longClickable="true"
android:clickable="true"
android:id="#+id/breakfastcard">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/framebox">
<TextView
android:layout_width="wrap_content"
android:id="#+id/breakfastheading"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/breakfastdetails"
android:layout_margin="2dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
I just want that as soon as the user Longclicks the cardview element the card should start shaking so as to notify that on being touched for long time its moving continuosly.
Try to change this:
breakfast.setAnimation(shake);
To this:
breakfast.startAnimation(shake);
You are setting the animation but not starting it.

Animation: In android how to scale & fade a view consisting of multiple lines of text from a central point

I want to animate a view consisting of multiple lines of text as shown in link https://dl.dropboxusercontent.com/u/8003415/animation.gif . This view consists of two TextViews enclosed in a parent view.
This parent view, contains two sets of animation.
1) the first set of view contains multiple lines of text which gets scaled from normal to large and then fades out.
2) second set of view contains another set of multiple text which fades and scales from small to normal.
please note that scaling and fading should happen simultaneously.
for the first set I designed the below animation set.This animation set scales((1,1)to (1.5,1.5))) and fades the text block from centre.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#anim/custom_decelerator" >
<scale
android:duration="50"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<alpha
android:duration="2000"
android:fromAlpha="2.0"
android:toAlpha="0.0" />
</set>
and for the second set also scales ((-1,-1)to (1,1)))and fades the text block from centre
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#anim/custom_decelerator" >
<alpha
android:duration="50"
android:fromAlpha="0.0"
android:toAlpha="0.2" />
<scale
android:duration="3000"
android:fromXScale="-1"
android:fromYScale="-1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
The problem is that, the two sets of animation is exactly not matching the animation specified in above link.
The animation doesn’t seem to originate from the dead centre of the text blocks.
I haven`t really worked with the animations, so thought to give it a try.
Trial and error led to POC level app. Hence the bunch of code.
Note that i have used some static views and repeated the animations on them.That can be polished for the expected output mentioned.
There were some minor problems in the animation example, i have fixed that.
Code snippets are as follows.
anim/zoom_to_medium.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<scale
android:duration="1000"
android:fromXScale="0"
android:fromYScale="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="restart"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="50"
android:fromAlpha="0.0"
android:toAlpha="0.2" />
</set>
anim\zoom_to_full.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<scale
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="restart"
android:toXScale="2"
android:toYScale="2" />
<alpha
android:duration="1000"
android:fromAlpha="2.0"
android:toAlpha="0.0" />
</set>
activity_main.xml
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/border_circle"
android:orientation="vertical">
<LinearLayout
android:id="#+id/llFish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="36%"
android:textColor="#android:color/black"
android:textSize="42dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:includeFontPadding="false"
android:text="Cat \n owners"
android:textColor="#android:color/black"
android:textSize="42dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/llDog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="54%"
android:textColor="#android:color/black"
android:textSize="42dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:includeFontPadding="false"
android:text="Fish \n owners"
android:textColor="#android:color/black"
android:textSize="42dp" />
</LinearLayout>
</RelativeLayout>
<Button
android:id="#+id/btnToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Click Me" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private LinearLayout llDog;
private LinearLayout llFish;
private Animation zoomToMed, zoomToFull;
private Button btnToggle;
private boolean isRunning;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llDog = (LinearLayout) findViewById(R.id.llDog);
llFish = (LinearLayout) findViewById(R.id.llFish);
zoomToMed = AnimationUtils.loadAnimation(this, R.anim.zoom_to_medium);
zoomToFull = AnimationUtils.loadAnimation(this, R.anim.zoom_to_full);
zoomToFull.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
hideViews();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startAnimations();
}
}, 1000);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
btnToggle = (Button) findViewById(R.id.btnToggle);
btnToggle.setOnClickListener(this);
}
private void hideViews() {
llDog.setVisibility(View.GONE);
llDog.setVisibility(View.GONE);
}
private void startAnimations() {
llDog.setVisibility(View.VISIBLE);
llFish.setVisibility(View.VISIBLE);
llDog.startAnimation(zoomToMed);
llFish.startAnimation(zoomToFull);
}
#Override
public void onClick(View view) {
isRunning = !isRunning;
if (isRunning) {
startAnimations();
} else {
hideViews();
}
}
}
and the bonus shape drawable for the red circle.
border_circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#android:color/holo_red_dark"></stroke>
<size android:width="250dp" android:height="250dp" />
</shape>
Applying these sets of animations simultaneously on that parent view would not result in the animation you are looking for. If I understand you correctly you are doing:
parentView.startAnimation(animSet1);
parentView.startAnimation(animSet2);
Am I right? If so, you are doing wrong! you should instead have the following layout: (This is not a real XML code, it is a pseudo XML code)
<Parent>
<Frame1>
<TextViews>
</Frame1>
<Frame2>
<TextViews>
</Frame2>
</Parent>
By this design you should consider that Parent element as a FrameLayout and that Frame elements can be whatever layout you want. Now you can achieve your desire animation by:
frame1.startAnimation(animSet1);
frame2.startAnimation(animSet2);
If something is unclear, don't hesitate, let me know.

Android animation - how add start margin?

I am trying to make animation of group of buttons sliding in and out but I am struggling with one problem.
Current animation is moving correctly but is sliding out from the very left of the screen. Just where is the button used to show up this. So this makes whole animation looks bad, because this group is sliding on this.
What I want to achieve is add some margin, or something similar that will make start of the animation just from the point where is the button.
I want it to look like this:
That means those 3 images will start showing from this white line, not from the very left of the screen.
Main activity onCreate, where everything is going on:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image1 = (LinearLayout)findViewById(R.id.imageLayout);
button = (Button)findViewById(R.id.buttonShow);
animationSlideInLeft = AnimationUtils.loadAnimation(this,
R.anim.push_right_in);
animationSlideOutRight = AnimationUtils.loadAnimation(this,
R.anim.push_left_out);
animationSlideInLeft.setDuration(1000);
animationSlideOutRight.setDuration(1000);
animationSlideInLeft.setAnimationListener(animationSlideInLeftListener);
animationSlideOutRight.setAnimationListener(animationSlideOutRightListener);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(v == button)
{
if(!on)
{
curSlidingImage = image1;
image1.startAnimation(animationSlideInLeft);
image1.setVisibility(View.VISIBLE);
on = true;
}
else
{
image1.startAnimation(animationSlideOutRight);
image1.setVisibility(View.INVISIBLE);
on = false;
}
}
}
});
}
animations files:
push_right_in
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="-100%p"
android:toXDelta="0" />
<alpha
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
push_left_out
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromXDelta="0"
android:toXDelta="-100%p" />
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/buttonShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Btn" />
<LinearLayout
android:id="#+id/imageLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" >
<ImageView
android:id="#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<ImageView
android:id="#+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<ImageView
android:id="#+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
</LinearLayout>
</LinearLayout>
try this code:
TranslateAnimation anim = new TranslateAnimation(-50, 0, 0, 0);
anim.setStartOffset(0);
anim.setDuration(3000);
imageView.startAnimation(anim);
Hope it Helps!!
You can add Padding to the parent Relative layout in this case the animations starts from padding like this
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="64dp">

how to add animated light around image border?

this is my code for animated image is work fine but i want to add green illuminated lighting border around image how i do this? like this image http://imgur.com/qg0JM0q ?
for fade in fade out on image rounded border???
public class MainActivity extends Activity {
Button btnAnimation, btnAnimation1, btnAnimation2;
Animation performAnimation, performAnimation1, performAnimation2;
ImageView androidImageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
performAnimation1 = AnimationUtils.loadAnimation(this, R.layout.animation1);
performAnimation1.setRepeatCount(4);
androidImageView = (ImageView)findViewById(R.id.androidImageView);
btnAnimation1 = (Button)findViewById(R.id.btn_animation1);
btnAnimation1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
androidImageView.startAnimation(performAnimation1);
}
});
}
}
<RelativeLayout 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" >
<ImageView
android:id="#+id/androidImageView"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="192dp"
android:adjustViewBounds="false"
android:src="#drawable/alertlogo" />
<Button
android:id="#+id/btn_animation1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/androidImageView"
android:layout_below="#+id/btn_animation"
android:text="perform animation 2" />
</RelativeLayout>
<!------------ animation1.xml---->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="8000" />
</set>

Categories

Resources