Android threading order / ontouch webview - java

I have a GridView fill with images, when I click on one image, it is displayed in full screen in a PagerActivity and for each images in full screen a piece of html is displayed at the bottom in a webview. On touch some webViews launch a video and some do nothing.
My problem is if I touch a "webView video", the video is displayed in the next layout.
Example : I have 3 images in my Grid. I clicked on the first, this image is displayed in full screen with a webview at the bottom and when I clicked on the webview in order to launch the video on top of the image 1 the video is launched on top of the image 2.
Here a part of my code, ImagePagerActivity :
public class ImagePagerActivity extends BaseActivity {
...
private class ImagePagerAdapter extends PagerAdapter implements OnTouchListener,Handler.Callback {
...
public Object instantiateItem(View view, final int position) {
final View imageLayout = inflater.inflate(R.layout.item_pager_image, null);
final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
final WebView webView = (WebView) imageLayout.findViewById(R.id.webView1);
final TextView textView=(TextView) imageLayout.findViewById(R.id.edit_message);
topLevelLayout = (RelativeLayout) imageLayout.findViewById(R.id.top_layout);
videoView = (VideoView) imageLayout.findViewById(R.id.videoView);
topLevelLayout.setBackgroundColor(0x00000000);
((ViewPager) view).addView(imageLayout, 0);
imageLoader.displayImage(images[position], imageView, options, new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted() {
...
}
#Override
public void onLoadingFailed(FailReason failReason) {
...
}
#Override
public void onLoadingComplete(Bitmap loadedImage) {
// HERE ADD THE WEBVIEW ON THE BOTTOM OF THE IMAGE
}
});
return imageLayout;
}
public boolean onTouch(View v, MotionEvent event) {
if(isWebViewLaunchViedo()){
PlayVideo(positionVideo);
}
}
}
I use this project for the GridActivity and PagerActivity Universal image loader
I understand the instantiateItem(..) method is called twice, so that's why when I try to play a video the videoView is already affect to the new one. But I don't know How can I fix this issue?

I finally create a new transparent activity and play the video in the new activity.
Manifest :
<activity
android:name="com.ad.AdVideoActivity"
android:theme="#style/Theme.Transparent" <------
android:label="#string/title_activity_ad_video" >
</activity>
New Ontouch :
public boolean onTouch(View v, MotionEvent event) {
if (ADS_VIDEO.contains(v.getId()) && event.getAction() == MotionEvent.ACTION_DOWN) {
intentVideo = new Intent(getBaseContext(),AdVideoActivity.class);
startActivity(intentVideo);
}
return false;
}

Related

How to hide the navigation bar when entering fullview in webview?

I have found this code to enter in full screen when playing a video in webview, but there is a problem in it.
For example if I am in YouTube all works properly at the first time (when I play the video), but when I click to change the settings of the video like the quality or the speed, the navigation bar appears and still there until I exit the video.
}
public class CustomWebClient extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout frame;
// Initially mOriginalOrientation is set to Landscape
private int mOriginalOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
private int mOriginalSystemUiVisibility;
// Constructor for CustomWebClient
public CustomWebClient() {}
public Bitmap getDefaultVideoPoster() {
if (MainActivity.this == null) {
return null; }
return BitmapFactory.decodeResource(MainActivity.this.getApplicationContext().getResources(), 2130837573); }
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback viewCallback) {
if (this.mCustomView != null) {
onHideCustomView();
return; }
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = MainActivity.this.getWindow().getDecorView().getSystemUiVisibility();
// When CustomView is shown screen orientation changes to mOriginalOrientation (Landscape).
MainActivity.this.setRequestedOrientation(this.mOriginalOrientation);
//
//here is the code I'm using to hide status bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// After that mOriginalOrientation is set to portrait.
this.mOriginalOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
this.mCustomViewCallback = viewCallback; ((FrameLayout)MainActivity.this.getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1)); MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(3846);
//
//here is the code I'm using to hide navigation bar
getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//
}
public void onHideCustomView() {
((FrameLayout)MainActivity.this.getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
MainActivity.this.getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
// When CustomView is hidden, screen orientation is set to mOriginalOrientation (portrait).
MainActivity.this.setRequestedOrientation(this.mOriginalOrientation);
//////////////
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_IMMERSIVE | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//////////////
// After that mOriginalOrientation is set to landscape.
this.mOriginalOrientation = android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
}
{
Here is some pictures of the problem:

favorite button in android not working properly

Hii i'm making an app where i have small heart image onclick of that image i've set an onclick listener so when i click on that image it is replaced by another red coloured heart image.
so my problem is when i again click on red heart image it doesnt turn to its normal state which is blank heart vector image as i'm new in android i dont have much knowledge please if someone can guide.
my code for image onclick
#Override
public void onBindViewHolder(FeedViewHolder holder, int position) {
int image_id = images[position];
//holder.background_image_layout.setImageDrawable(null);
//holder.background_image_layout.setImageResource(image_id);
holder.background_image.setBackgroundResource(image_id);
}
#Override
public int getItemCount() {
return images.length;
}
public static class FeedViewHolder extends RecyclerView.ViewHolder {
CustomTextViewMedium first_text,second_text,third_text,fourth_text,fifth_text,sixth_text,
seventh_text;
ImageView favourite_image;
CardView primary_card;
LinearLayout background_image;
ImageView background_image_layout;
CircleImageView profile_image;
public FeedViewHolder(View itemView) {
super(itemView);
background_image = (LinearLayout)itemView.findViewById(R.id.background_image);
primary_card = (CardView)itemView.findViewById(R.id.primary_card);
first_text = (CustomTextViewMedium)itemView.findViewById(R.id.first_text);
second_text = (CustomTextViewMedium)itemView.findViewById(R.id.second_text);
third_text = (CustomTextViewMedium)itemView.findViewById(R.id.third_text);
fourth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fourth_text);
fifth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fifth_text);
sixth_text = (CustomTextViewMedium)itemView.findViewById(R.id.sixth_text);
seventh_text = (CustomTextViewMedium)itemView.findViewById(R.id.seventh_text);
favourite_image = (ImageView)itemView.findViewById(R.id.favourite_image);
background_image_layout = (ImageView) itemView.findViewById(R.id.background_image_layout);
profile_image = (CircleImageView)itemView.findViewById(R.id.profile_image);
favourite_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(favourite_image.isPressed()){
favourite_image.setImageResource(R.drawable.ic_heart);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
else {
favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}
});
}
}
Use if condition like this :-
first globally define like this :-
boolean imageChange = true;
then do this in setOnClickListener like this :-
favourite_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(imageChange ){
favourite_image.setImageResource(R.drawable.ic_heart);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageChange = false;
}else {
favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageChange = true;
}
}
});
Problem is that favourite_image.isPressed() is not persistent state of the View. It will be true only while user holds its finger on the view.
To overcome this you have two similar options:
Option #1
Instead of checking isPressed you can check some other state, for example isSelected. Since this is ImageView you probably need to set the state manually on click.
public void onClick(View v) {
if(favourite_image.isSelected()){
favourite_image.setImageResource(R.drawable.ic_heart);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
favourite_image.setSelected(false)
}
else {
favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
favourite_image.setSelected(true)
}
}
Options #2
Use some simple boolean to keep track of the selected state, and set/chec its values similarity to above code.

Setting image resourse by obtaining tag position for an imageview android

I am making use of recycler view. I have a layout that is highlighted in light red,this layout is included for each item in the recycler view. The light red layout is placed over the background image. I am using setTag method to identify the clicks of the buttons in red layout. That is working properly when i click i get the position. The problem is i want to change the image at specific position.
For example : Consider the heart button. I have set a tag on it like this.
heartButton = findViewById(id);
heartButton.setTag(position);
now i get the position by using the getTag method. But now i want to change the image of the heartButton at the a specific position. Is there something like
heartButton.getTag(position).setImageResouce(drawable);
If not how do i do this then.
use setBackgroundResource(R.drawable.XXX)
http://developer.android.com/reference/android/view/View.html#setBackgroundResource(int)
Proper way to do this is,
You have to keep the state of the heart button stored in the model(POJO) which is passed to custom adapter.
e.g.
class ModelListItem{
public static final int HEART=1,BROKEN_HEART=2;
int heartButtonState;
}
Now in onClick() of heart button, get that object from adapter using position,cosidering you have already figured it out on how to preserve position from heart button
ModelListItem item = (ModelListItem)adapter.getItem(position)
Change the state of heart button;
item.setHeartButtonState(ModelListItem.BROKEN_HEART);
adapter.notifyDatasetChanged();
You already know below explaination but just in case
To work this properly,in your getView methode of adapter you need to put the check on heartButtonState(); and use appropriate image resource.
getView(BOILERPLATE){
BOILERPLATE
switch(item.getheartButtonState()){
case ModelItemList.HEART:
heartbutton.setImageResource(heart_image);
break;
case ModelItemList.BROKEN_HEART:
heartbutton.setImageResource(broken_heart_image);
break;
}
I made a custom click listener and updated the like in the setter getter.But this works only when the view has been moved out of the view (i think it is the scrapeview)
The Setter Getter Class
public class DemoData {
int background;
boolean liked;
public DemoData(int background) {
this.background = background;
}
public int getBackground() {
return background;
}
// public void setBackground(int background) {
// this.background = background;
// }
public boolean isLiked() {
return liked;
}
public void setLiked(boolean liked) {
this.liked = liked;
}
}
The onBindViewHolder function of the recycler view
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
background = (ImageView) holder.view.findViewById(R.id.image);
layout = (LinearLayout) holder.view.findViewById(R.id.layout);
delete = (ImageView) layout.findViewById(R.id.delete);
lock = (ImageView) layout.findViewById(R.id.lock);
delete.setTag("delete_"+position);
lock.setTag("lock_"+position);
if(Constants.demoDatas.get(position).isLiked()){
delete.setImageResource(R.drawable.ic_launcher);
}
else{
delete.setImageResource(android.R.drawable.ic_delete);
}
delete.setOnClickListener(new CustomClickListener(position));
lock.setOnClickListener(new CustomClickListener(position));
}
The custom click listener is as below
public class CustomClickListener implements View.OnClickListener {
int position;
public CustomClickListener(int position) {
this.position = position;
}
#Override
public void onClick(View v) {
String tag = (String) v.getTag();
String identifier[] = tag.split("_");
// this line saves my state in the Setter Getter Class
Constants.demoDatas.get(position).setLiked(true);
}
}

Android Animation Bug wont let me click after animation move to the right

So, here's my problem? I have it where it will slide out and then it will stay in that position and then you should be able to click on it again and it will slide back to the original location.
But instead I have to click on where it FIRST was at. Even when it's slided out. I want to make it after the animation I can click it any where, that it slided out.
Here's my code
public void sideBar()
{
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
if(out == 0)
{
mSlideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
mSlideInRight.setFillAfter(true);
sidebar.startAnimation(mSlideInRight);
out= 1;
}
else if(out == 1)
{
mSlideInLeft = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
sidebar.startAnimation(mSlideInLeft);
out=0;
}
}
and this is the code for when you click on it
public void onClick(View v) {
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
ImageView popup = (ImageView)findViewById(R.id.popup);
ImageView popup2 = (ImageView)findViewById(R.id.popup2);
switch(v.getId())
{
case R.id.sidebar:
sideBar();
break;
}
}
Animations only affect how your sidebar ImageView is drawn, not its actual position. After your animation completes, you should update the sidebar's layout parameters to your desired position. You can do this in an AnimationListener if you want the position to be updated as the animation runs or after it is complete, or right after your call to startAnimation if you want the position change to happen as the animation starts.
mSlideInRight.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation anim) {};
#Override
public void onAnimationRepeat(Animation anim) {};
#Override
public void onAnimationEnd(Animation anim) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(...);
// Set properties of layoutParams here
sidebar.setLayoutParams(layoutParams);
};
});
Replace RelativeLayout with whatever layout your ImageView is a child of.

Android ViewPager - adding slideshow with fade-in/out animation to switch between views

I use ViewPager to implement my own image gallery. The views consists of ImageViews. The user can normally navigate between images using gestures (as in ViewPager).
What I want to add is the slideshow feature. When user chooses an option "slideshow" from the menu I want to be able to start the animation of ViewPager items - ideally the animation would be the fade-in/out effect between images/slides.
Is it possible to implement this with ViewPager?
Here's how I did it. First I used a lot of the code from the Android Displaying Bitmaps in Your UI tutorial.
Then, I added the following tweaks:
In the OnCreate of the activity that contains the ViewPager:
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
PhotoViewerFragment currentFragment = mAdapter
.getFragment(arg0);
if (currentFragment != null) {
if (mRunningSlideshow) {
currentFragment.addImageTransition();
}
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
Runnable that runs slideshow in the activity that contains the ViewPager:
private Runnable runSlideshow = new Runnable() {
public void run() {
// Second parameter of false turns ViewPager scroll animation off
mPager.setCurrentItem(mPager.getCurrentItem() + 1, false);
mSlideshowHandler.postDelayed(runSlideshow,
SLIDESHOW_IMAGE_DURATION);
}
};
In the fragment that displays the image:
private static final int FADE_IN_TIME = 200;
/**
* Adds fade-in transition when in slideshow mode. Called from PhotoViewerActivity.
*/
public void addImageTransition() {
// Transition drawable with a transparent drawable and the final bitmap
final TransitionDrawable td = new TransitionDrawable(new Drawable[] {
new ColorDrawable(android.R.color.transparent),
mImageView.getDrawable() });
mImageView.setImageDrawable(td);
td.startTransition(FADE_IN_TIME);
}

Categories