I'm trying to develop an app that consists of multiple arrays and textviews and I've run into a problem.
I have 3 textviews and 2 arrays.
One of my arrays contains sentences.
My first question is how can I highlight specific words in each sentence?
Ex: "This is my first array item"
I need to highlight a word in the string so that when it is displayed in textview1, it will appear like this...
"This is my first array item"
My other array contains words. They are displayed in textview2 and should also be highlighted.
My next question is how can I replace the current highlighted word in textview1 with the new highlighted word from textview2, and also have a 3rd textview displaying the new sentence?
Ex: If the chosen item from my sentence array is "This is a test" (displayed in textview1) And the item from my word array is "website" (displayed in textview2) the result would be "This is a website" (displayed in textview3)
Here is my code so far
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Button;
public class Class1 extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class1);
Button btn_sentence = (Button) findViewById(R.id.btn_sentence );
Button btn_word = (Button) findViewById(R.id.btn_word );
Button btn_newsentence = (Button) findViewById(R.id.btn_newsentence );
final Random ran = new Random();
final Resources res = getResources();
final TextView text1 = (TextView) findViewById(R.id.sentencetext);
final TextView text2 = (TextView) findViewById(R.id.wordtext);
final TextView text3 = (TextView) findViewById(R.id.newsentencetext);
btn_sentence.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] NewText = null;
NewText = res.getStringArray(R.array.sentences);
String strRandom = NewText[ran.nextInt(NewText.length)];
text1.setText(strRandom);
}
});
btn_word.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String[] NewText2 = null;
NewText2 = res.getStringArray(R.array.words);
String strRandom2 = NewText2[ran.nextInt(NewText2.length)];
text2.setText(strRandom2);
}
});
btn_newsentence.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
**text3.setText(" " + (text1.getText()) + " " + (text2.getText()) + " ");**
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}}
The bold text is as close as I've gotten, but all that really does is combine both TexttView1 and textview2, and display it in textview3... which isn't exactly what I want.
Help would be much appreciated
~TylerNormal
You can use simple HTML-tags, wrap your words you want to highlight into those using this:
TextView text = findViewByID(R.id.myTextView);
String test = "This is my first array item";
// example - the word in brackets is to be replaced and captured
// it is just an example to show how you can use it later with more
// complex cases
test = test.replaceAll("(first)", "<b>$1</b>");
text.setText(Html.fromHtml(test));
Related
I want to build a questionnaire type app with questions that show up individually and when a user submits an answer and clicks the button the data is stored in a textView above and the question goes to the next question where that answer is stored in a new textView. I am having difficulty getting the question to change to the next question.
Below I have attempted using the array method but have gotten errors both here
myTexts[0]=findViewById(R.id.question1);
myTexts[1]=findViewById(R.id.question2);
-- and
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
Below is complete source code:
package com.example.greg;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class menu extends Activity {
Button mButton;
EditText mEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.userAnswereditText);
TextView [] myTexts = new TextView[2];
myTexts[0]=findViewById(R.id.question1);
myTexts[1]=findViewById(R.id.question2);
int questionNumber = 0;
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
To change question you should have array of String or List of String that can hold all the question and as soon as button is clicked and the answer is stored in the TextView you will load the next question from that array or List of String that is holding the questions.
String [] questions;
int numberOfQuestions = 2;
and then in onCreate() method
questions=new String[numberOfQuestions];//numberOfQuestion refers to integer that holds total number of questions
questions[0]="This is first question?";
questions[1]="This is second question?";
and now in your view.onClickListener
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
questionNumber++;
//here you will have to load the next question
if(questionNumber < numberOfQuestions)
questionTextViewHolder.setText(questions[questionNumber]);
else
Toast.makeText(menu.this,"No more questions!",Toast.LENGTHLONG).show();
//Note one thing that your questionNumber variable should not increase myTexts length or questions length because if it does you will get exception
}
});
questionTextViewHolder is the TextView in which you are displaying the question that is to be answered, you will have to replace "questionTextViewHolder " with your textview in which you are displaying the questions !
Class name menu starting letter should be capital if you follow the coding conventions.
Shouldn't this...
myTexts[0]=findViewById(R.id.question1);
be a cast to TextView instead?...
myTexts[0]=(TextView)findViewById(R.id.question1);
That will definitely eliminate a ClassCastException
I am new to Android and Java and don't know why the Text value is null after I assign Text to it? This is a project for University They didn't teach us Java only gave us Eclipse and Android Sdk and Java need some help please!
package com.example.glossaryapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Syntax extends Activity {
static String Text;
public static TextItem[] Syntax = new TextItem[50];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.syntax);
final TextView show = (TextView) Syntax.this
.findViewById(R.id.textViewSyn);
// Linking the text View widget
int Count1 = 0;
// Using the TextView's shows method set text to display the appropriate
// arrays and indexes
if (Syntax[0] != null) {
Text = Syntax[0].displayText();
show.setText(Text);
}
// The Add button for if a user wants to add a new TextItem to the Array
Button but1 = (Button) findViewById(R.id.butAddSyntax);
but1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Create.strSender = "Syntax1";
startActivity(new Intent(Syntax.this, Create.class));
}// end of on click
});// butAddSyntax
// Home Button
Button but2 = (Button) findViewById(R.id.buttHomeSyntax);
but2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}// end of on click
});// buttonHomeSyntax
}
public void onResume() {
super.onResume();
TextView show = (TextView) Syntax.this.findViewById(R.id.textViewSyn);
if (Syntax[0] != null) {
Text = Syntax[0].displayText().toString();
show.setText(Text);
}
}
}
Using Android's LogCat will help you to solve this kind of problems.
Here you have a nice article:
http://www.vogella.com/tutorials/AndroidLogging/article.html
I think the problem is in here:
if( Syntax[0] != null ){
Text = Syntax[0].displayText();
show.setText(Text);
}
you could begin using LogCat trying this:
if( Syntax[0] != null ){
Log.d("MyApp", "Sintax (0) = " + Syntax[0].displayText());
Text = Syntax[0].displayText();
show.setText(Text);
} else Log.d("MyApp", "Nothing ");
And watch the result in the Logcat pane.
In my game i have a hash button which acts like a submit button. when i press the hash button it displays INCORRECT even if the answer is correct, Do i have to add a different if statement for each position in the array? It displays INCORRECT each expression.
(Sorry i'm a new in this)
package com.gamesup.braingame;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Easy extends Activity implements OnClickListener{
EditText display;
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5", "9"},
{"20 * 3","60"},
{"99 - 9","90"}};
TextView displayExpression;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.easy);
display = (EditText)findViewById(R.id.displayText);
display.setText("?");
displayExpression = (TextView) findViewById(R.id.expression);
Button generate = (Button) findViewById(R.id.random_gen);
generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random ranGenerate = new Random ();
int random = ranGenerate.nextInt(multiArray.length) ;
// Fetch your random question
String Rquestion = multiArray[random][0];
displayExpression.setText(Rquestion);
}
});
}
static boolean isEmpty = true;
public void num_Clicked(View v){
Button btn = (Button) findViewById(v.getId());
//getting the button object and using a view to get the id of the buttons
if (v.getId()== R.id.del_button){
String s = display.getText().toString();
s = s.substring(0, s.length() - 1);
display.setText(s);
return;
}
if(isEmpty){
display.setText(btn.getText());
isEmpty = false;
}
else{
display.append(btn.getText().toString());
}
public void hash_Clicked(View v){
if (v.getId()== R.id.hash_button){
// Get the Answer from your EditText
String answer = display.getText().toString();
// Using a for loop iterate on the base index
for(int i = 0; i < multiArray.length ; i++)
{
// if the answer is in position 1 of Array [i]
if(answer.equals(multiArray[i][1]))
{
// We have found the answer, Congratulate the User
displayExpression.setText("CORRECT");
}else{
// Tell them how bad they are since they can't solve simple equations!
displayExpression.setText("INCORRECT");
}
}
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Put break statement after you show that the answer is correct, so that you don't iterate through the remaining answers in your array and compare with current, because if you have a correct answer at say index 0, you are still going through the remaining answers in an array, so your "CORRECT" gets replaced by "INCORRECT".
// if the answer is in position 1 of Array [i]
if(answer.equals(multiArray[i][1]))
{
// We have found the answer, Congratulate the User
displayExpression.setText("CORRECT");
break; //<---add this to exit the loop, because correct answer has been found
}
I want to develop an aptitude app..
for that in my text view i have to display first question .. On clicking next button i have to display second question.. on again clicking same next button third question have to be displayed.. liked that i want to display some 30 questions ..all questions should be displayed in single java file.I Tried to display two questions . but for multiple questions i could not find the code..
package com.example.asl;
import java.util.Arrays;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Aptitude extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
final TextView tv=(TextView) findViewById(R.id.textView1);
final String Question[]={"what is UR Name","What is ur Age","Whats ur Qualification"};
Button btnNext = (Button) findViewById(R.id.button1);
final TextView cumulos = (TextView) findViewById(R.id.textView1);
//TextView respostas = (TextView)findViewById(R.id.respostas);
Random randPhrase = new Random();
final String[] cum = {"what is UR Name","What is ur Age","Whats ur Qualification"};
//String[] resp = getResources().getStringArray(R.array.resp_cumulos);
String textout = "";
String textresp = "";
//Button btnPrevious = (Button) findViewById(R.id.yourPreviousbutton);
btnNext.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
int i = 0;
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
}
}
});
//btnPrevious.setOnClickListener(new OnClickListener(){
//public void onClick(View arg0) {
//if(i>0){
// i-=1;
// cumulos.setText(cum[i]);
// respostas.setText(resp[i]);
// }
// }
//});
}
}
enter code here
Initializing your counter in your onClick() is always going to reset it
Initialize it outside of onClick() and increment it in onClick() as you are.
public class Aptitude extends Activity {
int i = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aptitude);
Button b=(Button) findViewById(R.id.button1);
...
}
public void onClick(View arg0) { // rename arg0 to something meaningful
// like v or view for readibility
// int i = 0; remove this guy
if(i<cum.length-1){
i+=1;
cumulos.setText(cum[i]);
If this doesn't fix your problem then please explain what the problem is but I'm sure this part was causing you trouble.
simple question what do i need to add to this code so that when the button is pressed it replaces the text view with one of the strings from the array list?
package com.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView2);
tx.setText("Hello");
}
});
}
}
-----------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, main!</string>
<string name="app_name">Ihavenever</string>
<string-array
name="list">
<item>#string/girl</item>
<item>#string/boy</item>
<item>#string/man</item>
<item>#string/women</item>
<item>#string/dog</item>
<item>#string/cat</item>
</string-array>
</resources>
so in a nutshell the app launches there is a button at the bottom once pushed it puts one of the items in the string array in the textview in the middle of screen and when you push it again it gives you another one and another one till there is none left and then restarts from the first one ?
Make
int counter = 0;
global (outside onCreate()).
Then in onCreate() after declaring your button:
Button b = (Button) findViewById(R.id.button1);
Resources res = getResources();
final String[] list = res.getStringArray(R.array.list); //get the array
((TextView) findViewById(R.id.textView2)).setText (list [counter]); //set the initial message.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView2);
counter++;
if (counter >= list.length)
counter = 0;
tx.setText(list [counter]); //set the new message.
}
});