Reputation: 103
In android studio, I am trying to display two strings on top of each other. These strings are going to be concatenated with a variable, so I won't be able to use XML to 'hardcode' it in there.
Here is the code I am trying to run:
public void submitOrder(View view) {
int finalPrice = quantity*5;
displayText("Thank you!");
displayText("Please display this too");
}
And here is the code from my DisplayText function:
private void displayText(String phrase){
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(phrase);
}
The first line is displaying, ("Thank you!"), but the second one isn't... I've seen if there's anything wrong with the XML or the Gradle scripts, but I'm not getting any errors there...
However, Here is my XML code for the price_text_view:
<TextView
android:id="@+id/price_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:text="$10"
android:textColor="@android:color/black" />
Any help will be appreciated... thanks!
Upvotes: 2
Views: 306
Reputation: 171
well, you can add "\n" in the end for the first phrase:
for Andrew code
priceTextView.setText(priceTextView.getText()+"\n"+phrase);
you can check it in you xml whit this code:
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text=" Thank you!\nPlease display this too " />
but I don't recommend do it because if the user changes the font size for his phone can looks weird,
sometimes is better adding to TextView for each phrase into a vertical Linear Layout
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textview_phrase1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text=" Thank you!" />
<TextView
android:id="@+id/textview_phrase2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Please display this too" />
</LinearLayout>
Upvotes: 1
Reputation: 471
Change your display method like this:
private void displayText(String phrase){
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
priceTextView.setText(priceTextView.getText()+phrase);
}
And you can modify method to clear your field or not like this:
private void displayText(String phrase, boolean clean){
TextView priceTextView = (TextView) findViewById(R.id.price_text_view);
if (clean)
priceTextView.setText(priceTextView.getText()+phrase);
else priceTextView.setText(phrase);
}
Upvotes: 2