How to create a new "TextView" object,by concatenating two strings? - android

I want to create a new "TextView" object in "MainActivity" code by concatenating
Two String names. for example:
String s1 = "num";
String s2 = "ber";
String s3 = s1+s2;
TextView s3 = new TextView(this);
How cast s3 to TextView object,so i dont get anyy error,code above?
I mean i want to use s3 as a "TextView" name object.

You would do something like this.
TextView textView = new TextView(this);
textView.setText(s3);
or
TextView s3 = new TextView(this);
s3.setText(s1 + s2);
or programmatically in a loop
for (int i = 0; i < list.size(); i++) {
TextView textView = new TextView(this);
textView.setId(s3); //set textview id, this WILL NOT make it a variable of 'number'
linearLayout.addView(textView);
}

First problem is you declared 2 variables with the same name. Fix it by giving TextView a better name and then as #soldforapp answered already, set the text using the method .setText();
edit:
Wait, so you want to assign the value of the TextView to the string variable s3?
I don't really understand your problem. If so, if your code would look like this (so it runs)
String s1 = "num";
String s2 = "ber";
String s3 = s1+s2;
TextView tv = new TextView(this);
This line will assign the variable s3 the text inside your TextView.
s3 = tv.getText().toString();

Using the same name for the different variable in one scope is not possible in JAVA. (even with different types)
Using StringBuilder is better option than concatenating with + operation, so:
String s1 = "num";
String s2 = "ber";
String concat = new StringBuilder().append(s1).append(s2).toString();
TextView s3 = new TextView(this);
s3.setText(concat);
Edit:
What you want is not as easy as what exists in script languages like PHP but you can do it with reflection with efforts. But there is an easier option with using Map:
Map<String,TextView> map = new HashMap<>();
map.put(concat, new TextView(this));
You can get the TextViews with:
map.get(concat).setText("Your String");

Related

Translating, anuvaad karana or अनुवाद करना

When you make translations do you use English Characters or The cultures native characters? So for example would I Put "Anuvaad karana" or "अनुवाद करना" instead of "Translating" when translating into Hindi?
English TextView tv = (TextView) findViewById(R.id.EnglishTxt);
List<String> list = new ArrayList<String>();
list.add("Translating");
Hindi TextView tv = (TextView) findViewById(R.id.HindiTxt);
List<String> list = new ArrayList<String>();
list.add("Anuvaad karana");
HindiC TextView tv = (TextView) findViewById(R.id.HindiCTxt);
List<String> list = new ArrayList<String>();
list.add("अनुवाद करना");
Random rand = new Random();
String random = list.get(rand.nextInt(list.size()));
tv.setText(random);
Thanks in Advance.
P.S The code is just an example it's not actually anything.
I always use the culture's native characters in translation, so the people who will use the app and switch to there own language would understand the context.
for example (good) would be in Arabic جيد

Obscure issue with string formatting from SQLite database

I appreciate there's a lot of helpful stack questions and answers to my question but I'm running into problems I've not had in the past.
The Problem:
I am using a cursor to populate textviews in rows on a view (without using listview - that's crazy I know). I am trying to format the string value(s) taken from from the database column STUDENT_POINTS that are put into a textview tpoints. Here is the code I am using:
public void bindView(View v, final Context context, Cursor c) {
final int id = c.getInt(c.getColumnIndex(Students.STUDENT_ID));
final String name = c.getString(c.getColumnIndex(Students.STUDENT_NAME));
final String age = c.getString(c.getColumnIndex(Students.STUDENT_AGE));
final String points = c.getString(c.getColumnIndex(Students.STUDENT_POINTS));
final String teachernote = c.getString(c.getColumnIndex(Students.TEACHERNOTE));
final byte[] image = c.getBlob(c.getColumnIndex(Students.IMAGE));
ImageView iv = (ImageView) v.findViewById(R.id.photo);
if (image != null) {
if (image.length > 3) {
iv.setImageBitmap(BitmapFactory.decodeByteArray(image, 0,image.length));
}
}
TextView tname = (TextView) v.findViewById(R.id.name);
tname.setText(name);
TextView tage = (TextView) v.findViewById(R.id.age);
tage.setText(age);
TextView tpoints = (TextView) v.findViewById(R.id.points);
tpoints.setText(String.format(points, "%1$,.2f"));
final StudentsConnector sqlCon = new StudentsConnector(context);
The rest of bindView is for buttons so I have not included it here. The problem is with the line:
tpoints.setText(String.format(points, "%1$,.2f"));
I'm intending to have commas to separate out large numbers but this does nothing! If anybody has the time could you please tell me what I'm doing wrong?
Thanks in advance.
You have your two parameters backwards-- you should have the format string followed by the data string: String.format("%1$,.2f", points );
This formatted nicely for me with this little snippet in my code:
double points = 56789.45f;
String boogie = String.format("%1$,.2f", points );
and it generated a number 56,789.45
But bigger numbers don't work well due to precision in the formater. You may want to split the mantissa off of their, format them separately and combine them.

Android: adding multiple Views programmatically

I want to add a LinearLayout wrapped around a TextView and Button programmatically. I want it to take a String array and then using the length of the string array, add that many TextViews each with their own button.
So first:
String [] s = { .... the values ....}
int sL = s.length;
TextView t1 = new TextView (this);
// then somehow create t2, t3... etc. matching the length of the String array.
Is this the best way to do this or is there another way to do this? For some context, it's a quiz app and I've created a list of categories inside resources as values and I'm trying to programmatically get my app to create as many TextViews as there are categories then set each TextView to each category then get each button to take the user to that category of questions.
You are starting it right, just do a for loop and add textviews to your linearlayout.
// You linearlayout in which you want your textview
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
String [] s = { .... the values ....}
int sL = s.length;
TextView textView = null;
// For later use if you'd like
ArrayList<TextView> tViews = new ArrayList<TextView>();
for (int i = 0; i < sL; i++)
{
textView = new TextView(this);
textView.setText(s[i]);
linearLayout.addView(textView);
tViews.add(textView);
}
There is nothing wrong with this way of doing it. If you want to use these textview later on (set text for them or something) store them in an Array of some kind. Edited code
You can do the following:
for(int i=0;i<s.length;i++){
TextView t=new TextView(this);
t.setText(s[i]);
yourLinearLayout.addView(t);
}
But I really think that using a ListView would be better for performance ;)

Is there a way to italicize certain words when displaing it in a TextView on Android?

My Android app is displaying text in a TextView.
Are there any tags or anything to put around words that I want italicized? I don't need to set the TextView as italics because the whole sentence would be that way, and I only need specific words italicized.
You need to use a Spannable: see Is there any example about Spanned and Spannable text for an example.
I agree, this isn't a database issue. If this is not a custom app you're working on, you're out of luck. If it is, save the field in the database as HTML.
This function sets a string as the text of a TextView and italicizes one or more spans within that string as long as the span or spans are marked with the tags [i] and [/i]. Of course, it can be modified for other types of styling.
private void doItalicize(TextView xTextView, String xString) {
ArrayList<Integer> IndexStart = new ArrayList<>();
ArrayList<Integer> IndexEnd = new ArrayList<>();
ArrayList<StyleSpan> SpanArray = new ArrayList<>();
int i = 0;
do {
IndexStart.add(i, xString.indexOf("[i]"));
IndexEnd.add(i, xString.indexOf("[/i]") - 3);
xString = xString.replaceFirst("\\[i\\]", "");
xString = xString.replaceFirst("\\[/i\\]", "");
xTextView.setText(xString, TextView.BufferType.SPANNABLE);
SpanArray.add(i, new StyleSpan(Typeface.ITALIC));
Log.d(LOG_TAG, "i: " + i);
i++;
} while (xString.contains("[i]"));
Spannable xSpannable = (Spannable) xTextView.getText();
for (int j = 0; j < i; j++)
xSpannable.setSpan(SpanArray.get(j), IndexStart.get(j), IndexEnd.get(j), Spanned
.SPAN_EXCLUSIVE_EXCLUSIVE);
}
The solution would seem to be to save the content in the database as HTML and then output the HTML in the textview.
See the following article: How to display HTML in TextView?

How to create Custom Text Views in android?

Hai Friends,
I am parsing the url to display the contents in it, my requirement i have to display the each content in separate textviews.
For Instance:
Let us assume the contents in that url are FootBall, Carom , chess, VolleyBall and so on . I want to display FootBall as a individual textview similarly others. so i cannot declare the textviews in xml what i usually do.
(<TextView android:text=" " android:layout_width="wrap_content"
android:gravity="center_horizontal" android:paddingLeft="7dp" android:layout_height="wrap_content"
/>).
so i planned to create textview via java code
This is my parsing code which parse the url contents and store the result in a string array namely san_tagname; depending upon the length of this variable i want to create number of textviews.
List<Message_category> l_obj_tagname = new ArrayList<Message_category>();
l_obj_tagname = obj_parse1.parse_tagname();
System.out.println("l_obj_tagname"+l_obj_tagname.size());
String[] san = new String[l_obj_tagname.size()];
Iterator<Message_category> it_id1 = l_obj_tagname.iterator();
i=-1;
while (it_id1.hasNext()) {
i++;
san[i] = it_id1.next().toString();
System.out.println("Id="+san[i].toString());
san_tagname[i]=san[i];
//vm.setTitle(it.next().toString());
}
for(int z=0;z<san_tagname.length;z++)
{
//how to create textview here ...............
}
I am really struggling on this, pls help me regarding on this friends.................
Thanks In Advance
Tilsan The Fighter...
TextView tv = new TextView(context);
tv.setText(myText);
parent.addView(tv, {LayoutParams for parent container type})
Here is the answer,
Parse the Contents from web
//manipulation to parse id
List<Message_category> l_obj_id = new ArrayList<Message_category>();
l_obj_id = obj_parse1.parse_id();
VAL1 = new String[l_obj_id.size()];
Iterator<Message_category> it_id = l_obj_id.iterator();
while (it_id.hasNext()) {
i++;
VAL1[i] = it_id.next().toString();
System.out.println("Id="+VAL1[i].toString());
//vm.setTitle(it.next().toString());
}
//manipulation to parse tagname
List<Message_category> l_obj_tagname = new ArrayList<Message_category>();
obj_parse1.parse_tagname();
obj_parse1.storedata();
san_tagname= new String[obj_parse1.santagname.length];
for(int k=0;k<obj_parse1.santagname.length;k++)
{
san_tagname[k]=ParsingHandler.temptag[k];
if(san_tagname[k].contains("%20"))
{
san_tagname[k]=san_tagname[k].replace("%20"," ");
System.out.println("San_tagName1"+san_tagname[k]+"S"+k);
}
else
{
System.out.println("San_tagName2"+san_tagname[k]+"S"+k);
}
}
gal_lay = (LinearLayout) findViewById(R.id.rl_1);
navagtion_bar= (LinearLayout) findViewById(R.id.san_tag);
hv = (HorizontalScrollView)findViewById(R.id.gv);
// This is the Code i needed finally i stirkes with the help of hackbod
**for(int z=0;z<san_tagname.length;z++)
{
TextView san_text[]= new TextView[san_tagname.length];
san_text[z]= (TextView) new TextView(this);
san_text[z].setText(" "+san_tagname[z]+" ");
san_text[z].setTextSize(15);
navagtion_bar.addView(san_text[z]);
}**

Categories

Resources