Android: How to dynamically create a Dialog container with an imageview inside - android

I am a hybrid apps developer and new to android. I am trying to fix an android related issue for my hybrid app.
I have the following two methods on my activity: onStart and onPause.
When the app starts I need it to start as usual. When the app is on pause, I need to show an image (my companyLogo). If I set it with setContentView(R.layout.activity_base), the image is shown when the app starts again. Is there a way to dynamically create an image, and show it when app is on pause? Also, how do I remove the image once it starts? Since I am not hands on in Android, I am not sure how to go about it.
I suppose I need to create some sort of a Dialog and set an image view inside it.
Also, I am not sure, how to remove this image, when the app Starts again.
My code snippet:
public void onStart() {
Toast.makeText(getApplicationContext(), "App is in foreground", Toast.LENGTH_LONG).show();
}
public void onPause() {
Toast.makeText(getApplicationContext(), "App is in background", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_base);
}

This code is im run it. i think u need this.
inside ur activity
ImageView img_view;
TextView txt_show;
int images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);
img_view = (ImageView) findViewById(R.id.img_show);
txt_show = (TextView) findViewById(R.id.txt_my);
}
private void animate(final ImageView imageView, final int images[], final int imageIndex, final boolean forever) {
int fadeInDuration = 1000;
int timeBetween = 5000;
int fadeOutDuration = 1000;
imageView.setVisibility(View.INVISIBLE);
imageView.setImageResource(images[imageIndex]);
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.setDuration(fadeInDuration);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(fadeOutDuration);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
imageView.setAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
if (images.length - 1 > imageIndex) {
animate(imageView, images, imageIndex + 1, forever);
} else {
if (forever) {
animate(imageView, images, 0, forever);
}
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
#Override
protected void onPause() {
super.onPause();
txt_show.setVisibility(View.GONE);
img_view.setVisibility(View.VISIBLE);
animate(img_view, images, 0, false);
}
#Override
protected void onResume() {
super.onResume();
img_view.setVisibility(View.GONE);
txt_show.setVisibility(View.VISIBLE);
}
inside ur xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txt_my"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_gravity="center"
android:gravity="center"
android:text="buvan"
android:textColor="#color/colorPrimary" />
<ImageView
android:id="#+id/img_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

onResume is called of minimize from cme to screen.
try this one
#Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_main);
}

The feature you want will not work without FLAG_SECURE, because when you send the app in the background, the view you are viewing, is an OS-handled view and only OS has the permission to update the view. For that reason, you have to call FLAG_SECURE to notify OS that this view needs to be secured. Otherwise, changing views would have worked.
I think you got the idea.

Related

How do I change Imageresource quickly?

I want to make it so that once I press a button, it moves and does an animation by quickly swapping the src of the imageview mid animation.
I've tried to use Thread.sleep .
first.setImageResource(R.drawable.cata2);
first.setImageResource(R.drawable.catb2);
ObjectAnimation Code
x += 10
first.setImageResource(R.drawable.cata2);
ObjectAnimator d = ObjectAnimator.ofFloat(first, "translationX", x);
d.setDuration(50);
d.start();
It always just switches to cata2.
Try this one. If the changing image resource not working, try to change it in background resource. Just adjust some duration and the thread sleep.
in C#:
Try to put this all inside the listener of your button
Setting up the Animation
var clockwise = AnimationUtils.LoadAnimation(this.Activity, Resource.Animation.clockwise);
clockwise.Duration = 2000;
To set your first to cata2
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.cata2);
});
To change with animation from cata2 to catb2
Thread t = new Thread(delegate ()
{
first.StartAnimation(clockwise);
Thread.Sleep(500);
this.Activity.RunOnUiThread(() =>
{
first.setImageResource(R.drawable.catb2);
first.ClearAnimation();
return;
});
});
t.Start();
We can add a listeter to your animation
d.addListener(this);
Then generate overridess
#Override
public void onAnimationStart(Animator animation){
//add some codes
}
#Override
public void onAnimationEnd(Animator animation){
//add some codes
}
#Override
public void onAnimationRepeat(Animator animation){
//add some codes
}
#Override
public void onAnimationCancel(Animator animation){
}

2 images that swap when I tap on either one

What the title says. At first, I created a method to swap from one image to the other one, and it works. When I add the 2nd method though, to reverse the action and swap back to the first one, nothing changes. It does not even revert from the first image to the second one. Below is the code:
public void fade(View view) {
ImageView laxus = (ImageView) findViewById(R.id.laxus_shirt);
ImageView laxos2 = (ImageView) findViewById(R.id.laxus2);
laxus.animate().alpha(0f).setDuration(1000);
laxos2.animate().alpha(1f).setDuration(1000);
}
public void fadetoblack(View view) {
ImageView laxusius = (ImageView) findViewById(R.id.laxus_shirt);
ImageView laxosios2 = (ImageView) findViewById(R.id.laxus2);
laxosios2.animate().alpha(0f).setDuration(1000);
laxusius.animate().alpha(1f).setDuration(1000);
}
Thank you in advance.
You need to call start() to trigger the animation.
laxus.animate().alpha(0f).setDuration(1000).start();
laxos2.animate().alpha(1f).setDuration(1000).start();
I would suggest you to use Util method to perform crossfading. Something like below
public static void crossFade(View incomingView, View outGoingView, int outGoingViewVisibility) {
outGoingView.setAlpha(1);
ViewCompat.animate(outGoingView).alpha(0).setListener(new ViewPropertyAnimatorListener() {
#Override
public void onAnimationStart(View view) {
}
#Override
public void onAnimationEnd(View view) {
view.setVisibility(outGoingViewVisibility);
}
#Override
public void onAnimationCancel(View view) {
}
}).start();
incomingView.setVisibility(View.VISIBLE);
incomingView.setAlpha(0);
ViewCompat.animate(incomingView).setListener(null).alpha(1).start();
}
Maybe it will be an option to use ViewSwitcher.
Add a viewSwitcher with 2 ImageViews.
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/laxus"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="#+id/laxus2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ViewSwitcher>`
On Activity onCreate add code
ViewSwitcher viewSwitcher=(ViewSwitcher)findViewById(R.id. switcher); // initiate a ViewSwitcher
Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); // load in animation
Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); // load out animation
viewSwitcher.setInAnimation(in); // set in Animation for ViewSwitcher
viewSwitcher.setOutAnimation(out); // set out Animation for ViewSwitcher
When you want to switch between ImageViews call method viewSwitcher.showNext(); or viewSwitcher.showPrevious();
To verify which view is displayed use viewSwitcher.getCurrentView();

OverridePendingTransition is not working

I've seen other threads with this issue, but none of them has worked for me.
I have an Activity A, which calls Activity B. I'm doing a fade animation programmatically both entry and exit, and a shared FAB is translated to its new position. This translation works, but the fade does not.
Code of Activity A
profilePic.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Activity A starting Activity B
Activity activity = MainScreenUI.this;
Intent intent = new Intent(MainScreenUI.this, ProfileUI.class);
int[] imageScreenLocation = new int[2];
profilePic.getLocationInWindow(imageScreenLocation);
intent.putExtra(Properties.PACKAGE + ".left", imageScreenLocation[0])
.putExtra(Properties.PACKAGE + ".top", imageScreenLocation[1])
.putExtra(Properties.PACKAGE + ".width", profilePic.getWidth())
.putExtra(Properties.PACKAGE + ".height", profilePic.getHeight());
startActivity(intent);
activity.overridePendingTransition(0, 0);
}
});
Code of Activity B
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
...
mTopLevelLayout = (CoordinatorLayout)findViewById(R.id.profile_coordinator);
mBackground = new ColorDrawable(Color.WHITE);
mTopLevelLayout.setBackground(mBackground);
if (savedInstanceState == null)
{
final ViewTreeObserver observer = mProfileFAB.getViewTreeObserver();
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
{
#Override
public boolean onPreDraw()
{
mProfileFAB.getViewTreeObserver().removeOnPreDrawListener(this);
...
_runEnterAnimation();
return true;
}
});
}
}
private void _runEnterAnimation()
{
mProfileFAB.setPivotX(0);
mProfileFAB.setPivotY(0);
mProfileFAB.setScaleX(mWidthScaleImage);
mProfileFAB.setScaleY(mHeightScaleImage);
mProfileFAB.setTranslationX(mLeftDeltaImage);
mProfileFAB.setTranslationY(mTopDeltaImage);
mProfileFAB.animate()
.setDuration(ANIM_DURATION)
.scaleX(1).scaleY(1)
.translationX(0).translationY(0)
.setInterpolator(ACCELERATE_DECELERATE_INTERPOLATOR);
// This animation is not applied
ObjectAnimator bgAnim = ObjectAnimator.ofInt(mBackground, "alpha", 0, 255);
bgAnim.setDuration(ANIM_DURATION);
bgAnim.start();
}
#Override
public void onBackPressed()
{
_runExitAnimation(new Runnable() {
public void run() {
finish();
}
});
}
#Override
public void finish()
{
super.finish();
overridePendingTransition(0, 0);
}
private void _runExitAnimation(final Runnable endAction)
{
int[] currentLocation = new int[2];
mProfileFAB.getLocationOnScreen(currentLocation);
mTopDeltaImage = mThumbnailTop - currentLocation[1];
mProfileFAB.animate()
.setDuration(ANIM_DURATION)
.setStartDelay(0)
.scaleX(mWidthScaleImage).scaleY(mHeightScaleImage)
.translationX(mLeftDeltaImage).translationY(mTopDeltaImage)
.withEndAction(endAction);
// This animation is not working either
ObjectAnimator bgAnim = ObjectAnimator.ofInt(mBackground, "alpha", 0);
bgAnim.setDuration(ANIM_DURATION);
bgAnim.start();
}
XML of Activity B
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/profile_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activities.ProfileUI"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:id="#+id/profile_appbar_layout"
android:layout_width="match_parent"
android:layout_height="192dp"
android:background="#drawable/filter_bottom_border">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/profile_collapsing_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="?attr/colorPrimary">
...
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#android:color/white"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
...
</android.support.v4.widget.NestedScrollView>
<com.wallakoala.wallakoala.Views.FloatingActionImageView
android:id="#+id/profile_floating_pic"
android:layout_width="84dp"
android:layout_height="84dp"
android:src="#drawable/female_icon"
app:borderWidth="0dp"
app:layout_anchor="#id/profile_appbar_layout"
app:layout_anchorGravity="bottom|center"/>
</android.support.design.widget.CoordinatorLayout>
The strange thing is that this logic is applied in other places and it's working fine, I don't know what I've messed here.
Thanks in advance,
Provide exit and enter animations inside overridePendingTransition() method in your activity A> onclick method. There you provided overridePendingTransition(0,0).
You should rather use overridePendingTransition() in the onCreate() method of the Activity that you are trying to open than after raising the intent for the same.
If you kept the parameters 0 just to hide your code in the question its fine, but if not, let me tell you that it expects animation resource files as the input parameters. You can probably use the fade animations available in the support library as:
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
It takes in the first parameter as the enter animation of the Activity being opened, the second parameter for the exit animation of the previous one.

How to make my ImageView responsive to onClickevents while .startAnimation() is running?

I'm new to android, and I'm facing a problem where I'm trying to make my ImageView, which is playing an animation through startAnimation(), to become Invisible as the user clicks on it. what happens however is that only after the animation is done the Imageview becomes invisible. my assumptions was it's because they both run on the UI thread. surprisingly however, when I changed my event handling from setVisibility to startAnimation() it actually listened to the click, interrupted the animation and restarted it!
To override my problem, which did turn out nice, I made a new Animation object that opposite to the first, which shows the Imageview, hides the Imageview and it worked perfectly, but I'm still curious as to why my Imageview was selectively responsive to different event handling?
This code change visibility only after the animation is done
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
face.setVisibility(View.INVISIBLE);
}
});
}
this code changes the animation "while" the original is playing
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
face = (ImageView)findViewById(R.id.face);
final Animation showAnimation=AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation);
face.startAnimation(showAnimation);
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation hideAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.animation1);
face.startAnimation(hideAnimation);
}
});
}
You should not call face.setVisibility(...), instaed tt should be:
face.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setVisibility(View.INVISIBLE);
}
});

How to provide visual feedback when user is tapping ImageView?

I have a view in the UI that is technically an extended ImageView inside a RelativeLayout that I'd rather not change, if possible. I want to show a Drawable from resources (a small PNG with a gradient or a frame) at specific coordinates as a visual feedback when user is tapping the screen (there even is a onShowPress callback). What is the simplest way to do this? I don't want to modify the layout.xml from which UI is inflated, and I'm not too enthusiastic about overriding onDraw to do extra job there.
This is one easy way to do it using view animation class:
final ImageView aboutButton = (ImageView) findViewById(R.id.AboutButton);
aboutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation myAnim2 = AnimationUtils.loadAnimation(LaunchActivity.this, R.anim.buttons);
aboutButton.startAnimation(myAnim2);
myAnim2.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//do on click after animation ends
Intent intent = new Intent(LaunchActivity.this, MyDialog.class);
startActivity(intent);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
//change the backround of the button etc on click etc...
}
});
}});

Categories

Resources