Display touch coordinates on screen - android

My objective is to have the user touch anywhere on the screen, and then to have the touch coordinates appear in a textview on the screen.
This is what I have so far.
activity_main.xml:
<LinearLayout 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:orientation="vertical"
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" >
<TextView
android:id="#+id/touchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="To be filled" />
</LinearLayout>
Main Activity.java:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.MyFirstApp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView1);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
#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;
}
}
SOLVED My first problem is that on MainActivity.java, there is an error on line 18, in the code setContentView(R.layout.main);. Researching online, it looks like the way to fix this error is to change import android.R; to import your.application.packagename.R;. However, I do not have import android.R; in my .java file. Does anyone know how to fix this error?
EDIT Second, the textView only displays the touch coordinates when the first textview is clicked, it does not display the touch coordinates of a touchpoint anywhere on the screen (it only displays the touch coordinates of a touch point on the first text view). How can I change this so that the touch coordinates of anywhere on the screen are displayed?
Another Edit One idea I had for the second problem was giving the LinearLayout I had an id, such as android:id="#+id/touchLayout". I was then planning on modifying the .java file as such:
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.MyFirstApp.MESSAGE";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView)findViewById(R.id.textView1);
// this is the view on which you will listen for touch events
final View touchLayout = findViewById(R.id.touchLayout);
touchLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}
#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;
}
}

Change the following:
setContentView(R.layout.activity_main);

Yup, the answer I had in the second edit was correct. I simply replaced gave the LinearLayout an id, and replaced the touchView id with the LinearLayout id

Related

I am having slideshow code in ImageView, But in addition to that I need to navigate to next next image while swiping

package cppandi.apjquotes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class quotes extends AppCompatActivity {
int mFlipping = 0 ; // Initially flipping is off
Button mButton ; // Reference to button available in the layout to start and stop the flipper
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quotes);
/** Click event handler for button */
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if (mFlipping == 0) {
/** Start Flipping */
flipper.startFlipping();
mFlipping = 1;
mButton.setText(R.string.str_btn_stop);
} else {
/** Stop Flipping */
flipper.stopFlipping();
mFlipping = 0;
mButton.setText(R.string.str_btn_start);
}
}
};
/** Getting a reference to the button available in the resource */
mButton = (Button) findViewById(R.id.btn);
/** Setting click event listner for the button */
mButton.setOnClickListener(listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
I need to navigate to next corresponding image when user swipe the screen. Then what code should I use?
Here is my xml page with ViewFlipper and Imageview.
<?xml version="1.0" encoding="utf-8"?>
<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:background="#drawable/background">
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/str_btn_start" />
<ViewFlipper
android:id="#+id/flipper1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:flipInterval="3000"
android:inAnimation="#anim/slide_in_right"
android:outAnimation="#anim/slide_out_left"
android:layout_centerInParent="true" >
//25 Images in imageview
</ViewFlipper>
</RelativeLayout>
Then I need to know the usage of inAnimation and outAnimation.

Getting "Unfortunately, app has stopped" while executing

I am learning Android dev in Android Studio.
I created an app which displays some text on fling and some other on click of a button.
But its showing "Unfortunately app has stopped"
Also, I have only one activity. So, its not possible that I forgot to show it in AndroidManifest.xml
here is my java code :
package com.example.mahe.pro2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Button;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.support.v4.view.GestureDetectorCompat;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
private TextView tv = (TextView) findViewById(R.id.t);
private GestureDetectorCompat gd;
private Button bt = (Button) findViewById(R.id.b);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.gd = new GestureDetectorCompat(this,this);
bt.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
tv.setText("That was a Click!");
}
}
);
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
}
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
tv.setText("That was a Swipe, Baby");
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
this.gd.onTouchEvent(event);
return super.onTouchEvent(event);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here's my 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="YO!"
android:id="#+id/t"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="#+id/b"
android:layout_below="#+id/t"
android:layout_centerHorizontal="true"
android:layout_marginTop="164dp" />
Just move
TextView tv = (TextView) findViewById(R.id.t);
Button bt = (Button) findViewById(R.id.b);
under onCreate(...) after setContentView(R.layout.activity_main);
as #M D said
you must first create your view and setview to can get R.id.b
but if you want to declare them as a global variable , use
private TextView tv;
private Button bt
above the onCreate Method , then in onCreate method and under
setContentView(R.layout.activity_main);
use
tv = (TextView) findViewById(R.id.t);
bt = (Button) findViewById(R.id.b);

touch event when finger moves to next view

I am dragging my finger and it moves to the next view. Both views implement setOnTouchListener().
How to find out that the finger is now on second view.
ACTION_MOVE is getting returned for first view even though my finger is on second view.
Do I need to implement any other listener?
<LinearLayout 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="com.example.textview.MainActivity" >
<TextView
android:id = "#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:id = "#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world1" />
<TextView
android:id = "#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world2" />
</LinearLayout>
MainActivity.java:
package com.example.textview;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt1 = (TextView) findViewById(R.id.text1);
txt1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
TextView txt1 = (TextView) findViewById(v.getId());
Log.d("txt1.getText()",txt1.getText().toString());
return false;
}
});
txt1 = (TextView) findViewById(R.id.text2);
txt1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
TextView txt1 = (TextView) findViewById(v.getId());
Log.d("txt1.getText()",txt1.getText().toString());
return false;
}
});
txt1 = (TextView) findViewById(R.id.text3);
txt1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
TextView txt1 = (TextView) findViewById(v.getId());
Log.d("txt1.getText()",txt1.getText().toString());
return false;
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can check for the current MotionEvent x and y is in the 2nd view hit rect
Rect viewHitRect = new Rect();
secondView.getHitRect(viewHitRect);
if(viewHitRect.contains((int) motionEvent.getX(), (int) motionEvent.getY())) {//You are in 2nd view}
When finger is coming on textview2, it will get ACTION_HOVER_ENTER and
textview1 will get ACTION_HOVER_EXIT
Check on these parameters, ACTION_MOVE will be given to textview1 as you mentioned but hover parameter clearly decide which textfield is under finger at this time
UPDATE :
If finger is clicked on views then you will get ACTION_DOWN on textview2 and parallely textview1 will get ACTION_UP

Emulator not displaying image on button click android

Please bear with me on this I'm new to android programming. I was trying to show an image on a button click in android but unfortunately my emulator is not showing the required output. Here is my layout.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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:text="Button" />
</RelativeLayout>
main_activity.java
package com.example.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
image.setImageResource(R.drawable.optimus);
}
});
}
}
I have included the image file under /res/drawable-mdpi. When I try running on emulator it does not show image on button click. Is there any problem with the code?
In your onCreate() method write this please :
Button theButton = (Button)findViewById(R.id.button1);
theButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
theButton.setBackgroundResource(R.drawable.optimus);
}
Assuming that you have an ImageView called imageView1,
You have to assign it, before using it.
So change this:
#Override
public void onClick(View arg0)
{
image.setImageResource(R.drawable.optimus);
}
to this
#Override
public void onClick(View arg0)
{
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.optimus);
}
You have to write onclick method like this code and also you need to call addListenerOnButton method in the onCreate() method
package com.example.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
#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;
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
button.setBackgroundResource(R.drawable.optimus);
}
});
}
}
Try This:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/background" <!-- you're adding this here -->
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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:onClick="showImage" <!-- you're adding this here -->
android:text="Button" />
</RelativeLayout>
Then in the .java file...
Package com.example.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
public void showImage(View view) {
RelativeLayout background = (RelativeLayout) findViewById(R.id.background);
background.setBackgroundResource(R.drawable.optimus);
} /* This is a whole new section of code to replace the onClick listener */
}
Since you're already using the XML Layouts, try to use the built in onClick listener whenever possible. You can do this by naming a function in your .java file called yourFunctionName with a single argument of (View view).
Then, in your XML, you need to point to it by adding android:onClick="yourFunctionName" for the view that needs the onClick method.
Lastly, you never had the image you were referring to actually pointing to anything. You simply said that the object "image" now used "R.drawable.optimus" but that Image was never used! So instead, I just pointed to placing it in the background of your RelativeView. If instead you wanted it in a separate image, you'll have to add an ImageView to the XML file and set the background resource for the ImageView the same way.
Best of luck on learning Android!

Android: My Help Page comes up BLANK!

I am trying to create a very simple help page in my app. The layout xml for the page contains only a textview that displays the help info. In the Eclipse layout view, the layout looks perfect... however, when i try to load it in the emulator (via an OptionsMenu) it comes up as a blank black screen. What am I doing wrong??
Here is the Code:
//This is my Help page code
package com.soapbox.servicerec;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class HelpPage extends Activity {
private static final int INSERT_ID = Menu.FIRST;
private static final int ACTIVITY_CREATE=0;
protected void onCreate(){
setContentView(R.layout.help_page);
setTitle(R.string.help_title);
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void createNote() {
Intent i = new Intent(this, NoteEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
}
AND the 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"
android:background="#android:color/white">
<TextView android:textSize="16px" android:textColor="#android:color/black" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:gravity="center_horizontal" android:paddingLeft="10px"
android:paddingRight="10px" android:text="#string/help_text"/>
</LinearLayout>
You must have your onCreate() function defined as ::
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help_page);
setTitle(R.string.help_title);
}
Your onCreate method signature is wrong.
notice the difference below
your method protected void onCreate()
proper way #Override public void onCreate(Bundle savedInstanceState)
The #Override notation is optional, but very useful because it ensure the method signature matches one from a parent class, helping prevent these situations.

Categories

Resources