0

I have a program that draws lines in random colour! So I made a variable "colour" and 3 buttons. Every button got its value, and this value sets the colour. But the colour setting command is in another class, so I have to pass the "colour" value to the DrawArea class. So my question is, how can I do that? I tried it somehow with getter and setter but failed...

One is a Activity and one is a View

Main Activity

package com.example.drawproject;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends Activity {

Button b1; //Gelb
Button b2; //Blau
Button b3; //Grün       
public int colour = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawlayout);
    DrawArea da = new DrawArea(this, null);

    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(handler);
    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(handler);
    b3 = (Button) findViewById(R.id.button3);
    b3.setOnClickListener(handler);
}

View.OnClickListener handler = new View.OnClickListener(){

    public void onClick(View v){
        if(v==b1){

        farbe = 1;  
    }
        if(v==b2){

        farbe = 2;  
        }

        if(v==b3){

        farbe = 3;  
        }   
}};   
}

DrawArea

package com.example.drawproject;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DrawArea extends View {

private List<Stroke> _allStrokes; //all strokes that need to be drawn
private SparseArray<Stroke> _activeStrokes; //use to retrieve the currently drawn strokes
private Random _rdmColor = new Random();


int count = 1;
public DrawArea(Context context, AttributeSet attrs) {
    super(context, attrs);

    _allStrokes = new ArrayList<Stroke>();
    _activeStrokes = new SparseArray<Stroke>();
    setFocusable(true);
    setFocusableInTouchMode(true);

}


public void onDraw(Canvas canvas) {
    if (_allStrokes != null) {
        for (Stroke stroke: _allStrokes) {
            if (stroke != null) {
                Path path = stroke.getPath();
                Paint painter = stroke.getPaint();
                if ((path != null) && (painter != null)) {
                    if(count%2 != 0){
                    canvas.drawPath(path, painter);
                    }
                }
            }
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    final int action = event.getActionMasked();
    final int pointerCount = event.getPointerCount();

    switch (action) {
        case MotionEvent.ACTION_DOWN: {
            count++;
            if(count%2 != 1)
            {pointDown((int)event.getX(), (int)event.getY(), event.getPointerId(0));
            break;
            }
            if (count%2 != 0){
                for (int pc = 0; pc < pointerCount; pc++) {
                    pointDown((int)event.getX(pc), (int)event.getY(pc), event.getPointerId(pc));
        }
            }
        }
        case MotionEvent.ACTION_MOVE: {

            break;
        }

        case MotionEvent.ACTION_UP: {
            break;
        }

    }
    invalidate();
    return true;
}

private void pointDown(int x, int y, int id) {

    if(count%2 !=1){
    //create a paint with random color
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(10);

    paint.setColor(_rdmColor.nextInt()); //Here should the values be added!

    //create the Stroke
    Point pt = new Point(x, y);
    Stroke stroke = new Stroke(paint);
    stroke.addPoint(pt);
    _activeStrokes.put(id, stroke);
    _allStrokes.add(stroke);
    }

    if (count%2 != 0){
    //retrieve the stroke and add new point to its path
    Stroke stroke = _activeStrokes.get(id);
    if (stroke != null) {
        Point pt = new Point(x, y);
        stroke.addPoint(pt);
    }
    }
}
}
Aless55
  • 2,652
  • 2
  • 15
  • 26

4 Answers4

1

I think you are asking about passing values from one activity to the other. Then you need to use Intent

> In the FirstActivity

Intent intent = new Intent(CurrentActivity.this,ActivityWhereYouWantGo.class);
intent.putExtra("Variable", "value");
startActivity(intent);

When you will call the above lines,your second activity will start,and the variable value from the first activity will available in the second activity,to get it you have to do

>In the second activity

Bundle extras = getIntent().getExtras();
  if (extras != null) {
   String datas= extras.getString("Variable");
   if (datas!= null) {
        // do stuff
   }   

For sending int values as you asked in comments

in first activity use

 intent.putExtra("Variable", intvalue);

and in second activity write

int num=extras.getIntExtra("Variable");
nobalG
  • 4,544
  • 3
  • 34
  • 72
1

in your MainActivity

public class MainActivity extends Activity {

Button b1; //Gelb
Button b2; //Blau
Button b3; //Grün       
public int colour = 0;
@Override

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drawlayout);
    DrawArea da = new DrawArea(this, null);

    b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(handler);
    b2 = (Button) findViewById(R.id.button2);
    b2.setOnClickListener(handler);
    b3 = (Button) findViewById(R.id.button3);
    b3.setOnClickListener(handler);

     public static Integer getColorValue() {

      // Assign your color value
      Integer value=Whatever_value _you_want;

      return value
     }
}

to get this value in DrawArea Class

public class DrawArea extends View {

       //  get value any where in DrawAreaClass like this
    Integer Value = MainActivity.getColorValue();
}
jaimin
  • 563
  • 8
  • 25
  • Hei my friend, I wanna thank you very very much you saved my day! It's now working fine, thank you!! – Aless55 Aug 07 '14 at 12:43
  • instead of thanks you can vote for answer so that it can help others too – jaimin Aug 07 '14 at 12:44
  • Hei I've got a question to this again. I have to pass a value which changes constantly after every touch and I tried it like this but then the value stays the same and doesent change, what should I change? – Aless55 Aug 08 '14 at 08:34
0

in firstclass

intent i = new Intent(firstclass.this, secondclass.class).putextra("Key", "testing");
startActivity(i);

in secondclass

String value = getIntent.getExtra("key");
0

to pass values between two activities you need to use intent like this

Source class :

Intent intent = new Intent(this, NewActivity.class); 
myIntent.putExtra("variable ", "value");
startActivity(intent)

Destination activity :

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);    

Intent intent = getIntent();

String val= intent.getStringExtra("variable");


}

As in your case you can do this

Create a method getValue() in MaiActivity like this

public int getValue(){
{
   Int Val;

   // assign value here
   // and return it

    return Val;
 }

and from DrawArea class access it like this

Int color_value = MainActivity.getValue();

reference from enter link description here

Community
  • 1
  • 1
jaimin
  • 563
  • 8
  • 25
  • it doesn't matter that one of them is a View class? – Aless55 Aug 07 '14 at 08:04
  • An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed – jaimin Aug 07 '14 at 08:06
  • it is just like throw value from source activity and catch from destination activity. – jaimin Aug 07 '14 at 08:10
  • if you like answer than you can vote up or mark as answer if you think its the answer – jaimin Aug 07 '14 at 08:27
  • I get the sam error as in the answer above, in the line Intent intent = getIntent(); it says that The method getIntent() is undefined for the type DrawArea... – Aless55 Aug 07 '14 at 08:29
  • make sure you have imported `import android.content.Intent;` – jaimin Aug 07 '14 at 08:32
  • it is still not working... can you add these things to my code and post it, so maybe you will do something els than I... – Aless55 Aug 07 '14 at 09:03