How to create a double with two decimals in Android Studio - numbers

if users input "5" at "akenari" textview and click "altmis" button "bkenari" print 1.66666666√3 but I want print 1.66√3 how can I do?
altmis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
double sayi1f=Double.parseDouble(akenari.getText().toString());
bkenari.setText(String.valueOf(sayi1f / 3 + "√3"));
}catch(NumberFormatException nfe){
}
}

let your value to be roundoff is x
double x = 1.66666;
double y = Math.round(x*100.0)/100.0;
then y will easily give you the answer
no big logic used here

Related

Centre dialog on the screen in eclipse e4 RCP application immediately when Part is activated

I am opening a dialog when left Part in my Eclipse RCP application is Activated.
In Part's #PostConstruct method, I am registering IPartListner to EPartService.
Following code shows that :
partService.addPartListener(new IPartListener() {
#Override
public void partActivated(MPart part) {
if(part.getElementId().equals("left_part_id")) {
SignInDialog dialog = new SignInDialog(shell, display, eventBroker);
dialog.open();
}
}
});
Where in The SignInDialog which extends the JFace Dialog, I am doing this :
#Override
protected void configureShell(Shell newShell) {
Monitor monitor = display.getPrimaryMonitor();
Rectangle monitorRect = monitor.getBounds();
int x = monitorRect.x + (monitorRect.width - 600) / 2;
int y = monitorRect.y + (monitorRect.height - 360) / 2;
newShell.setLocation(x, y);
newShell.setText("Sign In");
super.configureShell(newShell);
}
#Override
protected Point getInitialSize() {
return new Point(600, 360);
}
Please note that left Part is always visible and bundled through Application.e4xmi. My problem is that as soon as the part gets activated the dialog is shown in LowerRight corner on the monitor. If same dialog is opened from a button click, it shows correctly at the centre. Any help would be appreciated, thanks in advance!
Use the getInitialLocation method to set the location of the dialog. The size set in configureShell is being overridden by the default getInitialLocation
#Override
protected Point getInitialLocation(final Point initialSize)
{
Display display = getShell().getDisplay();
Monitor monitor = display.getPrimaryMonitor();
Rectangle monitorRect = monitor.getBounds();
int x = monitorRect.x + (monitorRect.width - 600) / 2;
int y = monitorRect.y + (monitorRect.height - 360) / 2;
return new Point(x, y);
}

Sprite long click in andengine

I am using an animated sprite and want to perform some action on its long click events, but it is not working. Please help guys...
AnimatedSprite playBtn = new AnimatedSprite(MainActivity.CAMERA_WIDTH/2, MainActivity.CAMERA_HEIGHT - 100, activity.tTextureRegion3, activity.getVertexBufferObjectManager())
{
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y)
{
if (pSceneTouchEvent.isActionDown())
{
scoreInc = scoreInc + 10;
score.setText("Score: " + scoreInc);
width = width + 5;
progressRectangle();
return true;
}
return false;
};
};
this.registerTouchArea(playBtn);
this.setTouchAreaBindingOnActionDownEnabled(true);
I have used this
#Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float X, float Y)
{
switch(pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_MOVE:{ //Done my Work}
and it worked

GWT Canvas: Drawing images in a loop using an arraylist in java

I am having difficulties drawing all images in a ArrayList to the canvas.
I was first able to correctly draw all the images using just one path name for all images in the code below:
private ArrayList<Module> list;
//... some code loading in modules
public void loadModuleImages() {
addModuleImage = new Image("images/Plain.jpg");
addModuleImage.setVisible(true);
addModuleImage.setVisible(true);
addModuleElement = ImageElement.as(addModuleImage.getElement());
addModuleImage.addLoadHandler( new LoadHandler(){
public void onLoad(final LoadEvent event){
for(int i = 1; i<=list.size()+1;i++){
context.drawImage(addModuleElement, list.get(i-1).getX(),list.get(i-1).getY());
}
}
});
canvas.setVisible(true);
addModuleImage.setVisible(false);
RootPanel.get().add(addModuleImage);
}
Now when I start to use an ArrayList to display all the images I end up drawing only the last image to the screen. Using the code below:
public void loadModuleImages() {
final ArrayList<Image> images = new ArrayList<Image>();
final ArrayList<ImageElement> imagesE = new ArrayList<ImageElement>();
for(int i = 1; i<=list.size()+1;i++){
images.add(new Image(list.get(i-1).getImageName()));
x = list.get(i-1).getX();
y = list.get(i-1).getY();
images.get(i-1).setVisible(true);
imagesE.add(ImageElement.as(images.get(i-1).getElement()));
addModuleElement = imagesE.get(i-1);
images.get(i-1).addLoadHandler( new LoadHandler(){
public void onLoad(final LoadEvent event){
context.drawImage(addModuleElement, x, y);
}
});
canvas.setVisible(true);
images.get(i-1).setVisible(false);
RootPanel.get().add(images.get(i-1));
}
}
The Module class contains accessor methods for the x position (getX()),y position getY(), and image name (getImageName()). For debugging reasons all the images are all "images/Plain.jpg".
It appears something in my loop is making the last image get drawn over and over, not switching to the next image.
Please help me figure out how to correctly load all the images or any suggestions about a different way to approach my problem. Thank you
Excerpt from your code
x = list.get(i-1).getX();
y = list.get(i-1).getY();
images.get(i-1).setVisible(true);
imagesE.add(ImageElement.as(images.get(i-1).getElement()));
addModuleElement = imagesE.get(i-1);
images.get(i-1).addLoadHandler( new LoadHandler(){
public void onLoad(final LoadEvent event){
context.drawImage(addModuleElement, x, y);
//...
I assume then it follows that your class has three fields:
private int x;
private int y;
private ImageElement addModuleElement;
Lets add some quick logging and try running the code in our heads:
log("setting up step #" + i);
x = list.get(i-1).getX();
y = list.get(i-1).getY();
log("x,y updated: " + x + "," + y);
images.get(i-1).setVisible(true);
imagesE.add(ImageElement.as(images.get(i-1).getElement()));
addModuleElement = imagesE.get(i-1);
images.get(i-1).addLoadHandler( new LoadHandler(){
public void onLoad(final LoadEvent event){
log("element loaded #" + i + ", x/y = " + x + "/" + y);
context.drawImage(addModuleElement, x, y);
Remembering that image downloading is asynchronous, we would expect to see logging come out of that code like this:
setting up step #1
x,y updated: 10,10
setting up step #2
x,y updated: 20,20
setting up step #3
x,y updated: 30,30
Then a pause while the images are downloaded
element loaded #1, x/y= 30/30
element loaded #2, x/y= 30/30
element loaded #3, x/y= 30/30
Whats happening is that you reassign x, y, addModuleElement every step through the loop, so that when the event handlers eventually run, they all have the data from the very last item.
Instead, use those values directly, and get rid of the fields:
final int x = list.get(i-1).getX();
final int y = list.get(i-1).getY();
images.get(i-1).setVisible(true);
imagesE.add(ImageElement.as(images.get(i-1).getElement()));
final ImageElement addModuleElement = imagesE.get(i-1);
images.get(i-1).addLoadHandler( new LoadHandler(){
public void onLoad(final LoadEvent event){
context.drawImage(addModuleElement, x, y);
This way each LoadHandler will have its own copy of those variables, and they won't all be trying to share the same ones.
(see also the classic GWT thread, "That's Not Async" - though this is addressing RPC, its the same general problem that you are facing.)

incompatible types error in toast declaration

im trying to create a toast with a message that displays the coordinates depending where the user clicks on the screen. when i declare 'mContext=this' i get an error: incompatible types required: Context.
#Override
public boolean onTouchEvent(MotionEvent event)
{
//motionevent detects motion from the user
float x;
x = event.getX();
float y;
y = event.getY();
Context mContext;
switch (event.getAction())
{
case MotionEvent.ACTION_UP:
//touch_up(x, y);
mContext= this;
float Cox = event.getRawX();
float Coy = event.getRawY();
String text = "You clicked at x = " + Cox+ "and y =" + Coy;
//AlertDialog.Builder builder = new AlertDialog.Builder();
Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
//toast.show();
invalidate();
break;
}
return true;
}
Is this within an Activity or Fragment?
this will work in an Activity, however in a Fragment you will need to call the context of the host Activity.
Something like:
mContext = getActivity();
Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();

Is there any way to find a string's position and click on it using robotium?

I have the following list appearing on UI of an android application which tells about the date and the temperature on that date.
temp degree
11/01 --.-- c
11/02 21.7 c
11/03 22.5 c
Here I want to find the position of string "--.--" and then click on it, so that I can update the list. Is there any way to find the position of string? I know that solo.searchText() will tell whether the string is present or not, but what about the position?
// i think it will help you
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
// TODO Auto-generated method stub
if(yourliststring.trim().equals("--.--"))
{
//your condition
}
else
{
//your condition
}
}
});
I got a simple way to find and click on the string
if(solo.searchText("--.--")){
solo.clickLongOnText("--.--");
}
All I need is to click on the string "--.--".
I'm not sure what position you need, however if you need position on screen you can simple do:
TextView textView = solo.getText("--.--"); // or solo.getText(Pattern.quote("--.--")); if needed
int[] xy = new int[2];
textView.getLocationOnScreen(xy);
final int viewWidth = textView.getWidth();
final int viewHeight = textView.getHeight();
final float x = xy[0] + (viewWidth / 2.0f);
final float y = xy[1] + (viewHeight / 2.0f);
this way you have central point of view (x, y).
If you need position of text in list, you can do:
private int getPosition(ListView listView, String text) {
for (int i = 0; i < listView.getAdapter().getCount(); i++) {
Object o = listView.getAdapter.getItemAtPosition(i);
if (o instanceof TextView) {
if (((TextView)o).getText().toString().equals(text)) {
return i;
}
}
}
return -1; // not found
}
you can call it for instance this way (set proper parameters):
int position = getPosition((ListView) solo.getView(ListView.class, 0), "--.--");
Everything written without testing and even compiling, however main idea should be fine.