Reputation: 8742
String data="tv";
Also in my xml file i have a TextView called tv1. I have casted the textView in the Activity.
TextView tv1 = (TextView) findViewById(R.id.tv1);
I want to cast the string into TextView...
so that I can perform this operation on string instead on tv1.
data.setText("abc"); // on the string..
Can i achieve this or not..Also how to dynamically assign an id to a textView. Thanx...
This is my activity code:
public class Winnings extends Activity {
TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10, tv11, tv12,
tv13, tv14, tv15;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.winnings);
setupVariables();
// TODO Auto-generated method stub
backGroundToChange(7);
}
private void setupVariables() {
// TODO Auto-generated method stub
tv1 = (TextView) findViewById(R.id.TextView01);
tv2 = (TextView) findViewById(R.id.TextView02);
tv3 = (TextView) findViewById(R.id.TextView03);
tv4 = (TextView) findViewById(R.id.TextView04);
tv5 = (TextView) findViewById(R.id.TextView05);
tv6 = (TextView) findViewById(R.id.TextView06);
tv7 = (TextView) findViewById(R.id.TextView07);
tv8 = (TextView) findViewById(R.id.TextView08);
tv9 = (TextView) findViewById(R.id.TextView09);
tv10 = (TextView) findViewById(R.id.TextView10);
tv11 = (TextView) findViewById(R.id.TextView11);
tv12 = (TextView) findViewById(R.id.TextView12);
tv13 = (TextView) findViewById(R.id.TextView13);
tv14 = (TextView) findViewById(R.id.TextView14);
tv15 = (TextView) findViewById(R.id.TextView15);
}
void backGroundToChange(int position) {
//What i want to do is this..
String data = "tv" + position;
//The casting of string into a TextView and so that I can perform this...
data.setBackgroundResource(R.drawable.option_correct);
// so by this way i don't need the switch and case that was actually used, shown below.....
//We normally implement it this way
/*
switch (position) {
case 1:
tv1.setBackgroundResource(R.drawable.option_correct);
break;
case 2:
tv2.setBackgroundResource(R.drawable.option_correct);
break;
case 3:
tv3.setBackgroundResource(R.drawable.option_correct);
break;
case 4:
tv4.setBackgroundResource(R.drawable.option_correct);
break;
case 5:
tv5.setBackgroundResource(R.drawable.option_correct);
break;
case 6:
tv6.setBackgroundResource(R.drawable.option_correct);
break;
case 7:
tv7.setBackgroundResource(R.drawable.option_correct);
break;
case 8:
tv8.setBackgroundResource(R.drawable.option_correct);
break;
case 9:
tv9.setBackgroundResource(R.drawable.option_correct);
break;
case 10:
tv10.setBackgroundResource(R.drawable.option_correct);
break;
case 11:
tv11.setBackgroundResource(R.drawable.option_correct);
break;
case 12:
tv12.setBackgroundResource(R.drawable.option_correct);
break;
case 13:
tv13.setBackgroundResource(R.drawable.option_correct);
break;
case 14:
tv14.setBackgroundResource(R.drawable.option_correct);
break;
case 15:
tv15.setBackgroundResource(R.drawable.option_correct);
break;
default:
break;
}
*/
}
xml code....
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TextView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="5 Crore"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#D4A017"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="1 Crore"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="50 Lakhs"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="25 Lakhs"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="12,50,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="6,40,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#D4A017"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="3,20,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="1,60,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="80,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="40,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="20,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#D4A017"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="10,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="5,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="2,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/option_bar"
android:gravity="center"
android:text="1,000"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
Images used...
Upvotes: 2
Views: 13437
Reputation: 361
There is a setText() method in the TextView class. All you need to do is call it like this:
mMyTextViewObject.setText("some text");
Upvotes: 0
Reputation: 11114
Maybe with this approach can work for you. It is the way to represent String text in TextView programmatically
mTextViewOutput.append(outputString);
Upvotes: 0
Reputation: 14153
You can't "cast" a String
to a TextView
, so I assume that what you really meant to ask is how to find a View
based on its name (not its integer ID). IOW, you have a String
that contains the named identifier of your TextView
.
Normally you'd need to use Java reflection to do something like this, but Android provides an alternative solution through the use of getIdentifier()
. This method takes a string and returns a resource ID (int), allowing you to work with resources by name. Try something like this:
private void backGroundToChange(int position) {
String resourceName = "TextView" + position;
int resourceID = getResources().getIdentifier(resourceName, "id",
getPackageName());
if (resourceID != 0) {
TextView tv = (TextView) findViewById(resourceID);
if (tv != null) {
// Take action on TextView tv here...
tv.setBackgroundResource(R.drawable.option_correct);
}
}
}
The "id" in line #3 means you're looking at the names of the R.id.*
constants. You could use the same technique to find other types of resources by name (for example, Drawable
s). In that case, you would replace "id" with "drawable" and provide the name of one of your R.drawable.*
resources.
Finally, do take note of the warning in the Android documentation:
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
The above code is not very efficient at all because you are calling both getIdentifier()
and findViewById()
, neither of which are relatively cheap operations. See my follow-up answer for a better solution.
Upvotes: 7
Reputation: 14153
I'm leaving my first answer because it still answers the original question, but I don't think that's the best way to solve the problem as you've rephrased it.
Based on your updated question, what you really should do is put your TextView
s into an array or Collection
of some sort so you can access them later by position. Even findViewById()
is a relatively expensive operation, so you should minimize your use of it. Consider something like this:
List<TextView> tv = new ArrayList<TextView>();
tv.add((TextView) findViewById(R.id.TextView01));
tv.add((TextView) findViewById(R.id.TextView02));
tv.add((TextView) findViewById(R.id.TextView03));
// This will get TextView02
// Remember Collections are indexed from zero
tv.get(1);
Upvotes: 3
Reputation: 12566
Why on earth would you want to assign a TextView to a String? The whole point of casting is so you can leverage polymorphism (ie, cast a parent [View] to a child [TextView, Button, etc]) for your own convenience, so you don't have to write findTextViewById()
, findButtonById()
, etc.
It makes no sense to run .setData() on a String, but if you really want to, you can subclass String, and add that method, and update String's internal value in it.
And, in your XML, you can say:
<TextView id="@+id/new_id" />
which will generate an id for it at runtime.
EDIT: Since you're just updating a TextView's background, and a TextView is just an integer, you can do this:
public void updateBg( TextView aView )
{
aView.setBackgroundResource( R.drawable.option_correct );
}
// usage, assuming tv1 is already pointing to a TextView
upodateBg( tv1 );
EDIT 2:
private ArrayList<TextView> views = new ArrayList<TextView>();
//populate
views.add( (TextView)findViewById(R.id.whatever) );
//
public void updateView( int index )
{
(TextView)views.get(index).setBackgroundResource();
}
//usage
updateView( 7 );
EDIT 3: Or, you can just store the id's:
private static final int[] tvIds = { R.id.tv1, ... };
public void updateBg( int index )
{
((TextView)findViewById( tvIds[index] )).setBackground(...);
}
Upvotes: 3