This question already has answers here:
Change button text and action - android development
(2 answers)
Closed 6 years ago.
I am new to java, and I am making a small little game that says "yes" or "no" when the user presses a button. I am not sure how to code to make the label change based on the user pushing the button. The code is below. Any other problems that you see with the code, I am interested in discovering those also.
public class FirstProject extends AppCompatActivity {
TextView answerTextView;
EditText name1Txt;
EditText name2Txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
answerTextView = (TextView) findViewById(R.id.answerLbl);
name1Txt = (EditText) findViewById(R.id.nameoneTxt);
name2Txt = (EditText) findViewById(R.id.name2Txt);
Button compBtn = (Button) findViewById(R.id.compBtn);
compBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
//where I want to make the button react
}
});
}
Every EditText has a method you have to call
EditText#setText(...);
Example:
name1Txt = (EditText) findViewById(R.id.nameoneTxt);
name2Txt = (EditText) findViewById(R.id.name2Txt);
Button compBtn = (Button) findViewById(R.id.compBtn);
compBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
//where I want to make the button react
name1Txt.setText("Hello...");
name2Txt.setText("...world");
}
});
Related
This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 2 years ago.
i am new to java and android studio ; i have set myself the goal of making a simple counter app in which one taps a button and a counter increases. Android studio shows no errors until i run t on my phone where it crashes as soon as i touch a button
i have tried to define the buttons as TextView tv; with no luck
public class MainActivity extends AppCompatActivity {
private int score;//this lets me use it else where within the public class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startCounter(View v) {
score = 0;
TextView tv = findViewById(R.id.textView); //the text view defines it as tv
tv.setText(score); //set the text of the label
}
public void buttonOnClick(View v) {
score++; //adds 1 to score
TextView tv = findViewById(R.id.textView); //the text view defines it as tv
tv.setText(score); //set the text of the label
}
}
i expect that the result is that the text view is updated to show the variable score but instead i get the error Invalid ID 0x00000000. and then D/AndroidRuntime: Shutting down VM
I just replace your code so please copy and follow
int score=0;
public class MainActivity extends AppCompatActivity {
private int score;//this lets me use it else where within the public class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView)findViewByID(R.id.tvID);
Button myButton = (Button)findViewByID(R.id.buttonID);
myButton.setOnClickListener(new View.OnClickListener{
score++;
tv.setText(score.toString());
});
}
I want to create this textview, and then display it after this button is clicked, but no textview is displayed.
I dont't want to use findViewById(), if possible, because I want to create a new textview every time the button is pressed. I've tried making a linear layout first, but I've seen a lot of websites say that you don't need to, and I would prefer not to. Any advice would be helpful. Thank you.
EditText name=layout.findViewById(R.id.enterName);
final String Name=name.getText().toString();
Button create=layout.findViewById(R.id.create);
create.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView ProgrammaticallyTextView = new TextView(MainActivity.this);
ProgrammaticallyTextView.setText(Name);
ProgrammaticallyTextView.setTextSize(22);
popup.dismiss();
}
});
There are no error messages and the logcat doesn't say that anything is wrong.
Try like this :
private LinearLayout lLayout;
private EditText lEditText;
private Button lButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lLayout = (LinearLayout) findViewById(R.id.linearLayout);
lButton = (Button) findViewById(R.id.button);
lEditText = (EditText) findViewById(R.id.editText);
lButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("Text New");
}
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
lLayout.addView(createTextView(lEditText.getText().toString()));
}
};
}
private TextView createTextView(String text) {
final LayoutParams loutParams = new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(loutParams );
textView.setText("Text is " + text);
return textView;
}
Use the activity's findViewById() method to reference your layout views. For example, you can replace
EditText name=layout.findViewById(R.id.enterName);
Button create=layout.findViewById(R.id.create);
with
EditText name=getActivity().findViewById(R.id.enterName);
Button create=getActivity().layout.findViewById(R.id.create);
Note: if you are not using fragments then there is no need to use getActivity since findViewById() is a method of the superclass AppCompactActvity( or Activity).
I guess your code is not working because the Button View and Editext Views have not been reference when activity starts for the oncreate() method
I am testing my knowledge of android studio and seem to be running into a problem I am trying to make an app that takes the value of a edittext and puts that value into a textview when you click a button. After that it brings you to a page with a back button on it and when I click that it brings me to the first page. But when I try to click the first button it doesn't work anymore.
Code:
public class TestApp extends AppCompatActivity {
private EditText ET;
private Button Add;
private TextView TV;
public String Array[] = new String[9999999];
private Button GoBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_app);
ET = (EditText) findViewById(R.id.ET);
Add = (Button) findViewById(R.id.Add);
Add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String getC = ET.getText().toString();
setContentView(R.layout.value);
TV = (TextView) findViewById(R.id.TV);
GoBack = (Button) findViewById(R.id.GoBack);
if (getC.length() > 0){
for (int i = 0; i < Array.length; i++){
if (Array[i] == null){
Array[i] = getC;
TV.setText(Reminders[i]);
}
}
}
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_calender_for_glasses);
}
});
}
});
}
}
thank you for your help.
If not necessary, don't modify the current content view. If you want multiple screens, you have to create multiple activities or fragments...
Moreover :
at the beginning, you inflate R.layout.activity_test_app
when you click on goBack button (1), you inflate R.layout.activity_calender_for_glasses and it's not the same as the first screen
(1) be careful to coding rules: https://source.android.com/setup/contribute/code-style
Trying to create notes application. I am trying to add dynamic text view after button click and it should be added to layout, one after another. In my code, only one text view is getting added successfully but from second time, app got crashed. Please help
XML Code:
<EditText
android:id="#+id/text_thingsToDo"
android:layout_width="#dimen/ThingsToDo_EditText_Width"
android:layout_height="#dimen/ThingsToDo_EditText_Height"
android:layout_margin="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_EditText_Color"/>
<Button
android:id="#+id/save_thingsToDo"
android:layout_toRightOf="#+id/text_thingsToDo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ThingsToDo_Save_Button_Text"
android:layout_marginRight="#dimen/Standardize_Margin"
android:layout_marginTop="#dimen/Standardize_Margin"
android:layout_marginBottom="#dimen/Standardize_Margin"
android:background="#color/ThingsToDo_Save_Button"
android:textColor="#color/White"
android:layout_centerVertical="true"/>
Java Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_things_to_do);
final EditText editText = (EditText) findViewById(R.id.text_thingsToDo);
Button button = (Button) findViewById(R.id.save_thingsToDo);
final TextView notesView = new TextView(this);
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.rootView_ThingsToDo);
final ArrayList<String> arrayList = new ArrayList<String>();
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
}
Perhaps create a new TextView every time you press the button? Try to declare the TextView as a class member and then in your OnClickListener do this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notesView = new TextView(this);
notesView.setText(editText.getText().toString());
linearLayout.addView(notesView);
}
});
Hopefully this will fix your problem!
I'm new to Android development, I am currently trying to see if a value entered is equal to a value. Here I am seeing if the user input equals to 5, they are currently set as strings as to is the text field.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uk_postage);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button home = (Button) findViewById (R.id.btnHome);
Button calculate = (Button) findViewById (R.id.btnCalculate);
home.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(ukPostage.this, MainActivity.class));
}
});
EditText lengthInput = (EditText) findViewById(R.id.editTextLength);
final String lengths = lengthInput.getText().toString();
final TextView amount = (TextView) findViewById (R.id.txtAmount);
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
if (lengths.equals("5")) {
amount.setVisibility(View.VISIBLE);
}
}
});
The text goes to visible if i click the button without the if statement there however won't once I write the if statement.
Thanks in advance.
if (lengths.equals("5")) {
String is immutable and once you reference it you need to reference it again with your edit text to reflect the latest value.
Instead of the above code you need to reference the lengths back with the lengthInput value each time you click the calculate button.
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
lengths = lengthInput.getText().toString();
if (lengths.equals("5")) {
amount.setVisibility(View.VISIBLE);
}
}
});
IMPORTANT: You need to set the lengths as a global variable instead of being a final variable since it can only be initialized once.