Android Studio TextView format one String center - android

Hello I got two Strings and a TextView:
TextView test = (TextView) findViewById(R.id.eins);
String one = "DynamicText";
String two = "Title";
test.setText(two+one);
Now I would to set the two Strings in one TextView but I like to formate the String two text align to center and the String one normal (left). How is this possible?

TextView txtOne = (TextView) findViewById(R.id.txtOne );
TextView txtTwo = (TextView) findViewById(R.id.txtOne);
textOne.setText("DynamicText");
txtTwo.setText("Title");
txtTwo.setGravity(Gravity.CENTER);

Related

Textview declaration using a variable String

Is it possible declare many diferent TextView. Like tv_concept1, tv_concept2, tv_concept3... and so on.
Using a variable string? If the string is "1" it would declare tv_concept1. So it will change the tv_concept1 text to Hi. But if the variable is it 2 then other TextView will do it.
String textview = "tv_concept" + value;
TextView textview = findViewById(R.id.textview);
textview.setText("Hi");```
You could use an array of your object.

How to get textview string from inside recyclerview

Recyclerview contain cardlayout text1,text2 (id) two textviews.i want get text1 string value .if i touched one card.(touchlistner).how to print text1 string.i tried many nothing worked .
If you have the position of your recyclerView list item. you can find the view with
TextView text1 = yourRecyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.text1);
and then get textView string.
String text1Str = text1.getText().toString();
System.out.println("my textView string : " + text1Str);

Unicode to text in java

How can I convert a Unicode string like
"\u0646\u0627\u0645"
to a human-readable string that I can put in a TextView?
No need to do anything more than this
TextView text = (TextView) findViewById(R.id.yourTextView);
text.setText("\u0646\u0627\u0645");

Android array of ids to reference textViews

I have a screen with 24 TextViews that I want to change values of. It doesn't matter in which order I just need to change the value from every text field. Right now my code is as follows:
textView = (TextView) findViewById(R.id.textView1);
textView.setText(value1);
textView = (TextView) findViewById(R.id.textView2);
textView.setText(value2);
textView = (TextView) findViewById(R.id.textView3);
textView.setText(value3);
textView = (TextView) findViewById(R.id.textView4);
textView.setText(value4);
etc...
The number of TextViews is constant at 24. I am looking for a loop solution however the issue I run into is dynamically getting the R.id.textViewX value id. Is there a simple way to accomplish this so that I can use it in the following manner:
// Somehow get textViewArray of textView id's //
for(int i=0;i<textViewArray.length;i++) {
textView = (TextView) findViewById(textViewArray[i]);
textView.setText(value[i])
}
I'm open to the idea of dynamically creating the textfields in the Activity class but am looking for an XML solution. I'm not sure if this can be done using the arrays class. I've seen this done for #drawables but never for id's
Important to note is that the textView's are NOT in a list.
Use the getIdentifier() method:
for (int i = 0; i < 24; i++) {
int id = getResources().getIdentifier("textView" + i, "id", getPackageName());
TextView textView = (TextView) findViewById(id);
textView.setText("Set text");
}

replace a character from only font style bold

I'm new to android and I'm trying to replace a character from bold style at a specific character in a arraylist string. What I'm doing is:
String myName = "76492";
This is my search string and i want to search this string in to the arraylist then i want to bold those character when getting on the arraylist.Arraylist string is 78758,3660,56798,6514,90679.
TextView txtf = (TextView)findViewById(R.id.textView4);
TextView txtS = (TextView) findViewById(R.id.textView5);
TextView txtT = (TextView)findViewById(R.id.textView6);
TextView txt1 = (TextView) findViewById(R.id.textView9);
TextView txt2 = (TextView) findViewById(R.id.textView13);
String str= myName.replaceAll("6", "<b>6</b>");
Spanned text = Html.fromHtml(str);
txtf.setText(text);
txtS.setText(text);
txtT.setText(text);
txt1.setText(text);
txt2.setText(text);
But this is replace all Textview String by the 76492 string with match charater bold style. 1879 search string this is replace old string but i donot want this type replace .iI want only specific position character change in bold style
<style name="boldText">
<item name="android:textStyle">bold</item>
</style>
Paste this code snippet in your style.xml which is in valuse folder under layout.
txtf.setTextAppearance(this, R.style.boldText);
This line of code is for your text view appear as a bold.
Use spans. Then you can specify parts of your string and assign any format or color you want. Have a look at this.
Edit: For clarification: With spans you don't replace the characters. Use some string method to find the position of the characters you want to change to bold and create a span on these positions and assign bold format to that span.

Categories

Resources