Custom View Scrolling issues Android - java

I've been designing an application that requires the use of a scrollbar on a custom view. The problem that I have run into is that my custom view's height will constantly change when new items are added by the user. I suspect that this isn't a very unusual problem, but to complicate things, I'm not actually adding new objects to my custom view, I'm just drawing Bitmap images on the canvas. The reason this is troublesome to me is when Android inflates the layout, it sets the size to be whatever I tell it (0dip) and as I add the Bitmaps, it simply changes the size to fill the Scrollview; thereby, whenever the Custom View draws an image beyond the current height constraints, instead of scrolling, the app simply cuts the picture off to maintain the current height.
(This is for a card game app, so I wanted to fan out your current deck in a easy to view and edit fashion.)
Here is the relevant code :
public class DeckEdit extends Activity implements FilterQueryProvider {
static final int PROGRESS_DIALOG = 0;
private Deck deck;
private CardListAdapter lstAdpt;
private ArrayList<CardImage> cards;
private ProgressDialog progressDialog;
private ProgressThread progressThread;
private String[] cardList;
private DeckEditCardView deckGrid;
private ScrollView sc;
protected Cursor cursor;
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
public void onCreate(Bundle savedInstanceBundle){
super.onCreate(savedInstanceBundle);
deck = new Deck();
//This is what stores the Card Images that I draw to the canvas with
cards = new ArrayList<CardImage>();
setContentView(R.layout.deck_edit_layout);
sc = (ScrollView) findViewById(R.id.deck_scroller);
deckGrid = (DeckEditCardView) findViewById(R.id.deck_grid);
filterText = (EditText) findViewById(R.id.card_search_box);
filterText.addTextChangedListener(filterTextWatcher);
pickDeck();
}
And then this is called after I load the deck
private void finishSetup(){
//this is where I set up the deckGrid to have all the cards and then
deckGrid.setCards(cards);
//draw them to the screen
deckGrid.invalidate();
//This is where I realized that the height value might be causing the problem
Log.d("finishSetup()", "deckGrid width: "+deckGrid.getWidth());
Log.d("finishSetup()", "deckGrid height: "+deckGrid.getHeight());
}
The DeckEditCardView class contains this
public class DeckEditCardView extends View implements OnTouchListener {
private ArrayList<CardImage> cards;
private int X_COLS;
#SuppressWarnings("unused")
private int Y_ROWS;
private final int COL_WIDTH;
private final int ROW_HEIGHT;
private Context context;
private final int X_PADDING = 2;
private final int Y_PADDING = 2;
private final int X_CARD_OVERLAY = 7;
private final int Y_CARD_OVERLAY = 5;
public DeckEditCardView(Context context) {
super(context);
cards = new ArrayList<CardImage>();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 1: "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 1: "+ROW_HEIGHT);
}
public DeckEditCardView(Context context, AttributeSet attrs) {
super(context, attrs);
cards = new ArrayList<CardImage>();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 2 : "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 2: "+ROW_HEIGHT);
}
/*
public DeckEditCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
cards = new ArrayList<CardImage>();
int width = getWidth();
int height = getHeight();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 3: "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 3: "+ROW_HEIGHT);
X_COLS = (width/COL_WIDTH);
Y_ROWS = (height/ROW_HEIGHT);
}
*/
#Override
public void onFinishInflate(){
super.onFinishInflate();
//this.
}
public void onSizeChanged(int w, int h, int oldw, int oldh){
int width = getWidth();
int height = getHeight();
X_COLS = (width/COL_WIDTH);
Y_ROWS = (height/ROW_HEIGHT);
}
public Point getNextCardPoint(Point curP, boolean sameCard){
Point p=null;
if (sameCard)
p = new Point(curP.x+X_CARD_OVERLAY,curP.y+Y_CARD_OVERLAY);
else { //need to be on the same column
if (point2Col(curP)+1<X_COLS)
p = new Point(COL_WIDTH*(point2Col(curP)+1)+X_PADDING,
ROW_HEIGHT*(point2Row(curP))+Y_PADDING);
else {//add to new column
if (point2Row(curP)+1 > Y_ROWS)
Y_ROWS++;
p = new Point(X_PADDING,
ROW_HEIGHT*(point2Row(curP)+1)+Y_PADDING);
}
}
return p;
}
public void setCards(ArrayList<CardImage> cards){
this.cards = cards;
}
private int point2Col(Point p){
return p.x / COL_WIDTH;
}
private int point2Row(Point p){
return p.y / ROW_HEIGHT;
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//ScrollView s = new ScrollView(context);
//s.addView(canvas);
int length = cards.size();
CardImage c;
for (int i = 0; i < length; i++) {
//Log.d("onDraw", "output a card");
c = cards.get(i);
canvas.drawBitmap(c.getBitmap(), c.getX(), c.getY(), null);
}
}
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
So in conclusion, I don't know how to allow the user to scroll and see the cards that don't fit on the screen (its almost guaranteed that a fully built deck will take a scrollbar to see all of them. At most with this code it'll put in all the card images I need, but the ones that go of screen I don't know what to do :/
And lastly my Layout.xml file
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="#+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="#+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="#+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>

Your layout doesn't work properly because the DeckEditCardView doesn't measure itself. You must override the View.onMeasure() method in the DeckEditCardView class and set measured size depending on view's content. In your layout xml file you must specify wrap_content as a height of the DeckEditCardView.

Related

How to prevent user for going new line when the current line is empty in edittext android?

I have this code for printing bullet and number in my edit text
XML File
<com.example.nutrifood.myEditText
android:id="#+id/mealIngredientsInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="true"
android:fontFamily="#font/hindmadurai_regular"
android:gravity="top"
android:inputType="textMultiLine"
android:minLines="4"
android:paddingStart="25dp"
android:paddingEnd="20dp"
android:textColor="#color/light"
android:textSize="16sp" />
myEditText.java
public class myEditText extends androidx.appcompat.widget.AppCompatEditText {
public Rect rect = new Rect();
public Paint paint = new Paint();
private myEditText editTextinput = findViewById(R.id.editTextinput);
boolean isTyping;
public myEditText(Context context, AttributeSet attrs) {
super(context, attrs);
...
editTextInput.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
if (text.length() == 0) {
isTyping = false;
return;
}
isTyping = true;
}
...
});
}
#Override
protected void onDraw(Canvas canvas) {
if (isTyping) {
int baseline = getBaseline();
for (int i = 0; i < getLineCount(); i++) {
canvas.drawText(" •", rect.left, baseline, paint);
baseline += getLineHeight();
}
}
super.onDraw(canvas);
}
}
This will give me the output like this.
• One
• Two
•
•
•
Is there a way to disable the new line in keyboard when the current line is empty?
There is no way to disable keys in the keyboard, especially not conditionally. You can use digits in the xml, but all that does is apply a textwatcher which eliminates changes that aren't what it expects, and its not conditional.
You can get what you want fairly easily though. Create a new TextWatcher with the following function:
void onTextChanged (CharSequence s,
int start,
int before,
int count) {
String newText = s.toString().replace("\n\n","\n");
if(newText.charAt(0) == '\n') {
newText = newText.substring(1);
}
if(!s.toString().equals(newText)) {
setText(newText);
}
}
Basically, that looks for any double newlines an replaces it with single newlines. Then it chops off any leading newlines. If either of those things changes the string (if any of them were found) it sets the text to that new string formed by fixing them. If not, it exits without changing the text.

How do I set a RecyclerView using GridLayoutManager to match the device screen width?

I have a RecyclerView that I want to always have 3 columns. For some reason the width when using a GridLayoutManager seems to be a static width and I'm not sure how to get it to match the screen.
On iOS it is easy as I use a UICollectionView and auto layout manages that like so:
How do I get the same result on Android? Here is my content_symptom_tracker.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
android:background="#color/bzPrimary"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".SymptomTracker"
tools:showIn="#layout/activity_symptom_tracker">
<com.cryptixltd.peterruppert.brainzaps.GridRecyclerView
android:id="#+id/symptomRecycler"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:foregroundGravity="center"
android:layoutAnimation="#anim/grid_layout_animation_from_bottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Here is how I setup the Recycler in the Activity:
recyclerView = findViewById(R.id.symptomRecycler);
int numberOfColumns = 3;
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
adapter = new SymptomAdapter(this, common_symptoms);
recyclerView.setAdapter(adapter);
But it gives me this result, the same width no matter the device:
I don't want to change the size of the icons, just make the spacing of the width match the parent basically.
Thanks!
EDIT: Here is the GridRecyclerView class, it is a custom class to help with the Grid Animations:
public class GridRecyclerView extends RecyclerView {
public GridRecyclerView(Context context) { super(context); }
public GridRecyclerView(Context context, AttributeSet attrs) { super(context, attrs); }
public GridRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); }
#Override
protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params,
int index, int count) {
final LayoutManager layoutManager = getLayoutManager();
if (getAdapter() != null && layoutManager instanceof GridLayoutManager){
GridLayoutAnimationController.AnimationParameters animationParams =
(GridLayoutAnimationController.AnimationParameters) params.layoutAnimationParameters;
if (animationParams == null) {
// If there are no animation parameters, create new once and attach them to
// the LayoutParams.
animationParams = new GridLayoutAnimationController.AnimationParameters();
params.layoutAnimationParameters = animationParams;
}
// Next we are updating the parameters
// Set the number of items in the RecyclerView and the index of this item
animationParams.count = count;
animationParams.index = index;
// Calculate the number of columns and rows in the grid
final int columns = ((GridLayoutManager) layoutManager).getSpanCount();
animationParams.columnsCount = columns;
animationParams.rowsCount = count / columns;
// Calculate the column/row position in the grid
final int invertedIndex = count - 1 - index;
animationParams.column = columns - 1 - (invertedIndex % columns);
animationParams.row = animationParams.rowsCount - 1 - invertedIndex / columns;
} else {
// Proceed as normal if using another type of LayoutManager
super.attachLayoutAnimationParameters(child, params, index, count);
}
}
}
try to set the GridRecyclerView to match constraints.
in your case just make android:layout_width=0dp.

Coloring TextView during a certain time period Android

Is there a way to color text in TextView or something else in Android during certain time period. So it would start off with fully white text and then the coloring would move from left to right and fill it up during a certain duration.
So for example if the duration would be 10 then the whole line should be color in 10 seconds, but it should also move with the same pace.
Which would look something like this:
There is a way to do it on IOS with CATextLayer, but I haven't yet found a way to do it on Android.
I'm just creating a self-defined TextView in last year,here is my class
public class AKChangeColorTextView extends TextView {
public AKChangeColorTextView(Context context) {
this(context,null);
}
String TAG = "AKChangeColorTextView";
public AKChangeColorTextView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
RectF mRectF;
float mX;
float mY;
public AKChangeColorTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLUE);
PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
mPaint.setXfermode(mode);
float x = 60;
float y = 10;
mY = 0;
mRectF = new RectF(x, y, x + 50, y + 50);
mTPaint = getPaint();
mX = 0;
}
Paint mPaint;
TextPaint mTPaint;
Bitmap shadowBitmap ;
Rect bounds = new Rect();
Canvas textCanvas;
#Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
if (shadowBitmap == null) {
shadowBitmap = Bitmap.createBitmap(getMeasuredWidth(),getMeasuredHeight(), Bitmap.Config.ARGB_8888);
}
if (textCanvas == null) {
textCanvas = new Canvas(shadowBitmap);
}
textCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
if (mTPaint == null) {
mTPaint = getPaint();
}
String content = getText().toString();
mTPaint.getTextBounds(content,0,content.length(),bounds);
textCanvas.drawText(content,0,bounds.height(),mTPaint);
mRectF.set(colorLeft,mY,colorRight,mY+bounds.height()+10);
textCanvas.drawRect(mRectF,mPaint);
canvas.drawBitmap(shadowBitmap,0,0,null);
}
float colorLeft;
float colorRight;
public void setXOffset(float left,float right){
colorLeft = left;
colorRight = right;
invalidate();
}
}
my demo code:
class MainActivity : AppCompatActivity() {
val TAG = "MainActivity"
lateinit var countDownTimer:CountDownTimer
var currentOffsetx = 0
var textWidth = 0
var isIncrease = true
lateinit var txt:AKChangeColorTextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
(findViewById<View>(R.id.tv_hello) as AKChangeColorTextView).apply{
txt = this
}
countDownTimer = object:CountDownTimer(300000,200){
override fun onFinish() {
}
override fun onTick(millisUntilFinished: Long) {
if (textWidth == 0) {
textWidth = txt.width
}
if (currentOffsetx <= textWidth) {
if (isIncrease) {
currentOffsetx += 10
currentOffsetx = min(currentOffsetx, textWidth)
} else {
currentOffsetx -= 10
currentOffsetx = max(currentOffsetx, 0)
}
}
if (currentOffsetx == textWidth) {
isIncrease = false
}else if (currentOffsetx == 0) {
isIncrease = true
}
txt.setXOffset(0f,currentOffsetx.toFloat())
Log.w(TAG,"check current tick:$millisUntilFinished,offsetx:$currentOffsetx,txtWidth:$textWidth")
}
}
countDownTimer.start()
}
}
my xml layout :
<com.example.administrator.myapplication.AKChangeColorTextView
android:id="#+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I Found a Love For Aolphn"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
following is effect
There is a way to do this optimized, simple, smooth and beautiful: Vector Animated Drawables (API21+).
There are a lot of tutorials out there (for example this video) that show how you can make beautiful animations. Basically the steps are following:
create two SVG vector images of your text, one with normal color, the other one with
colored letters. You can do this easily in Adobe Illustrator for example.
Import both into shapeshifter.design.
Create an animation for the second (colored layer) by your liking.
Copy the resulting xml file into your drawables and you're done!
Good luck!
You can use a text spannable and Foreground Color Span and animate one character at a time

Cannot draw on canvas with custom view

I have a class GameActivityas follows (I just post the relevant part):
public class GameActivity extends AppCompatActivity {
// initiate variables
private GameView mGameView;
private Display mDisplay;
private Point mSize;
public static int window_width = 0;
public static int window_height = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mDisplay = getWindowManager().getDefaultDisplay();
mSize = new Point();
mDisplay.getSize(mSize);
window_width = mSize.x;
window_height = mSize.y;
// initialize game view object
mGameView = new GameView(this);
// adding it to the content view
//setContentView(mGameView);
setContentView(R.layout.activity_game);
}
And I have the class GameView as follows with two constructors because of the custom view (also just the relevant parts to keep it clear):
private Context mContext;
//These objects will be used for drawing
private SurfaceHolder mSurfaceHolder;
private Paint mPaint;
public GameView(Context context){
super(context);
Log.d("TEST","Constructor 1");
init(context, null);
// clear all lists
...
// create object of map in beginning of game
...
// create objects of HQs
...
// create other sprite objects and add them to lists
...
}
public GameView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.d("TEST","Constructor 2");
init(context, attrs);
}
And I call in both of the constructors the init() method
private void init(Context context, AttributeSet attrs) {
mContext = context;
mSurfaceHolder = getHolder();
mPaint = new Paint();
mPaint.setColor(Color.DKGRAY);
}
My run() is like this:
#Override
public void run(){
Log.d("TEST","RUN");
long current_time = 0;
long old_time;
while (playing) {
old_time = current_time;
// update the game
update();
//to draw the frame
//onDraw(canvas);
draw();
//to control (lets the game sleep for some milliseconds)
control();
current_time = SystemClock.elapsedRealtime();
frametime = current_time - old_time;
framerate = 1000/ (current_time - old_time);
}
}
And the draw() (where the mistake happens) is like this:
private void draw() {
// BUG: mSurfaceHolder.getSurface.isValid ist niemals valid
Canvas canvas;
//if(true){
//checking if surface is valid
if (mSurfaceHolder.getSurface().isValid()) {
Log.d("Test","DRAW");
//locking the canvas
canvas = mSurfaceHolder.lockCanvas();
//drawing a background color for canvas
canvas.drawColor(Color.GREEN);
// call draw methods here
// bigger objects should be called before smaller objects for visibility
draw_ground(canvas);
draw_hq(canvas);
draw_soldiers(canvas);
draw_bullets(canvas);
draw_HUD(canvas);
//Unlocking the canvas
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
No the question is:
If I in the first class GameActivity pass the object mGameView of the class GameView to setContentView everything is fine and the draw() methods works normal which means mSurfaceHolder.getSurface().isValid()) is valid.
But if I put in the first class setContentView(R.layout.activity_game); then mSurfaceHolder.getSurface().isValid()) is never valid. Everything else works (e.g. I hear the game sound) but he doesn't draw. If I don't check for mSurfaceHolder.getSurface().isValid()) then the game crashes because in the code below in draw() the canvas is empty.
My aim is to create a custom view and overlay this with buttons.
Here is the XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".GameActivity">
<!--Test-->
<com.example.mirco.battlefront.GameView
android:id="#+id/gameView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Button
android:id="#+id/button"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:text="Button" />
<!--Test-->
</FrameLayout>
The XML seems to be correct because Android Studio recognizes it (custom view with overlayed button). But he doesn't draw in it.
Like I said with setContentView(mGameView); everything is fine except that I only have one view where I can't overlay buttons and so on which is not what I want.
What am I doing wrong? I tried to follow that LINK as good as possible. I am trying for days now to solve this but also can't find the solution anywhere.
Instead of
mGameView = new GameView(this);
// adding it to the content view
//setContentView(mGameView);
setContentView(R.layout.activity_game);
You should do
setContentView(R.layout.activity_game);
mGameView = (GameView)findViewById (R.id.gameView);
Then the mGameview is the one in your layout.

Android Studio: exchange data between classes

I would like to learn how to exchange data between classes(in Android Studio).
For that purpose I have created three Java classes:
1) GraficActivity.java:
public class GraficActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView) findViewById(R.id.testview);
//not working: iv.update(100,100);
DataHolder.setData(100, 100);
}
}
2) DrawingView.java:
public class DrawingView extends ImageView {
public DrawingView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private int X=200;
private int Y=200;
//not working: X = DataHolder.getX();
//not working: Y = DataHolder.getY();
public void update(int dataX, int dataY) {
X=dataX;
Y=dataY;
this.invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint pinsel = new Paint();
pinsel.setColor(Color.rgb(64, 64, 255));
pinsel.setStrokeWidth(5);
canvas.drawLine(0, 0, X, Y, pinsel);
}
}
3) DataHolder.java:
public class DataHolder {
private static int X;
private static int Y;
public static int getX() {return X;}
public static int getY() {return Y;}
public static void setData(int dataX, int dataY) {X = dataX; Y=dataY;}
}
I included DrawingView in the layout (together with other elements) with the following code in main.xml:
<de.carpelibrum.grafik.DrawingView
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#ffffff"
android:id="#+id/testview" />
The app works in principle, but I could not find a way to transfer data from GraficActivity to DrawingView.
I tried two methods(as indicated in the code above):
Via a separate DataHolder as global variable
With the function update(int, int) in DrawingView.
Finally, I will need to transfer not only two integers, but the content of an array: int data[][];.
How to solve the problem?
Thank you in advance for your suggestions.
Cheers,
Kyriakos.
ImageView does not have a method update(int, int). You should cast the View associated with the id R.id.testView to your type DrawingView. Then update(int, int) should become available at compile-time:
DrawingView iv = (DrawingView) findViewById(R.id.testview);
Also, consider that your DataHolder and DrawingView are both storing position data. Pick one. If the data is specific to an instance of DrawingView, then you don't need DataHolder.
perfect! Thank you.
Regarding the database, I put it in "GraficActivity", sratched "DataHolder", and used a pointer as parameter in the "update" function:
in "GraficActivity":
private int data[][]=new int[10][10];
data[0][0] = 200;
data[0][1] = 100;
iv.update(data);
in "DrawingView":
public void update(int[][] dataXY) {
X=dataXY[0][0];
Y=dataXY[0][1];
this.invalidate();
}
Now this is a good working basis.
Cheers,
Kyriakos.

Categories

Resources