Android - change textView at run time - android

I´m working with a lineal layout and I´m trying to change the text of a TextView but it doesn´t refresh. When I debug I´ve checked that the text have changed correctly.
Thanks
public class Position extends Activity {
Button botonempezar;
Button botonsalir;
TextView text;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.position);
botonsalir = (Button) findViewById(R.id.salir);
botonempezar = (Button) findViewById(R.id.Empezar);
text = (TextView) findViewById(R.id.Textoposicion);
botonsalir.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onDestroy();
finish();
}
});
botonempezar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
searchPosition();
}
});
}
private void searchPosition(){
boolean condition = true;
do{
determinatePosition();
}while(condition == true);
}
private void determinatePosition(){
....
this.text.setText("New text");
//this.text.invalidate();
this.text.postInvalidate();
Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT);// this doesnt work neither.
....
}
Here I post the code of the xml file. Its really silly but it could be the problem:
<?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">
<TextView
android:layout_height="wrap_content"
android:id="#+id/Textoposicion"
android:text="Posición desconocida"
android:layout_width="fill_parent"
android:layout_marginTop="20dip"
android:inputType="text">
</TextView>
<Button android:layout_height="wrap_content"
android:id="#+id/Empezar" android:text="Empezar"
android:layout_width="fill_parent"
android:layout_marginTop="20dip">
</Button>
<Button
android:layout_height="wrap_content"
android:id="#+id/salir"
android:layout_marginTop="20dip"
android:text="Finalizar programa"
android:gravity="center" android:layout_width="fill_parent">
</Button>
</LinearLayout>
I have edited the post because I omitted part of the code to make it simpler but I think I may suppressed the problem too. It runs in a endless loop, could be this the mistake?

Try to call invalidate on the view.
http://developer.android.com/reference/android/view/View.html#invalidate()
this.text.invalidate();
This will cause a redraw.
Sorry, too quick, I saw that you already tried that.
Didn't that work? Why did you comment that away?

You forgot to call show() method on Toast :). Try this:
Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT).show();

Try not using getBaseContext(). Use Position.this
Well now your infinite loop is caused by
private void searchPosition(){
boolean condition = true;
do{
determinatePosition();
}while(condition == true);
}
Try moving the toast into public void onClick(View v)
Also, show us your imports

Related

Is it possible to add method invoked by clicking on a button in Android (not adding listener)

I wanted to invoke method when the button is clicked. I have found this solution
Button buttonOne = (Button) findViewById(R.id.button1);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do stuff here
}
});
but this solution is horrible.
Is it possible to add method invocation in XML when the:
<Button
android:id="#+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/actionButtonUnclicked"
/>
is clicked? In similar manner that it can be done in Windows Phone's and WPF's XAML? Something like android:onClick=clickMethod().
You can add method in xml using android:onClick="clickMethod"
In xml:
<Button
android:id="#+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/actionButtonUnclicked"
android:onClick="clickMethod"
/>
and in activity/ fragment add
your method should receive View v
public void clickMethod(View v){
// do smth
}
PS: If you want to use same method for multiple buttons
public void clickMethod(View v){
// check for id
if(v.getId() == R.id.button1){
//operation for button 1 click
} else if(v.getId() == R.id.button2){ //operation for button 1 click
}
}
Here:
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myButtonClick" />
<!-- even more layout elements -->
and in your class:
public void myButtonClick(View v) {
// does something very interesting
}

onClick not working In OnCreate

So I have a 2 buttons in a layout
like so:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot" />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"/>
</LinearLayout>
Then in the onCreate for the activity for this I have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
But for some reason when I tap the buttons nothing happens. I've looked around at others asking the question and tried those but still nothing. Can someone please help me figure out why it isn't working?
Thank you,
Tyler
You have to add the onClick properties in the Button's definition, not in the LinearLayout.
Even if you want to reuse the same method called onClick you can set a tag for each button, and do a switch for each tag. For instance:
In your layout:
android:tag="1"
In your code:
public void onClick(View v) {
String tag = (String) v.getTag();
switch (Integer.parseInt(tag)) {
case 1: // First button
...
break;
case 2: // Second button
...
break;
}
}
Remove onClick from your LinearLayout. Right now you have nested onClickListeners. The LinearLayout is intercepting the event and likely not passing it down.
edit: somebody is blindly downvoting all these answers for some reason. Post a comment explaining why this is wrong.
Please make your Button(s) clickable:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot"
android:clickable:true />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"
android:clickable:true/>
</LinearLayout>
Also implement the onClick() method in your Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
public void onCLick(View v)
{
//Your Implementation
}
I hope this helps.

setOnClickListener causes a crash of an APP

Hello I have this very simple code I'm trying to run via Android Studio
public class MainActivity extends ActionBarActivity {
Button random;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
random = (Button) findViewById(R.id.button);
display = (TextView) findViewById(R.id.TextView);
random.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}
});
I haven't really added very much, but whenever I use the setOnClickListener no matter what's inside of it crashes the app. I couldn't find a solution for this.
Thank you.
//edit: sorry. I added a wrong code, random is a button
It doesn't seem to be your case but I had the same problem and it was caused by having these lines
mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ...
}
});
before
setContentView(R.layout.activity_main);
You are setting the setOnClickListener method on the random object which by your code is currently Null.
So you are getting a NullPointerException.
I think you intended it to be button instead.
What is "random"? I think correctly will be:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}
});
In your code you are setting click listener on a button. Please make sure you get right id and right object to set listener. The button you want to set clcik listener on should have same id to the id you defined in you activity
In Activity
Button button =(Button)findViewById(R.id.button1);
In XML
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Firstly,check logcat by error level
Secondly,random is not initialized!!
You need
1.Create button in activity_main.xml
For example,
<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" >
<Button
android:id="#+id/myCoolButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
2.Init that button in code
Button myCoolButton = (Button) findViewById(R.id.myCoolButton);
3.Attach listener to button
myCoolButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
display.setText("I have changed");
}});
And it works! Hope that help you

Android ImageButton not firing the onclick event, what is wrong with my code?

I have very simple code that includes ImageButton with OnClickListener that points to another Activity, the click on the ImageButton doesn't fire the onClick (The same problem was with Button) :
public class ToolsActivity extends Activity {
private ImageButton btnCamera;
final Context context = ToolsActivity.this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
this.btnCamera = (ImageButton) findViewById(R.id.cameraButton);
this.btnCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_tools, menu);
return true;
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<ImageButton
android:id="#+id/cameraButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/btncamera"
android:contentDescription="#string/desc" />
I can't see anything wrong with the code. If it works with a regular button my guess is that you maybe have to set android:clickable="true" in the xml (you can also do it in code).
I opened new Android project and copy paste may code, and it works, I guess something with the project was damaged.

Set button clickable property to false

I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});

Categories

Resources