Add dynamic text to RelativeLayout - android

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);

Related

How to create Edittext programmatically inside of DialogBox depends on Array size?

I have created Dialog Box with 4 EditText. But now I need to do this by dynamically depends on API response JsonObject count.
Here is my response:
{
"additional_charges":{
"1":"121324324",
"2":"245657687",
"3":"379809733",
"4":"4467797894"
}
}
If "additional_charges", has "1","2","3" ---> I have to show 3
Edittext.
If "additional_charges", has "1","2","3","4" ---> I have to show 4
Edittext in Dialog box.
How to do this?
dialog.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fare_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:id="#+id/currentlocTxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:gravity="center"
android:lines="1"
android:marqueeRepeatLimit="marquee_forever"
android:padding="5dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="#string/additionalcharge"
android:textColor="#color/button_accept"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:id="#+id/chargeType"
android:orientation="vertical"
android:padding="3dp"
android:weightSum="1">
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="#dimen/com_facebook_share_button_compound_drawable_padding"
android:background="#color/hintcolor" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<Button
android:id="#+id/cancelBut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.5"
android:background="#color/white"
android:text="Cancel"
android:textColor="#color/button_accept" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/com_facebook_share_button_compound_drawable_padding"
android:background="#color/hintcolor" />
<Button
android:id="#+id/submitBut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#color/white"
android:text="Submit"
android:textColor="#color/button_accept" />
</LinearLayout>
</LinearLayout>
Dialog.class:
private void ShowDialog(final String completeUrl, Context context) {
if (NotificationAct.passenger_mobile != null) {
final View view = View.inflate(OngoingAct.this, R.layout.additional_charges, null);
cDialog = new Dialog(OngoingAct.this, R.style.dialogwinddow);
cDialog.setContentView(view);
cDialog.setCancelable(false);
cDialog.show();
FontHelper.applyFont(OngoingAct.this, cDialog.findViewById(R.id.alert_id));
LinearLayout linlay_container = (LinearLayout) findViewById(R.id.chargeType);
//length from json response
int arrayLength = 4;
EditText editText[] = new EditText[0];
for (int i = 0; i < arrayLength; i++) {
editText[i] = new EditText(this);
editText[i].setBackgroundResource(R.color.white);
editText[i].setId(i);
linlay_container.addView(editText[i]);
}
final Button button_success = (Button) cDialog.findViewById(R.id.submitBut);
final Button button_failure = (Button) cDialog.findViewById(R.id.cancelBut);
button_failure.setVisibility(View.VISIBLE);
}
}
Firstly, parse the Json response and get the length of the array.
Then, create a layout container in the Dialog box where you want to add the EditText's.
Then, you can iterate through for loop by creating new EditText object and adding it to the container according to the array list.
Have a look on a code snippets,
LinearLayout linlay_container=(LinearLayout)findViewById(R.id.linlay_container);
//length from json response
int arrayLength=?;
for (int i = 0; i <arrayLength ; i++) {
EditText editText=new EditText(this);
linlay_container.addView(editText);
}
Here, linlay_container is the container layout of Dialog box in which you want to add EditText and arraylength is the array size from your json response.

Dynamically created ImageView not respecting RelativeLayout rules

I'm adding ImageViews dynamically to a RelativeLayout. I'm trying to align each image to the right of the last image. The initial image aligns correctly but when I set the next image to align to the right side of the last image, it displays at the top left of the screen, disregarding the alignment rules.
Hierarchy View in the Android Device Monitor is indicating that the IDs are correctly getting set and the ImageView is recognizing that it's supposed to align to the id of the last ImageView.
List<String> image_urls = rally.getTransportationImgs();
List<String> methods = rally.getTransportationStrs();
ArrayList<ImageView> IMGS = new ArrayList<ImageView>();
for(int i = 0; i < methods.size(); i++) {
String method = methods.get(i);
for(int j = 0; j < image_urls.size(); j++) {
String img_url = image_urls.get(j);
if(img_url.toLowerCase().contains(method.toLowerCase()) == true) {
ImageView methodImage = new ImageView(this);
methodImage.setId(j + 100);
IMGS.add(methodImage);
RelativeLayout detailsLayout = (RelativeLayout) findViewById(R.id.details_layout);
TextView transportationText = (TextView) detailsLayout.findViewById(R.id.transportationText);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (IMGS.size() > 1) {
params.addRule(RelativeLayout.RIGHT_OF, IMGS.get(IMGS.size() - 1).getId());
params.addRule(RelativeLayout.ALIGN_TOP, IMGS.get(IMGS.size() - 1).getId());
params.addRule(RelativeLayout.END_OF, IMGS.get(IMGS.size() - 1).getId());
params.height = 5000;
params.width = 65;
} else {
params.addRule(RelativeLayout.RIGHT_OF, transportationText.getId());
params.addRule(RelativeLayout.ALIGN_TOP, transportationText.getId());
params.addRule(RelativeLayout.END_OF, transportationText.getId());
params.height = 65;
params.width = 65;
}
methodImage.setLayoutParams(params);
detailsLayout.addView(methodImage);
Picasso.with(getApplicationContext()).load(img_url).fit().centerCrop().placeholder(R.drawable.ic_launcher).into(methodImage);
}
}
}
XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:orientation="vertical"
android:weightSum="1"
android:id="#+id/details_layout">
<ImageView
android:layout_width="match_parent"
android:layout_height="275dp"
android:id="#+id/image" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/name2"
android:layout_below="#id/image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/date2"
android:layout_below="#id/name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/creator_name2"
android:layout_below="#id/date2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/venues"
android:layout_below="#id/creator_name2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/transportationText"
android:layout_below="#id/venues"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/categories"
android:layout_below="#id/transportationText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
For debugging purposes, I set the image height obnoxiously large. The gray is part of an image that is supposed to go to the right side of the ImageView that correctly aligns next to "Transportation:"
The problem is with this line:
methodImage.setId(j + 100);
If there's more than one Object in methods array there will be conflict in ImageViews ids. You're generating your ids based on only the index of the inner array (j).
For example if:
There are two items in methods
There are three items in image_urls
Your ImageViews would have ids: [101, 102, 103, 101, 102, 103]
Therefore you will add multiple Views with the same ids.
To solve the issue either generate ids based on both i and j, or generate them in more conventional way.
EDIT:
Well, the other issue is that you're trying to set RelativeLayout Rules that are aligning a particular View to the same View.
Take a look at your code:
You're creating an ImageView, setting its id and immediately adding it to the IMGS list, with this:
IMGS.add(methodImage);
And then you're trying to set your RelativeLayout Rules, that should align your ImageView, but you're referencing to the last item of the IMGS list, with:
IMGS.get(IMGS.size() - 1)
And the last item in the array is the same View, you're trying to set up!
To solve the issue either:
a) Move IMGS.add(methodImage); after the if-else, and change the if condition to if (IMGS.size() > 0) {
or
b) Change the if body to:
params.addRule(RelativeLayout.RIGHT_OF, IMGS.get(IMGS.size() - 2).getId());
params.addRule(RelativeLayout.ALIGN_TOP, IMGS.get(IMGS.size() - 2).getId());
params.addRule(RelativeLayout.END_OF, IMGS.get(IMGS.size() - 2).getId());

How to switch between 2 TextViews places on the layout?

I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.
I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:
Before clicking:
TextView A
TextView B
After clicking:
TextView B
TextView A
How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!
actvity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
</RelativeLayout>
MainActivity.java:
txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);
float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();
txtA.setX(mBX);
txtA.setY(mBY);
txtB.setX(mAX);
txtB.setY(mAY);
Trick is changing the x and y axis of both views :
Your xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:text="A"
android:tag="A"
android:clickable="true"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA"
android:text="B"
android:tag="B"
android:clickable="true"
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch"
android:id="#+id/btn1"
android:onClick="onClickButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java Code :
public class MainActivity extends Activity {
TextView txtA,txtB;
boolean _isOnTxtA;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
btn1 = (Button)findViewById(R.id.btn1);
}
public void onClickButton(View v)
{
float mPos1x,mPos1y,mPos2x,mPos2y;
mPos1x = txtB.getX();
mPos1y = txtB.getY();
mPos2x = txtA.getX();
mPos2y= txtA.getY();
if(_isOnTxtA)
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA = false;
}
else
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA= true;
}
}
}
View.bringToFront method may be useful.
Where your layout is like:
<LinearLayout>
<TextView A>
<TextView B>
</LinearLayout>
To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).
<LinearLayout>
<TextView B>
<TextView A>
</LinearLayout>
For more detailed example, below is layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_a" />
<TextView
android:id="#+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_b" />
</LinearLayout>
In your OnClickListener (in your Activity) call:
View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();
This should work.
Toggling behavior can be achieved by this application. For example:
ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();
ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.
All previous answers do not work because you use a relative layout where a linear layout will suffice.
If you have to go RelativeLayout, do the following in your click listener
TextView textA = (TextView) findViewById(R.id.txtA);
TextView textB = (TextView) findViewById(R.id.txtB);
RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
textA.setLayoutParams(textALayoutParams);
RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textBLayoutParams.removeRule(RelativeLayout.BELOW);
} else {
textBLayoutParams.addRule(RelativeLayout.BELOW,0);
}
textB.setLayoutParams(textBLayoutParams);
This will place A under B. You can do the same for reversing.

TextViews getting repositioned after calling setText(). Is this a bug?

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.

textview does not display text (one text view push another far away from the screen)

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()));

Categories

Resources