dynamic edittext when is destroyed android - java

I'm writing spreadsheet app for android and for getting cell value i use dynamic edittext.
Is this any way to get info when text is written and save it to cell value?
here is part of code:
#Override
public boolean onTouchEvent(MotionEvent event){
if(Selection != null) {
Cell prevSelection = hostActivity.logic.getCell(Selection.left, Selection.top);
SelTxt = hostActivity.txtInput.getText().toString();
if(SelTxt.length() != 0 && prevSelection.GetValue() != SelTxt && prevSelection.getChanged()) {
prevSelection.setValue(SelTxt);
hostActivity.txtInput.clearComposingText();
prevSelection.Modified(false);
SelTxt = "";
}
}
switch(event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
int x = (int) event.getX();
int y = (int) event.getY();
calculateSelectionRect(x, y);
SelectionChanged = true;
if(x > startTableX && y > startTableY && !GroupSelected) {
Cell tmpCell = hostActivity.logic.getCell(Selection.left, Selection.top);
hostActivity.EditSelectedCellValue(Selection.left, Selection.top, Selection.right, Selection.bottom, tmpCell);
}
invalidate();
and
if(txtInput != null)
frameLayout.removeView(txtInput);
String tmpText = "";
txtInput = new EditText(this.getApplicationContext());
FrameLayout.LayoutParams txtParams = new FrameLayout.LayoutParams((right - left), (bottom - top));
txtInput.setX(left);
txtInput.setY(top);
txtInput.setText(value.GetValue());
txtInput.setTextColor(R.color.Selected_Cell_Border);
txtInput.setBackgroundColor(getResources().getColor(R.color.Selected_Cell));
frameLayout.addView(txtInput, txtParams);
setContentView(frameLayout);
tmpText = txtInput.getText().toString();
if(txtInput.getText().toString() != value.GetValue())
value.Modified(true);
txtInput.clearComposingText();
return tmpText;
}
in app it works in a way that after filling one cell and selecting cell directly below value is copied once, another selecting this cell gives good empty cell and is ok. But it's annoying and shouldn't happen

Related

Draw bitmap in specified XY positions in the Canvas

I am trying to overlay a image in a specific position in the video so that i can combine both.But could not place the image to exact position selected.its moving somewhere else the final output.
I am using this library to combine image and video https://github.com/MasayukiSuda/Mp4Composer-android (out of box Any other library suggestions)In this library i can set the image in 4 different positions:
public enum Position {
LEFT_TOP,
LEFT_BOTTOM,
RIGHT_TOP,
RIGHT_BOTTOM
}
mp4Composer = new Mp4Composer(inputVideoPath, videoPath)
.filter(new WatermarkFilter(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher_sample),WatermarkFilter.Position.LEFT_TOP))
In the Processing this happens:
case LEFT_TOP:
canvas.drawBitmap(bitmap,0,0,null);
break;
case LEFT_BOTTOM:
canvas.drawBitmap(bitmap, 0, canvas.getHeight() - bitmap.getHeight(), null);
break;
What i did i slightly modified my code to set anywhere in the canvas by getting the X Y position of the bitmap using MotionEvent
protected boolean onTouchDown(#NonNull MotionEvent event) {
currentMode = ActionMode.DRAG;
downX = event.getX();
downY = event.getY();
midPoint = calculateMidPoint();
oldDistance = calculateDistance(midPoint.x, midPoint.y, downX, downY);
oldRotation = calculateRotation(midPoint.x, midPoint.y, downX, downY);
currentIcon = findCurrentIconTouched();
if (currentIcon != null) {
currentMode = ActionMode.ICON;
currentIcon.onActionDown(this, event);
} else {
handlingSticker = findHandlingSticker();
}
if (handlingSticker != null) {
downMatrix.set(handlingSticker.getMatrix());
if (bringToFrontCurrentSticker) {
stickers.remove(handlingSticker);
stickers.add(handlingSticker);
}
if (onStickerOperationListener != null){
onStickerOperationListener.onStickerTouchedDown(handlingSticker);
}
}
if (currentIcon == null && handlingSticker == null) {
return false;
}
invalidate();
return true;
}
public float[] getMappedBoundPoints() {
float[] dst = new float[8];
getMappedPoints(dst, getBoundPoints());
return dst;
}
By this Method i get my X Y points
float X = sticker.getMappedBoundPoints()[0];
float Y = sticker.getMappedBoundPoints()[1];
Log.d(TAG, "SavedVideo: " + "X" + X + "?/" + "Y" + Y);
And Finally Passing my X Y to the Mp4Composer method:
.filter(new WatermarkFilter(bitmap,X,Y))
on the other side
canvas.drawBitmap(bitmap,X,Y,null);
Can anyone help out or point out where the mistake is ! Thanks

Making a responsive control in JavaFX

I'm trying to make a responsive design like in Word. I used Toolbar and ToolbarSkin code and made changes to it. Now I don't know how to make it so it will check the priority of the control, and the lowest priority gets resized, while the control with the highest priority still shows full-size.
Code I changed in ToolbarSkin:
private void addNodesToToolBar() {
final Responser toolbar = getSkinnable();
double length = 0;
if (getSkinnable().getOrientation() == Orientation.VERTICAL) {
length = snapSize(toolbar.getHeight()) - snappedTopInset() - snappedBottomInset() + getSpacing();
} else {
length = snapSize(toolbar.getWidth()) - snappedLeftInset() - snappedRightInset() + getSpacing();
}
// Is there overflow ?
double x = 0;
boolean hasOverflow = false;
for (Node node : getSkinnable().getItems()) {
if (getSkinnable().getOrientation() == Orientation.VERTICAL) {
x += snapSize(node.prefHeight(-1)) + getSpacing();
} else {
x += snapSize(node.prefWidth(-1)) + getSpacing();
}
if (x > length) {
hasOverflow = true;
break;
}
}
if (hasOverflow) {
if (getSkinnable().getOrientation() == Orientation.VERTICAL) {
length -= snapSize(overflowMenu.prefHeight(-1));
} else {
length -= snapSize(overflowMenu.prefWidth(-1));
}
length -= getSpacing();
}
// Determine which node goes to the toolbar and which goes to the overflow.
x = 0;
overflowMenuItems.clear();
box.getChildren().clear();
for (Node node : getSkinnable().getItems()) {
node.getStyleClass().remove("menu-item");
node.getStyleClass().remove("custom-menu-item");
if (getSkinnable().getOrientation() == Orientation.VERTICAL) {
x += snapSize(node.prefHeight(-1)) + getSpacing();
} else {
x += snapSize(node.prefWidth(-1)) + getSpacing();
}
if (x <= length) {
box.getChildren().add(node);
} else {
if (node.isFocused()) {
if (!box.getChildren().isEmpty()) {
Node last = engine.selectLast();
if (last != null) {
last.requestFocus();
}
} else {
overflowMenu.requestFocus();
}
}
if (node instanceof Separator) {
overflowMenuItems.add(new SeparatorMenuItem());
} else {
CustomMenuItem customMenuItem = new CustomMenuItem(node);
// RT-36455:
// We can't be totally certain of all nodes, but for the
// most common nodes we can check to see whether we should
// hide the menu when the node is clicked on. The common
// case is for TextField or Slider.
// This list won't be exhaustive (there is no point really
// considering the ListView case), but it should try to
// include most common control types that find themselves
// placed in menus.
final String nodeType = node.getTypeSelector();
switch (nodeType) {
case "Button":
case "Hyperlink":
case "Label":
customMenuItem.setHideOnClick(true);
break;
case "CheckBox":
case "ChoiceBox":
case "ColorPicker":
case "ComboBox":
case "DatePicker":
case "MenuButton":
case "PasswordField":
case "RadioButton":
case "ScrollBar":
case "ScrollPane":
case "Slider":
case "SplitMenuButton":
case "SplitPane":
case "TextArea":
case "TextField":
case "ToggleButton":
case "ToolBar":
customMenuItem.setHideOnClick(false);
break;
}
overflowMenuItems.add(customMenuItem);
box.getChildren().clear();
setNameToButton(make,box.getParent().getId());
box.getChildren().add(make);
// overflowMenuItems.remove(overflowMenuItems.size()-1);
}
}
}
// Check if we overflowed.
overflow = overflowMenuItems.size() > 0;
if (!overflow && overflowMenu.isFocused()) {
Node last = engine.selectLast();
if (last != null) {
last.requestFocus();
}
}
overflowMenu.setVisible(overflow);
overflowMenu.setManaged(overflow);
}
and:
#Override protected void layoutChildren(final double x,final double y,
final double w, final double h) {
final Responser toolbar = getSkinnable();
if (toolbar.getOrientation() == Orientation.VERTICAL) {
if (snapSize(toolbar.getHeight()) != previousHeight || needsUpdate) {
((VBox)box).setSpacing(getSpacing());
((VBox)box).setAlignment(getBoxAlignment());
previousHeight = snapSize(toolbar.getHeight());
addNodesToToolBar();
}
} else {
if (snapSize(toolbar.getWidth()) != previousWidth || needsUpdate) {
((HBox)box).setSpacing(getSpacing());
((HBox)box).setAlignment(getBoxAlignment());
previousWidth = snapSize(toolbar.getWidth());
addNodesToToolBar();
}
}
needsUpdate = false;
double toolbarWidth = w;
double toolbarHeight = h;
if (getSkinnable().getOrientation() == Orientation.VERTICAL) {
toolbarHeight -= (overflow ? snapSize(overflowMenu.prefHeight(-1)) : 0);
} else {
toolbarWidth -= (overflow ? snapSize(overflowMenu.prefWidth(-1)) : 0);
}
box.resize(toolbarWidth, toolbarHeight);
positionInArea(box, x, y,
toolbarWidth, toolbarHeight, /*baseline ignored*/0, HPos.CENTER, VPos.CENTER);
// If popup menu is not null show the overflowControl
if (overflow) {
double overflowMenuWidth = snapSize(overflowMenu.prefWidth(-1));
double overflowMenuHeight = snapSize(overflowMenu.prefHeight(-1));
double overflowX = x;
double overflowY = x;
if (getSkinnable().getOrientation() == Orientation.VERTICAL) {
// This is to prevent the overflow menu from moving when there
// are no items in the toolbar.
if (toolbarWidth == 0) {
toolbarWidth = savedPrefWidth;
}
HPos pos = ((VBox)box).getAlignment().getHpos();
if (HPos.LEFT.equals(pos)) {
overflowX = x + Math.abs((toolbarWidth - overflowMenuWidth)/2);
} else if (HPos.RIGHT.equals(pos)) {
overflowX = (snapSize(toolbar.getWidth()) - snappedRightInset() - toolbarWidth) +
Math.abs((toolbarWidth - overflowMenuWidth)/2);
} else {
overflowX = x +
Math.abs((snapSize(toolbar.getWidth()) - (x) +
snappedRightInset() - overflowMenuWidth)/2);
}
overflowY = snapSize(toolbar.getHeight()) - overflowMenuHeight - y;
} else {
// This is to prevent the overflow menu from moving when there
// are no items in the toolbar.
if (toolbarHeight == 0) {
toolbarHeight = savedPrefHeight;
}
VPos pos = ((HBox)box).getAlignment().getVpos();
if (VPos.TOP.equals(pos)) {
overflowY = y +
Math.abs((toolbarHeight - overflowMenuHeight)/2);
} else if (VPos.BOTTOM.equals(pos)) {
overflowY = (snapSize(toolbar.getHeight()) - snappedBottomInset() - toolbarHeight) +
Math.abs((toolbarHeight - overflowMenuHeight)/2);
} else {
overflowY = y + Math.abs((toolbarHeight - overflowMenuHeight)/2);
}
overflowX = snapSize(toolbar.getWidth()) - overflowMenuWidth - snappedRightInset();
}
overflowMenu.resize(overflowMenuWidth, overflowMenuHeight);
positionInArea(overflowMenu, overflowX, overflowY, overflowMenuWidth, overflowMenuHeight, /*baseline ignored*/0,
HPos.CENTER, VPos.CENTER);
}
}
Also I don't know how can I implement so only one control will shrink at the time, not all of them.
Thank you for your help.

Running loop to check integer, not working

I'm trying to get the location of the user's finger touch and detect if it is within the bounds of an image view. I'm getting all the right integers returned to me, there is no problem with the values. The problem I'm getting is when trying to detect whether returnX is greater or less than the imageview X int.
ImageView img;
float eventX;
float eventY;
float x,y,x2,y2;
String TAG = "imgPos";
String TAG2 = "downcheck";
String TAG3 = "bounds";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.imageView1);
}
public void fingerDownLoop(){
if (SingleTouchEventView.userDown == true){
Log.e(TAG2, "Down is true");
Thread myThread = new Thread(myRunnable);
myThread.start();
} else if (SingleTouchEventView.userDown == false){
Log.e(TAG2, "Down is false");
}
}
public void getFingerLocation(){
float returnX = SingleTouchEventView.eventX;
float returnY = SingleTouchEventView.eventY;
Log.e(TAG3, " - X -");
Log.e(TAG3, " getX: " + returnX);
Log.e(TAG3, " - Y -");
Log.e(TAG3, " getY: " + returnY);
if (returnX < x){
Log.e(TAG3, "Outside bounds");
}
if (returnX > x){
Log.e(TAG3, "Inside bounds");
}
}
Runnable myRunnable = new Runnable() {
#Override
public void run() {
while (SingleTouchEventView.userDown == true) {
try {
Log.e(TAG2, "FingerDown");
getFingerLocation();
x = img.getLeft();
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // Waits for 1 second (1000 milliseconds)
}
}
};
}
testText is a button click, it gets the imageviews position and stores it as the values. The problem I'm getting is here:
if (returnX < x){
Log.e(TAG3, "Outside bounds");
}
if (returnX > x){
Log.e(TAG3, "Inside bounds");
}
It is all ways showing as Inside bounds even though returnX is less than X. Any ideas?
Updated get img position.
int[] img_coordinates = new int[2];
img.getLocationOnScreen(img_coordinates);
double x_center = (double)img_coordinates[0] + img.getWidth()/2.0;
double y_center = (double)img_coordinates[1] + img.getHeight()/2.0;
int halfwidth = 150 / 2;
int halfheight = 150 / 2;
yboundtop = y_center - halfheight;
yboundbottom = y_center + halfheight;
xboundleft = x_center - halfwidth;
xboundright = x_center + halfwidth;
float returnX = eventX;
float returnY = eventY;
if (returnX < xboundleft || returnX > xboundright || returnY < yboundtop || returnY > yboundbottom) {
Log.e(TAG3, "Outside bounds");
}
if (returnX > xboundleft && returnX < xboundright && returnY > yboundtop && returnY < yboundbottom) {
Log.e(TAG3, "Inside bounds");
}
I'am not sure what you want to accomplish by your code, but checking in a loop is not very efficient and in this case probably not necessary.
Your imageView has a method getHitRect() and you could use it like this:
Rect hitRect = new Rect();
imageView.getHitRect(hitRect);
if(hitRect.contains(touchEvent.getX(), touchEvent.getY())){
// your touch is inside, do something useful
}
Use it inside your touch-method
Now that I kind of know what you are looking for I think I may be able to help. Override the onTouchEvent() method of your Activity to capture your touch events. This will be much more efficient than doing the loop yourself.
private boolean mTouchInImage = false;
public boolean onTouchEvent(MotionEvent event) {
boolean handledTouch = false;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Rect hitRect = new Rect((int)img.getX(), (int)img.getY(),
(int)(mg.getX() + img.getWidth()), (int)(img.getY() + img.getHeight()));
handledTouch = mTouchInImage = hitRect.contains((int)event.getX(), (int)event.getY());
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
handledTouch = mTouchInImage;
mTouchInImage = false;
break;
case MotionEvent.ACTION_MOVE:
if (mTouchInImage) {
img.setX(event.getX() - img.getWidth() / 2);
img.setY(event.getY() - img.getHeight() / 2);
img.invalidate();
handledTouch = true;
}
break;
}
return handledTouch;
}

Error on code for touchlistener - Android

I am a newbie here at Android, I am having trouble with this code block for touchListener for a TicTacToe game following an online tutorial.
The error I keep getting is:
The operator && is undefined for the argument type(s) boolean, void
The following code is located in the MainActivity.java. I get this error on the line highlighted with stars below:
// Listen for touches on the board
private OnTouchListener mTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Determine which cell was touched
int col = (int) event.getX() / mBoardView.getBoardCellWidth();
int row = (int) event.getY() / mBoardView.getBoardCellHeight();
int pos = row * 3 + col;
if (!mGameOver && setMove(TicTacToeGame.HUMAN_PLAYER, pos)) { //*****************
// If no winner yet, let the computer make a move
int winner = mGame.checkForWinner();
if (winner == 0) {
mInfoTextView.setText(R.string.turn_computer);
setMove(TicTacToeGame.COMPUTER_PLAYER, pos);
winner = mGame.checkForWinner();
}
}
return false;
}
};
I think this is because the setMove() is void in the TicTacToeGame.java:
public void setMove(char player, int location) {
if (location >= 0 && location < BOARD_SIZE &&
mBoard[location] == OPEN_SPOT) {
mBoard[location] = player;
}
}
I have followed the tutorial precisely http://www.harding.edu/fmccown/classes/comp475-s10/tic-tac-toe-graphics.pdf
I would be very grateful of any help.
Many thanks,
Beth Ann
In the PDF you linked to, setMove() has a boolean return type (Page 5, top):
private boolean setMove(char player, int location) {
if (mGame.setMove(player, location)) {
mBoardView.invalidate(); // Redraw the board
if (player == TicTacToeGame.HUMAN_PLAYER)
mHumanMediaPlayer.start();
else
mComputerMediaPlayer.start();
return true;
}
return false;
}

How to change the layout when we touch wrong item?

I need to run this app as follow -> : if I touch correct item , score+=5, else change to gameover.xml
I don't know how to change activity to another layout?
public boolean onTouchEvent(MotionEvent event) {
int tx = (int) event.getX();
int ty = (int) event.getY();
int i = 0;
for (Element element : mElements) {
if ((element.getmX() < tx && tx < element.getmX()
+ element.getWidth())
&& (element.getmY() < ty && ty < element.getmY()
+ element.getHeight())) {
***if (mGarbagType == element.getGtype()) {
score += 5;
}else score-=5;***
Log.v("DustmanGame", "Touch on:" + element.getGtype()
+ "Score : 5");
mElements.remove(i);
mElementNumber = mElements.size();
mGarbagType = randomGarbage();
break;
}
i++;
}
return super.onTouchEvent(event);
}
I don't know how to change activity to another layout
In your else {} block simply launch a new Activity with startActivity() to use gameover.xml or call setContentView(R.layout.gameover) in this Activity.
Have different layout in your xml, use View.GONE or View.VISIBLE depends on your requirement

Categories

Resources