can't repeat text in editText using a button - java

I have a simple activity in android with a button and an editText widget.
I have referenced the resource for the button, created an instance variable for the button, and invoked the listener method on it.
When I run the application, the button prints to the edit text widget, but only once.
I cannot continue to type "0000000", which is the ideal output that I want, rather than being stuck on "0".
The code I have used is:
public class MainActivity extends Activity implements OnClickListener {
Button btn1;
EditText text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn0);
// set an onclick listener to listen for when the buttons are clicked
toe.setOnClickListener(this);
text = (EditText) findViewById(R.id.editTextSimple);
}
#Override
public void onClick(View v) {
text.setText(btn1.getText().toString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Is there a method or a work around that I could implement in order to print more digits to the edit text view?
Thank you for your help

There is no need to reinvent the wheel here. You don't even have to use direct Strings.
Your onClick could simply be:
#Override
public void onClick(View v) {
text.append(btn.getText());
}
Also, I don't know what "toe" is in
toe.setOnClickListener(this);
but you should be setting the clicklistener on btn1

Replace
text.setText(btn1.getText().toString());
with
String t=text.getText().toString();
text.setText(t+btn1.getText().toString())

Related

Make android app backround change when button is pushed, and is on value true or false

In my android app I have a button named "button1". When you click the button a boolean gets either true or false and displays some text appropriately for either true or false.
What I would like to have it do is along with change the text I would like it to change the background based on whether the value after you click the button is true or false.
I have tried anything and everything I can think of so any suggestions and help would be appreciated.
Below is the code without any modifications to try and make the background change.
public class MainActivity extends Activity {
Random r = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void calculate(View v){
EditText number1text=(EditText)findViewById(R.id.num1text);
String num1=(number1text.getText().toString());
String ans=num1;
TextView answer=(TextView)findViewById(R.id.answer);
boolean value = r.nextBoolean();
if(value==true){
answer.setTextColor(Color.GREEN);
answer.setText("It is a cause for celebration! So CELEBRATE!");}
if(value==false){
answer.setTextColor(Color.RED);
answer.setText("Exciting, but not a cause for celebration!");}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
If you want to change background of your activity then try this,
View root = findViewById(R.id.main);
root.setBackgroundColor(getResources().getColor(R.color.red));
so your full activity code would look something like this
public class MainActivity extends Activity {
Random r = new Random();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View root = findViewById(R.id.main);
}
public void calculate(View v){
EditText number1text=(EditText)findViewById(R.id.num1text);
String num1=(number1text.getText().toString());
String ans=num1;
TextView answer=(TextView)findViewById(R.id.answer);
boolean value = r.nextBoolean();
if(value==true){
answer.setTextColor(Color.GREEN);
root.setBackgroundColor(getResources().getColor(R.color.red));
answer.setText("It is a cause for celebration! So CELEBRATE!");}
else{
root.setBackgroundColor(getResources().getColor(R.color.green));
answer.setTextColor(Color.RED);
answer.setText("Exciting, but not a cause for celebration!");}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Android/Java passing value from one screen to another and displaying value

I am a beginner, and having trouble.
I have two pages. On logon.java user enters name and clicks button. On second screen I need to have "Hello"+ the name they entered on the first page.
So for the first page I have:
public class LogonActivity extends Activity
{
private EditText enterName;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//associate view with activity
setContentView(R.layout.logon);
//set the button
Button button=(Button)findViewById(R.id.button);
//access the field where the user entered the name
final EditText editTextName= (EditText)findViewById(R.id.enterName);
//button listener
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//save the editTextName to a new variable -enterName
String enterName=editTextName.getText().toString();
//create explicit intent
Intent nameResultsIntent = new
Intent(getApplicationContext(),
ConfirmationActivity.class);
//pass that to next activity
nameResultsIntent.putExtra("PERSON_NAME",enterName);
startActivity(nameResultsIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.logon_main, menu);
return true;
On the second page is:
public class ConfirmationActivity extends Activity {
EditText enterName;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//associate the layout with this activity using setContentView
setContentView(R.layout.confirmation);
//get the name the user entered
String enterName=getIntent().getStringExtra("PERSON_NAME");
findViewById(R.id.confirmationText);
String confirmationText = enterName;
//confirmationText.setText(enterName);
finish();
}
}
}
}
So... its the last lines on the ConfirmationPage.
The Text field on the 1st page is : android:id="#+id/enterName"
The field on the 2nd page where I want the text to appear is :
Can someone help with the last line to show the text on the second page?
Try something like this
//get the name the user entered
String enterName=getIntent().getStringExtra("PERSON_NAME");
EditText confirmationText = (EditText)findViewById(R.id.confirmationText);
confirmationText.setText(enterName);
finish();

Quit item menu in Android application?

This is a way to close an application using a button:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
System.exit(0);
}
});
}
Instead using a button, how can i create a quit item menu? Thanks
Use methode onOptionsItemSelected(...)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
...
}
#Override
public boolean onOptionsItemSelected(MenuItem Item) {
...
}
see:
http://developer.android.com/guide/topics/ui/menus.html
You can add menu in action bar by defining it in a menu.xml and can detect on click on that menu using onOptionsItemSelected and can create menu using onCreateOptionsMenu method and Here is the very good tutorial for this -: http://www.vogella.com/articles/AndroidActionBar/article.html
Best way to close you're application using click count by pressing back button you can use click count by pressing two times and use
activity.finish();
System.exit(0);
Try this after the menu thing:
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
This will kill your com.myapp.activity process

Where should i declare the button of different layout?

I've made a calculator apps and I'm trying to create an about page showing some information.
The OK button will be coded setContentView(originallayout.xml) to return to the calculator layout.
Where should i put these codes to declare the OK button?
private Button btnOK;
btnOK = (Button)findViewById(R.id.btnOK);
btnok.setOnClickListener(OKListener);
I tried to put these code just below where i did for the buttons at the main layout but the apps just stopped after launch.
07-18 09:39:43.290: E/AndroidRuntime(6984): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hychentsa.calculator/com.hychentsa.calculator.CalculatorActivity}: java.lang.NullPointerException
Instead of using setContentView() to change screens, you should have separate activities. Then, in your about activity, you can simply call finish() on button click to go back to the main activity.
http://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)
if your layout doesn't content the id of the button (in your case btnOK), Eclipse throws NullPointerException - it can not find it in your layout's content.
So when you set your layout (or menu), it must content the id btnOK. Check it!
Put your button initialization after setContentView(R.layout.your_about_layout_name);
Put all this code in
Button btnOK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
btnOK = (Button)findViewById(R.id.btnOK);
btnok.setOnClickListener(OKListener);
}
Update:
Look at the answer of invertigo:
It's wrong to change the layout when you click on the button.
You have to do it this way:
CalculatorActivity
public class CalculatorActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator_black);
// initialization of your views stays here
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.calculator_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.your_id_to_go_in_about_activity:
Intent intent = new Intent(CalculatorActivity.this, AboutActivity.class);
// put some extras if you need to send information from this page to the
// AboutActivity page with this code: intent.putExtra();
startActivity(intent); // with this code you go to AboutActivity
return true;
case R.id.theme:
// Do Something with the theme
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Now, the place for your initialization of OKButton is in new class, lets call it
AboutActivity
here you can put my earlier code:
public class AboutActivity extends Activity{
Button btnOK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
btnOK = (Button)findViewById(R.id.btnOK);
btnok.setOnClickListener(OKListener);
}
// and the listener for your OK button have to look like this:
OnClickListener OKListener = new OnClickListener() {
#Override
public void onClick(View v) {
// Do something here if you need
finish(); // with finish() you are returning to the previous page
// which is CalculatorActivity
}
};
}

Button Handlers in Android App

I am trying to add multiple button handlers for my app. I have successfully created one button that links to a web page. I am running into trouble with my next button, however. I want to go to a new screen on click. I have create the new screen xml file and the corresponding java file. I have also added the activity to the manifest. I'm just not sure how to add multiple button handlers to the main java page. I have attached how I did my first button. Any advice on how to add a second handler to this page for a button that will change view?
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent browserIntent =
new Intent(Intent.ACTION_VIEW, Uri.parse("http://m.noblenet.org/eg/opac/home?locg=1"));
startActivity(browserIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
If I understand it correctly then buttons appear through onCreateOptionsMenu logic.
Best way to handle the click events in this case would be to override onOptionsItemSelected API. Look here for more details
One of the simplest ways of handling button click is to use android:onClick attribute in xml file for a button view. For example in Button View of your xml add android:onClick="clickMe"
and in your java code write Public void clickMe(View view) { //do something on button click }

Categories