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...
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, Drawables). 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.
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(...);
}
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 TextViews 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);
Maybe with this approach can work for you. It is the way to represent String text in TextView programmatically
mTextViewOutput.append(outputString);
There is a setText() method in the TextView class. All you need to do is call it like this:
mMyTextViewObject.setText("some text");
Related
For example. There is picture on the top of the screen below that there are some empty boxes and below the boxes there are some buttons. Every button has a character for text("a","c","t"). You click on a button and the button's text appear in the box. You can click them in the order you want to but the answer is "cat" so when you put the characters in the correct order then you got a toast.
I tried to do it with TextViews and Buttons. I can make the button disappear when i click on it and a textview appear in the same time. But every textview has a fix place on the screen, so i need to put every character in every box invisible and when i click on the "c" character it appear in the first box and the other "c" characters stay invisible. But if i click on the "a" first, then it appears in the second box because there is too much variation to do all. I'm not good at explaining but if anyone has an idea how to do that easier please response!
Here is my code:
public class MainActivity extends ActionBarActivity implements OnClickListener{
Button b1;
Button b2;
Button b3;
TextView tg1;
TextView tg2;
TextView tg3;
TextView to1;
TextView to2;
TextView to3;
TextView tl1;
TextView tl2;
TextView tl3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.bg);
b1.setOnClickListener(this);
b2 = (Button)findViewById(R.id.bo);
b2.setOnClickListener(this);
b3 = (Button)findViewById(R.id.bl);
b3.setOnClickListener(this);
tg1 = (TextView)findViewById(R.id.tg1);
tg2 = (TextView)findViewById(R.id.tg2);
tg3 = (TextView)findViewById(R.id.tg3);
to1 = (TextView)findViewById(R.id.to1);
to2 = (TextView)findViewById(R.id.to2);
to3 = (TextView)findViewById(R.id.to3);
tl1 = (TextView)findViewById(R.id.tl1);
tl2 = (TextView)findViewById(R.id.tl2);
tl3 = (TextView)findViewById(R.id.tl3);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.bg:
b1.setVisibility(View.INVISIBLE);
tg1.setVisibility(View.VISIBLE);
tg2.setVisibility(View.INVISIBLE);
tg3.setVisibility(View.INVISIBLE);
break;
case R.id.bo:
b2.setVisibility(View.INVISIBLE);
to2.setVisibility(View.VISIBLE);
to1.setVisibility(View.INVISIBLE);
to3.setVisibility(View.INVISIBLE);
break;
case R.id.bl:
b3.setVisibility(View.INVISIBLE);
tl3.setVisibility(View.VISIBLE);
tl2.setVisibility(View.INVISIBLE);
tl1.setVisibility(View.INVISIBLE);
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="hu.szada.gombokelso.MainActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/tl1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
android:text="l"/>
<Button
android:id="#+id/bo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="86dp"
android:onClick="onClick"
android:text="o" />
<Button
android:id="#+id/bl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="#+id/bg"
android:layout_alignBottom="#+id/bg"
android:layout_alignParentLeft="true"
android:layout_marginLeft="36dp"
android:onClick="onClick"
android:text="l" />
<Button
android:id="#+id/bg"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toLeftOf="#+id/bo"
android:onClick="onClick"
android:text="g" />
<TextView
android:id="#+id/tg1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/to1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl"
android:layout_alignBottom="#+id/tl"
android:layout_alignLeft="#+id/tl"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
/// Second
<TextView
android:id="#+id/to2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tl1"
android:layout_alignBottom="#+id/tl1"
android:layout_marginLeft="19dp"
android:layout_toRightOf="#+id/tl1"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
<TextView
android:id="#+id/tg2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignLeft="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/to2"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
/// Third
<TextView
android:id="#+id/tg3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/to2"
android:layout_alignBottom="#+id/to2"
android:layout_alignRight="#+id/bl"
android:layout_weight="1"
android:visibility="invisible"
android:text="g" />
<TextView
android:id="#+id/tl3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_alignLeft="#+id/tg3"
android:layout_weight="1"
android:visibility="invisible"
android:text="l" />
<TextView
android:id="#+id/to3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tg3"
android:layout_alignBottom="#+id/tg3"
android:layout_toRightOf="#+id/tl3"
android:layout_weight="1"
android:visibility="invisible"
android:text="o" />
You might want to try a slightly different approach.
If I understand you correctly, you want to "type" a word out using given lettered buttons. Like one of those hangman style games.
Why not append the text views on the fly.
Something like
#Override
public void onClick(View v) {
//Grab the surrounding layout for the textviews
GridView answerGrid = (GridView)getViewById(R.id.answerGrid);
//Get the text that was on the button
Button b = (Button)v;
String btnText = b.getText().toString();
//Make a text view with text
TextView txt = new TextView();
text.setText(btnText);
//Append to text view container
answerGrid.addView(txt);
//Invisible button
b.setVisibility(View.INVISIBLE);
}
Haven't tested to see if this is perfect, but its a start.
=====
I've looked at your xml
Why not use GridViews?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
....>
<GridView android:id="#+id/answerGrid"
....>
<!-- Put nothing here. This is for answers -->
</GridView>
<GridView android:id="#+id/lettersGrid"
android:layout_below="answerGrid"
....>
<!-- Buttons in here -->
</GridView>
</RelativeLayout>
This way you can customise the number of rows/columns based on the length of the word you're playing with. And GridView will automatically give you a neat layout and spacing.
Have a look at the GridView doc and get it customised the way you want it.
See my edits above for the Java code.
I have a listview in which I inflate a layout with multiple textviews and buttons. I understand to get the text from a view that was clicked is ((Textview)view.... However I am trying to get the text from the specific textview that is located in the layout in which the user clicked. I have tried using OnItemClick but when I use this the item must be focused before the any of the buttons functions work. I resorted to and prefer using onClickListeners in the getView method of my custom adapter. So simply put, how do I click a Button and get the text that is in TextView that is located in the appropriate inflated layout list view item, given that since each inflated layout is considered as one list item?
Here are pictures to clarify what i am looking for. Both layouts are members of a listview.
I want to click the button with the date on it and get the text from the textview in the middle of the layout. However when I click the button with the date on it, I can only get the text from the textview in the middle of the layout of the last child. If "My Party" is the first child in the listview and "3303 going away service..." is the second child, when I click the date button the code in my custom adapter returns the text from the last loaded text in the view which will be "3303 going away service". What I am trying to do is when I click the date button on "My party", get the text "My party". Like wise with the second child.
Here is the getView() in my custom adapter.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewHolder = new ViewHolder();
positionHolder = position;
Log.i("Position", "" + position);
if(convertView == null) {
try {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.post_layout, parent, false);
postLayout = convertView;
viewHolder.unameTV = (TextView) postLayout.findViewById(R.id.postUnameTv);
viewHolder.unameTV.setText(viewContent.get(index));
viewHolder.unameTV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Starting new intent
Intent in = new Intent(getActivity(),
Profile.class);
// sending pid to next activity
String username =((TextView)view).getText().toString();
in.putExtra("username", username);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
viewHolder.fillSpace = (TextView)postLayout.findViewById(R.id.posthelpSpace);
viewHolder.fillSpace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewHolder.unameTV.performClick();
}
});
viewHolder.image = (ImageView) postLayout.findViewById(R.id.postProfPic);
DisplayImageOptions options = initiateDisplayImageOptions();
viewHolder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
viewHolder.unameTV.performClick();
}
});
ImageLoader imageloader = ImageLoader.getInstance();
initImageLoader(getActivity());
imageloader.displayImage(viewContent.get(index + 1), viewHolder.image, options);
viewHolder.addToCalendarButton = (TextView) postLayout.findViewById(R.id.addToCalendarButton);
viewHolder.addToCalendarButton.setText(viewContent.get(index + 2));
viewHolder.addToCalendarButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.add(Calendar.MONTH, 2);
long time = cal.getTime().getTime();
Uri.Builder builder =
CalendarContract.CONTENT_URI.buildUpon();
builder.appendPath("time");
builder.appendPath(Long.toString(time));
Intent intent =
new Intent(Intent.ACTION_INSERT, CalendarContract.Events.CONTENT_URI);
title = testText.getText().toString();
Log.i("Title", "" + title);
intent.putExtra("title", title); // **NOT WORKING**
startActivity(intent);
}
});
viewHolder.eventTitle = (TextView) postLayout.findViewById(R.id.postTitleTV);
viewHolder.eventTitle.setText(viewContent.get(index + 3));
viewHolder.eventTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
title = ((TextView)view).getText().toString();
Log.i("TITLE", "" + title);
}
});
testText = viewHolder.eventTitle;
viewHolder.eventImage = (ImageView) postLayout.findViewById(R.id.eventImage);
imageloader.displayImage(viewContent.get(index + 4), viewHolder.eventImage, options);
viewHolder.likesTV = (TextView) postLayout.findViewById(R.id.likesTV);
viewHolder.likesTV.setText("" + viewContent.get(index + 5));
viewHolder.planToAttendTV = (TextView) postLayout.findViewById(R.id.planToAttendTV);
viewHolder.planToAttendTV.setText(viewContent.get(index + 6));
viewHolder.addressTV = (TextView) postLayout.findViewById(R.id.postLocationTV);
viewHolder.addressTV.setText("" + viewContent.get(index + 7));
index = index + 8;
}
catch (IndexOutOfBoundsException ie)
{
ie.printStackTrace();
}
}
else
{
viewHolder = (ViewHolder) postLayout.getTag();
}
return postLayout;
}
UPDATE
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:background="#drawable/fill_back"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="400dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/relativeLayout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/postUnameTv"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:gravity="center|center_vertical|center_horizontal"
android:textColor="#ff518eff"
android:textSize="12dp"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/postProfPic"
android:text="Joshua" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Public"
android:id="#+id/postProfileIcon"
android:background="#drawable/publicicon"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp"
android:layout_marginRight="70dp"
/>
<ImageButton
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/postProfPic"
android:layout_below="#+id/postUnameTv"
android:layout_centerHorizontal="true"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="330dp"
android:id="#+id/eventImage"
android:layout_alignTop="#+id/space"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:layout_alignBottom="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:id="#+id/space" />
<Space
android:layout_width="20px"
android:layout_height="20px"
android:layout_above="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:id="#+id/space2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/postTitleTV"
android:layout_below="#+id/postProfPic"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:gravity="center|center_vertical|center_horizontal"
android:textSize="28dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:background="#drawable/details_button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="15dp"
android:layout_marginBottom="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/postLocationTV"
android:layout_below="#+id/postProfPic"
android:layout_alignParentStart="true"
android:drawableLeft="#drawable/locate_button"
android:drawablePadding="5dp"
android:gravity="clip_horizontal"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textSize="12dp"
android:singleLine="true"
android:layout_alignEnd="#+id/space"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/addToCalendarButton"
android:textSize="12dp"
android:layout_alignBottom="#+id/postLocationTV"
android:drawableLeft="#drawable/add_to_cal_button"
android:drawablePadding="5dp"
android:gravity="bottom"
android:layout_alignEnd="#+id/imageButton3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2 hours ago"
android:id="#+id/textView2"
android:textSize="8dp"
android:gravity="center_vertical|center_horizontal"
android:paddingRight="10dp"
android:textColor="#ff828084"
android:layout_above="#+id/eventImage"
android:layout_toRightOf="#+id/postProfPic"
android:layout_alignParentEnd="true"
android:layout_marginBottom="3dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/posthelpSpace"
android:layout_alignBottom="#+id/space"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/postProfPic"
android:layout_alignTop="#+id/postProfPic" />
</RelativeLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/likeButton"
android:background="#drawable/like_button_unsel"
android:layout_below="#+id/relativeLayout"
android:layout_alignParentStart="true"
android:layout_marginLeft="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/likesTV"
android:textColor="#ffe11100"
android:layout_alignBottom="#+id/likeButton"
android:layout_toRightOf="#+id/likeButton"
android:layout_alignTop="#+id/likeButton"
android:gravity="center|center_vertical|center_horizontal"
android:paddingLeft="5dp"
android:paddingTop="5dp"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="0.3dp"
android:id="#+id/imageView"
android:layout_below="#+id/likeButton"
android:layout_alignParentStart="true"
android:layout_marginTop="100dp"
android:background="#drawable/divider" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:id="#+id/planToAttendTV"
android:layout_below="#+id/likesTV"
android:paddingLeft="5dp"
android:layout_marginTop="10dp"
android:layout_alignParentEnd="true"
android:gravity="center"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="Yes"
android:id="#+id/button"
android:textSize="10dp"
android:layout_toRightOf="#+id/textView3"
android:background="#drawable/border_circular"
android:layout_marginLeft="2dp"
android:layout_below="#+id/planToAttendTV"
android:layout_marginTop="5dp"
/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="No"
android:id="#+id/button2"
android:textSize="10dp"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:background="#drawable/border_circular"
android:layout_marginLeft="8dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Are you attending?"
android:id="#+id/textView3"
android:gravity="center"
android:layout_alignBottom="#+id/button"
android:layout_toRightOf="#+id/likeButton"
android:layout_marginBottom="7dp"
android:layout_marginLeft="45dp"
android:layout_marginRight="4dp"
/>
I would recommend making the child its own layout as a list item. I would then create a custom list view adapter which would make your layout much simpler as well as being able to get the text from the specific item clicked without having to worry about repeated code.
Allright I guess I figured out what you are trying to get. Correct me If I'm wrong.
Normally, the idea of using custom adapter is based on List of Objects. You need to store all the information you have in an Object and create an ArrayList of those objects.
Then, whichever child you click in the list view, you will get the clicked child's index. This index is also the index of Object in your ArrayList.
After you know which index is clicked, then you can simply get the Object from your ArrayList with this index.
e.g If the 1st list item is clicked, the position you will get is "0", then you will need to use yourArrayList.get(0).getTheDataYouWant()
I'm experiencing the weirdest problem I've ever seen. I have a basic two-pane view. There's a pane that lists medications, and clicking a medication brings up the second pane, which contains the details about that medication. The details fragment is supposed to be arranged like this:
[Med Name]
[Med Dosage]
[Date Filled]
[Duration]
And the XML code bears this out, as far as I can tell:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/nameDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="48dp"
android:freezesText="true"
android:layout_centerHorizontal="true"
android:text="name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/dosageDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/nameDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:freezesText="true"
android:text="dosage"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/dateFilledDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dosageDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="date"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/durationDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dateFilledDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:text="duration"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
I set the strings to be displayed by each field using this code:
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor mCursor) {
int count = mCursor.getCount();
String str = "There are " + count + " rows.";
// TODO: REMOVE THIS TOAST
Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
int nameIndex = mCursor.getColumnIndex(MedTable.MED_NAME);
int dosageIndex = mCursor.getColumnIndex(MedTable.MED_DOSAGE);
int dateIndex = mCursor.getColumnIndex(MedTable.MED_DATE_FILLED);
int durationIndex = mCursor.getColumnIndex(MedTable.MED_DURATION);
if(mCursor != null) {
// Moves to the next row in the cursor.
while(mCursor.moveToNext()) {
// Create the name string
String medName = "Name: " + mCursor.getString(nameIndex);
// Create the dosage string
String medDosage = "Dosage: " + mCursor.getString(dosageIndex);
// Create a date format to parse the date string
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String epochString = mCursor.getString(dateIndex);
long epoch = Long.parseLong(epochString);
String date = sdf.format(new Date(epoch * 1000));
// Create the date string
String medDate = "Date Filled: " + date;
// Create the duration string
String medDuration = "Duration: " + mCursor.getString(durationIndex) + " days";
// Setting the name
TextView nameView = (TextView) getActivity().findViewById(R.id.nameDetailsView);
nameView.setText(medName);
// Setting dosage
TextView dosageView = (TextView) getActivity().findViewById(R.id.dosageDetailsView);
dosageView.setText(medDosage);
// Setting the date
TextView dateView = (TextView) getActivity().findViewById(R.id.dateFilledDetailsView);
dateView.setText(medDate);
// Setting the duration
TextView durationView = (TextView) getActivity().findViewById(R.id.durationDetailsView);
durationView.setText(medDuration);
// end of while loop
}
}
}
But when I run the program, the weirdest thing happens. Namely, the TextViews get reordered somehow.
Instead of the desired order, I somehow get
[Date Filled]
[Dosage]
[Name]
[Duration]
Can anyone tell me why on earth this might be happening?
EDIT: Commenting out the various calls to setText() causes the TextViews to display in the correct order, albeit with the hardcoded text, instead of what I want.
EDIT AGAIN: Turns out all I needed to do was clean the project. Now I know.
First try and remove + sign from all android:layout_below tags and make it like:
android:layout_below="#id/dateFilledDetailsView". The + sign in the id declaration should be used only on the first appearance of the id. So have something as follows and comment the result:
<TextView
android:id="#+id/nameDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="48dp"
android:freezesText="true"
android:layout_centerHorizontal="true"
android:text="name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/dosageDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/nameDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:freezesText="true"
android:text="dosage"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/dateFilledDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dosageDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="date"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/durationDetailsView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dateFilledDetailsView"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:text="duration"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Update:
For the sake keeping the answer updated, #Squonk's comment worked perfectly. The real issue was that R.java got corrupt. Cleaning the project solved the problem.
I don't think you should find view in Fragment like this:
TextView nameView = (TextView) getActivity().findViewById(R.id.nameDetailsView);
Why don't you inflate layout file in onCreateView method in Fragment, then find view by inflated layout view.
I want to create a "Enter your name"-page (for the highscore) for my Android-game but I’m experiencing some problems.
I want it to look like this:
You have reached (=enthst1)
.........here’s the score..............
points! (=enthst2)
Please type in your name to save your score! (= enthst3)
........EditText for name.....
Back (=button) ................... Enter (=button)
But I don’t seem to be able to add the score (int) to my ContentView!
Here’s the code:
Java in "onCreate":
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_hs);
layo = new RelativeLayout(this);
MySco = new TextView(this) ;
Back = (Button)findViewById(R.id.enthsre1);
Back.setOnClickListener(this);
Enter =(Button)findViewById(R.id.enthsok1);
Enter.setOnClickListener(this);
Eingabe = (EditText) findViewById(R.id.editText1) ;
Texta = (TextView) findViewById(R.id.enthst1) ;
Textb = (TextView) findViewById(R.id.enthst2) ;
Textc = (TextView) findViewById(R.id.enthst3) ;
Intent intentk = getIntent();
kontro = intentk.getStringExtra("from").equals("MainActivity") ;
score = 0 ;
if(kontro == false){
score = punkteRechnen(tuleb, le0leb, le1leb, le2leb) ; //calculate the score
} else {
score = 10 ;
}
scoint = "" + score ;
MySco.setText(scoint) ;
MySco.setTextColor(Color.WHITE) ;
MySco.setTextSize(20);
/*I know that this will throw me an IllegalStateException (the specified child already has a parent)
layo.addView(Texta) ;
layo.addView(MySco) ;
layo.addView(Textb) ;
layo.addView(Textc) ;
layo.addView(Eingabe) ;
layo.addView(Back) ;
layo.addView(Enter) ;
*/
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
android:orientation="vertical"
tools:context=".EnterHSActivity" >
<TextView
android:id="#+id/enthst1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/enthst1a"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/enthst2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:text="#string/enthst1b"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="20sp" />
<TextView
android:id="#+id/enthst3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:text="#string/enthst1c"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="18sp" />
<Button
android:id="#+id/enthsre1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="100dp"
android:text="#string/retour" />
<Button
android:id="#+id/enthsok1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="100dp"
android:text="#string/allesklar" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/enthsre1"
android:layout_alignRight="#+id/enthsok1"
android:layout_below="#+id/enthst3"
android:ems="10"
android:inputType="text"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textColorLink="#color/red" >
<requestFocus />
</EditText>
</RelativeLayout>
So, how do I add the score to my layout?
You should not add a view to a layout if it is already a child of that layout. When you call setcontentview your XML layout is inflated and all those views are created and added to your view hierarchy.
I would add the MySco text view to your layout XML then call findviewbyid to get it. I would avoid dynamically adding views. You can always create a view that is hidden and then show it later to get a similar effect.
Here is the modified / stripped down code for your onCreate method.
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_hs);
MySco = (TextView)findViewById(R.id.score);
Intent intentk = getIntent();
kontro = intentk.getStringExtra("from").equals("MainActivity") ;
score = 0 ;
if(kontro == false){
score = punkteRechnen(tuleb, le0leb, le1leb, le2leb) ; //calculate the score
} else {
score = 10 ;
}
scoint = "" + score ;
MySco.setText(scoint) ;
MySco.setTextColor(Color.WHITE) ;
MySco.setTextSize(20);
In your layout add this:
<TextView
android:id="#+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="20sp" />
You can't call addView on textA, textB, etc, because they're already existant in your XML file, and are already children of a view. Instead you should make an empty RelativeLayout in your XML file, then create textA, etc, with new TextView(), and add those to the layout instead.
Try this it might help you.
Add the textview in relative layout to add score on layout.
MySco.setLayoutParams(new RelativeLayout.LayoutParams(300,300);
layo.addView(MySco);
Now textview shows the statement if i will scroll far away to the bottom. Looks like promis textview become to large. Is there a way to fix it?
everything under stars is part of promis textview. It is a single string with several newline characters
another screenshot
code that generate promises
prm = doc.getElementsByTagName("promise");
for (int j = 0; j < prm.getLength(); j++) {
prom += parser.getValue(e, KEY_PROMISES, j) + "\n";
}
prom.substring(0,prom.lastIndexOf('\n'));
layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/top"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/face"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/name"
android:layout_width="243dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/face"
android:text="from" />
<TextView
android:id="#+id/office"
android:layout_width="242dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/face"
android:layout_alignParentRight="true"
android:layout_below="#+id/name"
android:layout_toRightOf="#id/face"
android:text="subject" />
</RelativeLayout>
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/top"
android:numStars="6"
android:singleLine="false" />
<TextView
android:id="#+id/promises"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ratingBar1"
android:layout_marginTop="5dp"
android:text="TextView" />
<ScrollView
android:id="#+id/scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/promises"
android:layout_marginRight="22dp"
android:layout_marginTop="26dp" >
<TextView
android:id="#+id/statement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ScrollView>
</RelativeLayout>
activity for my app
public class SingleMenuItemActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.candidate);
Log.d("here ?", "No ");
int loader = R.drawable.loader;
// getting intent data
Intent in = getIntent();
Log.d("here ?", "No ");
// Get XML values from previous intent
String name = in.getStringExtra("name");
Log.d("Name ", name);
Log.d("here ?", "No ");
DatabaseHandler db = new DatabaseHandler(this);
Log.d("here ?", "No ");
Candidate p = db.getCandidate(name);
Log.d("Did i take candidate? ", "yea ");
db.close();
Log.d("Statement", p.get_statment());
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name);
TextView lblOffice = (TextView) findViewById(R.id.office);
TextView lblPromise = (TextView) findViewById(R.id.promises);
TextView lblStatement = (TextView) findViewById(R.id.statement);
ImageView lblFace = (ImageView) findViewById(R.id.face);
RatingBar lblRating = (RatingBar) findViewById(R.id.ratingBar1);
ImageLoader imgLoader = new ImageLoader(this);
lblName.setText(p.get_name());
lblOffice.setText(p.get_office());
lblPromise.setText(p.get_promises());
lblStatement.setText(p.get_statment());
lblRating.setRating(p.get_ranking());
imgLoader.DisplayImage(p.get_photograph(), loader, lblFace);
}
}
logcat says p.get_statment() has proper value in it
12-16 05:07:16.089: D/Statement(279): I strive for the highest standards of appearance and personal grooming. If, like me, you think that the University's reputation is being dragged down by lecturers who have little or no regard for fashion and are content to wear ill-fitting or patched clothes then vote for me: the stylish choice.
reputation is to low to post image %)
It looks like your RatingBar is covering your statement TextView:
<RatingBar
android:layout_above="#+id/statement"
... />
Perhaps you meant layout_below?
Also your promises TextView has a very large top margin, it could be pushing the statement TextView off the bottom of the screen:
<TextView
android:id="#+id/promises"
android:layout_marginTop="124dp"
... />
Addition
Apparently there are also a number of newline characters at the end of your promises TextView. If you cannot remove these character from the source you can use trim():
prom = prom.trim();
Your code seems correct.
If the textView with the id lblStatement doesn't show the text probably you have a background where you cannot discern the text (so try to change color), or the margin are inadequate for the textview position.
If p.get_statment() has the correct value there are no others possible reasons.
Ok i found solution, besides i dont understand why textview expanded so much i know how to fix it now:)
first of all we need to know number of lines, which string will produce
private static int countLines(String str){
String[] lines = str.split("\r\n|\r|\n");
return lines.length;
}
then simply limit textview in the size
lblPromise.setMaxLines(countLines(p.get_promises()));