Populate radiobuttons from database android - android

i'm developing a driving school test app and I need some help.
I have xml file like this:
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget28"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#AAC1E2" >
<LinearLayout
android:id="#+id/widget29"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enun"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<ImageView
android:id="#+id/i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/e2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enun"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/radioGroup2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
... x 30 times and a button that will correct the test and show if you passed or not. You pass the test when you fail 3 questions or less.
Questions are stored this way:
Id_question Id_topic Id_test Question AnswerA AnswerB AnswerC AnswerOK Image
Id_question Id_topic and id_test are integer fields
Question and AnswerA-C are text fields
AnswerOK is an integer field. If AnswerA is OK = 1, AnswerB = 2, AnswerC = 3
Image is the name of the related image, extracted from resources folder.
I use a database to populate every field in the xml. Images and questions are extracted from database this way:
<!-- language: lang-java -->
Cursor c = adbh.query(bundle.getString("test"));
//"test"-->"SELECT question, image FROM table WHERE id_test = 1"
Integer i = 1;
if (c.moveToFirst()) {
do {
String path = c.getString(index);
String nimage = "i"+i;
int idImage = getResources().getIdentifier(nimage, "id", this.getPackageName());
ImageView image = (ImageView) findViewById(idImage);
int idDrawable = getResources().getIdentifier(ruta, "drawable", this.getPackageName());
image.setImageResource(idDrawable);
String nenun = "e"+i;
String enun = c.getString(anotherIndex);
int idEnun = getResources().getIdentifier(nenun, "id", this.getPackageName());
TextView txtenun = (TextView) findViewById(idEnun);
txtenun.setText(enun);
i++;
} while (c.moveToNext());
How can I populate radiobuttons? The query would be "SELECT AnswerA, AnswerB, AnswerC from table WHERE id_test = 1"
And my last question is how can I correct the test. I think on storing radiobutton pressed (I don't know exactly how) in an array and then compare with correct answers. The query would be "SELECT AnswerOK from table WHERE id_test = 1".
Example:
Array of answered questions:
1 1 1 2 2 3 2 3 1 3 2...
Array of correct answers:
3 2 1 2 2 3 2 3 2 2 2...
<!-- language: lang-java -->
for (int i=0; i<30; i++)
if (a[i] == b[i])
numberOfRightAnswers++;
Thank you :)

How can I populate radiobuttons? The query would be "SELECT AnswerA,
AnswerB, AnswerC from table WHERE id_test = 1"
Will probably want to select those values directly in the first query, so instead of :
Cursor c = adbh.query(bundle.getString("test"));
//"test"-->"SELECT question, image FROM table WHERE id_test = 1"
will have:
Cursor c = adbh.query(bundle.getString("test"));
//"test"-->"SELECT question, AnswerA, AnswerB, AnswerC, image FROM table WHERE id_test = 1"
Then in that while loop assign the text to the RadioButtons like you did for TextView txtenun.
And my last question is how can I correct the test. I think on storing
radiobutton pressed (I don't know exactly how) in an array and then
compare with correct answers.
Add a OnCheckedChangeListener() to all of your RadioGroups. In that listener you'll get the RadioGroup where the user checked something and the id of the checked RadioButton. Use that to construct the array of correct answers.
Some advices:
Maybe you should modify your layout and your current approach. Making the user scroll your 30 question layout might not be such a good idea, also you're loading many resources although the user will not actually see them until it gets to that particular question(I don't know the size of your image, if they are small this probably isn't an issue). You're other alternatives are a ListView layout or a simple layout of one question that you populate on demand depending on which questions is the user at. My advice would be the second option. That option will also help you avoid the getIdentifier method calls, this method is slower then the findViewById and should be avoided. Bellow is a layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#AAC1E2" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/questionImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/questionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enun"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="3" >
<Button
android:id="#+id/previousQuestion"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="previous" />
<Button
android:id="#+id/validateTest"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="validate" />
<Button
android:id="#+id/nextQuestion"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="next" />
</LinearLayout>
</LinearLayout>
</ScrollView>
You'll make a query for all the question in that test(including all the data) and start with a static field int counter = 0. When the app is started you'll move the cursor to the counter value(which is 0 at the start):
c.moveToPosition(counter);
and use the values from that row to populate the above question layout:
ImageView questionImage = (ImageView) findViewById(R.id.questionImage);
int idDrawable = getResources().getIdentifier(ruta, "drawable", this.getPackageName());
questionImage.setImageResource(idDrawable);
String enun = c.getString(anotherIndex);
TextView txtenun = (TextView) findViewById(R.id.questionText);
txtenun.setText(enun);
// will do the same to populate the other parts of the question from the cursor
When the user presses the nextQuestion Button you'll increment the counter value(you should do some checks so you don't go overboard the 30 questions), move the cursor to the counter position again and then populate the layout like above. When the user presses previousQuestion you decrement counter, move the cursor and again populate the layout with data.
Also you'll add only one OnCheckedChangeListener to your RadioGroup. When that listener fires you should store the question id(from the counter value you should be able to tell on which question you are) and the selected RadioButton in a data structure(probably a HashMap), retrieve the correct answers for the test from the database and see if the user it's a good driver.
Also you could(probably) optimize your database.
Edit:
private ArrayList<Integer> responses = new ArrayList<Integer>(); // this will hold the number of the correct answer. Note that this will work if the user answers question one after the other
// if the user skips question you'll get in trouble, this is one of the reasons why your design is not so good.
String rg = "radioGroup" + i;
int idRg = getResources().getIdentifier(rg, "id", this.getPackageName());
RadioGroup radioGroup = (TextView) findViewById(idRg);
radioGroup.setOnChekedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged (RadioGroup group, int checkedId) {
if (group.getChildAt(0).getId() == checkedId) {
responses.add(0); // the first answer was checked
} else if (group.getChildAt(1).getId() == checkedId) {
responses.add(1); // the first answer was checked
} else if (group.getChildAt(2).getId() == checkedId) {
responses.add(2); // the first answer was checked
}
}
});

According to #Luksprog answer this is my finally working loop:
Cursor c = adbh.query(bundle.getString("test"));
Integer i = 1;
if (c.moveToFirst()) {
do {
String ruta = c.getString(4);
String nimage = "i" + i;
int idImage = getResources().getIdentifier(nimage, "id",
this.getPackageName());
ImageView image = (ImageView) findViewById(idImage);
int idDrawable = getResources().getIdentifier(ruta, "drawable",
this.getPackageName());
image.setImageResource(idDrawable);
String rgname = "radioGroup"+i;
int idRg = getResources().getIdentifier(rgname, "id",
this.getPackageName());
RadioGroup rg = (RadioGroup) findViewById(idRg);
rg.removeAllViews();
RadioButton rb1 = new RadioButton(this);
rb1.setId(i);
rb1.setText(c.getString(1));
rg.addView(rb1);
RadioButton rb2 = new RadioButton(this);
rb2.setId(i+1);
rb2.setText(c.getString(2));
rg.addView(rb2);
RadioButton rb3 = new RadioButton(this);
rb3.setId(i+2);
rb3.setText(c.getString(3));
rg.addView(rb3);
String nenun = "e" + i;
String enun = c.getString(0);
int idEnun = getResources().getIdentifier(nenun, "id",
this.getPackageName());
TextView txtenun = (TextView) findViewById(idEnun);
txtenun.setText(enun);
i++;
} while (c.moveToNext());

Related

radiobutton nullpointerexecption in fragment

I want to get the text from RadioButton which is Male or Female but however I got the result nullPointerExeception. Below is my code:
sex = (RadioGroup)v.findViewById(R.id.radioGroup);
int selectSex = sex.getCheckedRadioButtonId();
radioButton = (RadioButton)v.findViewById(selectSex);
String empSex = radioButton.getText().toString();
Toast.makeText(getContext(),empSex + "",Toast.LENGTH_LONG).show();
XML:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
android:layout_margin="5dp"
android:orientation="horizontal"
android:weightSum="1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/male"
android:id="#+id/maleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/female"
android:id="#+id/femaleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"/>
</RadioGroup>
PS: This is in Fragment page.
Can try if-else statement
in your case, male and female
so
if(male.isChecked()){
//do somethings
}
else if(female.isChecked()){
//do somethings
}
this is the simplest and easier.
Its an issue with your usage.
You don't want to show a toast directly when your fragment is created.
It should be when the user selects any radio button or user left it selected by default.
You should use sex.setOnCheckedChangeListener, Answer you are looking for
is here:- https://stackoverflow.com/a/8323973/3594268
and here:- https://stackoverflow.com/a/9175703/3594268
And for context here:-https://stackoverflow.com/a/8215398/3594268 //this is your null pointer exception in Toast

how to dynamically change the value of the find view by id in android?

i am a basic learner in android. What i am trying to achieve from my code is that : i want to dynamically change the value of radio buttons of a radio group inside a for loop. Then, grab the text value so that i can compare, lets say if the value is "Take Out", i want the rb1 to be checked.
Here is the radio group
<RadioGroup
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:layout_alignBottom="#+id/add"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp"
android:id="#+id/rg1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Out"
android:id="#+id/rb1"
android:checked="true"
android:onClick="selectSomething"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sit down"
android:id="#+id/rb2"
android:checked="false" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delivery"
android:id="#+id/rb3"
android:checked="false" />
</RadioGroup>
What i have tried so far:
int count = rg1.getChildCount();
//Make checked if the value is equal
for(int i = 0; i < count;i++){
String myshit = "rb"+i;
int id = getResources().getIdentifier(myshit, "id", getPackageName());
RadioButton RadioValue = (RadioButton) findViewById(id);
String _RadioValue = RadioValue.getText().toString();
Log.d("abc",_RadioValue);
}
For testing, i wanted to print out the values in Log but it does not compile and throws error. I am just half way thorugh in my code. Is there a better way to solve this problem ? Need your expertise. Thank You !
You are doing it the wrong way.
All of your RadioButtons should point to a common function.
That is, all of them should have this property assigned:
android:onClick="selectSomething"
Inside that method, do something like:
public final void selectSomething(final View v)
{
switch(v.getId())
{
case R.id.rb1:
{
String str = "Take out";
System.out.println(str);
break;
}
// all other cases
// ...
}
}

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.

Add dynamic text to RelativeLayout

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

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