I am writing an Android painter.
this is my main:
package com.example.hwpainterandfilemanager;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
public class MainPainter extends Activity {
MyPainter painter ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
painter = new MyPainter(getApplicationContext());
setContentView(R.layout.activity_main_painter);
}
#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_painter, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()){
case (R.id.Blue ):{
painter.mColor = Color.BLUE;
break;
}
case (R.id.Red ):{
painter.mColor=Color.RED;
break;
}
case (R.id.Yellow ):{
painter.mColor = Color.YELLOW;
break;
}
case (R.id.Green ):{
painter.mColor = Color.GREEN;
break;
}
}
return super.onOptionsItemSelected(item);
}
}
This is MyPanter:
package com.example.hwpainterandfilemanager;
import java.util.Vector;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyPainter extends View {
Vector<MyDot> allDots;
int mColor;
Paint p ;
public MyPainter(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
init();
}
public MyPainter(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
public MyPainter(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public void init(){
allDots = new Vector <MyDot> ();
mColor = Color.BLUE;
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
p= new Paint();
p.setStrokeWidth(5);
for (int i =0 ; i<allDots.size();i++){
p.setColor(allDots.get(i).mColor);
canvas.drawPoint(allDots.get(i).mX, allDots.get(i).mY, p);
}
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int x,y;
if(MotionEvent.ACTION_DOWN==event.getAction() || MotionEvent.ACTION_MOVE==event.getAction()){
x= (int) event.getX();
y= (int) event.getY();
MyDot tmp;
tmp = new MyDot(x, y, mColor);
Log.i("Painter Color", "" + mColor);
allDots.add(tmp);
invalidate();
}
// TODO Auto-generated method stub
return true;
}
}
and MyDot class:
package com.example.hwpainterandfilemanager;
public class MyDot {
int mX =0;
int mY =0;
int mColor;
public MyDot (int x, int y, int color) {
mX=x;
mY=y;
mColor = color;
}
}
when trying to change the color from blue to any other color the color will not change and stay the same.
the only way I find it working is by setting mColor as static but it is not the way i want to solve this issue.
what am I missing ?
thx,
Avner.
You're creating two instances of Painter class and you're updating wrong instance. Problem is in MyPainter activity. Change this:
painter = new MyPainter(getApplicationContext());
setContentView(R.layout.activity_main_painter);
Into this:
painter = new MyPainter(getApplicationContext());
setContentView(painter);
Related
I was trying to transfer event from one view to another button. When user touches on the view under a certain condition I wanted to transfer the event to the button. In button inside onTouchEvent I call requestLayout(). This requestLayout() gets called when I click the button but the requestLayout() does not get called when I transfer the event from the view to the button. This is my follow up question to the previous one.
/// MaintActivity.java
package com.example.mylayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#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;
}
#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);
}
}
//-------activity_main.xml
<com.example.mylayout.MyViewGroup
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">
<com.example.mylayout.MyView
android:id="#+id/myview"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/green">
</com.example.mylayout.MyView>
<com.example.mylayout.MyButton
android:id="#+id/mybutton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/Next"
android:background="#color/red">
</com.example.mylayout.MyButton>
</com.example.mylayout.MyViewGroup>
//--------MyViewGroup-------
package com.example.mylayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class MyViewGroup extends ViewGroup{
public MyViewGroup(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyViewGroup(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
}
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
// TODO Auto-generated method stub
System.out.println("I am from MyViewGroup:onLayout");
for(int i = 0; i < getChildCount(); i++){
final View child = getChildAt(i);
if(i == 0)child.layout(0,0,800,800);
if(i == 1)child.layout(10,10, 200, 200);
}
}
}
//--------------MyView-------
package com.example.mylayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View{
MyButton objbutton;
public MyView(Context context) {
// TODO Auto-generated constructor stub
super(context);
objbutton = new MyButton(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
objbutton = new MyButton(context, attrs);
// TODO Auto-generated constructor stu
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
System.out.println("I am from MyView: dispatchTouchEvent");
objbutton.dispatchTouchEvent(event);
return false;
}
}
//-------MyButton----
package com.example.mylayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;
public class MyButton extends Button{
public MyButton(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stu
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
System.out.println("I am from MyButton: dispatchTouchEvent");
requestLayout();
return super.dispatchTouchEvent(event);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
System.out.println("I am from MyButton: dispatchTouchEvent");
requestLayout();
return true;
}
void printme(){
System.out.println("I am from MyButton: printme ..");
}
}
From the comments:
According to your paste, it looks like you're creating an entire new instance of MyButton inside MyView. It doesn't know of the MyButton which you have included in the layout xml. Instead of doing that, create a setter in MyView and pass it in from MainActivity via findViewById().
Like this:
package com.example.mylayout;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View {
private MyButton objbutton;
public MyView(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
public void setMyButton(MyButton myButton) {
objbutton = myButton;
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
System.out.println("I am from MyView: dispatchTouchEvent");
if(objbutton != null)
objbutton.dispatchTouchEvent(event);
return false;
}
}
I am trying to create my first android game (in eclipse) and I cannot seem to get OnTouchListner to work, mostly because I don't know how or where to create it. I am trying to figure out where someone taps the screen. Can someone please tell me how and where to create the OnTouchListner!
Activity class:
package com.gregsapps.fallingbird;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class Game extends Activity implements OnTouchListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new GameView(this));
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.){
System.out.println("TOUCH");
}
return false;
}
}
View class:
package com.gregsapps.fallingbird;
import android.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
public class GameView extends View{
private Bird bird;
private boolean runOnce = false;
private Context context;
public GameView(Context context) {
super(context);
this.context = context;
this.setDrawingCacheEnabled(true);
// TODO add setup code
}
protected void onDraw(Canvas canvas){
update(canvas);
//TODO add drawing code
this.buildDrawingCache();
//bird.canvasImage = this.getDrawingCache(true);
canvas.drawBitmap(bird.image, bird.x, bird.y, null);
System.out.println("drawing");
invalidate();
}
private void update(Canvas canvas){
//TODO add code to update stuff
if(runOnce == false){
bird = new Bird(canvas, com.gregsapps.fallingbird.R.drawable.bird, context);
runOnce = true;
}
bird.move();
}
}
Implement it like this :-
public class Game extends Activity implements OnTouchListener{
GameView gv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gv = new GameView(this);
setContentView(gv);
gv.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
int x = event.getX();
int y = event.getY();
System.out.println("Touched view at X: " + X + " Y: " + Y );
}
return false;
}
}
You should just be able to set it on the view you create:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GameView gv = new GameView(this);
setContentView(gv);
// set the touch listener for the view
gv.setOnTouchListener(this);
}
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 need help regarding the viewpager. Basically I have a viewpager contained in a page container:
package com.example.CustomViews;
/**
* Created by imrankhan on 4/29/2015.
*/
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.graphics.Point;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import com.example.imrankhan.newlawdojo.DashboardActivity;
import com.example.imrankhan.newlawdojo.QuizActivity;
import com.example.imrankhan.newlawdojo.R;
/**
* PagerContainer: A layout that displays a ViewPager with its children that are outside
* the typical pager bounds.
*/
public class PageContainer extends FrameLayout implements ViewPager.OnPageChangeListener {
private ViewPager mPager;
boolean mNeedsRedraw = false;
private Camera mCamera = new Camera();
private int mMaxRotationAngle = 60;
private int mMaxZoom = -120;
public PageContainer(Context context) {
super(context);
init();
}
public PageContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public PageContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
//Disable clipping of children so non-selected pages are visible
setClipChildren(false);
//Child clipping doesn't work with hardware acceleration in Android 3.x/4.x
//You need to set this value here if using hardware acceleration in an
// application targeted at these releases.
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
#Override
protected void onFinishInflate() {
try {
mPager = (ViewPager) getChildAt(0);
mPager.setOnPageChangeListener(this);
} catch (Exception e) {
throw new IllegalStateException("The root child of PagerContainer must be a ViewPager");
}
}
public ViewPager getViewPager() {
return mPager;
}
private Point mCenter = new Point();
private Point mInitialTouch = new Point();
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCenter.x = w / 4;
mCenter.y = h / 4;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
//We capture any touches not already handled by the ViewPager
// to implement scrolling from a touch outside the pager bounds.
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mInitialTouch.x = (int)ev.getX();
mInitialTouch.y = (int)ev.getY();
default:
ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
break;
}
return mPager.dispatchTouchEvent(ev);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Force the container to redraw on scrolling.
//Without this the outer pages render initially and then stay static
if (mNeedsRedraw) invalidate();
}
#Override
public void onPageSelected(int position) {
View v =this.getViewPager().getChildAt(position);
}
private void transformImageBitmap(View child, Transformation t, int rotationAngle) {
mCamera.save();
final Matrix imageMatrix = t.getMatrix();;
final int imageHeight = child.getLayoutParams().height;;
final int imageWidth = child.getLayoutParams().width;
final int rotation = Math.abs(rotationAngle);
mCamera.translate(0.0f, 0.0f, 100.0f);
if ( rotation < mMaxRotationAngle ) {
float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
mCamera.translate(0.0f, 0.0f, zoomAmount);
}
mCamera.rotateY(rotationAngle);
mCamera.getMatrix(imageMatrix);
imageMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2));
imageMatrix.postTranslate((imageWidth/2), (imageHeight/2));
mCamera.restore();
}
#Override
public void onPageScrollStateChanged(int state) {
mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);
}
}
on onPageSelected event I got the view I need to zoom in when selected and zoom out after selecting another item. Please help me out. I added an image to give you an idea what I want.
http://i.stack.imgur.com/xtE89.png
i made three activities for a game i want to go from 1st to 2nd to 3rd and then go 1st 2nd 3rd again but i got confused with the whole intent thing tried all kind of combinations but for some reason i stuck always with a problem currently it goes from 1st to 2nd to 3rd to 1st and when it supposed to lunch 2nd it exits from the whole app
//1st activity
Intent discoverintent = new Intent(getBaseContext(), discoverstring.class);
//discoverintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(discoverintent);
//2nd activity
Intent MainIntent = new Intent(getBaseContext(), learningword.class);
//MainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainIntent.putExtra("text", text);
MainIntent.putExtra("speakeronoff", Integer.toString(speakeronoff));
MainIntent.putExtra("previous", Integer.toString(previous));
startActivity(MainIntent);
and the 3rd activity just uses finish
package self.yanfaingold.talkgame;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
#SuppressLint("NewApi") public class savewords extends Activity implements OnTouchListener {
OurView v;
//Intent secondintent;
Canvas c;
int cheight,cwidth,loop=0;
Bitmap notalk,notalkscaled,bittalk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v=new OurView(this);
v.setOnTouchListener(this);
//Remove title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(v);
notalk=BitmapFactory.decodeResource(getResources(), R.drawable.nottalk);
// bittalk=BitmapFactory.decodeResource(getResources(), R.drawable.talkmid);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable {
Thread t=null;
SurfaceHolder holder;
boolean isItOK=false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder=getHolder();
}
public void run(){
while(isItOK){
if(!holder.getSurface().isValid()){
continue;
}
c=holder.lockCanvas();
cheight=c.getHeight();
cwidth=c.getWidth();
scale();
drawing(c);
holder.unlockCanvasAndPost(c);
}
}
private void scale() {
// TODO Auto-generated method stub
if(loop==0)
notalkscaled=Bitmap.createScaledBitmap(notalk,(int)(cwidth),(int)(cheight), false);
}
protected void drawing(Canvas canvas){
//canvas.drawARGB(255, 255, 255, 255);
canvas.drawBitmap(notalkscaled,0,0, null);
if(loop>500){
loop=0;
wannalearnaword();
}
loop++;
}
private void wannalearnaword() {
// TODO Auto-generated method stub
//1st activity
Intent secondintent = new Intent(getApplicationContext(), discoverstring.class);
secondintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(secondintent);
}
public void pause(){
isItOK=false;
while(true){
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){
isItOK=true;
t=new Thread(this);
t.start();
}
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
}
return true;
}
}