If I have an xml button in a file called test.xml like this:
<Button
android:id="#+id/newgame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Game"
/>
Is it possible to change the "New game" text to something else? I have to do this many times.
The big problem is: I have to do it from inside a java file! Is that even possible?
Yes.
1) Get a reference to the Button
2) use the setText method to give text a new value
Button button = (Button)findViewById(R.id.newgame);
button.setText("Something Else");
definitly buddy,this type of question shoul not be ask for smart phones ,u can do it by update in xml as well as by java coding
ex....
Button buttontext = (Button)findViewById(R.id.button1);
button.setText("this is new text");
or in xml..
in xml it would be static for make it dynamic,set text in java class..
thanks
Related
I have developed an Health App in Android, and i have an Activity where the first question is a Yes/No answer (i make with 2 checkboxes), depending the Yes/No answer the activity shows diferents EditText to complete. I really know how to hide EditText and how to show when the user click the checkboxes, but the question is if exist a correct design pattern to do this? i read the Material Desing web , but i didnt find nothing about this. It is correct way to do? Or i must enable/disable the EditTexts.
If you only need to set a few EditText's, your way is right.
OPTION A
Imagine that "foo()" returns which is the EditText that you have to show.
In your layout:
<EditText
android:id="#+id/edit1"
android:visibility="GONE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/edit2"
android:visibility="GONE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
....
Now, in your code:
...
switch(foo()){
case 1: //You have to show the first EditText
EditText edit1 = (EditText)findViewById(R.id.edit1);
edit1.setVisibility(View.VISIBLE);
break;
case 2: //You have to show the second EditText
EditText edit2 = (EditText)findViewById(R.id.edit2);
edit2.setVisibility(View.VISIBLE);
break;
....
}
OPTION B
Another choice would be adding the EditText programatically, something like:
EditText editText = new EditText(context); // Pass it an Activity or Context
editText.setLayoutParams(new LayoutParams(..., ...)); // Pass two args; must be LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, or an integer pixel value.
myLayout.addView(editText);
In this case, you dont need to add the EditText's in the layout file, you are going to add it dynamically only if you need it!
Generating Edit Text Programatically in android
Hope it helps!
I know how to create text views dynamically, but i want to change the data of an existing view dynamically and display it. is there a way to do it?
What are you trying to do? Do you really want to dynamically change the text in your TextView objects when the user enter data? 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/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text"/>
The string "#string/text" refers to an entry in your strings.xml file that looks like this:
<string name="text">Any string</string>
If you really want to change this text later 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.text);
textViewToChange.setText(
"The new text that I'd like to display now that the user has entered");
New to android and programming in general. This question might sound silly but I'd appreciate the answer. The question description and reasoning is at the beginning and the question is at the end of it all.
I want to apply a listener to a button in android. The way I understood from android.googlesource.com is that there is two way to do it:
applying an OnClickListener to the button in the activity.java
or
assign a method to my button in the xml layout using this
{#link android.R.attr#onClick android:onClick}
they gave the following xml layout example:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="selfDestruct"
android:text="#string/self_destruct" />
plus the the code in activity.java
public void selfDestruct(View view) {
// Kabloey
}
android.googlesource.com
Questions:
According to this: {#link android.R.attr#onClick android:onClick} android.R.attr in the example are the following:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
?
Does this:
android:text="#string/self_destruct"
android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
If I want to add more than one button listener in xml form how do I write it in the java document?
Thank you in advance
Really appreciate it.
Does this:
android:text="#string/self_destruct" android:onClick="selfDestruct"
mean that the button called self_destruct registered as a listener?
The button isn't "called" anything, it just has the text of the value for #string/self_destruct defined in the strings.xml file.
But, yes, the public void selfDestruct(View view) method is the method that will be called for the listener that is setup by the XML.
If I want to add more than one button listener in xml form how do I write it in the java document?
You can only set one click listener for a View.
Yes, layout_height, layout_width and text are attributes. You
can learn more about android attributes
here.
But keep in mind, that different views can use the same attributes
in different way.
It does not. The android:text attribute in this sample just refer to a string-resource called self_destruct to decide which text should be shown within the button. To distinguish views you can use android:id.
As already mentioned by other people, you are able to specify only one onClickListener using XML.
I am new to the world of android app creation. I am going through the tutorials. I am having trouble adding strings and buttons. Any help would be great. Thanks
to add a button lets say its "id" is "button1"
Declare it on main activity like below.
Button submit = (Button)findViewById(R.id.button1);
// The is of yout button is found in ur xml file.
Make sure you give your button an id in your XML file. If you want your button to have text it will tell you you need to give it a string name. You will find the strings xml file under res/values.
This is an example of how your button could look in your xml file
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button1" />
In the java code you need to do something like this to declare it.
Button b1 = (Button) findViewById(R.id.button1);
I have the following code:
<TextView
android:text="Color Yellow"
android:textColor="#000000"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="True"
/>
The android:clickable="True" was added because I thought it needed to be there (please do correct me if I'm wrong). However, the answer I'm seeking right now is how do I go by making another box (filled with text) pop-up upon clicking the "yellow box".
I would be grateful if someone could provide me with ideas and/or hints regarding how to actually create this scenario.
The android:clickable element does what you think and what it name tells you. It allows you to receive click events for that view (TextView here) to act on these.
To create a popup, you have to assign something to that TextView that tells you when it actually gets clicked. That is a OnClickListener. You can do that either in code or partially in code and XML. I'll just focus on the code example, but for the record, the XML one is also pretty easy. It involves setting the android:onClick="myOnClick" attribute to a certain function name that you like ("myOnClick" here) and creating a function like public void myOnClick(View v) in your activity.
What you have to do in code is
Referencing the TextView that you have in your layout
Assign the OnClickListener
Write an action that will be executed once the click gets registered
First point: To reference your TextView you have to use findViewById
TextView myTextView = (TextView) findViewById(R.id.mytextviewid);
Note that you have to assign a ID to your TextView to identify it. You can set that id via the android:id attribute in your XML layout (e.g. android:id="#+id/mytextviewid").
Second point: Once you have the reference, use TextView.setOnClickListener() to register one.
This usually looks like this:
myTextView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Add an action here
}
});
Third point: All you have to do now is displaying your dialog/message insite the onClick() function. There is more than one way to display that, you may use a Toast or an AlertDialog. Check out the links, there are some examples for that.