How to enable/disable zooming at runtime? - android

I am working on images, I am going to display image in full screen, as user touches on screen then some controls will be shown. So I want to disable zooming when controls are on the screen.
Or somebody tell me how to detect the image is zoom or not?
activity_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="#+id/imgSelected"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="centerInside"
android:src="#drawable/placeholder"
android:contentDescription="#string/image" />
<LinearLayout
android:id="#+id/imageControls"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:weightSum="9"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="horizontal"
android:visibility="gone"
android:background="#drawable/image_controls">
<Button
android:id="#+id/btnSetAsBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="left|center_vertical"
android:background="#00000000"
style="#style/ImageControls"
android:text="Set"/>
<Button
android:id="#+id/btnFavorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="#00000000"
style="#style/ImageControls"
android:text="Favourite"/>
<Button
android:id="#+id/btnShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="right|center_vertical"
android:background="#00000000"
style="#style/ImageControls"
android:text="Share"/>
</LinearLayout>
</RelativeLayout>
activity_zoom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
</LinearLayout>
ImageActivity.java
public class ImageActivity extends Activity {
private int imageId;
private int categoryId;
private String imageUrl;
private ZoomView zoomView;
private ImageView imageView;
private LinearLayout main_container;
private boolean controlFlag;
private LinearLayout imageControls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoom);
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
final View convertView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.activity_image, null, false);
convertView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageControls = (LinearLayout) convertView.findViewById(R.id.imageControls);
controlFlag = false;
zoomView = new ZoomView(this);
imageId = getIntent().getExtras().getInt("image");
categoryId = getIntent().getExtras().getInt("category");
imageView = (ImageView) convertView.findViewById(R.id.imgSelected);
imageUrl = SplashActivity.urlList.get(categoryId).get(imageId);
Log.e(String.valueOf(categoryId),imageUrl);
Picasso
.with(this)
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.into(imageView, new Callback() {
#Override
public void onError() {
}
#Override
public void onSuccess() {
imageView.setScaleType(ScaleType.FIT_CENTER);
}
});
zoomView.addView(convertView);
main_container = (LinearLayout) findViewById(R.id.main_container);
main_container.addView(zoomView);
convertView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(controlFlag) {
imageControls.setVisibility(View.GONE);
controlFlag = false;
} else {
imageControls.setVisibility(View.VISIBLE);
controlFlag = true;
}
return false;
}
});
}
}
I am using android-zoom-view.jar library for zooming. I want to display LinearLayout (imageControls) only if image is not zoom Or disable zooming if LinearLayout (imageControls) is visible. Because ZoomView instance perform zooming on all over activity so the imageControls will also be zoom. This is not a good user friendly design.
So please help me to make this layout better.
Thanks...

In zoomview class thinking you using this https://github.com/Polidea/android-zoom-view/blob/master/src/pl/polidea/view/ZoomView.java
just add this
boolean isZoomEnable= true;
public void setIsZoomEnable(boolean value){
isZoomEnable = value;
}
#Override
public boolean dispatchTouchEvent(final MotionEvent ev) {
if(!isZoomEnable) return false;
// single touch
if (ev.getPointerCount() == 1) {
processSingleTouchEvent(ev);
}
// // double touch
if (ev.getPointerCount() == 2) {
processDoubleTouchEvent(ev);
}
// redraw
getRootView().invalidate();
invalidate();
return true;
}
Then in your code
convertView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if(controlFlag) {
imageControls.setVisibility(View.GONE);
controlFlag = false;
} else {
imageControls.setVisibility(View.VISIBLE);
controlFlag = true;
}
zoomView.setIsZoomEnable(!controlFlag);
return false;
}
});

Related

Intersecting two images in different Layouts

How it looks
I want to check intersection between ImageView to ImageView2 as LinearLayout slides towards ImageView2.
I used Rect but it is not working, ImageView2 Just passed throught it without getting intersect.
Please help me!!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >
<ImageView
android:id="#+id/tile"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/tile" />
<LinearLayout
android:id="#+id/l1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:background="#000"
android:src="#drawable/b" />
<ImageView
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_weight="50"
android:background="#000"
android:src="#drawable/b" />
</LinearLayout>
#SuppressLint("NewApi")
public class MainActivity extends Activity {
Rect tileRect = new Rect();
Rect b1Rect = new Rect();
Rect b2Rect = new Rect();
ImageView tile,b1,b2;
RelativeLayout layout;
LinearLayout l1;
final Handler h = new Handler();
Boolean tileRight=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
move();
}
private void init() {
// TODO Auto-generated method stub
b1 = (ImageView)findViewById(R.id.b1);
b2 = (ImageView)findViewById(R.id.b2);
tile = (ImageView)findViewById(R.id.tile);
layout = (RelativeLayout)findViewById(R.id.layout);
l1 = (LinearLayout)findViewById(R.id.l1);
tile.setX(320);
tile.setY(800);
l1.setVisibility(View.VISIBLE);
}
public void move()
{
final int delay = 45;
h.postDelayed(new Runnable()
{
#Override
public void run() {
// TODO Auto-generated method stub
layout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_UP)
{
if(tileRight==true)
tileRight=false;
else
tileRight=true;
return true;
}
return false;
}
});
if(tileRight==true)
{
if(tile.getX()>600f)
{
tile.setX(tile.getX());
}
else{
tile.setX(tile.getX()+speedTile);
}
}
else{
if(tile.getX()<40f)
{
tile.setX(tile.getX());
}
else{
tile.setX(tile.getX()-speedTile);
}
}
tile.getHitRect(tileRect);
b1.getHitRect(b1Rect);
b2.getHitRect(b2Rect);
if(Rect.intersects(tileRect, b1Rect) || Rect.intersects(tileRect, b2Rect))
{
gameOver();
}
l1.setY(l1.getY()+10f);
h.postDelayed(this, delay);
}
},delay);
}
private void gameOver() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Android's View has getLocationOnScreen() method that shows absolute position of your view.
https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])
The issue occurs because view.getHitRect() is used before the layouts are inflated.
Any of the view's position or measurement APIs like getWidth(), getTop(), getRight() etc. will return 0 (getHitRect() initializes the Rect with (0,0,0,0)) in onCreate() or onResume() before the views are inflated.
In your case, it appears that the Handler's delay period executes the intersection logic earlier than view inflation.
You could post from the view and the run() method will execute after the view inflates and then obtain the view's measurement parameters or a Rect .
Here is an example:
public class MyTestActivity extends AppCompatActivity{
int mNumberOfViewsInitialized = 0;
ImageView mImageViewRight, mImageViewLeft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sliding_image_activity);
mImageViewLeft = (ImageView) findViewById(R.id.image_view_left);
mImageViewRight = (ImageView) findViewById(R.id.image_view_right);
// calling method here will log that the views do not intersect
// which happens because they haven't been inflated yet.
// doViewsOverlap();
mImageViewLeft.post(new Runnable(){
#Override
public void run() {
mNumberOfViewsInitialized++;
intersectionLogic();
}
});
mImageViewRight.post(new Runnable(){
#Override
public void run() {
mNumberOfViewsInitialized++;
intersectionLogic();
}
});
}
private void intersectionLogic(){
/* the constant could be something else, depending
on the number of views you'd like to test intersection for*/
if(mNumberOfViewsInitialized == 2){
doViewsOverlap(mImageViewLeft,mImageViewRight);
}
}
private void doViewsOverlap(final View firstView, final View secondView){
Rect leftRect = new Rect();
firstView.getHitRect(leftRect);
Rect rightRect = new Rect();
secondView.getHitRect(rightRect);
if(Rect.intersects(leftRect,rightRect) || Rect.intersects(rightRect,leftRect))
Log.v("intersects","views intersect");
else
Log.v("intersects","views do not intersect");
}
}
I used a layout which does not animate / move the images, but the same issue should occur when the images are moved as well
<ImageView
android:id="#+id/image_view_left"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:background="#android:color/holo_blue_dark"
android:src = "#mipmap/ic_launcher"/>
<ImageView
android:id="#+id/image_view_right"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentRight="true"
android:background="#android:color/holo_green_dark"
android:gravity="end"
android:src="#mipmap/ic_launcher"/>
Images contained in a RelativeLayout.
Here is a screenshot of the UI:
This problem is related to this: If I call getMeasuredWidth() or getWidth() for layout in onResume they return 0
Hope this helps.

Drag and Drop images on Custom View

I am planning to make and app in which I can drag and drop different types of ball images into a some sort of a field. To do so, I have made a simple example, in which I have examples of ball's images and I want to drag an drop multiple copies of it inside the field. This is the example I've been doing some research and I've found that to do so, the best way is to have a customview object that will act as the field. It will contain a list of all the images I have and their positions in the field.
I am missing last step where I drop the image inside the customview and it is added inside the list of images it contains with it X and Y position so I am able to recover the image later with all its balls.
How can I do it? Here is the code I already have:
Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="10dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LIST OF ITEMS TO DRAG"
android:textSize="25sp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/ball"
android:layout_margin="10dp"
android:id="#+id/image_to_drag"
/>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/ball2"
android:layout_margin="10dp"
android:id="#+id/image_to_drag2"
/>
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/ball3"
android:layout_margin="10dp"
android:id="#+id/image_to_drag3"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CUSTOMVIEW WHERE DROP"
android:textSize="25sp"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<com.dragandrop.canvas.canvas.Salon
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/salon_1"
android:background="#96e29b"
/>
</FrameLayout>
</LinearLayout>
CustomView class
public class Field extends View
{
Context context;
public Salon(Context context, AttributeSet attrs)
{
super(context,attrs);
this.context = context;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Resources res = getResources();
float left = 0;
float top = 0;
}
}
Main Activity
public class MainActivity extends AppCompatActivity {
ImageView image;
ImageView image2;
ImageView image3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image_to_drag);
image2 = (ImageView) findViewById(R.id.image_to_drag2);
image3 = (ImageView) findViewById(R.id.image_to_drag3);
image.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData clipData = ClipData.newPlainText("","");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(image);
image.startDrag(clipData,shadowBuilder,image,0);
return false;
}
});
image2.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData clipData = ClipData.newPlainText("","");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(image2);
image2.startDrag(clipData,shadowBuilder,image2,0);
return false;
}
});
image3.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
ClipData clipData = ClipData.newPlainText("","");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(image3);
image3.startDrag(clipData,shadowBuilder,image3,0);
return false;
}
});
}
}

Compare 2 ImageViews in android

I currently working on a Drag 'n' Drop application.
My app has 2 layouts.
The 1st layout is 2 balls (Images) of different color.
The 2nd layout is 3 balls (Images) of different color.
When the user drags one ball onto any layout & drops that ball onto the other layout, the app then checks if a ball with the same color is in this layout: if so, it then removes those 2 balls, otherwise adds the ball to that layout.
My XML file
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="#+id/relative">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="150sp"
android:background="#00FF00"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball_red" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball_blue" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ball_pink" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/linearLayout1"
android:layout_centerHorizontal="true"
android:background="#0F0F0F">
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ball_red" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ball_yellow" />
</LinearLayout>
</RelativeLayout>
My Activity Class
public class MainActivity extends Activity implements OnTouchListener, OnDragListener{
ImageView i1,i2,i3,i4,i5,i6;
Drawable d1,d2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i1=(ImageView) findViewById(R.id.imageView1); i2=(ImageView) findViewById(R.id.imageView2);
i5=(ImageView) findViewById(R.id.imageView5); i6=(ImageView) findViewById(R.id.imageView6); i4=(ImageView) findViewById(R.id.imageView4);
findViewById(R.id.linearLayout1).setOnDragListener(this);
findViewById(R.id.linearLayout2).setOnDragListener(this);
i6.setOnTouchListener(this); i5.setOnTouchListener(this); i2.setOnTouchListener(this); i1.setOnTouchListener(this);
i4.setOnTouchListener(this);
d1=i1.getDrawable(); d2=i6.getDrawable();
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
v.setVisibility(View.VISIBLE);
return true;
}
else {
return false;
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#SuppressLint("NewApi")
#Override
public boolean onDrag(View v, DragEvent e) {
// TODO Auto-generated method stub
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
ViewGroup from = (ViewGroup) view.getParent();
from.removeView(view);
LinearLayout to = (LinearLayout) v;
boolean comp=false;
comp=compareDrawable(d1, d2);;
if(comp == false)
{
to.addView(view);
view.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Add.", Toast.LENGTH_LONG ).show();
}
else
{
to.removeView(view);
from.addView(view);
view.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Remove.", Toast.LENGTH_LONG ).show();
}
}
return true;
}
public boolean compareDrawable(Drawable d1, Drawable d2){
try{
Bitmap bitmap1 = ((BitmapDrawable)d1).getBitmap();
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, stream1);
stream1.flush();
byte[] bitmapdata1 = stream1.toByteArray();
stream1.close();
Bitmap bitmap2 = ((BitmapDrawable)d2).getBitmap();
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, stream2);
stream2.flush();
byte[] bitmapdata2 = stream2.toByteArray();
stream2.close();
return bitmapdata1.equals(bitmapdata2);
}
catch (Exception e) {
// TODO: handle exception
}
return false;
}
}
Get the 2 images ID then compare it,
if(img1.getId()==img2.getId())
{
//Your stuff
}
or try this
Bitmap bitmap = ((BitmapDrawable)img1).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)img2).getBitmap();
if(bitmap == bitmap2)
{
//Code blcok
}
I believe that what you actually need is to compare if two bitmaps are the same content.
For this you must use Bitmap.sameAs(Bitmap) look at it here
As an advice don't compare objects with "==" but using equals!
PS : sameAs method is starting from API 12

android - onClickListener causes a reset of my animation

I have an Activity with some elements like ImageView, Button, ToggleButton, ... . And a subview (LinearLayout) that contains an HorizontalScrollView of ImageView.
The subview is an element that I want to hide / show with an animation.
My animation works successfully. But when I touch a ToggleButton or I apply a Filter, the subview is reseted and back to its origin position.
I have deduce that the subview is replaced in its origin when an element of the view visually change.
But I don't understand why...
The Activity class
public class CameraActivity extends Activity implements PictureCallback
{
private ToggleButton flashButton;
private Button filterScrollButton;
private LinearLayout filterScrollView;
private LinearLayout filterScrollViewLayout;
private Boolean filtersIsOpened = false;
private ImageView filterImageView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
flashButton = (ToggleButton)findViewById(R.id.button_flash);
filterScrollButton = (Button)findViewById(R.id.button_open_filters);
filterScrollView = (LinearLayout)findViewById(R.id.camera_scroll_filters);
filterScrollViewLayout = (LinearLayout)findViewById(R.id.camera_scroll_filters_layout);
}
...
private void initScrollFilters()
{
String[] filters = getResources().getStringArray(R.array.array_filters);
for (final String string : filters)
{
ImageView v = new ImageView(CameraActivity.this);
int imageFilterId = -1;
if (string != null && !string.isEmpty())
{
final int imageId = getResources().getIdentifier("#drawable/filter_" + string, null, getPackageName());
imageFilterId = getResources().getIdentifier("#drawable/filter_" + string, null, getPackageName());
v.setImageDrawable(getResources().getDrawable(imageId));
}
final int finalImageFilterId = imageFilterId;
v.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view) {
Log.d(string + " filter image is touched");
CameraActivity.this.cameraManager.setImageFilter(finalImageFilterId); // Apply the new filter into filterImageView
}
});
filterScrollViewLayout.addView(v, 100, 100);
}
}
private void initListeners()
{
// Flash
flashButton.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// camera parameters is changed
}
});
// Filter scroll view
filterScrollButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Log.d("You click the filter scroll button men!!");
final float direction = (CameraActivity.this.filtersIsOpened) ? -1 : 1;
final float yDelta = -100;
final Animation animation = new TranslateAnimation(0, 0, 0, yDelta * direction);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationEnd(Animation animation)
{
TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
anim.setDuration(1);
CameraActivity.this.filterScrollView.startAnimation(anim);
int top = (int) (CameraActivity.this.filterScrollView.getTop() + (yDelta * direction));
CameraActivity.this.filterScrollView.setTop(top);
}
});
animation.setDuration(500);
CameraActivity.this.filterScrollView.startAnimation(animation);
CameraActivity.this.filtersIsOpened = ! CameraActivity.this.filtersIsOpened;
}
});
}
...
}
The xml view
<FrameLayout 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=".CameraActivity" >
<CameraPreview
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/camera_preview_filter"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:rotation="90"
android:scaleType="center"
android:contentDescription="#string/content_desc_overlay" />
<LinearLayout
android:id="#+id/camera_scroll_filters"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="-40dp"
android:paddingTop="40dp"
android:orientation="vertical"
android:clickable="false"
android:longClickable="false" >
<Button
android:id="#+id/button_open_filters"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="top|center_horizontal"
android:text="Filters" >
</Button>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF00FF00"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/camera_scroll_filters_layout"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="bottom"
android:background="#FF000000" >
</FrameLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:onClick="onCaptureClick"
android:text="#string/button_capture_text" />
</FrameLayout>

click event of button not getting raised

I am trying to make something like customised quick badge with some buttons on it. I have successfully desgined it but when i am clicking on those buttons it does nothing. Can someone tell me where am i going wrong.
demo_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/likemenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</TextView>
<Button
android:id="#+id/likequickaction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="30dp"
android:text="Button" />
</LinearLayout>
popup_grid_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<FrameLayout
android:id="#+id/header2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10.0dip"
android:background="#drawable/quickaction_top_frame" />
<ImageView
android:id="#+id/arrow_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/quickaction_arrow_up" />
<HorizontalScrollView
android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header2"
android:background="#drawable/quickaction_slider_background"
android:fadingEdgeLength="0.0dip"
android:paddingLeft="1.0dip"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_left" />
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/api_buttons" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/quickaction_slider_grip_right" />
</LinearLayout>
</HorizontalScrollView>
<FrameLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/scroll"
android:background="#drawable/quickaction_bottom_frame" />
</RelativeLayout>
api_buttons.xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/quickaction_slider_background" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/one"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="One" />
<Button
android:id="#+id/two"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Two" />
<Button
android:id="#+id/three"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Three" />
<Button
android:id="#+id/four"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Four" />
<Button
android:id="#+id/five"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Five" />
<Button
android:id="#+id/six"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="80dp"
android:minWidth="80dp"
android:text="Six" />
</TableRow>
</HorizontalScrollView>
BetterPopupWindow.java
public class BetterPopupWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;
public BetterPopupWindow(View anchor) {
this.anchor = anchor;
this.window = new PopupWindow(anchor.getContext());
// when a touch even happens outside of the window
// make the window go away
this.window.setTouchInterceptor(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
BetterPopupWindow.this.window.dismiss();
return true;
}
return false;
}
});
this.windowManager = (WindowManager) this.anchor.getContext()
.getSystemService(Context.WINDOW_SERVICE);
onCreate();
}
protected void onCreate() {
}
protected void onShow() {
}
private void preShow() {
if (this.root == null) {
throw new IllegalStateException(
"setContentView was not called with a view to display.");
}
onShow();
if (this.background == null) {
this.window.setBackgroundDrawable(new BitmapDrawable());
} else {
this.window.setBackgroundDrawable(this.background);
}
this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setTouchable(true);
this.window.setFocusable(true);
this.window.setOutsideTouchable(true);
this.window.setContentView(this.root);
}
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
public void setContentView(View root) {
this.root = root;
this.window.setContentView(root);
}
public void setContentView(int layoutResID) {
LayoutInflater inflator = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setContentView(inflator.inflate(layoutResID, null));
}
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
this.window.setOnDismissListener(listener);
}
public void showLikePopDownMenu() {
this.showLikePopDownMenu(0, 0);
}
public void showLikePopDownMenu(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_PopDownMenu);
this.window.showAsDropDown(this.anchor, xOffset, yOffset);
}
public void showLikeQuickAction() {
this.showLikeQuickAction(0, 0);
}
public void showLikeQuickAction(int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
int[] location = new int[2];
this.anchor.getLocationOnScreen(location);
Rect anchorRect = new Rect(location[0], location[1], location[0]
+ this.anchor.getWidth(), location[1] + this.anchor.getHeight());
this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();
int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;
// display on bottom
if (rootHeight > anchorRect.top) {
yPos = anchorRect.bottom + yOffset;
this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
}
this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
public void dismiss() {
this.window.dismiss();
}
}
LikeQuickActionDemo.java
public class LikeQuickActionsDemo extends Activity {
private Button likemenuButton;
private Button likequickactionButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.demo_layout);
this.likemenuButton = (Button) this.findViewById(R.id.likemenu);
this.likemenuButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu();
}
});
this.likequickactionButton = (Button) this
.findViewById(R.id.likequickaction);
this.likequickactionButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DemoPopupWindow dw = new DemoPopupWindow(v);
dw.showLikePopDownMenu(0, 200);
}
});
}
private static class DemoPopupWindow extends BetterPopupWindow implements
OnClickListener {
public DemoPopupWindow(View anchor) {
super(anchor);
}
protected void onCreate() {
// inflate layout
LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
// setup button events
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
TableRow row = (TableRow) v;
for (int j = 0, jcount = row.getChildCount(); j < jcount; j++) {
View item = row.getChildAt(j);
if (item instanceof Button) {
Button b = (Button) item;
b.setOnClickListener(this);
}
}
}
}
// set the inflated view as what we want to display
this.setContentView(root);
}
public void onClick(View v) {
// we'll just display a simple toast on a button click
Button b = (Button) v;
Toast.makeText(this.anchor.getContext(), b.getText(),
Toast.LENGTH_SHORT).show();
this.dismiss();
}
}
}
The problem is in DemoPopupWindow.onCreate method:
ViewGroup root = (ViewGroup) inflater.inflate(
R.layout.popup_grid_layout, null);
for (int i = 0, icount = root.getChildCount(); i < icount; i++) {
View v = root.getChildAt(i);
if (v instanceof TableRow) {
root will be RelativeLayout defined in popup_grid_layout.xml. Later you iterate through all its children and set onclick listeners only if child view is TableRow, which is never the case. I guess what you want there is to find horizontalScrollView1 view and do the same with its children instead, something like:
View horizontalScrollView1 = findViewById(R.id.horizontalScrollView1);
for (int i = 0, icount = horizontalScrollView1.getChildCount(); i < icount; i++) {
View v = horizontalScrollView1.getChildAt(i);
if (v instanceof TableRow) {
...

Categories

Resources