Android SDK Change text when Button clicked - android

package com.purelymean.earnings;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity{
/**Called when activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button);
TextView tx = (TextView) findViewById(R.id.textView1);
}
}
What do I need to put so that when the button is clicked it will change the textview to whatever i put?

You should add an OnClickListener to your button and override onClick function to set your desire text in your TextView
Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("yourtext");
}
});

Add an OnClickListener to your button and thats all you need.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tx.setText("It Works!");
}
});

Related

I want my button to be click once and i want to disable double click

package com.example.shery.tictactoe;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
TextView tv1,tv2;
String field1 = tv1.getText().toString();
#SuppressLint("CutPasteId")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn3 = findViewById(R.id.btn3);
btn4 = findViewById(R.id.btn4);
btn5 = findViewById(R.id.btn5);
btn6 = findViewById(R.id.btn6);
btn7 = findViewById(R.id.btn7);
btn8 = findViewById(R.id.btn8);
btn9 = findViewById(R.id.btn8);
tv1 = (TextView)findViewById(R.id.tv1);
if (field1.equals("Turn X")){
tv1.setTextColor(Color.GREEN);
}else if (field1.equals("Turn O")){
tv1.setTextColor(Color.GREEN);
}
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(field1.equals("Turn X")){
btn1.setText("X");
tv1.setText("Turn O");
} else {
btn1.setText("O");
tv1.setText("Turn X");
}
}
});
I want my button to be click once and I want to disable double click so what will be method of doing this and how to do that in my code and want to stop double clicking this is my code below you can check. I want my button to be click once and I want to disable double click :
Just call view.setEnabled(false); in your onClickListener to disable the view after the first click.
From what I understand, you want to disable de double click on the button. This is not possible because Android queues the clicks, but there's a trick I always use: Storing the last time it was clicked:
private long btn1ClickTime= 0; //local variable declaration
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (SystemClock.elapsedRealtime() - btn1ClickTime< 1000){
return;//do nothing
}
btn1ClickTime= SystemClock.elapsedRealtime();
//write your code here
}
}

click listener is not working

Hi I am new in android learning and I'm practice it. I Want to implement click listener on my button but it is giving me error. even i imported android.view.View.OnClickListener; and other all libraries.
please have look at my code check where is my mistake
Cannot Resolve symbol onClickelistner.
package com.example.nexus.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
}
button.OnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
Try this and put it in onCreate method
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
Combine R2R and Toan Tran answers will give you the best solution:
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
This should work.
Button button = (Button) findViewById(R.id.testButton);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// TODO Auto-generated method stub
}
});
Try button click inside Oncreate()
public class MainActivity extends AppCompatActivity {
Button button;
TextView loginresulttext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.loginbutton);
loginresulttext = (TextView) findViewById(R.id.loginresult);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
Toast.makeText(getApplicationContext(),"Button Is Clicked",Toast.LENGTH_LONG).show();
}
});
}
}

issue with my first android app

before posting my question , i wanna clarify that is my first post on stackoverflow and let's the story beggin.
as the title said , i'm making my first app on android and i found myself blocked with an issue .
there is 3 button on my app :
button1 : give textview2 "hello world again " and make it VISIBLE
// button2 : make textView2 INVISIBLE
// button3 : make textView1 INVISIBLE
this is the code freom main_activity :
package com.example.ismail.app_test_1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button button_aff;
Button button_hide;
Button button_hide_hw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_aff = (Button) findViewById(R.id.button);
button_hide = (Button) findViewById(R.id.button2);
button_hide_hw = (Button) findViewById(R.id.button3);
button_aff.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Show("hello world again");
}
});
button_hide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide();
}
});
button_hide_hw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide_hw();
}
});
}
public void Show(String str)
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.VISIBLE);
text.setText(str);
setContentView(text);
}
public void Hide()
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
public void Hide_hw()
{
TextView text;
text = (TextView) findViewById(R.id.textView);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
}
after getting it on my phone , when i touch any button : "Unfortunatly,app_test_1 has stopped !
can someone help me ?
Edit : i removed the setContentView and it works , thanks a lot guys. if someone has a good tuto that will help me to improve my android programming skills i'm a taker
Do not use setContentView() method outside of the onCreate()
just delete it in show and hide functions. Should work probably
Remove setContentView(text); and it will work. You don't need to call it.

Android TextView and Buttons not working in my app

I am learning Android and trying one simple Android app development, I got one demo code from my lecture and the teacher simply do the following:
There are 2 buttons, 1 textview. When touching button A, it will show "text A" in the textview, while touching button B, it will present "text B" in textview.
I followed the code and rewrote it, but i can't get the correct result when I ran with emulator.
When I touch either button, there's no content in the TextView. But my teacher's reference code works:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
How is report(View v) called? I can't understand how this class is called. Could someone please help me out?
You need to let your button know that, when pressed, report() should be called. This may be done through the android:onClick attribute of your button on your layout's XML:
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/button_text"
android:onClick="report" />
Or by code, attaching an OnClickListener to the button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
report(v);
}
});
}
Hope it helps.
You should implements the View.OnClickListener, and override that OnClick method. You also need to setOnClickListener on your buttons. So your code should be like:
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity implements View.OnClickListener{
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
import android.app.Activity;
public class ActTwo extends Activity {
private TextView tv;
private Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_act_two);
tv = (TextView) this.findViewById(R.id.textView2);
button1 =(Button)this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
button2 =(Button)this.findViewById(R.id.button2):
button2.setOnClickListener(new OnClickListener(){
public void onClick(View v){
report(v);
}
});
}
public void report(View v) {
if(v.getId() == R.id.button1)
tv.setText(R.string.anrep);
else
tv.setText(R.string.iprep);
}
}

Activity not moving to next activity when button click in android?

I am trying to move from one activity to another activity by clicking to the ImageButton. But when I click to button, it doesn't move to the activity which I specify in the code, and even it does not throw an error. I'm not getting where is problem
Here is my code which calls next activity :
package com.birthdayreminder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
ImageButton view, add, edit;
TextView tvadd, tvedit, tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
}
public void innicialize() {
// assigning buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// assign textview
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
I have also declared the class name in the manifest.xml but it does not working
Logcat Log file : 5 lines of last logcat file after clicking the button :
05-15 18:19:25.495: W/AudioFlinger(33): write blocked for 69 msecs, 1245 delayed
writes, thread 0xc658
05-15 18:19:28.964: I/ActivityManager(60): Starting: Intent {
act=com.birthdayreminder.REMINDER cmp=com.birthdayreminder/.Reminder } from pid 548
05-15 18:19:29.409: I/ActivityManager(60): Displayed
com.birthdayreminder/.Reminder: +426ms
try using getApplicationContext() in place of Remainder.this
check if you have a click listener on you imagebutton, you have declared what to do on click, but if you don't have any listener on the imagebutton, it won't work anyway.
You can try something like this :
yourImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Reminder.this, Addreminder.class);
startActivity(intent);
}
});
After doing this, you must have declared in your XML file the activity
<activity android:name="packageName.className" />
Finally, check if the new class is working well too but it must be ok!
==================================================================================
You mustn't declare a function or procedure in a function (like you did in onCreate), you can simply do what you do in initialize in your onCreate function like this :
package com.birthdayreminder;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class Reminder extends Activity {
private ImageButton view;
private ImageButton add;
private ImageButton edit;
private TextView tvadd;
private TextView tvedit;
private TextView tvview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder);
// Instantiating Buttons
view = (ImageButton) findViewById(R.id.bView);
add = (ImageButton) findViewById(R.id.bAdd);
edit = (ImageButton) findViewById(R.id.bEdit);
// Instantiating Views
tvadd = (TextView) findViewById(R.id.tvAdd);
tvedit = (TextView) findViewById(R.id.tvEdit);
tvview = (TextView) findViewById(R.id.tvView);
// Adding a clickListener on add button
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
// Adding a clickListener on edit button
edit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
// Adding a clickListener on view button
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
}
}
If you still have a problem, check your XML files, and look if you have well declared your buttons.
Problem Fixed: Never define a function in a function, because everytime you call the first function you'll define again the second function.
What you did which is wrong :
onCreate { //WRONG VERSION
bla bla bla bla
initialisation {
bla bla bla
}
}
What you have to do :
onCreate { //GOOD VERSION
initialisation();
bla bla bla
}
initialisation {
bla bla bla
}
you can call a function in a function, but you can't define a function in a function.
Moreover, you never call your initialisation function, you must call it now in your onCreate function or nothing will happen.
have you set onClickListener on add button.
btnAdd.setOnClickListener(this);
also check the
is your v.getId() and case R.id.bAdd: same?
debug and check that your case R.id.bAdd: is getting executed..
Seperately initialize the buttons in the first activity as
Button bAddbutton= (Button) findViewById(R.id.bAdd);
Button bEditBtn= (Button) findViewById(R.id.bEdit);
Button bViewBtn= (Button) findViewById(R.id.bView);
Then add seperate onClick listeners for each button as given below,
bAddbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Reminder.this, Addreminder.class);
startActivity(i);
}
});
bEditBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bEditBtn click here
}
});
bViewBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// enter the code for bViewBtn click here
}
});
initialize the button and add listener first in oncreate()
Button btnAdd = (Button) findViewById(R.id.bAdd);
btnAdd.setOnClickListener(this);

Categories

Resources