Issues with Graph ploting in android achrtengine - android

I am using achartengine for graph ploting . I have issues with matching points with a straight line . Like If I have to make a line for a week and let say I don't have points for any two days (Tuesday and wed.) Now how I am gonna match point of Monday directly to Thursday and so on .
This is the code I am using for making chart .
multiRenderer.addSeriesRenderer(dataRenderer);
mChart = ChartFactory.getLineChartView(PatientHome.this,dataset, multiRenderer);
// adding the view to the linearlayout
final LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
100));
l.addView(mChart);
l.setPadding(5, 10, 0, 0);
final int p = pos;
runOnUiThread(new Runnable() {
public void run() {
chartContainer.addView(l, p);
}
});
pos++;
}

I will assume that you are using TimeSeries, for your data points.
You could use instead regular XYSeries that will have consecutive numbers, and add your own labels to X axis like with something like this:
for (int i = 0; i < yourData.size(); i++) {
multiRenderer.addXTextLabel(i, yourDataLabel);
}

Related

Regenarate location if (X,Y) already contains ImageView there

I have an ImageViewArray and I am randomizing their locations, I want to check if the current location already contains an ImageView without keeping a lot of values of x,y for every ImageView in the array.
Here is my code:
ImageView[] imageViewArray = new ImageView[40];
for (int i = 0; i < 40; i++) {
imageViewArray[i] = new ImageView(this);
imageViewArray[i].setTag(i);
imageViewArray[i].setImageResource(R.mipmap.enemy);
rlt.addView(imageViewArray[i]);
imageViewArray[i].setX(rand.nextInt(rlt.getWidth()));
imageViewArray[i].setY(rand.nextInt(rlt.getHeight()));
if(imageViewArray[i].getX()=) // here I want to check if it already contains an ImageView.
}
Possible Solution
Creating IntArray and adding X value to it and also every Y value for it, then compare between them, is it the best solution?
Problem with the solution - nothing happens, the imageview doesn't change the place and the Toast is not executed.
code:
ImageView[] imageViewArray = new ImageView[20];
ArrayList<Float> xarray = new ArrayList<>();
ArrayList<Float> yarray = new ArrayList<>();
for (int i = 0; i < 20; i++) {
imageViewArray[i] = new ImageView(this);
imageViewArray[i].setTag(i);
imageViewArray[i].setImageResource(R.mipmap.enemy);
imageViewArray[i].setX((float)rand.nextInt(1 + layoutwidth));
imageViewArray[i].setY((float)rand.nextInt(1 + layoutheight));
xarray.add(imageViewArray[i].getX());
yarray.add(imageViewArray[i].getY());
rlt.addView(imageViewArray[i]);
Toast.makeText(MainActivity.this,imageViewArray[i].getX() +"blabla",Toast.LENGTH_LONG);
}
EDIT
layoutwidth is zero :
private int layoutwidth, layoutheight, randx, randy;
private RelativeLayout rlt;
....
rlt = (RelativeLayout) findViewById(R.id.layout);
rlt.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rlt.getViewTreeObserver().removeOnGlobalLayoutListener(this);
layoutwidth = rlt.getWidth();
layoutheight = rlt.getHeight();
}
});
Yes, that is the best solution. You need to load the rectangles from somewhere. You might merge rectangles if one contains the other, but then you would over-complicate your task and in your quest of writing a more performant and a clearer code, you would end up with a slow and complicated code. Write your code with storing pairs of X, Y points where X is the let-top corner position and Y is the right-bottom corner position.
Note, that I have assumed that the pictures are not rotated. If the images might be rotated, then you need a more general solution, using the inequalities defining the rectangles to see where a point set of a rectangle intersects the point set of the other rectangle. If the intersection is empty set, then the "space is not used up".

Specifying TableRow child dimensions programmatically

I'm trying to create a TableLayout programmatically, in order to make a grid-like layout with a configurable row and column count. This would likely be trivial to do in a layout file, but because each child element will be nearly identical and I want the row and column count to be configurable, I would like to do it programmatically if possible.
So far this is what I have.
MainActivity.java
private void displayBoard() {
TableLayout l = (TableLayout) findViewById(R.id.GameGrid);
l.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int y = 0; y < b.height(); y++) {
TableRow r = new TableRow(this);
r.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, 15));
for (int x = 0; x < b.width(); x++) {
r.addView(b.getSpaceAt(x, y));
}
l.addView(r);
}
}
Board.java
for (int y = 0; y < spaces.length; y++) {
for (int x = 0; x < spaces[0].length; x++) {
spaces[y][x] = new BoardSpace(context, x, y);
}
}
BoardSpace.java
public class BoardSpace extends Button {
private ReversiPiece piece;
protected int x, y;
private BoardSpace(Context c) {
super(c);
}
public BoardSpace(Context c, int x, int y) {
super(c);
// this.setWidth(25); // Doesn't make a difference, either
// this.setHeight(15); // Doesn't make a difference, either
piece = null;
this.x = x;
this.y = y;
}
public ReversiPiece piece() {
return piece;
}
public void setPiece(ReversiPiece p) {
piece = p;
}
}
I'm basically trying to create a grid of buttons, but so far I cannot get the size of the buttons to change so I can make sure they all fit on the screen. At the moment they appear at a standard size, and any changes I make to the LayoutParams definitions do not make a difference.
I suspect this issue has to do with my usage of LayoutParams. There are a lot of LayoutParams classes associated with each View type, so perhaps I'm not using the right one? Any help would be appreciated.
EDIT: Now creating a new TableLayout.LayoutParams object for each TableRow based on cyanide's recommendation, but the buttons are still displaying at default size.
You are right, that's about LayoutParams, which must not be shared between views: each view should have its own copy of LayoutParams.
In other words, this is what you need:
r.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 15));
About getSpaceAt (there is not code for it). Does it create a view programmatically? Remember to set LayoutParams and again it should be separate for each view.
Hopefully it will help.
Got it! I was trying to manipulate the LayoutParams of my TableLayout and TableRows when in reality I needed to set TableRow.LayoutParams on the buttons themselves, when I add them to TableRow. This involves specifying the weight of each child element (Button) as well.
private void displayBoard() {
TableLayout l = (TableLayout) findViewById(R.id.GameGrid);
l.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
for (int y = 0; y < b.height(); y++) {
TableRow r = new TableRow(this);
r.setWeightSum(b.width());
for (int x = 0; x < b.width(); x++) {
BoardSpace s = b.getSpaceAt(x, y);
s.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT, 1.0f));
r.addView(s);
}
l.addView(r);
}
}
TableRow.setWeightSum() is used to set the weight sum of the row to the number of elements in the row, then passing a weight of 1 to the LayoutParams for the button causes the buttons to adjust in size to fit on the screen, as intended.
Surprisingly this answer was for a more generic question but this is what led me to the solution:
How to set layout_weight attribute dynamically from code?

How do I randomly place/set/allocate items within a 2D Array?

I’ve been trying to create a minesweeper game in android, and so far, all has gone accordingly. However, I’m currently stuck on the part where I have to randomly place the mines within the game board.
I’ve tried a few things that I could think of, but none of which worked, except one. However, it doesn’t give me the results that I want. Here is how I am drawing the game board (using a 2D array of buttons).
final Button currentButton = new Button(this);
final int bombState = R.drawable.bomb_state;
final Button[][] buttonArray = new Button[6][6];
final int mine = R.drawable.bomb;
final Random rand = new Random();
final int number = 36;
int button;
int row;
//create new Linear Layout
RelativeLayout linearLayout = new RelativeLayout(this);
//creating the layout Params
LayoutParams linLayoutParam = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
//set layout background
linearLayout.setBackgroundDrawable(getWallpaper());
//set LinearLayout as a root element of the screen
setContentView(linearLayout, linLayoutParam);
//create a new Table Layout for the game grid
TableLayout mineGrid = new TableLayout(this);
/*
* creates TableRows and Buttons using the for loop
* then add the buttons into the rows and the rows
* into the TableLayout
*/
for(row = 0; row < 6; row++){
//create new Table Row
TableRow currentRow = new TableRow(this);
for(button = 0; button < 6; button++){
//create new Button
for(int id = 0; id < number; id++){
currentButton.setId(id);
}
currentButton.setText(" ");
//storing the buttons into the array of Buttons
buttonArray[row][button] = currentButton;
if(currentButton.isClickable()){
currentButton.setOnClickListener(new OnClickListener() {
/*
* (non-Javadoc)
* #see android.view.View.OnClickListener#onClick(android.view.View)
*/
public void onClick(View v) {
try
{
for(int i = 0; i < 10; i++){
if(rand.nextInt(10) == i){
currentButton.setBackgroundResource(mine);
restart.setBackgroundResource(bombState);
}
}
} catch(Exception e)
{
Toast.makeText(Game.this,e.getMessage() + "Error : ",
Toast.LENGTH_SHORT).show();
}
}
});
}
//store the button into the Table Row
currentRow.addView(currentButton);
}
//add the newly created row into the table
mineGrid.addView(currentRow);
}
linearLayout.addView(score, params3);
linearLayout.addView(mineGrid, params);
}
What the above code gives me, is a 6x6 grid made up of buttons.
And the following is where I’m trying to randomly place n amount of mines within the board.
try
{
for(int i = 0; i < 10; i++){
if(rand.nextInt(10) == i){
currentButton.setBackgroundResource(mine);
restart.setBackgroundResource(bombState);
}
}
}
Unfortunately, this fills the whole board with mines, instead of only placing n amount of mine on the board. I know am missing something when I try to randomly set the mines! Can anyone advise me as to where I’m going wrong and help point me in the right direction?
Please ask me anything for clarification.
Thanks in advance.
You basically, on every click of a button try to place a mine instead placing them when you create buttons. Maybe You could add to a list, id of a buttons which are mines and only check if user has clicked on one of those buttons.
ArrayList<Integer> mines = new ArrayList<Integer>();
.
.
.
currentButton.setText(" ");
if(rand.nextInt(2)==1)
mines.add(currentButton.id);
and in onClick() You check if currentButton.id is in mines list and if it is, display appropriate image.

Android change visibility doesn't work

Simple card game setup creating rows and columns number of cards:
TableLayout tableCards = new TableLayout(this);
RelativeLayout.LayoutParams relLayParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayParams.addRule(RelativeLayout.CENTER_IN_PARENT);
tableCards.setLayoutParams(relLayParams);
int i = 0;
StringBuilder sbCardsDebug = new StringBuilder('\n');
for (int r = 0; r < rows; r++) {
// create a new row
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
for (int c = 0; c < columns; c++) {
ImageView card = new ImageView(this);
card.setTag("" + i);
card.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
card.setImageDrawable(bitmapCardBackScaled);
tr.addView(card);
card.setOnClickListener(cardClickListener);
i++;
}
// add the row:
tableCards.addView(tr);
}
relativeLayoutCardsHolder.addView(tableCards);
Somewhere in the listener I have a code part like this:
imageView.setVisibility(View.INVISIBLE);
Where the imageView is a card ( I have breakpoint set up also log and it shows the correct card.
The problem: it doesn't become invisible.
I am thinking maybe I am not in a good Thread or should I need something to do it after setting the invisible?
I have tried with :
imageView.post(new Runnable() {
public void run() {
imageView.setVisibility(View.INVISIBLE);
}
});
and
TableLayout parent = (TableLayout) imageView.getParent().getParent();
parent.invalidate();
parent.requestLayout();
and no success. Pls make any suggestion, I am out of ideas, thanks.
Edit1:
As suggestion I have tried with postDelayed too:
imageView.postDelayed(new Runnable() {
public void run() {
imageView.setVisibility(View.INVISIBLE);
}
}, 600);
Listener body is from line 353 to 424 calling several classes with animations, passing references and it will reveal my dirty work, also will show the business logic too (NDA agrement), which part are you interested? - min 1000 lines are executed.
If it's a correct ImageView, its must work.
imageView.setAlpha(0);
Hope it's help.

Android How to iterate an R.drawable object

I'm trying to display a frame by frame animation and want to iterate through the drawables so I don't have to type all their names in case the number of frames increases.
However I can't seem to find how to iterate through the drawables. I have looked up a fair bit of java for loop tutorials but they all just printed stuff which (as far as I'm sure) don't have to use here.
Here's the relevant code (the image's names are dude1, dude2, ...):
private void startAnimation(){
animation = new AnimationDrawable();
for (int i = 1; i < 4; i++) {
animation.addFrame(getResources().getDrawable(R.drawable.dude(i)), 100);
}
animation.setOneShot(true);
ImageView imageView = (ImageView) findViewById(R.id.img);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 90);
params.alignWithParent = true;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(params);
imageView.setImageDrawable(animation);
imageView.post(new Starter());
}
Thx!
I think the last answer is varely true but it may be:
for (int i=0; i <10;i++){
animation.addFrame(
getResources().getDrawable(getResources().getIdentifier("dude" + i,"drawable",
getPackageName()),100);
}
Try this. getResources() needs a context.
for (int i=0;i<10;i++){
animation.addFrame(getResources().getIdentifier("dude" + i,"drawable", getPackageName()),100);
}
Here, I have assumed 10 frames (i<10).
I have used Simon's answer to iterate through my drawables that are named "c1" through "c54" and placed in an array. Here is the code I just used that works for me.
private void getDeckDrawables() {
for (int i=1; i<53; i++){
intArrDeck[i-1] = getResources().getIdentifier("c"+i,"drawable",getPackageName());
}
}
Previously, I typed them manually which took up too much room for my taste.
private void getDeckDrawables() {
intArrDeck[0]=R.drawable.c1;
intArrDeck[1]=R.drawable.c2;
intArrDeck[2]=R.drawable.c3;
}

Categories

Resources