Hi all,
i am new to android development, in fact its my first application. i am making a simple calculator. i just want to know how to get the value of a pressed button.
Any help plz

Depends how you did setOnCLickListener. Can you post your code?

How to set OnClickListener?
my code is

package com.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CalculatorActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button b = (Button)findViewById(R.id.button1);
        String btn_val  =   b.getText().toString();
        EditText    edit_text   =   (EditText)findViewById(R.id.editText1);
        edit_text.setText(btn_val);



}

this code is getting the value of button id = button1 and setting it in text feild. i want to do all this work when clicked on any button e.g when clicked on 2, it should print 2 in text feild

Here is quick leg-up. From code bellow you can see I used onClick parameter in layout. This will call same method for update, but may get heairy in adding more functionality to handle mathematical operations. So you may consider using listeners as I will show at the end where inside the onClick method you will provide logic for what ever has to happen

layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <EditText
            android:id="@+id/display"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="0"
            android:gravity="right"
            />
    <TableLayout
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:weightSum="4">
        <TableRow
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
            <Button
                    android:id="@+id/button1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:text="1"
                    android:layout_weight="1"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="2"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="3"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button_plus"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="+"
                    android:onClick="updateDisplay"
                    />
        </TableRow>
        <TableRow
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
            <Button
                    android:id="@+id/button4"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="4"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button5"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="5"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button6"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="6"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button_minus"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="-"
                    android:onClick="updateDisplay"
                    />
        </TableRow>
        <TableRow
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
            <Button
                    android:id="@+id/button7"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="7"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button8"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="8"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button9"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="9"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button_times"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="*"
                    android:onClick="updateDisplay"
                    />
        </TableRow>
        <TableRow
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:layout_weight="1"
                android:weightSum="4">
            <Button
                    android:id="@+id/button0"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="0"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button_equals"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="="
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button_divide"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="/"
                    android:onClick="updateDisplay"
                    />

            <Button
                    android:id="@+id/button_clear"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="CE"
                    android:onClick="updateDisplay"
                    />
        </TableRow>
    </TableLayout>
</LinearLayout>

and activity

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CalculatorActivity extends Activity {
    private EditText display;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        display = (EditText)findViewById(R.id.display);
    }

    public void updateDisplay(View v){
        display.setText(display.getText().toString() + ((Button)v).getText());
    }
}

listener example

Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display.setText(display.getText().toString() + ((Button)view).getText());
            }
        });

When you use this listener remove android:onClick from each button in main.xml

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.