I am basically making a firstaid training app (to help get people prepared for NL (National lifeguard) and Standard first aid).
This is done in a multiple choice form factor, and I have an activity which pulls all the questions, answers, and explanations and stores them in an array. As the user answers each question, it moves onto the next by replacing the text on the buttons and textview of the question, and I have made it so android says the question using TextToSpeech.
My error, is that for some reason on the first question, the string (question) gets given to the TextToSpeech talker, but it does not say it? What is weird is that for the other questions (2,3,4,etc.) it works, just not for the first.
I thought that maybe it was that it didnt have enough time to construct (which some other thread said was the problem when the TextToSpeech lags), so I added a delay to the button that starts the multiple choice activity and it still did not work for the first question, but works for the rest.
Here is my code for the TextToSpeech:
In the onCreate of the class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_menu);
prefs = getSharedPreferences("com.comsci.michelaustin.comscisummative", MODE_PRIVATE);
moduleNumber = getIntent().getIntExtra("MODULE_ID", 0);
talker = new TextToSpeech(QuizMenuActivity.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS){
result = talker.setLanguage(Locale.UK);
}
else
{
Toast.makeText(getApplicationContext(), "Talking feature not supported on this device", Toast.LENGTH_SHORT).show();
}
}
});
In the method that sets the textview and buttons to the correct question and answers:
private void displayQuestions(){
String ques = mQuestionLibraryTest.getQuestion(questionNumber);
questionLabel.setText(ques);
talker.speak(ques,TextToSpeech.QUEUE_FLUSH,null);
option1.setText(getChoice(questionNumber,0));
option2.setText(getChoice(questionNumber,1));
option1.setEnabled(true);
option2.setEnabled(true);
option3.setEnabled(true);
option4.setEnabled(true);
option1.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
option2.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
option3.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
option4.setBackgroundColor(getResources().getColor(R.color.colorLightblue));
//to test specifically for true and false questions and to remove visibility of the last two buttons
if (testWhetherBlank(2)) {
option3.setVisibility(View.GONE);
}
else{
option3.setVisibility(View.VISIBLE);
option3.setText(getChoice(questionNumber,2));
}
if (testWhetherBlank(3)) {
option4.setVisibility(View.GONE);
}
else{
option4.setVisibility(View.VISIBLE);
option4.setText(getChoice(questionNumber,3));
}
//
//answerArray=(ArrayList<Object>)mQuestionLibrary.getCorrectAnswer(questionNumber).clone();
answerArray = (ArrayList<String>) mQuestionLibraryTest.getCorrectAnswer(questionNumber).clone();
//Collections.copy(mQuestionLibraryTest.getCorrectAnswer(questionNumber), answerArray);
//explanation=mQuestionLibrary.getExplanation(questionNumber);
explanation = mQuestionLibraryTest.getExplanation(questionNumber);
amountCorrect=mQuestionLibraryTest.getAnswerAmount(questionNumber);
nextArrowButton.setVisibility(View.GONE);
writeResume();
}
//allows the UI to test and remove buttons if the options are blank
public boolean testWhetherBlank(int q){
/*if(mQuestionLibrary.getChoice(questionNumber,q).equals("")){
return true;
}*/
if(mQuestionLibraryTest.getChoice(questionNumber,q).equals("")){
return true;
}
else{
return false;
}
}
Any help is greatly appreciated!
Related
I am making this eCommerce app,
And, i wanted this behaviour in it.
Which is-
At the opening, there will be pitures in the MainActivity.
And a search box.
When you click the picture it will take you to MainActivity2 and there is an ImageView in this activity which will change to become the same picture as the picture I clicked in MainActivity.
And
When you search something in the search box in the MainActivity,
if your searched text matches with the string object of the arraylist in the MainActivity2,
it will change the ImageView of MainActivity2 with the corresponding image of the string object of the arraylist.
I hope, i made minimum grammatical mistakes to make you understand my problem. 😅
Anyways,
For the first case-
I tried to make this work by using On touchListeners, Intenets and booleans.
As you can see in the code below,
touching my ImageView(iBtn1) will trigger Intent intent1, make boolean i1
=true and then send the boolean to next activity in the name "t1".
And ,
For the second case-
I tried using OnClickListeners, Intenets, arraylists and booleans.
First,
I changed the Edittext(input) into string.
For clicking the search Button(igo),
it will trigger Intent intent,
Then it will send the string Data of the input as "eText" and send boolean data x=true, as "x".
public class MainActivity extends AppCompatActivity {
EditText input;
ImageView iBtn1;
TextView tName, tPrice, tDescription;
ImageView iImage;
Button igo;
String eText;
boolean i1,x;
#SuppressLint("ClickableViewAccessibility")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i1=false;
x=false;
igo=findViewById(R.id.go);
iBtn1=findViewById(R.id.Btn1);
input=findViewById(R.id.edittext);
eText=input.getText().toString();
igo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
x=true;
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
intent.putExtra("eText",eText);
intent.putExtra("x",x);
startActivity(intent);
}
});
iBtn1.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setPressed(true);
iBtn1.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ectangle_31_1));
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
iBtn1.setBackground(ContextCompat.getDrawable(getApplicationContext(),R.drawable.ectangle_31));
overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
i1=true;
Intent intent1=new Intent(MainActivity.this,MainActivity2.class);
intent1.putExtra("t1",i1);
startActivity(intent1);
v.performClick();
v.setPressed(false);
return false;
} else {
return false;
}
});
In the next activity-
For my first case-
My target ImageView is iImage.
But at first, what i did was, get the (boolean) data from the previous Intent intent1 to the current Intent I1.
Then i have declared a variable(x1) containing the value of that boolean data.
Then i have implemented an if condition,
If x1 is true, it will change the image resource of the ImageView iImage.
Else, nothing happens.
And, for my 2nd case-
I have this arraylist made up. It contained string data as xName and imageId as xImageId.
Anyways,
First i got the string data from "eText" and boolean data using getIntent.
Then implementating if condition, i tried to compare the string data with the string of arraylist.
If it is equal.
Then i told the programm to change the ImageView just like the case-1
And, i got the data
public class MainActivity2 extends AppCompatActivity {
String eName;
boolean bx;
ImageView iImage;
TextView iname;
Intent I1,intent;
ArrayList<x> xList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
iImage=findViewById(R.id.xblock);
iname=findViewById(R.id.xname);
xList=new ArrayList<>();
xList.add(new x("engineer",R.drawable.ectangle_32));
xList.add(new x("Artists", R.drawable.ectangle_31));
xList.add(new x("Singers",R.drawable.ectangle_33));
xList.add(new x("Lawyer",R.drawable.ectangle_37));
xList.add(new x("Writters",R.drawable.ectangle_38));
xList.add(new x("developer",R.drawable.ectangle_36));
I1=getIntent();
intent=getIntent();
eName = intent.getStringExtra("eText");
bx = intent.getBooleanExtra("x",false);
if (bx=true) {
String yname = eName.toLowerCase();
for (x yList : xList) {
String xname = yList.getxName().toLowerCase();
if (xname.equals(yname)) {
iname.setText(yList.getxName());
iImage.setImageResource(yList.getxImageId());
}
}
}
boolean x1= intent.getBooleanExtra("t1",false);
if (x1=true){
iImage.setImageResource(R.drawable.ectangle_31);
}
else{x1 = false;}
}
}
But, my app crashes,
It can't get to MainActivity2,
And when check the problem in MainActivity2, it say that my variable is not assigned.
What is the problem here?
Thanks a lot for reading, all this gibberish writing.
Please let me know if i hade made any mistakes.
I think the problem is in getting boolean bx
It shouldbe like this
boolean bx = getIntent().getBooleanExtra(x);
try it if it works
i am starting with creating android apps, i already have some basic experience with C# and Java. Now i'm stuck at a strange problem, see below:
public void pictureSwitch (View view){
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
boolean switched = false;
if (switched){
imageView.setImageResource(R.drawable.catstart);
Log.i("Status-Start", "Wert: " + switched);
switched = false;
} else {
imageView.setImageResource(R.drawable.catswitch);
Log.i("Status-switched", "Wert: " + switched);
switched = true;
}
}
}
The Problem is, on the first click on the Button it changes to the catswitch drawable. But it never switches back and i dont know why.
Thanks for your help in advance.
Lets have a boolean as instance variable.
private boolean switched = false;
public void pictureSwitch() {
ImageView imageView = (ImageView) findViewById(R.id.imgVCat);
if (switched) {
imageView.setImageResource(R.drawable.catstart);
} else {
imageView.setImageResource(R.drawable.catswitch);
}
switched = !switched;
}
And you do not need a parameter View view if you are not using it .
I have tried different things for quite some hours now, but I cannot seem to wrap my head around how to update a TextView when a user inputs a new text through an EditTextPreference in Android.
I use a switch statement in the onSharedPreferenceChanged method, inside an anonymous listener class. However, when i set player1View.setText(value), nothing happens. Is there an easier way of accomplishing this task – namely updating the textView through user input?
Here is the bit of code I cannot make work:
public class SettingsActivity extends AppCompatActivity
{
private TextView player1View;
private TextView player2View;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
#Override
protected void onCreate(Bundle savedInstanceState)
{
player1View = findViewById(R.id.player1);
player2View = findViewById(R.id.player2);
super.onCreate(savedInstanceState);
getSupportFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
createListener();
}
private void createListener()
{
listener = new SharedPreferences.OnSharedPreferenceChangeListener()
{
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
EditTextPreference etp = (EditTextPreference) sharedPreferences;
String value = sharedPreferences.getString("player_one", "NULL");
switch(key)
{
case "switch_setting1":
//Do something
break;
case "player1_key":
player1View.setText(value);
break;
case "player2_key":
player2View.setText(value);
break;
}
}
};
PreferenceManager.getDefaultSharedPreferences(getApplicationContext())
.registerOnSharedPreferenceChangeListener(listener);
}
}
The screenshots below should emphasize more clearly what i mean. The fields I want to change are the ones named "Player 1" and "Player 2", and to accomplish that the user inputs new names through settings as shown.
Screenshots from the Android Emulator
Try maybe instead of:
String value = sharedPreferences.getString("player_one", "NULL");
This:
String value = sharedPreferences.getString("player_one", null);
or you might want to create two String variables(value, value2 for example), one for "player_one" and the other for "player_two" if you have forgot to add. I don't know exactly what you are trying to build.
I know this has got to be simple. But for the life of me i don't know why i can't get this right.
Ok so I want to go from a listview page (got that) then click a switch to make it go to the next page (also got that.) Then I want a int to tell me which position I am on form the last page (might be working?) now i can't get the If Else statement to work in the page.
public class NightmareParts extends Activity
{
public int current_AN_Number = NightmareList.AN_position_num;
private TextView edit_title;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.part_layout);
// isn't working here. Why?
// test_edit = (TextView)findViewById(R.id.directions_tv);
// test_edit.setText(R.string.directions_text_two);
// works without this being in here.
setDoneButtonListener();
}
//Set up the Done button to initialize intent and finish
private void setDoneButtonListener()
{
Button doneButton = (Button) findViewById(R.id.back_button);
doneButton.setOnClickListener (new View.OnClickListener() {
#Override
public void onClick(View v)
{
finish();
}
});
}
private void editTitle()
{
if (current_AN_Number = 1)
{
edit_title = (TextView)findViewById(R.id.part_title);
edit_title.setText(R.string.AN_title_1);
}
}
}
The current_AN_number is coming from the last page.
Your if statement is incorrect:
if (current_AN_Number = 1)
You've used the assignment operator, when you wanted to compare it with the == operator:
if (current_AN_Number == 1)
if (current_AN_Number = 1)
Should be
if (current_AN_Number == 1)
You're not setting current_AN_Number to be 1, you are comparing if it is equal to 1. So use ==.
test_edit = (TextView)findViewById(R.id.directions_tv);
is not working because test_edit is never declared.
I'm trying to think of the logic behind how I should do this. The way I am doing it right now is sort of a hack, but it works. It's just too many API requests and unnecessary, so hopefully someone here can help me envision on how I should do this.
I'm using the parse.com SDK for android to retrieve data to display videos.
Here is my code for the video function
int count = 9;
private void getVideos()
{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Videos");
query.whereEqualTo("videoid", count);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> videoData, ParseException e) {
if (e == null) {
/** check to see if there are any more videos **/
if(videoData.size() == 0){
Toast.makeText(getApplicationContext(), "No more videos found.", Toast.LENGTH_LONG).show();
}else{
for(int i=0; i<videoData.size(); i++){
String videoUrl = videoData.get(i).getString("url");
String videoTitle = videoData.get(i).getString("title");
String videoUser = videoData.get(i).getString("user");
TextView title = (TextView)findViewById(R.id.videoTitle);
title.setTextColor(Color.parseColor("#FFFFFF"));
title.setText(videoTitle);
TextView user = (TextView)findViewById(R.id.videoUser);
user.setTextColor(Color.parseColor("#FFFFFF"));
user.setText("By: " + videoUser);
myVideoView = (VideoView) findViewById(R.id.myvideoview);
myVideoView.setVideoPath(vineVideoUrl);
myVideoView.setMediaController(null);
myVideoView.start();
/** loop video **/
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
myVideoView.start();
}
});
/** next video **/
Button button= (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
Toast.makeText(getApplicationContext(), "Loading...", Toast.LENGTH_LONG).show();
getVideos();
}
});
}
}
} else {
Toast.makeText(getApplicationContext(), "Unable to retrieve videos, please check back later.", Toast.LENGTH_LONG).show();
}
}
});
}
Each time the button is pressed, it increments count, and gets the video for that ID from the API. What I am trying to figure out is how can I retrieve say the latest 20 videos from the API the correct way?
The way I can think the proper way to do it is this:
Get latest 20 videos from API, store in array, then when button is pressed loop thru each. How would I pass them each to the textview? And how should I know when it's the last video (20) and to call the API again and pass back the new results?
You can create a List<String> oldMovies which will hold the ids of the movies you already received, now each time you want bring more movies in your query add this constraint movieQuery.notConteindIn("onjectId", oldMovies). That shall return a list of movies that doesn't have one of the objectsId contained in the oldMovies. Each time you will need to update the oldMovie list of ids.