I've created 2 separate projects in Android from tutorials etc., - one which runs an animation of an horse running, and the other a scrolling background. Both work well - but because they implement separate Activities, how do I manage to run both inside one project?? ie the animation of the horse on the top of the scrolling background?
Here is my code for the separate projects:
FrameAnimateActivity
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class FrameAnimatedActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_animated);
ImageView gyroView = (ImageView) findViewById(R.id.gyro);
//set the animation drawable as background
gyroView.setBackgroundResource(R.drawable.gyro_animation);
//create an animation drawable using the background
AnimationDrawable gyroAnimation = (AnimationDrawable) gyroView.getBackground();
//start the animation
gyroAnimation.start();
ImageView img_animation = (ImageView) findViewById(R.id.gyro);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(5000); // animation duration
animation.setRepeatCount(5); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
img_animation.startAnimation(animation); // start animation
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.frame_animated, 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);
}
}
And the code for the scrolling background:
MainActivity
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MovingBackGround(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MovingBackground.java
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MovingBackGround extends SurfaceView implements
SurfaceHolder.Callback {
private Bitmap backGround;
public MovingBackGround(Context context) {
super(context);
backGround = BitmapFactory.decodeResource(context.getResources(),
R.drawable.ab);
setWillNotDraw(false);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
doDrawRunning(canvas);
invalidate();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
/**
* Draws current state of the game Canvas.
*/
private int mBGFarMoveX = 0;
private int mBGNearMoveX = 0;
private void doDrawRunning(Canvas canvas) {
// decrement the far background
mBGFarMoveX = mBGFarMoveX - 1;
// decrement the near background
mBGNearMoveX = mBGNearMoveX - 4;
// calculate the wrap factor for matching image draw
int newFarX = backGround.getWidth() - (-mBGFarMoveX);
// if we have scrolled all the way, reset to start
if (newFarX <= 0) {
mBGFarMoveX = 0;
// only need one draw
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
} else {
// need to draw original and wrap
canvas.drawBitmap(backGround, mBGFarMoveX, 0, null);
canvas.drawBitmap(backGround, newFarX, 0, null);
}
}
}
Related
I am creating a a puzzle match game where in order to match two buttons together you must swap the position of them. for example, i want to swap the position of button1 with the position that button2 is in. Anybody have any suggestions?
Here is my code below:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class puzzle extends ActionBarActivity {
private TextView moveCounter;
private TextView feedbackText;
private Button[] buttons;
private Boolean bad_move=false;
// private static final Integer[] goal = new Integer[] {0,1,2,3,4,5,6,7,8};
// private String [][] puz = new String [3][3];
Button b [][];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_puzzle);
swap();
}
private void setBoard(){
b = new Button[3][3];
b[0][0]= (Button).findViewById(R.id.button1);
b[0][1]= (Button).findViewById(R.id.button2);
b[0][2] = (Button).FindById(R.id.button3);
b[1][0] = (Button).FindBdyId(R.id.button4);
b[1][1] = (Button).FindById(R.id.button5);
b[1][2] = (Button).FindById(R.id.button6);
b[2][0] = (Button).FindById(R.id.button7);
b[2][1] = (Button).FindById(R.id.button8);
b[2][2] = (Button).FindById(R.id.button9);
}
private void swap() {
b.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
}
#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_puzzle, 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);
}
}
If you are using API level greater than 11, you can use getX() and getY() to get coordinates of a button.
And setX() and setY() to set a button in a particular coordinate.
for example,
// getting coordinates of button
float posX = button.getX();
float posY = button.getY();
// setting button2 in the coordinates that we got above
button2.setX(posX);
button2.setY(posY);
The way I've gotten this work is similar to your approach you had originally posted. Try giving this a shot:
Button btnOne = (Button) findViewById(R.id.buttonOne);
Button btnTwo = (Button) findViewById(R.id.buttonTwo);
float posOneX = btnOne.getX();
float posOneY = btnOne.getY();
float posTwoX = btnTwo.getX();
float posTwoY = btnTwo.getY();
btnOne.setX(posTwoX);
btnOne.setY(posTwoY);
btnTwo.setX(posOneX);
btnTwo.setY(posOneY);
I want to get the gray scale value of a pixel in OpenCV for Android. The problem is that OpenCV for Android is very much undocumented and a lot of the methods are different. I have found answers to this problem, but none of them work for android.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.hardware.camera2.CameraCaptureSession;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.JavaCameraView;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;
public class testActivity extends AppCompatActivity implements CvCameraViewListener2 {
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status){
switch (status){
case LoaderCallbackInterface.SUCCESS:
{
mOpenCvCameraView.enableView();
break;
}
default:
{
super.onManagerConnected(status);
}
}
}
};
private JavaCameraView mOpenCvCameraView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mOpenCvCameraView = (JavaCameraView) findViewById(R.id.testVideoView);
mOpenCvCameraView.setMaxFrameSize(240,320);
mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
mOpenCvCameraView.setCvCameraViewListener(this);
}
#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();
getSupportActionBar().hide();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onDestroy() {
super.onDestroy();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
public void onResume(){
super.onResume();
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9, this, mLoaderCallback);
} else {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
#Override
public void onPause() {
super.onPause();
if (mOpenCvCameraView != null)
mOpenCvCameraView.disableView();
}
#Override
public void onCameraViewStarted(int width, int height) {
}
#Override
public void onCameraViewStopped() {
}
#Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
return inputFrame.gray();
}
}
I'll assume you mean "how to extract gray pixel from RGB Mat", because getting the gray pixel from Mat that represents a grayscale image is trivial.
What you can do is define submat of size 1x1
for instance
Mat bgrPixel= rgbMat.submat(x,y,x,y).clone();
Mat grayPixel = new Mat(1,1, CvType.CV_8UC1);
Imgproc.cvtColor(bgrPixel, grayPixel, Imgproc.COLOR_BGR2GRAY);
(maybe the indexes are x,y,x+1,y+1, I'm not sure)
after that you can use your gray pixel.
It's not an elegant solution but it should work.
All the class is posted serially. This program was correct till the background of "GameActivity" was set by creating a custom view. But after then when i want to add single robot with every single touch in android device, it shows "an unexpected error".
WelcomeActivity
package com.emdad.androidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class WelcomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
final Button startButton=(Button) findViewById(R.id.androidButton);
startButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
startButton.getBackground().setAlpha(100);
break;
case MotionEvent.ACTION_UP:
Intent intent=new Intent(WelcomeActivity.this, GameActivity.class);
startActivity(intent);
startButton.getBackground().setAlpha(255);
break;
case MotionEvent.ACTION_MOVE:
break;
default:
break;
}
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.welcome, 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);
}
}
GameActivity
package com.emdad.androidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
public class GameActivity extends Activity {
GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
gameView=new GameView(this);
setContentView(gameView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, 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);
}
}
GameView
package com.emdad.androidapp;
import java.util.Random;
import android.content.Context;
import android.graphics.Point;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements Callback{
Context context;
SurfaceHolder surfaceHolder;
DrawingThread drawingThread;
public GameView(Context context) {
super(context);
this.context=context;
surfaceHolder=getHolder();
surfaceHolder.addCallback(this);
drawingThread=new DrawingThread(this, context);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
try {
drawingThread.start();
} catch (Exception e) {
restartThread();
}
}
private void restartThread() {
drawingThread.stopThread();
drawingThread=null;
drawingThread=new DrawingThread(this, context);
drawingThread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
drawingThread.stopThread();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
Point touchPoint=new Point();
touchPoint=new Point((int)event.getX(), (int)event.getY());
Random random=new Random();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawingThread.allRobots.add(new Robot(drawingThread.allPossibleRobots.get(random.nextInt(6)), touchPoint));
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
drawingThread.allRobots.get(drawingThread.allRobots.size()-1).setCenter(touchPoint);
break;
default:
break;
}
return super.onTouchEvent(event);
}
}
DrawingThread
package com.emdad.androidapp;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;
public class DrawingThread extends Thread{
private Canvas canvas;
private GameView gameView;
private Context context;
boolean threadFlag=false;
Bitmap backgroundBitmap;
int displayX, displayY;
ArrayList<Robot> allRobots;
ArrayList<Bitmap> allPossibleRobots;
public DrawingThread(GameView gameView, Context context) {
super();
this.gameView = gameView;
this.context = context;
initializaeAll();
}
private void initializaeAll() {
WindowManager windowManager=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display defaultDisplay=windowManager.getDefaultDisplay();
Point displayDimension=new Point();
defaultDisplay.getSize(displayDimension);
displayX=displayDimension.x;
displayY=displayDimension.y;
backgroundBitmap=BitmapFactory.decodeResource(context.getResources(), R.drawable.background1);
backgroundBitmap=Bitmap.createScaledBitmap(backgroundBitmap, displayX, displayY, true);
initializeAllPossibleRobots();
}
private void initializeAllPossibleRobots() {
allRobots=new ArrayList<Robot>();
allPossibleRobots=new ArrayList<Bitmap>();
allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android));
allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android2));
allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android3));
allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.android6));
allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.apple));
allPossibleRobots.add(giveResizedRobotBitmap(R.drawable.apple2));
}
private Bitmap giveResizedRobotBitmap(int resourceID) {
Bitmap tempBitmap=BitmapFactory.decodeResource(context.getResources(), resourceID);
tempBitmap=Bitmap.createScaledBitmap(tempBitmap, displayX/5, (tempBitmap.getHeight()/tempBitmap.getWidth())*(displayX/5), true);
return tempBitmap;
}
#Override
public void run() {
threadFlag=true;
while (threadFlag) {
canvas=gameView.surfaceHolder.lockCanvas();
try {
synchronized (canvas) {
updateDisplay();
}
} catch (Exception e) {
e.printStackTrace();
} finally{
if (canvas!=null) {
gameView.surfaceHolder.unlockCanvasAndPost(canvas);
}
}
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void updateDisplay() {
canvas.drawBitmap(backgroundBitmap, 0, 0, null);
for (int i = 0; i < allRobots.size(); i++) {
Robot temprRobot=allRobots.get(i);
canvas.drawBitmap(temprRobot.robotBitmap, temprRobot.centerX-(temprRobot.width/2), temprRobot.centerY-(temprRobot.height/2), temprRobot.robotPaint);
}
}
public void stopThread() {
threadFlag=false;
}
}
Robot
package com.emdad.androidapp;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Point;
public class Robot {
int centerX, centerY;
int height, width;
Bitmap robotBitmap;
Paint robotPaint;
public Robot(Bitmap bitmap) {
robotBitmap=bitmap;
centerX=centerY=0;
height=robotBitmap.getHeight();
width=robotBitmap.getWidth();
robotPaint=new Paint();
}
public Robot(Bitmap bitmap, int cX, int cY) {
this(bitmap);
centerX=cX;
centerY=cY;
}
public Robot(Bitmap bitmap, Point center) {
this(bitmap, center.x, center.y);
}
public void setCenter(Point centerPoint) {
centerX=centerPoint.x;
centerY=centerPoint.y;
}
}
I am working in Eclipse and struggling with a certain step in my assignment so any help would be appriciated because I actually dont know how to do it even though I realise how glaringly simple it probably is...
I have a shopping app and I have created my CheckoutActivity... now... it needs to start when the user clicks the checkout button in my CartActivity.
I need to add code in the ocCreate method of my CartActivity (which I will provide below) that specifies the listener for the Checkout button. then I need to define a button variable named btn inside the onCreate. I then need to assign the button element from the view using the id that you specified in my activity_cart.xml layout.
All I have so far is this... and I have no idea what to do...
btn.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
//I cant figure out how to get it to start the CheckoutActivity
}
});
Just in case you need to see the code I have so far... here is the code I have for my cartActivty
package uk.ac.uk.st265.shopper;
import java.text.DecimalFormat;
import java.util.List;
import java.util.ResourceBundle.Control;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class CartActivity extends Activity {
CartListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
TextView text = (TextView) findViewById(R.id.total_price); // its not in
// XML
DecimalFormat df = new DecimalFormat("#.00");
text.setText("£"
+ df.format(((ShopperApp) getApplication()).getCartTotal()));
adapter = new CartListAdapter(this);
ListView cartList = (ListView) findViewById(R.id.cart_list);
adapter.setItemList(((ShopperApp) getApplication()).cart);
cartList.setAdapter(adapter);
// Show the Up button in the action bar.
setupActionBar();
//work5ass2part6
//btn.setOnClickListener(new OnClickListener() {
//public void onClick(final View v) {
// I cant figure out how to get it to start the CheckoutActivity
//}
//});
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(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.cart, menu);
return true;
}
public class CartListAdapter extends BaseAdapter {
private final Context context;
private List<Product> itemList;
public CartListAdapter(Context c) {
context = c;
}
public void setItemList(List<Product> itemList) {
// this.itemList = itemList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View cell = convertView;
if (cell == null) {
// get layout from mobile xml
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
cell = inflater.inflate(R.layout.adapter_cart, parent, false);
}
Product p = itemList.get(position);
// set value into text view according to position
TextView textView = (TextView) cell
.findViewById(R.id.product_title);
textView.setText(p.getProductName());
textView = (TextView) cell.findViewById(R.id.product_info);
textView.setText("Price " + p.getPrice());
// set value into image view according to position
ImageView imgView = (ImageView) cell
.findViewById(R.id.product_image);
// clear the image
imgView.setImageDrawable(null);
// and load from the network
p.loadImage(imgView, 54, 54);
return cell;
}
public List<Product> getItemList() {
return itemList;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
case R.id.show_cart:
// Create the intent for the cart activity
Intent intent = new Intent(getApplicationContext(),
CartActivity.class);
startActivity(intent);
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
and here is the code I have so far on my CheckoutActivity:
package uk.ac.uk.st265.shopper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class CheckoutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(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.checkout, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
// myIntent.putExtra("key", value); //if you want to pass parameter
CurrentActivity.this.startActivity(myIntent);
thank you so much for reading. I have a textView that you can pinch zoom, and when I add "textview.setMovementMethod(new ScrollingMovementMethod());" to my code I can also scroll the zoomed textview which is exactly what I need. The problem is as soon as the text is zoomed, only the scrolling works but the pinch zoom doesnt work anymore, (unless I place one of my fingers outside the textview and the other inside). Any help will be really appreciated. I got the code from here: Pinch Zooming TextView
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView scaleGesture;
ScaleGestureDetector scaleGestureDetector;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleGesture = (TextView)findViewById(R.id.article);
scaleGesture.setText("this is some text");
scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
public class simpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
#Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
float size = scaleGesture.getTextSize();
Log.d("TextSizeStart", String.valueOf(size));
float factor = detector.getScaleFactor();
Log.d("Factor", String.valueOf(factor));
float product = size*factor;
Log.d("TextSize", String.valueOf(product));
scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
size = scaleGesture.getTextSize();
Log.d("TextSizeEnd", String.valueOf(size));
return true;
}
}
}`
I just resolve this problem (with the same example) only add one "if" in my code:
#Override
public boolean onTouchEvent(MotionEvent event) {
// get how many fingers is in touch (in that case, if are more
// than 1, the user don't want do the scroll
if (event.getPointerCount() > 1) {
scaleGestureDetector.onTouchEvent(event);
return true;
}
return false;
}