• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps EditText/Button Issue

poxkix

Lurker
I made simple android ativity consisting of 1 EditText and 1 Button.

The EditText will serve as the output when the Button is pressed.
My expected output is when I press the Button 3 times, in the EditText it should show 3 values but no matter how I press the Button it will only show 1 value. What is wrong with my code? I can't seem to find a solution.

XML code:
Code:
<Button[INDENT] android:layout_width="45dp"
android:layout_height="45dp"
android:text="@string/button_17"
android:id="@+id/aye"
android:textColor="@color/BLUE"
android:textSize="12dp"
android: onClick="onClickOne"
[/INDENT]/>
JAVA code:
Code:
package com.android.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class example extends Activity {
private EditText output;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
output = (EditText)findViewById(R.id.output);
}[INDENT] public void onClickOne(View v) {
output.setText(R.string.button_17);
[/INDENT]}
}
 
First of all, if this is all your code, you have not added a click listener to your button.
Second, you don't temporerily save the EditText text you overwrite it with new text.

Try this:
Code:
package com.android.example;

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

public class example extends Activity implements OnClickListener
{
private Button btn;
private EditText output;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn = (Button)findViewById(R.id.onClickOne);
btn.setOnClickListener(this);
output = (EditText)findViewById(R.id.output);
}

public void onClick(View v)
{
output.setText(output.getText().toString() + getString(R.string.button_17));
}
}
 
of course! This is my code :))

I thought the android: onClick="onClickOne" works as the click listener. So i'm wrong? Should I remove that?

thank you for the help, I will try this and get back here.
 
I'm sorry. My bad. Yes, the onClick:onClickOne is the click listener.
So the main reason you don't get the ouput you want is because you dont save the current text. This should do it for you: output.setText(output.getText().toString() + getString(R.string.button_17));
 
thank you so much. :)

btw, I set the EditText text default to "0.", now when I click the button the "0." will just append to the output. How do make it clear?
Code:
<EditText
        android:layout_width="280dp"
        android:layout_height="35dp"
        android:id="@+id/output"
       [COLOR=Red] android:text="0."[/COLOR]
        android:gravity="right"
        android:layout_gravity="center_horizontal"
        android:background="@color/WHITE"
        android:paddingTop="10dp"
        android:layout_marginTop="5dp"
        android:maxLength="25"
        android:singleLine="true"
        android:digits="1234567890"
    />
EDIT:
I thought it is because of the cursor position so I tried this:

output.getText().insert(output.getSelectionStart(), "1");

The cursor got transferred but the default text "0." won't clear.
 
Back
Top Bottom