I am developing a game application using android.
The Object(a box) slides up and down. And it has to hit the
objects(orange and pink balls) coming towards it from the right end of
the screen that would increase his score.
There will be black balls as well(shot from the right end of the
screen) which he should avoid hitting.
I am having problem with
onTouchEvent(MotionEvent me)
function while implementing the code.
I am following this tutorial in this series of tutorials.
My Questions:
To use the onTouchEvent(MotionEvent me) function do I need to import any class?
The tutorial has declared theonTouchEvent(MotionEvent me) outside the onCreate method. Which is okay. But the program has not called it anywhere. How does it work then?
After writing the code as mentioned in the tutorial, the program is not working as intended. The box appears when the activity starts. However, it disappears as soon as I click on the screen. What could be the problem?
ActivityMain.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/scoreLabel"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text=" : 300"
android:paddingLeft="10dp"
android:gravity="center_vertical" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/startLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="130dp"/>
<ImageView
android:id="#+id/box"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#drawable/box"
android:layout_gravity="center_vertical" />
<ImageView
android:id="#+id/orange"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/orange" />
<ImageView
android:id="#+id/black"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/black" />
<ImageView
android:id="#+id/pink"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/pink" />
</FrameLayout>
</RelativeLayout>
MainActivity.java
package com.example.catcheggs1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView scoreLabel;
private TextView startLabel;
private ImageView box;
private ImageView orange;
private ImageView black;
private ImageView pink;
//Position
private int boxY;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scoreLabel=(TextView)findViewById(R.id.scoreLabel);
startLabel=(TextView)findViewById(R.id.startLabel);
box=(ImageView)findViewById(R.id.box);
orange=(ImageView)findViewById(R.id.orange);
pink=(ImageView)findViewById(R.id.pink);
black=(ImageView)findViewById(R.id.black);
//Move To Out of Screen
orange.setX(-80);
orange.setY(-80);
pink.setX(-80);
pink.setY(-80);
black.setX(-80);
black.setY(-80);
//Temporary
startLabel.setVisibility(View.INVISIBLE);
boxY=500;
}
public boolean onTouchEvent(MotionEvent me)
{
if(me.getAction()==MotionEvent.ACTION_DOWN)
{
boxY -= 1 ;
}
box.setY(boxY);
return true;
}
}
1.) No, you do not need to import anything.
2.) For detailed information you can see the link.
It is an event method, it is automatically called, you do not need to call it.
https://developer.android.com/guide/topics/ui/ui-events#java
3.) you use boxY variable when touched and it is assigned an arbitrary number. Your problem is much likely to be caused by that. Rather than that, you should first get the current position than tweak it.
You can get current position with this method.
int[] location = new int[2];
imageView.getLocationOnScreen(location);
https://developer.android.com/reference/android/view/View.html#getLocationOnScreen(int[])
Bonus.) onTouchEvent is an activity method, so you should use view's own event listeners for specific tasks rather than onTouchEvent.
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
Related
Background: I'm trying to detect edge swipes in my app (to bring up a menu), and searching stackoverflow seems to indicate that the way to detect swipes is to start with fling detection.
I'm trying to detect a fling event in my app. I can override dispatchTouchEvent() or onTouchEvent() and pass the event to a gesture listener and everything works as you would expect.
However, my app has button widgets in it and I can't both detect the fling and use the widgets.
If I call the gesture detector from onTouchEvent(), flings are not detected if the gesture starts over a widget that consumes the event. If I call the gesture detector from dispatchTouchEvent(), the widgets don't get the events they need.
I've also tried attaching to the container's onTouchEvent() but the result was the same.
Finally, I've looked at the source code to ViewPager to see how they do it, and what they do is override onInterceptTouchEvent(). I can see how and why this works, but I was hoping there was a simpler solution that didn't require me to implement my own container widget.
Source code:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
public class Fling extends Activity {
private static final String TAG = "FlingTest";
protected GestureDetector gestureDetector;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureDetector = new GestureDetector(this, listener);
}
/*
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
return super.dispatchTouchEvent(event);
}
*/
#Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
private final SimpleOnGestureListener listener =
new SimpleOnGestureListener() {
public boolean onDown(MotionEvent e1) { return true; }
public boolean onFling(MotionEvent e1, MotionEvent e2, float vx, float vy) {
Log.d(TAG, "Fling: " + vx + "," + vy);
return true;
}
};
}
The layout.xml file, although pretty much any layout will show these issues:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/buttonPane"
android:background="#446"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:text="btn"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
<Button
android:text="btn"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:text="btn"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
<Button
android:text="btn"
android:layout_width="wrap_content" android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
I've found one answer that seems to work. I change dispatchTouchEvent() to both call the gesture detector and pass the event up the chain.
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.dispatchTouchEvent(event);
}
It's not pretty, and I'm not sure it's robust, but it seems to work.
Still hoping for a better answer, but I think I'll give this method a try.
This app is supposed to display images in which the students can count up what the money is worth in total, and then input the value in a editText text box, which is then compared against a stored value. Unfortunately, when I try to switch activities past a certain point (there are 11 active activities, with three of them displaying images fine), the images start blurring and the coins are hard to distinguish from each other. I do not know whether this is a Java or XML error, however I have pasted the code below. The following is XML code.
<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=".Ldsm" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:orientation="vertical" >
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submit" />
</LinearLayout>
<TextView
android:id="#+id/userQuestion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout"
android:layout_alignLeft="#+id/userAnswer"
android:layout_marginBottom="30dp"
android:text="#string/how_many_coins_total"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/userAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/userQuestion"
android:layout_alignParentTop="true"
android:layout_marginLeft="50dp"
android:layout_marginTop="118dp" >
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/userQuestion"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/linearLayout"
android:src="#drawable/ic_coin4" />
That's just one of the 7 screens that look blurry when they display the images. The following is the Java code for the same activity.
package com.example.ldsm3;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.example.ldsm3.Problem5;
public class Problem4 extends Activity
{
private final int COIN3_SCREEN_ANSWER = 95;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_problem4);
Button submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(submitButtonListener);
}
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;
}
private OnClickListener submitButtonListener = new OnClickListener()
{
public void onClick(View arg0)
{
EditText editText = (EditText)findViewById(R.id.userAnswer);
int userAnswerValue = Integer.parseInt(editText.getText().toString());
// Build the Alert Dialog
android.app.AlertDialog.Builder alert = new AlertDialog.Builder(Problem4.this);
alert.setTitle("Answer");
alert.setCancelable(false);
if(userAnswerValue == COIN3_SCREEN_ANSWER)
{
alert.setMessage("Congratulations!!!");
}
else
{
alert.setMessage("Sorry, that's not right.");
}
alert.setPositiveButton("OK",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int id)
{
Intent nextCoinScreenIntent = new Intent(Problem4.this, Problem5.class);
startActivity(nextCoinScreenIntent);
}
});
alert.show();
}
};
}
And this a screenshot when it is running on a Nexus 10:
Please let me know if any more information is needed.
It seems like it was fetching the icons that were not designed for the particular screen size. You can fix this by going to the workspace and deleting every icon that is any other folder besides the "drawable" one. (With the exception of the ic_launcher icon, as that is the one that allows you to display your app in the Android app menu and will not get blurry.)
Better than your solution would be to make sure the proper resolution file is in every folder. The proper resolution is your desired resolution with the following multipliers, as shown on the Android Design documents:
i'm a newbie of android :)
I want to create a simple slideshow with a numeric parameter(x) and a button.I have 21 images and, at the click of the button, I want to catch x and "slide" only the first x images of the list.Between one image and the next i want wait 1 second.
At the end i want to know how much time passed during the operation. (sorry for my english)
paste below the code of the activity and the layout:
package com.superpibenchmarknative;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageActivity extends Activity implements OnClickListener {
private Button start;
private EditText iteractions;
private TextView time;
private ImageView display;
private long after,before;
private int images[] = {R.drawable.image1,R.drawable.image2,R.drawable.image3,
R.drawable.image4,R.drawable.image5,R.drawable.image6,R.drawable.image7,
R.drawable.image8,R.drawable.image9,R.drawable.image10,R.drawable.image11,
R.drawable.image12,R.drawable.image13,R.drawable.image14,R.drawable.image15,
R.drawable.image16,R.drawable.image17,R.drawable.image18,R.drawable.image19,
R.drawable.image20,R.drawable.image21,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
setUpUI();
}
private void setUpUI() {
// TODO Auto-generated method stub
start = (Button) findViewById(R.id.bntStartCalcolation);
iteractions = (EditText) findViewById(R.id.etIterations);
time = (TextView) findViewById(R.id.tvTime);
start.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
//Creo le imageview in base al # di iteractions
before = System.currentTimeMillis();
for (int j = 0; j < Integer.parseInt(iteractions.getText().toString());j++){
display = (ImageView) findViewById(R.id.ivDisplay);
display.setImageResource(images[j]);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
display = null;
System.gc();
}
}, 1000);
}
display = null;
System.gc();
after = System.currentTimeMillis();
time.setText(String.valueOf(after-before-1000* Integer.parseInt(iteractions.getText().toString())));
}
}
and this is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".ImageActivity" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="10dp"
android:padding="5dp"
android:text="Benchmark del rendering di immagini" />
<EditText
android:id="#+id/etIterations"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:hint="# di iterazioni"
android:inputType="number"
android:padding="5dp" />
<Button
android:id="#+id/bntStartCalcolation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:text="Start Calcolation" />
<ImageView android:id="#+id/ivDisplay"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_weight="50"
android:text="Risultato ottenuto in: " />
<TextView
android:id="#+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_weight="50" />
</LinearLayout>
this code dosn't work, it create an exception
OutOfMemoryError: bitmap size exceed the vm budget
anyone can help me to solve this problem?? :) thanks at all however :D
great all works :D but i've a little more questions
I don't understand why the time is always negative!!!
anyone of you know why?? [postDelayed not works?!? :( ]
solved with: android.os.SystemClock.sleep(1000);
Another error, the ImageView dosn't change after the setting of an image, any suggestions??
There is a small guide within the developer site regarding loading large bitmap files. The idea is to load lower resolution files so that they should fit in memory: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
Add the largeheap to your application in your Manifest file.
<application ... android:largeHeap="true" >
Alternatively, make your photos smaller. You might have a variety of sizes dependent on what size of screen you are using, that will make it much less likely to take all of the memory up. See this page to find Android screen resolutions.
I wrote a simple cards game where the user plays his card
doing TAP on one of the three imageviews (the cards)
I'd like to add the possibility of playing the card
by dragging the ImageView on the "table"
(the table is another layout or simply another part of the screen).
I tryed to use the techinc indicated at
http://blahti.wordpress.com/2011/01/17/moving-views-part-2/
but as it uses AbsoluteLayout, it introduces a lot of limitations
on my current layout, more it requires adjustments depending
to the device screen resolution where the app runs.
I'd like to avoid this continue using
-if possibile- the RelativeLayout.
Maybe the starting point is extenting the ImageView
adding the onTouch support but i couldn't reproduce
the wished effect (drag)
Any idea or suggestion or sample code?
Thanks!
Just to give an idea, this is the draft of layout.
The 3 cards below should be moved via drag.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="150dip"
android:gravity="center_horizontal"
android:background="#55335588"
android:layout_above="#+id/player_cards_layout"
>
<ImageView android:id="#+id/img_pc_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/player_cards_layout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="#336699"
>
<ImageView android:id="#+id/img_user_card_1"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
/>
<ImageView android:id="#+id/img_user_card_2"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_1"
/>
<ImageView android:id="#+id/img_user_card_3"
android:layout_width="100dip" android:layout_height="150dip"
android:src="#drawable/icon"
android:layout_toRightOf="#+id/img_user_card_2"
/>
</RelativeLayout>
</RelativeLayout>
here is nice example from link. i hope this will help you.
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:gravity="center" android:id="#+id/LinearLayout01">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/btn"
android:text="Drag Me"></Button>
</FrameLayout>
src/com/beanie/example/Home.java
package com.beanie.example;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.FrameLayout.LayoutParams;
public class Home extends Activity implements OnTouchListener {
private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;
private Button btn;
private FrameLayout layout;
private int status;
private LayoutParams params;
private ImageView image;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (FrameLayout) findViewById(R.id.LinearLayout01);
// layout.setOnTouchListener(this);
btn = (Button) findViewById(R.id.btn);
btn.setDrawingCacheEnabled(true);
btn.setOnTouchListener(this);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
#Override
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
status = START_DRAGGING;
image = new ImageView(this);
image.setImageBitmap(btn.getDrawingCache());
layout.addView(image, params);
}
if (me.getAction() == MotionEvent.ACTION_UP) {
status = STOP_DRAGGING;
Log.i("Drag", "Stopped Dragging");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (status == START_DRAGGING) {
System.out.println("Dragging");
image.setPadding((int) me.getRawX(), (int) me.getRawY(), 0, 0);
image.invalidate();
}
}
return false;
}
}
I'm trying to get a simple onClick to fire from an ImageButton - it seems like a simple enough task, but I'm obviously missing something here.
Here is my java file:
package com.jlbeard.android.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class testapp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//handle the button press
ImageButton mainButton = (ImageButton) findViewById(R.id.mainButton);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//show message
Toast.makeText(testapp.this, "Button Pressed", Toast.LENGTH_LONG);
}
});
}
}
Here is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/whereToEat"
android:src="#drawable/where_to_eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8px"
/>
<ImageButton
android:id="#+id/mainButton"
android:src="#drawable/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#null"
android:clickable="true"
android:onClick="mainButtonClick"
/>
</RelativeLayout>
It seems to me that I'm missing something simple... but can't seem to figure it out. Thanks!
You didn't run show() method on Toast object. Very common mistake :-)
You also might have a problem due to the manifest setting onClick
android:onClick="mainButtonClick"
If mainButtonClick exists on post 1.5 devices it may be called instead, overriding the one you're setting in code
In my case, the imageButton was displayed behind a list. Because the list was empty, the ImageButton was seen but onClick was never fired.
Adding android:elevation="5dp" in the screen xml solve my problem
Note that if I use Button instead of ImageButton, elevation is not required.