Fade in/out Android ActionBar - java

I'm using the AppCompat ActionBar (Theme.AppCompat.Light.DarkActionBar) for my Android app.
After starting the app, the ActionBar fades out after 6 seconds (onCreate method):
h.postDelayed(new Runnable() {
#Override
public void run() {
getSupportActionBar().hide();
}
}, 6000);
So, how can I fade in the ActionBar with a touch gesture from the top of the screen to the middle ... and fade out it again after 10 seconds?
I think the problem is, that the ActionBar has no OnTouchListener.

Try something like this.
In the class you want to use the gesture:
private GestureDetectorCompat gestureDetectorCompat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetectorCompat = new GestureDetectorCompat(this, new MyGestureListener());
//Stuff in onCreate()
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetectorCompat.onTouchEvent(event);
return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
#Override
public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
//Swipe from the top to the bottom
if(event2.getY() > event1.getY()){
getSupportActionBar().show();
h.postDelayed(new Runnable() {
#Override
public void run() {
getSupportActionBar().hide();
}
}, 6000);
}
return true;
}
}

Related

How can I move the Splash screen with touch

My splash screen diseapear after 2 seconds but I want it to diseapear also after a screen touch
Like if I press screen I moved next page without waiting until the end of the timer.
Is it possible??
public class SplashActivity extends Activity {
Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashfile);
handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
}
},2000);
}
}
Tes it is possible, you can add a touch listener to your layout that do the same as what will happen after two seconds , some thing like that
findViewById(R.id.myLayout).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return true;
}
});
EDIT :
or you can instead add the next code outside onCeate method if the layout don't receive the touch event:
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
startActivity(new Intent(getApplicationContext(),LoginActivity.class));
finish();
return super.dispatchTouchEvent(ev);
}

Android Java Implement KEYCODE_VOLUME_UP and DOWN to my code

I wonder how to implement KEYCODE_VOLUME_UP and DOWN to my code. So every time when I press the volume up or down key will simultaneously reflect on my Notification Volume Control app.
The method I want to implement is as follows which will make my own app volume up and down when the physical volume up and down button is pressed:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
return true;
default:
return false;
}
}
The code I want to implement to is:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
run();
}
public void run()
{
mgr=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
final LayoutInflater inflater =
(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
final View ViewLayout = inflater.inflate(R.layout.activity_main,
(ViewGroup)findViewById(R.id.activity_main));
alarm=(SeekBar)ViewLayout.findViewById(R.id.alarm);
music=(SeekBar)ViewLayout.findViewById(R.id.music);
ring=(SeekBar)ViewLayout.findViewById(R.id.ring);
system=(SeekBar)ViewLayout.findViewById(R.id.system);
voice=(SeekBar)ViewLayout.findViewById(R.id.voice);
alarmVol=(TextView)ViewLayout.findViewById(R.id.alarmVol);
musicVol=(TextView)ViewLayout.findViewById(R.id.musicVol);
ringVol=(TextView)ViewLayout.findViewById(R.id.ringVol);
systemVol=(TextView)ViewLayout.findViewById(R.id.systemVol);
voiceVol=(TextView)ViewLayout.findViewById(R.id.voiceVol);
alertDialogBuilder = new AlertDialog.Builder(this);
//alertDialogBuilder.setIcon(R.mipmap.ic_launcher);
//alertDialogBuilder.setTitle("Volume Control");
alertDialogBuilder.setView(ViewLayout);
initBar(alarm, AudioManager.STREAM_ALARM, alarmVol);
initBar(music, AudioManager.STREAM_MUSIC, musicVol);
initBar(ring, AudioManager.STREAM_RING, ringVol);
initBar(system, AudioManager.STREAM_SYSTEM, systemVol);
initBar(voice, AudioManager.STREAM_VOICE_CALL, voiceVol);
alertDialogBuilder.create();
alertDialogBuilder.show();
}
private void initBar(SeekBar bar, final int stream, final TextView textView)
{
final float maxVolume=mgr.getStreamMaxVolume(stream);
float curVol = mgr.getStreamVolume((stream));
bar.setMax((int)maxVolume);
bar.setProgress((int)curVol);
bar.setKeyProgressIncrement(1);
textView.setText(String.valueOf((int)(curVol/maxVolume*100)));
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress,
boolean fromUser) {
mgr.setStreamVolume(stream, progress,
AudioManager.FLAG_PLAY_SOUND);
textView.setText(String.valueOf((int)
((progress/maxVolume)*100)));
}
public void onStartTrackingTouch(SeekBar bar) {
}
public void onStopTrackingTouch(SeekBar bar) {
}
});
}
}

Remove a FAB when a Fragment goes to backstack

I have seen a lot of answers but doesn't seems to find one. I am using new FAB in one of my fragments and want to remove it when that particular fragment goes to backstack, but I am not sure which method in fragment gets called when it is added to back stack and replaced by other fragment.
Following methods were called whenever the Fragment is replaced or added to backstack
1) onPause()
2) onStop()
3) onDestroyView()
call your FAB removing method in any one of the above methods in your Fragment.
http://developer.android.com/guide/components/fragments.html#Creating
Here is my suggestion -
First, add the animation code and backstack listener in your Activity:
public class MainActivity extends AppCompatActivity
implements FragmentManager.OnBackStackChangedListener {
private FloatingActionButton mFab;
private Animation mShowFab;
private Animation mHideFab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().addOnBackStackChangedListener(this);
mShowFab = AnimationUtils.makeInAnimation(this, false);
mShowFab.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
mFab.setVisibility(View.VISIBLE);
}
});
mHideFab = AnimationUtils.makeOutAnimation(this, true);
mHideFab.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
mFab.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
}
public void showFab(boolean show) {
boolean visible = mFab.isShown();
if (show) {
if (!visible)
mFab.startAnimation(mShowFab);
} else {
if (visible)
mFab.startAnimation(mHideFab);
}
}
And then - depending on backstack depth - show or hide the FAB:
#Override
public void onBackStackChanged() {
showFab(getSupportFragmentManager().getBackStackEntryCount() > 0);
}

Android image fade animation

When I'd ran this code, it did nothing, I'm sure that this event is called when I touch the button. but It doesn't change the opacity of imageView.
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f);
fadeAltAnim.start();
}
};
findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);
Is there anything wrong with my code?
Try this code, its using ViewPropertyAnimator :
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.animate().alpha(0.2f).setDuration(1000);
}
};
Its always good to set a duration, so the Animation knows how long its supposed to run.
EDIT : You might want to attach it to a MotionEvent if you are using onTouchListener , something like :
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if(motionEvent == MotionEvent.ACTION_DOWN)
view.animate().alpha(0.2f).setDuration(1000);
}
};
EDIT 2 :
If you want to use a Button its best to use an OnClickListener instead of onTouchListener, if you want to attach an Image you have to initiate it(for example in an ImageView) :
Button button = (Button) findViewById(R.id.your_button);
ImageView imageView = (ImageView) findViewById(R.id.imgTest);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView.animate().alpha(0.2f).setDuration(1000);
}
};
It's because you're passing in an id when the method requires the View. Try passing in the View.
So basically change ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f); to ObjectAnimator.ofFloat(view, "alpha", 0.2f);
Here's an example
findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(v, "alpha", 0.2f);
fadeAltAnim.start();
}
});
Is there any reason you're using ObjectAnimator? Can you do:
AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);
**try this**
public class Animationtest extends Activity {
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
imageView = (ImageView)findViewById(R.id.text);
setAnimation();
}
private void setAnimation(){
imageView.setOnTouchListener(new OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "touch", Toast.LENGTH_LONG).show();
ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(imageView, "alpha", 0.2f);
fadeAltAnim.start();
return false;
}
});
}
}

Android Hide Sliding Drawer After Some Time

I have a slidingdrawer in my activity, i open sliding drawer on swipe gesture from bottom to top what i want is to close that opened drawer after some second or delay let say after 5 seconds. How can i do that?
private class GestureDetector extends SimpleOnGestureListener {
SlidingDrawer drawer;
Timer timer;
public GestureDetector(SlidingDrawer drawer) {
this.drawer = drawer;
timer = new Timer();
}
TimerTask timerTask = new TimerTask() {
#Override
public void run() {
closeSlider();
}
};
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getY() > e2.getY()) {
this.drawer.animateOpen();
timer.scheduleAtFixedRate( timerTask , 0, 3000);
} else {
//this.drawer.animateClose();
}
}
public void closeSlider() {
this.drawer.animateClose();
}
}
//i call it on activity onCreate()
SlidingDrawer slidingDrawer = (SlidingDrawer) findViewById(R.id.slidingDrawer);
new GestureDetector(slidingDrawer);
Thanking you
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO call closeSlider method here.
}
}, 5000);

Categories

Resources