I have different screen to work in an android application.
I'm using ViewFlipper for this.
I decided to used different class for different view children
public main extends Activity{
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
HomeScreen s = new HomeScreen(getApplicationContext(), getCurrentFocus(), viewFlipper);
}
}
and this the Homescreen class is :-
public class HomeScreen {
private Button signIn;
private Button createAccount;
private View v;
private Context context;
private ViewFlipper viewflipper;
public HomeScreen(Context context,View v,ViewFlipper viewflipper ) {
this.v=v;
this.context = context;
this.viewflipper = viewflipper;
signIn = (Button) v.findViewById(R.id.button_sign_in_homeScreen);
createAccount = (Button)v.findViewById(R.id.button_createAccount_homeScreen);
signIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
viewflipper.setDisplayedChild(1);
}
});
}
but is shows run exception
java.lang.RuntimeException: Unable to start activity ComponentInfo
Can anyone please help me
is getCurrentFocus() is the correct way to get the view?
What i try to implement is
I need to use different class for defining, listening the controls of each child of view flipper
In the above example HomeScreen is one of my the child screen of view flipper
But the line v.findViewById is showing error i think that getCurrentFocus() is not the correct way to sent the view
I don't know weather i'm moving in the correct way? When i define and listen all the controls of all children of viewflipper in the Class where i define that viewflpper, that class become very big. That made me to think so..
Thanks...
U can perfrom animation using Intent to:
Step1: create anim folder under res directory in ur project.
Step2: create an slideleft.xml file
Step3: type the following code in that file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="400" />
</set>
step 4: similarly create slideright.xml
step5: use the above code, but change the following
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="400" />
step 6:
target.startAnimation(AnimationUtils.loadAnimation(HomeScreen.this, R.anim.slide_left));
perfroming fadein operation, just add the following code in fadein.xml file
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="300" />
similarly for fade out too
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="300" />
Related
I'm showing issues with animation. Animation works fine when i clicked on button to show linear layout when i clicked buttton to close linear layout animation perform as i want but when i click second time to open linear layout animation doesn't work for both to show layout or close layout. I also want to inform you that button click perform properly. layout visibility GONE & VISIBLE works proper but animation didn't work second time.
public class AdminViewComplaintActivity extends AppCompatActivity implements View.OnClickListener {
ImageView btn_search, btn_close_search ;
LinearLayout ll_search;
Animation animationIn, animationOut;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_complaint);
btn_search = findViewById(R.id.btn_search);
btn_close_search = findViewById(R.id.btn_close_search);
ll_search = findViewById(R.id.ll_search);
animationIn= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.view_in);
animationOut= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.view_out);
}
#Override
public void onClick(View view) {
if (view==btn_search){
if(ll_search.getVisibility()==View.GONE){
// show linear layout with animation
ll_search.setAnimation(animationIn);
ll_search.setVisibility(View.VISIBLE);
}
}
else if(view==btn_close_search){
// close linear layout with animation
ll_search.setAnimation(animationOut);
ll_search.setVisibility(View.GONE);
}
}
}
xml for animation view_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="1000"
android:fromXDelta="100%"
android:toXDelta="0%" />
</set>
xml for animation view_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<translate
android:duration="1000"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
add this code to view
anim.setFillEnabled(true);
anim.setFillAfter(true);
in programmatically use this :
Animation animation = AnimationUtils.loadAnimation(context, android.R.anim.nameYourAnim);
viewToAnimate.startAnimation(animation);
for example anim :
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-50%p" android:toXDelta="0"
android:duration="#android:integer/config_mediumAnimTime"/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="#android:integer/config_mediumAnimTime" />
I'm new to android studio and I want to animate an imageButton with a sequential animation set. The animation set (animation_boutons.xml)is in res/anim.
I've tried with animationSet in java but the app crashed every time I launched the emulator.
I've spent a long time looking for a solution. I hope someone can help me !
I apologize if it's something obvious.
java code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
configureCodeurBouton();
}
private void configureCodeurBouton() {
ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
animBoutons.setRepeatCount(Animation.INFINITE);
boutonCodeur.setAnimation(animBoutons);
boutonCodeur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, codeur.class));
}
});
}
}
xml code:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:fillAfter="true">
<rotate
android:fromDegrees="0"
android:toDegrees="20"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="1000"
/>
<rotate
android:startOffset="1000"
android:fromDegrees="20"
android:toDegrees="-20"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
/>
<rotate
android:fromDegrees="-20"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="3000"
android:duration="1000"
/>
</set>
Also, Vedprakash Wagh give me the advice to try animBoutons.setRepeatCount(Animation.INFINITE) but it has no effect).
Your app is crashing every time because you're trying to find your ImageButton when the class is created first, and not after the layout is set.
You're getting NullPointerException, as there is no ImageButton with id R.id.boutoncodeur in your View hierarchy when you're trying to find it.
You need to find your ImageView AFTER it is available in your View hierarchy i.e. after your setContentView();
You can either do this:
Remove your second line
ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
as you've already found your ImageView in your configureCodeurButton() function.
Or, you can keep one class variable of ImageView, and make findViewById call after setContentView like below.
public class MainActivity extends AppCompatActivity {
ImageButton boutonCodeur;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boutonCodeur = findViewById(R.id.boutoncodeur);
configureCodeurBouton();
}
private void configureCodeurBouton() {
Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
boutonCodeur.setAnimation(animBoutons);
boutonCodeur.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, codeur.class));
}
});
}
}
You can learn more about NullPointerException here. Also, learn how to read the errors from tutorials that are available. Or, simply open the logcat tab in Android Studio when the error occurs to know what Error you're getting.
To make your animation run infinitely, you can add this in your code.
animation.setRepeatCount(Animation.INFINITE)
I just had to change the whole xml anim_boutons file so I have only one animation and not three rotate animations. the repeatMode line says to repeat the animation backwards at each repeat. This gives the expected effect.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true"
android:fillAfter="true">
<rotate
android:fromDegrees="-20"
android:toDegrees="20"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
</set>
I need to rotate the drawable resource of ImageButton. I've succeeded rotating functionality to my Button but rotating functionality affect whole button. All I want to do is rotate only drawable inside ImageButton.
How to handle this situation ?
PS: I accessed drawable inside ImageButton but I wasn't able give any animation functionality.
Thanks in helpings
Here my ImageButton xml;
<ImageButton
android:id="#+id/button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:layout_marginTop="-40dp"
android:background="#color/titlebackground_color"
android:src="#drawable/open" />
Rotate first xml;
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<rotate
android:fromDegrees="-180"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:startOffset="0"
/>
</set>
Rotate Second xml;
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator">
<rotate
android:fromDegrees="-180"
android:toDegrees="-0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:startOffset="0"
/>
</set>
Animation functionality;
public class LayerInfoFragment extends Fragment {
int count = 0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.layer_info_main, container, false);
btnClose = (ImageButton) v.findViewById(R.id.button);
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation rotate = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_first);
Animation rotatex = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_last);
if (count % 2 == 0) {
v.setRotation(180);
v.setAnimation(rotate);
} else {
v.setRotation(0);
v.setAnimation(rotatex);
}
count++;
}
});
}
}
There are 2 things to try I think.
1) Add code v.startAnimation(rotate); after v.setAnimation(). I suspect your animation never got started. I was thinking before that android:startOffset in the settings would trigger a start but that is not clear to me.
2) Try RotateAnimation, a direct subclass of Animation, instead of Animation objects. It seems many used RotateAnimation more than anything else. Besides that, the other issue I suspect is in the layout file.
Example:
RotateAnimation rotate = (RotateAnimation) AnimationUtils.loadAnimation...
Tell us what happens.
Hi i have an image view position and the bottom of the page i'm wanting to animate so it moves down does anyone know how to do this? currently when i run it nothing happens
here is what i have tried so far
heres my animation
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator">
<translate android:fromYDelta="0" android:toXDelta="30" android:duration="1000"
android:fillAfter="true"/>
</set>
heres my java
public class IntialSetup extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_initialsetup);
animations();
}
public void animations(){
final ImageView image = (ImageView)findViewById(R.id.su_shirts);
Animation AnimationMovepos = AnimationUtils.loadAnimation(this, R.anim.shirt_anim);
image.startAnimation(AnimationMovepos);
}
}
Look at your animation:
<translate android:fromYDelta="0" android:toXDelta="30" .../>
fromYDelta ... toYDelta and not toXDelta.
Hope this was the problem.
I still try different basics with android, and now I'm stuck with animation. I'm trying to implement a simple animation. I've defined animation in xml file like this:
alpha android:interpolator="#android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" duration="3000" repeatCount="infinite"
In my main view group I have an ImageView defined like this:
<ImageView android:id="#+id/someb" android:src="#drawable/earth_normal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip"/>
And this is from my starting activity class:
public class Ohayou extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView earth = (ImageView)findViewById(R.id.someb);
Animation earthFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
earth.startAnimation(earthFadeInAnimation);
}
It finds ImageView successfuly and creates animation. but when I start emulator ImageView just shows the original src image, not an animation. What am I doing wrong?
Thanks
I see you have a correct code but I have doubt in your \anim\fade_in.xml, so try this
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:interpolator="#android:anim/accelerate_interpolator"
android:duration="3000" android:repeatCount="infinite"/>
</set>