I am currently creating android app. I have a login activity in which on top there is LinearLayout with ImageViewand below it there is GirdLayout where there are few EditTexts. I would like to move my LinearLayout to given height when any of the EditTexts has focus. I was trying the solution from here:
Android translate animation - permanently move View to new position using AnimationListener and put AnimationListener inside of onFocusChange, but in my case it appears to do nothing. Could You help me?
XML:
<?xml version="1.0" encoding="utf-8"?>
<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:weightSum="6"
android:background="#74B897"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/imageLayout"
>
<ImageView
android:layout_width="247dp"
android:layout_height="290dp"
android:id="#+id/logo_imageView"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_row="1"
android:src="#drawable/white_logo" />
</LinearLayout>
<GridLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/layout_shape"
android:layout_weight="2"
android:layout_row="7"
android:layout_column="1"
android:weightSum="5"
android:layout_below="#+id/logo_imageView"
android:layout_marginLeft = "15dp"
android:layout_marginRight = "15dp"
android:layout_marginBottom = "30dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fname_editText"
android:hint="First Name"
android:layout_rowWeight="0"
android:layout_marginTop = "15dp"
android:layout_row="1"
android:layout_column="0"
android:textColor="#FFFFFF"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sname_editText"
android:layout_below="#+id/fname_editText"
android:hint="Second Name"
android:layout_rowWeight="0"
android:layout_row="2"
android:layout_column="0"
android:textColor="#FFFFFF"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password_editText"
android:inputType="textPassword"
android:ems="10"
android:hint="Password"
android:layout_rowWeight="0"
android:layout_row="3"
android:layout_column="0"
android:textColor="#FFFFFF"/>
<Button
android:layout_width="141dp"
android:text="Log In"
android:id="#+id/log_button"
android:background="#drawable/button_shape"
android:layout_marginTop="10dp"
android:layout_marginBottom="40dp"
android:layout_below="#+id/id_editText"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:layout_rowWeight="1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_row="5"
android:layout_column="0"
android:layout_height="29dp"
android:textColor="#636363" />
</GridLayout>
</LinearLayout>
Activity:
public class LoginActivity extends Activity implements View.OnFocusChangeListener{
EditText sname;
EditText password;
TextView info;
ImageView img;
EditText fname;
LinearLayout imageLayout;
int oldX, oldY, newX, newY;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
fname = (EditText) findViewById(R.id.fname_editText);
sname = (EditText)findViewById(R.id.sname_editText);
password = (EditText)findViewById(R.id.password_editText);
img= (ImageView) findViewById(R.id.logo_imageView);
imageLayout = (LinearLayout) findViewById(R.id.imageLayout);
oldX = imageLayout.getWidth();
oldY = imageLayout.getHeight();
img.setImageResource(R.drawable.white_logo);
Button log_Button = (Button) findViewById(R.id.log_button);
fname.setFocusable(true);
sname.setFocusable(true);
password.setFocusable(true);
}
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(v.getId() == R.id.fname_editText || v.getId() == R.id.sname_editText || v.getId() == R.id.password_editText)
{
final ObjectAnimator moveDownAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 0.F, -300);
final ObjectAnimator moveUpAnim = ObjectAnimator.ofFloat(imageLayout, "translationY", 300, 0.F);
if(hasFocus){
//first option
moveDownAnim.start();
/*
second option
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, -300);
anim.setFillAfter(false);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageLayout.clearAnimation();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageLayout.getLayoutParams();
params.topMargin += -300;
imageLayout.setLayoutParams(params);
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, -300);
animation.setDuration(1);
imageLayout.startAnimation(animation);
}
});
imageLayout.startAnimation(anim);*/
}
}
}
}
I don't see you setting the OnFocusChangeListener for the EditTexts in your code.
fname.setOnFocusChangeListener(this);
sname.setOnFocusChangeListener(this);
Related
I have a HorizontalScrollView with a ChipGroup and some Chips. When I check a Chip which is cut out of the screen I want the ScrollView to snap to and show it fully.
This is how it looks like when I select it.
My layout file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
android:theme="#style/Theme.MaterialComponents">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="165dp" />
<LinearLayout
android:id="#+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="120dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<HorizontalScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.chip.ChipGroup
android:id="#+id/chip_group"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:chipSpacingHorizontal="10dp"
app:singleLine="true"
app:singleSelection="true"
app:selectionRequired="true">
<com.google.android.material.chip.Chip
android:id="#+id/chip_all"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginLeft="15dp"
android:backgroundTint="#color/indicator_chips"
android:checkable="true"
app:chipCornerRadius="10dp"
android:text="ALL"
android:textColor="#color/indicator_text"
app:checkedIconEnabled="false"
app:chipStrokeColor="#color/indicator_stroke"
app:chipStrokeWidth="1dp" />
<com.google.android.material.chip.Chip
android:id="#+id/chip_watching"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:checkable="true"
android:text="WATCHING"
android:textColor="#color/indicator_text"
app:chipCornerRadius="10dp"
app:checkedIconEnabled="false"
android:backgroundTint="#color/indicator_chips"
app:chipStrokeColor="#color/indicator_stroke"
app:chipStrokeWidth="1dp" />
<com.google.android.material.chip.Chip
android:id="#+id/chip_completed"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:checkable="true"
android:text="COMPLETED"
android:textColor="#color/indicator_text"
app:chipCornerRadius="10dp"
app:checkedIconEnabled="false"
android:backgroundTint="#color/indicator_chips"
app:chipStrokeColor="#color/indicator_stroke"
app:chipStrokeWidth="1dp" />
<com.google.android.material.chip.Chip
android:id="#+id/chip_onhold"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:checkable="true"
android:text="ON HOLD"
android:textColor="#color/indicator_text"
app:chipCornerRadius="10dp"
app:checkedIconEnabled="false"
android:backgroundTint="#color/indicator_chips"
app:chipStrokeColor="#color/indicator_stroke"
app:chipStrokeWidth="1dp" />
<com.google.android.material.chip.Chip
android:id="#+id/chip_dropped"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:checkable="true"
android:text="DROPPED"
android:textColor="#color/indicator_text"
app:chipCornerRadius="10dp"
app:checkedIconEnabled="false"
android:backgroundTint="#color/indicator_chips"
app:chipStrokeColor="#color/indicator_stroke"
app:chipStrokeWidth="1dp" />
<com.google.android.material.chip.Chip
android:id="#+id/chip_plantowatch"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_marginRight="15dp"
android:checkable="true"
android:text="PLAN TO WATCH"
android:textColor="#color/indicator_text"
app:chipCornerRadius="10dp"
app:checkedIconEnabled="false"
android:backgroundTint="#color/indicator_chips"
app:chipStrokeColor="#color/indicator_stroke"
app:chipStrokeWidth="1dp" />
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
And my class file:
public class LibraryFragment extends Fragment {
private HorizontalScrollView scrollView;
Chip chip_dropped;
ChipGroup chipGroup;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_library_anime, container, false);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
scrollView = view.findViewById(R.id.scroll_view);
chipGroup = view.findViewById(R.id.chip_group);
chipGroup.setOnCheckedChangeListener(checkedListener);
}
ChipGroup.OnCheckedChangeListener checkedListener = new ChipGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
switch (group.getCheckedChipId()) {
case R.id.chip_all:
fragmentTransaction.replace(R.id.fragment_container, new ListALL()).commit();
break;
case R.id.chip_watching:
fragmentTransaction.replace(R.id.fragment_container, new ListWATCHING()).commit();
break;
case R.id.chip_completed:
fragmentTransaction.replace(R.id.fragment_container, new ListCOMPLETED()).commit();
break;
case R.id.chip_onhold:
fragmentTransaction.replace(R.id.fragment_container, new ListONHOLD()).commit();
break;
case R.id.chip_dropped:
fragmentTransaction.replace(R.id.fragment_container, new ListDROPPED()).commit();
break;
case R.id.chip_plantowatch:
fragmentTransaction.replace(R.id.fragment_container, new ListPLANTOWATCH()).commit();
break;
}
}
};
}
So to repeat, I'm trying to make my ScrollView scroll to a Chip when it is clicked, like the play store with its. I tried .scroolTo and .smoothScrollTo but non of it work.
I made a sample project for You. I think You can easily convert it to Your app.
MainActivity.java
public class MainActivity extends AppCompatActivity
{
HorizontalScrollView scroll;
int widthScreen;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
widthScreen = displayMetrics.widthPixels;
scroll = findViewById(R.id.scroll);
LinearLayout linearLayout = findViewById(R.id.linLay);
for (int index = 0; index <= linearLayout.getChildCount() - 1; index++)
{
linearLayout.getChildAt(index).setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Rect r = new Rect();
v.getGlobalVisibleRect(r);
if (r.right == widthScreen)
{
Rect rr = new Rect();
v.getDrawingRect(rr);
scroll.smoothScrollBy(rr.right - (widthScreen - r.left), 0);
}
else if (r.left == 0)
{
Rect rr = new Rect();
v.getDrawingRect(rr);
scroll.smoothScrollBy(rr.right - (widthScreen - r.right), 0);
}
}
});
}
}
}
MainActivity.XML (just buttons in ScrollView)
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/linLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
</LinearLayout>
</HorizontalScrollView>
When You click on the button which is not fully on-screen ScrollView will be scrolled to a proper position to show full button.
Using the code that #iknow posted, I change it to work exactly like the play store with its Chips
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
chipGroup = view.findViewById(R.id.chip_group);
final Chip chip_all = view.findViewById(R.id.chip_all);
final Chip chip_watching = view.findViewById(R.id.chip_watching);
final Chip chip_completed = view.findViewById(R.id.chip_completed);
final Chip chip_onhold = view.findViewById(R.id.chip_onhold);
final Chip chip_dropped = view.findViewById(R.id.chip_dropped);
final Chip chip_plantowatch = view.findViewById(R.id.chip_plantowatch);
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
widthScreen = displayMetrics.widthPixels;
scrollView = view.findViewById(R.id.scroll_view);
chipGroup = view.findViewById(R.id.chip_group_anime);
for (int index = 0; index <= chipGroup.getChildCount() - 1; index++) {
chipGroup.getChildAt(index).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Rect r = new Rect();
view.getGlobalVisibleRect(r);
if(chip_all.isChecked()) {
Rect rr = new Rect();
view.getDrawingRect(rr);
scrollView.smoothScrollBy(rr.right - (widthScreen - r.right), 0);
}
if(chip_watching.isChecked()) {
Rect rr = new Rect();
view.getDrawingRect(rr);
scrollView.smoothScrollBy(rr.right - (widthScreen - r.right), 0);
}
if(chip_completed.isChecked()) {
scrollView.smoothScrollTo(chip_completed.getLeft() - (widthScreen / 2) + (chip_completed.getWidth() / 2), 0);
}
if(chip_onhold.isChecked()) {
scrollView.smoothScrollTo(chip_onhold.getLeft() - (widthScreen / 2) + (chip_onhold.getWidth() / 2), 0);
}
if(chip_dropped.isChecked()) {
Rect rr = new Rect();
view.getDrawingRect(rr);
scrollView.smoothScrollBy(r.right, 0);
}
if(chip_plantowatch.isChecked()) {
Rect rr = new Rect();
view.getDrawingRect(rr);
scrollView.smoothScrollBy(r.right, 0);
}
}
});
}
}
The end product looks like the gif above and again thank you to #iknow who provided the code!
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();
I have seen questions/solutions here for the keyboard not showing up, but none relating to my specific issue. When you click an EditText, the keyboard shows up fine. But, when I animate the EditText first, then when you click the EditText the keyboard will NOT show up.
Any ideas why this could be happening?
In my Activity I first animate my logo, and after it's done animating I create a fade-in animation for the EditTexts. After they finish the animation, I try and click any of them but the keyboard will not show up.
Here is my onCreate method in my Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //Remove title bar
setContentView(R.layout.login);
final EditText emailField = (EditText) findViewById(R.id.emailField);
final EditText passwordField = (EditText) findViewById(R.id.passwordField);
final TextView logInText = (TextView) findViewById(R.id.logInText);
final Button signupButton = (Button) findViewById(R.id.signupButton);
final Button loginButton = (Button) findViewById(R.id.loginButton);
//animation of logo
ImageView img_animation = (ImageView) findViewById(R.id.encoreLogo);
TranslateAnimation animation = new TranslateAnimation(0.0f, 00f,
0.0f, -400.0f);
animation.setDuration(4000);
animation.setFillAfter(true);
animation.setStartOffset(2000);
img_animation.startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
animFadeIn.setDuration(2000);
animFadeIn.setFillAfter(true);
emailField.setAnimation(animFadeIn);
passwordField.setAnimation(animFadeIn);
logInText.setAnimation(animFadeIn);
loginButton.setAnimation(animFadeIn);
signupButton.setAnimation(animFadeIn);
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationStart(Animation animation) {}
});
signupButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, SignUpActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
and here is my corresponding login.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"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/backroundImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:cropToPadding="false"
android:scaleType="centerCrop"
android:src="#drawable/crowdblur1" />
<ImageView
android:id="#+id/encoreLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:maxHeight="#dimen/thumbnail_height"
android:maxWidth="#dimen/thumbnail_width"
android:scaleType="centerInside"
android:src="#drawable/hand72" />
<TextView
android:id="#+id/logInText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/encoreLogo"
android:layout_alignLeft="#+id/passwordField"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_marginBottom="79dp"
android:paddingLeft="#dimen/left_padding_login_text"
android:text="Login to Encore"
android:textColor="#android:color/white"
android:textSize="#dimen/log_in_text"
android:textStyle="bold"
android:visibility="invisible" />
<EditText
android:id="#+id/emailField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/encoreLogo"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:background="#drawable/rounded_corners"
android:ems="10"
android:inputType="textEmailAddress"
android:hint="Email"
android:textColor="#android:color/black"
android:textStyle="italic"
android:visibility="invisible" />
<EditText
android:id="#+id/passwordField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/encoreLogo"
android:layout_centerHorizontal="true"
android:background="#drawable/rounded_corners"
android:ems="10"
android:inputType="textPassword"
android:hint="Password"
android:textStyle="italic"
android:textColor="#android:color/black"
android:visibility="invisible" />
<Button
android:id="#+id/signupButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/loginButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:background="#android:color/transparent"
android:text="#string/signUp"
android:textColor="#5FC2FF"
android:textSize="#dimen/signupText"
android:textStyle="bold"
android:visibility="invisible" />
<Button
android:id="#+id/loginButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/passwordField"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:background="#android:color/transparent"
android:text="#string/login"
android:textColor="#android:color/white"
android:textSize="#dimen/signupText"
android:visibility="invisible" />
Thanks in advance!
So it seems that when you have the visibility set to INVISIBLE on the EditText fields it doesn't want to get focus.
I fixed this issue by changing the visibility on those fields after the animation completes like this:
animFadeIn.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
emailField.setVisibility(View.VISIBLE);
passwordField.setVisibility(View.VISIBLE);
logInText.setVisibility(View.VISIBLE);
loginButton.setVisibility(View.VISIBLE);
signupButton.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
});
Hope this helps you :)
It seems like you want to wait for the image (logo) to finish its animation then start the animations for TextView and EditView. Instead of using setAnimationListener what you can do is set the start offset value of animFadeIn to 4000 miliseconds.
Try this:
TranslateAnimation animation = new TranslateAnimation(0.0f, 00f,
0.0f, -400.0f);
animation.setDuration(4000);
animation.setFillAfter(true);
animation.setStartOffset(2000);
img_animation.startAnimation(animation);
Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
animFadeIn.setDuration(2000);
animFadeIn.setStartOffset(4000);
animFadeIn.setFillAfter(true);
emailField.setAnimation(animFadeIn);
passwordField.setAnimation(animFadeIn);
logInText.setAnimation(animFadeIn);
loginButton.setAnimation(animFadeIn);
signupButton.setAnimation(animFadeIn);
The keyboard opens up fine for me.
Had the same problem. Just don't use fillAfter, it's crap. Combine with #Wextux's solution.
There are different types of Animations,
You are using the wrong type of animation! You want to use ObjectAnimator.
help me solve my problem. There is a main layout RelativeLayout it is Linearlayout where I want to programmatically add view. Here is my code.
public class GenBarCodeActivity extends Activity {
BarCodeView Barview = null;
LinearLayout.LayoutParams layoutParams;
LinearLayout ll;
RelativeLayout rl;
Button getBarCode;
private static final int ID = 34646456;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_barcode);
getBarCode = (Button)findViewById(R.id.btnGetBarCode);
getBarCode.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ll = (LinearLayout)findViewById(R.id.linearCodeView);
EANTEXT = edBarText.getText().toString();
ImageView old = (ImageView) ll.findViewById(ID);
if (old != null) {
((LinearLayout) old.getParent()).removeViewInLayout(old);
}
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.addView(Barview, layoutParams);
rl.invalidate();
}
});
fillLinear();
setBarCode();
}
private void fillLinear(){
rl = (RelativeLayout)findViewById(R.id.layout_barcode);
ll = (LinearLayout)findViewById(R.id.linearCodeView);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageView imBar = new ImageView(this);
Bitmap imbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.barcode);
imBar.setLayoutParams(layoutParams);
imBar.setImageBitmap(imbitmap);
imBar.setId(ID);
ll.setBackgroundColor(Color.TRANSPARENT);
ll.addView(imBar);
}
private void setBarCode(){
Barview = new BarCodeView(GenBarCodeActivity.this);
Barview.setBarCodeNum(0);
}
}
This is my View
public class BarCodeView extends View {
private static int numcode = 0;
private static String codedata = null;
public BarCodeView(Context context) {
super(context);
}
public void setBarCodeText(String text){
this.codedata = text;
}
public String getBarCodeText(){
return this.codedata;
}
public void setBarCodeNum(int num){
this.numcode = num;
}
public int getBarCodeNum(){
return this.numcode;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (numcode) {
// ean 13
case 0:
try{
showEAN13(canvas);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private static void showEAN13(Canvas canvas) throws Exception {
EAN13 barcode = new EAN13();
/*
EAN 13 Valid data char set:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits)
EAN 13 Valid data length: 12 digits only, excluding the last checksum digit
*/
barcode.setData(codedata);
// for EAN13 with supplement data (2 or 5 digits)
/*
barcode.setSupData("12");
// supplement bar height vs bar height ratio
barcode.setSupHeight(0.8f);
// space between barcode and supplement barcode (in pixel)
barcode.setSupSpace(15);
*/
// Unit of Measure, pixel, cm, or inch
barcode.setUom(IBarcode.UOM_PIXEL);
// barcode bar module width (X) in pixel
barcode.setX(2f);
// barcode bar module height (Y) in pixel
barcode.setY(90f);
// barcode image margins
barcode.setLeftMargin(10f);
barcode.setRightMargin(10f);
barcode.setTopMargin(10f);
barcode.setBottomMargin(10f);
// barcode image resolution in dpi
barcode.setResolution(72);
// disply barcode encoding data below the barcode
barcode.setShowText(true);
// barcode encoding data font style
barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 10));
// space between barcode and barcode encoding data
barcode.setTextMargin(6);
barcode.setTextColor(AndroidColor.black);
// barcode bar color and background color in Android device
barcode.setForeColor(AndroidColor.black);
barcode.setBackColor(AndroidColor.white);
/*
specify your barcode drawing area
*/
RectF bounds = new RectF(120, 120, 0, 0);
barcode.drawBarcode(canvas, bounds);
}
}
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_barcode"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0e0a2"
android:orientation="vertical" >
<ImageView
android:id="#+id/closeBarCode"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/txtInfo"
android:layout_marginRight="3dp"
android:src="#drawable/delete" />
<TextView
android:id="#+id/txtInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="3dp"
android:layout_marginTop="3dp"
android:text="#string/barcode_title"
android:textColor="#000"
android:textSize="20dip" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/closeBarCode" >
<TextView
android:id="#+id/txtEAN"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_ean"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spEAN"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtInfo"
android:layout_below="#+id/linearLayout1" >
<TextView
android:id="#+id/txtCodeColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearLayout2"
android:layout_below="#+id/linearLayout2" >
<TextView
android:id="#+id/txtCodeBackColor"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_back_color"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodeBackColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout3" >
<TextView
android:id="#+id/txtCodePosition"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/barcode_position"
android:textColor="#000"
android:textSize="20dip" />
<Spinner
android:id="#+id/spCodePosition"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearCodeView"
android:layout_width="300dip"
android:layout_height="200dip"
android:layout_below="#+id/edBarCode"
android:layout_marginRight="45dp"
android:layout_marginTop="25dip"
android:layout_toLeftOf="#+id/closeBarCode"
android:orientation="vertical" >
</LinearLayout>
<Button
android:id="#+id/btnSetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btnGetBarCode"
android:layout_alignBottom="#+id/btnGetBarCode"
android:layout_toLeftOf="#+id/closeBarCode"
android:text="#string/set_code" />
<Button
android:id="#+id/btnGetBarCode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="31dp"
android:text="#string/get_code" />
<EditText
android:id="#+id/edBarCode"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/linearCodeView"
android:layout_alignRight="#+id/linearCodeView"
android:layout_below="#+id/txtCodeText"
android:layout_marginTop="23dp"
android:ems="10" />
<TextView
android:id="#+id/txtCodeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/edBarCode"
android:layout_below="#+id/txtInfo"
android:layout_marginRight="86dp"
android:text="#string/code_text"
android:textColor="#000"
android:textSize="20dip" />
</RelativeLayout>
I need to Linearlayout "#+id/linearCodeView" insert my BarCodeView.
View is not visible after adding LinearLayout.
RelativeLayout.LayoutParams paramsBottom = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(ll, paramsBottom); //add linearlayout in relativelayout
Edit:-
LinearLayout.LayoutParams paramsBottom2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ll.addView(Barview, paramsBottom2);//add Barview in linearlayout
OR
RelativeLayout.LayoutParams paramsBottom2 = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rl.addView(Barview, paramsBottom2);//add Barview in relativelayout
I am implementing the listview with one image and framelayout(conatining button and linearLayout) , I want to animate the linearLayout on click of image. Is it possible? I have written following code.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/icon" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
<LinearLayout
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/a"
android:orientation="vertical" >
<TextView
android:id="#+id/from_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
<TextView
android:id="#+id/from_user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000" />
</LinearLayout>
</FrameLayout>
Please help me.
You can call animate method in onClickListener of imageview like..
ivmg.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
startLayoutAnimation(myLayout);
return false;
}
});
And define method as...
public void startLayoutAnimation(LinearLayout mLayout)
{
System.out.println("Inside startAnimation()");
Animation scaleAnim = new ScaleAnimation(0, 2, 0, 2);
scaleAnim.setDuration(5000);
scaleAnim.setRepeatCount(1);
scaleAnim.setInterpolator(new AccelerateInterpolator());
scaleAnim.setRepeatMode(Animation.REVERSE);
/* Animation rotateAnim = new RotateAnimation(0, 360);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(1);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);*/
Animation rotateAnim = new RotateAnimation(0, 360, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF);
rotateAnim.setDuration(5000);
rotateAnim.setRepeatCount(1);
rotateAnim.setInterpolator(new AccelerateInterpolator());
rotateAnim.setRepeatMode(Animation.REVERSE);
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(scaleAnim);
animationSet.addAnimation(rotateAnim);
mLayout.startAnimation(animationSet);
}
}
I haven't tried animating layout yet. So you will have to check whether its working or not. Hope it helps