I want to change the of a TextView by pressing a Button, but don't understand how to do it properly.
This is part of my layout:
<TextView android:id="#+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text" />
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change text!" />
And this is my activity:
public class Click extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
// ???
}
});
}
}
What should I put inside onClick() method?
According to:
http://developer.android.com/reference/android/widget/TextView.html
TextView view = (TextView) findViewById(R.id.counter);
view.setText("Do whatever");
Find the textview by id
Change the text by invoking yourTextView.setText("New text");
Refer findViewById and setText methods.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Click extends Activity {
int i=0;
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final TextView mTextView = (TextView) findViewById(R.id.counter)
mTextView.setText("hello "+i);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
i=i+1;
mTextView.setText("hello "+i);
}
});
}
}
Hope this serve your need
TextView tv = (TextView) v;
tv.setText("My new text");
Edit:
Here is your handler:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//TextView tv = (TextView) v; //code corrected
TextView tv= (TextView) findViewById(R.id.counter);
tv.setText("My new text");
}
});
TextView view = (TextView) findViewById(R.id.counter);
You can do it with the setText("anything") method.
In onclick, take the object of the TextView and set the desired text like so:
tvOBJECT.setText("your text");
Java only no XML version
To make things more explicit:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Main extends Activity {
private int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.i = 0;
final TextView tv = new TextView(this);
tv.setText(String.format("%d", this.i));
final Button button = new Button(this);
button.setText("click me");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Main.this.i++;
tv.setText(String.format("%d", Main.this.i));
}
});
final LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(button);
linearLayout.addView(tv);
this.setContentView(linearLayout);
}
}
Tested on Android 22.
Related
I would like to create checkbox with text that would behave something like what i have below in pseudo code:
Set in text in EditText field
if Button clicked:
create new Checkbox with entered text.
How do i go about doing that in Android?
So far ive got the button :) ... but dont know how to get new Checkbox with text in it ,if its clicked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button create = (Button) findViewById(R.id.button1);
create.setOnClickListener(this);
EditText textInCheckBox = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
// TODO Automatisch generierter Methodenstub
switch (v.getId()) {
case R.id.button1:
//after creating new checkbox with text in it
//Editbox should restet text field to null;
break;
}
}
}
Here is some code that will demonstrate how to add checkboxes dynamically with the title you entered into the EditText.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
final Button btnCreate = (Button) findViewById(R.id.btnCreate);
final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = txtTitle.getText().toString().trim();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
checkBox.setLayoutParams(params);
radContainer.addView(checkBox);
} else {
Toast.makeText(MainActivity.this, "Enter a title for the checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp" >
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone" />
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Checkbox" />
<RadioGroup
android:id="#+id/radContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I have a problem with my android project. I cannot get access to my button. I have attach my class code==http://pastebin.com/A5ZTBkhd. Please anyone help me.
Here is my code -
package com.droid.androiddoctor;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidDoctorMainActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_android_doctor_main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnSubmit:
String e="hello";
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
break;
}
}
}
.Here is my code.when i push submit nothing happens. and the xml file is====
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnSubmit"
androi:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit" />
</LinearLayout>
You did not set the listener for your widgets at all. Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
button1.setOnClickListener(this)
}
I would suggest you to go through Android's UI Guide.
I wouldn't implement OnClickListener like that, try to add it like this in your class:
public class AndroidDoctorMainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
Button button2 = (Button) findViewById(R.id.btnExit);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for click on this button
}});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for this button
//or if you just want to exit from activity, just call:
finish();
}});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
}
}
This has been bugging me, I'm a beginner and can't seem to figure this out. Here's my code:
package com.example.imhungry;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class ImHungry extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im_hungry);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(9);
String wordToDisplay = wordList[randomInt];
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_im_hungry, menu);
return true;
}
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(9);
String wordList[] = new String[9];
{
wordList[0] = "Mexican";
wordList[1] = "American";
wordList[2] = "Barbeque";
wordList[3] = "Chinese";
wordList[4] = "Indian";
wordList[5] = "Italian";
wordList[6] = "Thai";
wordList[7] = "Viatnamese";
wordList[8] = "Middle Eastern";
}
String wordToDisplay = wordList[randomInt];
}
Basically I am attempting to make the wordToDisplay show up in my textView but I cannot figure out how to do so.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text=""
tools:context=".ImHungry" />
Also, sorry for asking so many questions, i'm new at this. Thanks!
To make your test appear in TextView you have to set it with setText();, so basically you have to assign your textview id or tag (add android:id="#+id/mytextview) then get that object TextView tv = (TextView)findViewById(R.id.mytextview); tv.setText("foo"); I'd also recommend going through some tutorials on android basics - will help
Add id to the TextView:
<TextView
android:id="#+id/text_random_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="#dimen/padding_medium"
android:text=""
tools:context=".ImHungry" />
In the code do the next:
Add just after the button:
final TextView textView = (TextView) findViewById(R.id.text_random_text);
Set the wordToDisplay to the text in the onClick(View v) method
textView.setText(wordToDisplay);
For efficiency try this:
public class ImHungry extends Activity {
Random mRandomGenerator = new Random();
TextView mTextView;
String mWordList[] = new String[] {
"Mexican", "American", "Barbeque",
"Chinese", "Indian", "Italian",
"Thai", "Viatnamese", "Middle Eastern" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im_hungry);
mTextView = (TextView) findViewById(R.id.textView);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
mTextView.setText(mWordList[mRandomGenerator.nextInt(9)]);
}
});
}
}
You can remove the rest of the code.
You only need to create one random number object and you don't need to save the random Integer or corresponding String in local variables, if you only use them once. However it is okay to save them in local variables if you want to improve readability.
How would I use a list in this custom dialogue? ive set a list int the .xml of the listview but am unfamiliar with how to use it properly (I want the values for the list to come from a string array)
My main java:
package custom.dialouge.list;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
My custom dialouge .xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Back" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ListView
android:id="#+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TextView01" >
</ListView>
</RelativeLayout>
Try something like this:
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ListView view = dialog.findViewById(R.id.listview1);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
view.setAdapter(listAdapter);
Like so:
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
ListView view = dialog.findViewById(R.id.listview1);
view.setAdapter(listAdapter);
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
I have recently started on android.
i have written this piece of code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArrangeMe extends Activity {
private Button button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.button1 = (Button)findViewById(R.id.buttonOne);
this.button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
setContentView(R.layout.main);
}
}
and my main.xml looks as below
<?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="ArrangeMe"/>
<Button android:text="Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/buttonOne"></Button>
</LinearLayout>
but when i pass through this line
this.button1 = (Button)findViewById(R.id.buttonOne);
i observe button1 = null. but when i type R.id. eclipse does suggest an auto complete buttonOne (that suggests layout xml is correct !)
where am I going wrong ?
edit:
interstingly, i tried the following code,
still the button does not appear. it has stopped crashing, but the button does not appear !
button1 = new Button(getContext());
button1.setText("1");
addView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
this was giving error
i changed them to
button1 = new Button(getBaseContext());
button1.setText("1");
addContentView(button1, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
I think you should call setContentView(R.layout.main); After that you activity class should be aware there to find views.
You are assigning the button1 before setting the ContentView in your Activity. Change your code as follows,
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ArrangeMe extends Activity {
private Button button1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // you misplaced this line in your code
this.button1 = (Button)findViewById(R.id.buttonOne);
this.button1.setOnClickListener(new OnClickListener() {
//#Override
public void onClick(View v) {
finish();
}
});
}
}
you must use small letter for id of your view and second one build your project and clean that may generate id for button in R.java.