joannakomodo
New Member
I'm newbie in android development and going to create simple drag'n'Drop calculator.My goal is to make buttons from 0 to 9 and create numbers by clicking them.For example:0 1 2 3 4 5 6 7 8 9and by clicking them create number 245, then drag this number, for example, into the sum mark +, then repeat second number and automatically retrieve results.First, I'll show my code now...MainActivity.java\[code\]package com.coreprojects.calculator;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.app.Activity;public class MainActivity extends Activity { TextView calc_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } // This method comes from XML button = buttonClick public void buttonClick(View v) { // Textarea where numbers will be shown TextView TV = (TextView) findViewById(R.id.calc_cache_1); // Get the ID of buttons Button calc_btn_1 = (Button) findViewById(R.id.button1); Button calc_btn_2 = (Button) findViewById(R.id.button2); // INT type numbers convert to string, because of [setText] String buttonText_1 = calc_btn_1.getText().toString(); String buttonText_2 = calc_btn_2.getText().toString(); // Result as text TV.setText(buttonText_1); // first number is not shown TV.setText(buttonText_2); // second number // alert /** String pressed = "Operation Done"; new AlertDialog.Builder(this).setTitle("result").setMessage(pressed) .setNeutralButton("Done", null).show(); */ }}\[/code\]activity_main.xml\[code\]<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/calc_cache_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/main_text" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:clickable="true" androidnClick="buttonClick" android:text="@string/button_1" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginLeft="64dp" android:clickable="true" androidnClick="buttonClick" android:text="@string/button_2" /></RelativeLayout>\[/code\]strings.xml\[code\]<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Calculator</string> <string name="main_text"></string> <string name="menu_settings">Settings</string> <string name="button_1">1</string> <string name="button_2">2</string></resources>\[/code\]Now problem...When i click on buttons, any time result shows number 2, because i think \[quote\] TV.setText(buttonText_2) is second command and first one is not read any more.\[/quote\]I tried to write together, like TV.setText(buttonText_1 + buttonText_2), but now it results 12, even if i click any single button...so how can I append numbers by clicking them? and also, if u see here, i have clickable button in XML, and get ID of pressed button, but i want it to be more dynamic.For example: when i press button, don't get the ID of it, but simply identify the button ID and get value. i can show u simple analog in jQuery:\[code\]$(".button_class_not_ID").click( var elementValue = http://stackoverflow.com/questions/14456792/$(this).html(); or var elementValue = $(this).attr("id"); console.log(elementValue);\[/code\]