First let me preface this with being completely new to Android development. I am trying to simply have text on a different buttons change when I click on "this" button. I can change the visibility fine with setVisibility() so I think I am referencing the buttons correctly - but when I attempt to setText() I get the error: "Cannot resolve method 'setText(Java.Lang.String)'"
Why will it allow me to change visibility but not text? What do I need to do to correct the problem?
Here's a portion of the XML for one of the buttons I am attempting to change text on:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/songbutton4"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/button4"
android:layout_alignEnd="#+id/button4"
android:visibility="invisible" />
Here is the code for the onclick event:
public void onbutton1click(View v){
//on click turn the 4 buttons invisible, and show the other 5
View b1 = findViewById(R.id.button1);
b1.setVisibility(View.INVISIBLE);
View b2 = findViewById(R.id.button2);
b2.setVisibility(View.INVISIBLE);
View b3 = findViewById(R.id.button3);
b3.setVisibility(View.INVISIBLE);
View b4 = findViewById(R.id.button4);
b4.setVisibility(View.INVISIBLE);
View b5 = findViewById(R.id.button5);
b5.setVisibility(View.INVISIBLE);
View sb1 = findViewById(R.id.songbutton1);
sb1.setVisibility(View.VISIBLE);
sb1.setText("hello"); // THIS IS WHERE THE ERROR OCCURS
View sb2 = findViewById(R.id.songbutton2);
sb2.setVisibility(View.VISIBLE);
View sb3 = findViewById(R.id.songbutton3);
sb3.setVisibility(View.VISIBLE);
View sb4 = findViewById(R.id.songbutton4);
sb4.setVisibility(View.VISIBLE);
}
Are you doing it in Fragment or in Activity?
Anyway, you should do:
Button sb1 = (Button) findViewById(R.id.songbutton1);
This is how we add elements. If you set Button widget in your layout, in this way you need to have reference to this widget. Declaring proper type and casting using ( ).
If you are doing it in Fragment, not in Activity, you also need to bind it to proper view.
Button sb1 = (Button) v.findViewById(R.id.songbutton1);
And advice - don't get reference to views (widgets) in listener. Do it before listener.
Firstly assign the button to a variable i.e: Button btnSong = (Button) findViewById(R.id.songbutton4); then set an onclick listener to the button through the method btSong.setOnClickListener(); which will require you to declare a new onClickListener inside the button and a method will be created within which u can place the above code under the method. Alternatively add an XML attribute that points the button straight to the method.
Related
I don't have my source code right now, but I was wondering if it is possible to use visibility(GONE) with the ID's or something like that?
The reason : I have a form where I want to have 20 dropdowns, and a button "add a new activity" . When you click on the button, it unhide a new control. The problem is in how to tell the app what dropdown to unhide...
Example :
dropdown1 (visible) [Button add new]
dropdown2 (invisible)
dropdown3 (invisible)
[...]
user click on [Button Add new]
dropdown1 (visible)
dropdown2 (visible)
dropdown3 (invisible)
[...]
Or something similar in process.
Thanks! (sorry not english speaking person... I hope this is understandable as a question!)
In xml give id to all spinner (Dropdown) like drop1,drop2,...........
like this
<Spinner
android:id="#+id/drop1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/spinner_title"/>
In Java file add
Spinner drop1,drop2,............drop20;
Under onCreate Method
drop1 = (Spinner)findViewById(R.id.drop1);
drop2 = (Spinner)findViewById(R.id.drop2);
--------------------------------------
drop20 = (Spinner)findViewById(R.id.drop20);
Set Visibility
if(condition) // your condition to hide dropdown
{
drop1.setVisibility(Visible.GONE);
}
else
{
drop1.setVisibility(Visible.VISIBLE);
}
After mapping the control you can use setVisibility() method with it
eg:
Button btn=(Button)findViewById(R.id.button1);
btn.setVisibility(View.Visible);`
Yes, you can do it. Using android:id tag in XML layout.
In your Java code you can do it like this:
Button button1 = (Button) findViewById('button1');
Button button2 = (Button) findViewById('button2');
buton1.setOnClickListener( new View.OnClickListener( ) {
#Override
public void onClick( View v ) {
button2.setVisibility( View.VISIBLE );
}
} );
I'm trying to get my button to create a text field where the user can input information. This way they can only create as many lines as they would like. Also, is there a way to create multiple fields at once?
So, it'll end up being something like this:
"Add Event" (rest of the screen is blank until they click on that button)
Text field 1/ Text field 2/ Text field 3
(once they press that button and of course without the underlines, just an example)
So they can put in information that they want there. If they want another row, they click on the add button again.
Am I supposed to be using an onClickListener? I'm confused as to how I would go about making the button create that field for the user.
public class BudgetScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_budget_screen);
Button addBillExpense = (Button) findViewById(R.id.addBillExpense);
addBillExpense.setOnClickListener(new Button.OnClickListener() {
public void onClick (View v) {
TextView inputField = new TextView(BudgetScreen.this);
}
});
}
}
That is what I have so far. I've been stuck on this for a hot minute. I am aware that I haven't used "inputField" yet.
Suppose you have the following layout xml:
<LinearLayout ...>
<Button .../>
<LinearLayout ...
android:id="#+id/holder"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
in button onClickListener you can have something like:
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.addView(et,lp);
You can change the LayoutParams to get the layout you like.
If you want multiple EditText in a single row, you can do the following:
final int NUM_EDITTEXT_PER_ROW = 3;
LinearLayout layout = (LinearLayout) findViewById(R.id.holder);
Display display = ((WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth()/NUM_EDITTEXT_PER_ROW;
LinearLayout tempLayout = new LinearLayout(this);
tempLayout.setOrientation(LinearLayout.HORIZONTAL);
for(int i=0;i<NUM_EDITTEXT_PER_ROW;i++){
EditText et = new EditText(this);
LayoutParams lp = new LayoutParams(width,LayoutParams.WRAP_CONTENT);
tempLayout.addView(et,lp);
}
layout.addView(tempLayout);
I'm creating an EditText in onOptionsItemSelected() and trying to get it's information in onClick(). Here's the offending code:
onOptionItemSelected(MenuItem item){
...
EditText mealCalories = new EditText(context);
mealCalories.setId(MealCalId) //in this example it's just an integer 1.
...
}
onclick(View v){
EditText mealCaloriesInBox = (EditText)findViewById(mealCalId);
}
When I haven't selected an item from the menu (and thus haven't called onOptionItemSelected();) it doesn't crash when I click the button. However, when I actually have created the EditText and I click the button it crashes as it's trying to create the instance, giving me the aforementioned error. Any ideas on why it could be doing that?
EDIT
Here's more of my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Context context = getApplicationContext();
switch(item.getItemId()) {
case R.id.addMeal:
trackMealItems++;
mealCalId++;
mealFatId++;
mealCarbId++;
mealProteinId++;
//the base layout
LinearLayout root = (LinearLayout)findViewById(R.id.linearLayout1);
//make the layout that holds the meal item and add it to the base layout
LinearLayout mealItem = new LinearLayout(context);
mealItem.setId(trackMealItems);
mealItem.setOrientation(LinearLayout.VERTICAL);
mealItem.setLayoutParams(mealItemParams);
root.addView(mealItem);
//make the TextView that holds the name of the meal and add it to the mealItem layout
EditText mealName = new EditText(context);
mealName.setLayoutParams(mealNameParams);
mealItem.addView(mealName);
//make the TextViews that hold the information about the meal and stick them in a
//horizontal LinearLayout
LinearLayout mealStats = new LinearLayout(context);
mealStats.setOrientation(LinearLayout.HORIZONTAL);
mealStats.setLayoutParams(mealStatParams);
mealItem.addView(mealStats);
EditText mealCalories = new EditText(context);
mealCalories.setId(mealCalId);
mealCalories.setLayoutParams(mealStatParams);
mealStats.addView(mealCalories);
EditText mealFat = new EditText(context);
mealFat.setId(mealFatId);
mealFat.setLayoutParams(mealStatParams);
mealStats.addView(mealFat);
EditText mealCarbs = new EditText(context);
mealCarbs.setId(mealCarbId);
mealCarbs.setLayoutParams(mealStatParams);
mealStats.addView(mealCarbs);
EditText mealProtein = new EditText(context);
mealProtein.setId(mealProteinId);
mealProtein.setLayoutParams(mealStatParams);
mealStats.addView(mealProtein);
return true;
case R.id.removeMeal:
LinearLayout removeMe = (LinearLayout)findViewById(trackMealItems);
removeMe.setVisibility(View.GONE);
trackMealItems--;
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v){
EditText mealCaloriesInTextBox = (EditText)findViewById(mealCalId);
}
You seem to be using two different values: MealCalId when you create your EditText and mealCalId when you call findViewById. That's one possible problem. The other is that if you have more than one view with the same id, findViewById will not necessarily return the one you want.
EDIT
At first glance, your code looks like it should work. I don't know what's going wrong, but I have a suggestion for a work-around. When you create the view, instead of assigning it an ID, assign it a tag:
mealCalories.setTag(mealCalId);
(The int value will be autoboxed to an Integer.) Then in your onClick handler, retrieve it by tag:
EditText mealCaloriesInTextBox =
(EditText) getContentView().findViewWithTag(mealCalId);
If there's any kind of funny interaction with view IDs, this technique will avoid them.
If that doesn't work (or if you prefer anyway) you can also try diagnosing the ID-based retrieval using the Hierarchy Viewer.
For those who are having this error for the same reason I did....
Just try cleaning your project and re-building.
Solved it for me.
i tried to run your code and what i found is that
when menu item is not clicked and button is clicked, the edit text is null.
So if you will call any method on this object, it will crash with NULLPointerException
When menu item is clicked and then button is clicked, the edit text is not null so you call any method on this object.
I am relatively new to Android development but I do have a pretty good understanding of Java and xml etc. so excuse me if this is an easy question and I just can't put two and two together.
Anyway, I am trying to have a user input a few characters into an EditText field. When they press a Button, it will call a method that will output a String. I then want this String to be displayed on the same activity as the EditText field and Button.
How do I go about taking the String variable that is the result of the method and putting into a String in the strings.xml file?
See this question. I don't think that is possible, what you probably want to do is store what the user enters in SharedPreferences.
EDIT:
To take the string variable and display it on the screen you would want to add a TextView to your layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/textview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text=""/>
Then in code you will add a listener to your button to listen for click events, and have a reference to the TextView in your class:
Fields in class:
TextView tv;
Button myButton;
EditText et;
onCreate():
tv = (TextView)findViewById(R.id.textview);
myButton = (Button)findViewById(R.id.button);
et = (EditText)findViewById(R.id.edittext);
//Now set the onclick listener:
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(args....)
{
String txt = et.getText().toString();
tv.setText(txt);
}
});
Also check out this tutorial.
I am working on a android program. A user clicks on a button I do some math and I would like to change the values that I have on my view in some TextView objects. Can someone please tell me how to do it in my code?
I presume that this question is a continuation of this one.
What are you trying to do? Do you really want to dynamically change the text in your TextView objects when the user clicks a button? You can certainly do that, if you have a reason, but, if the text is static, it is usually set in the main.xml file, like this:
<TextView
android:id="#+id/rate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/rate"
/>
The string "#string/rate" refers to an entry in your strings.xml file that looks like this:
<string name="rate">Rate</string>
If you really want to change this text later, you can do so by using Nikolay's example - you'd get a reference to the TextView by utilizing the id defined for it within main.xml, like this:
final TextView textViewToChange = (TextView) findViewById(R.id.rate);
textViewToChange.setText(
"The new text that I'd like to display now that the user has pushed a button.");
First we need to find a Button:
Button mButton = (Button) findViewById(R.id.my_button);
After that, you must implement View.OnClickListener and there you should find the TextView and execute the method setText:
mButton.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
final TextView mTextView = (TextView) findViewById(R.id.my_text_view);
mTextView.setText("Some Text");
}
});
First, add a textView in the XML file
<TextView
android:id="#+id/rate_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/what_U_want_to_display_in_first_time"
/>
then add a button in xml file with id btn_change_textView and write this two line of code in onCreate() method of activity
Button btn= (Button) findViewById(R.id. btn_change_textView);
TextView textView=(TextView)findViewById(R.id.rate_id);
then use clickListener() on button object like this
btn.setOnClickListener(new View.OnClickListener {
public void onClick(View v) {
textView.setText("write here what u want to display after button click in string");
}
});