Android Apptitude practice app - android

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.

Related

issue with my first android app

before posting my question , i wanna clarify that is my first post on stackoverflow and let's the story beggin.
as the title said , i'm making my first app on android and i found myself blocked with an issue .
there is 3 button on my app :
button1 : give textview2 "hello world again " and make it VISIBLE
// button2 : make textView2 INVISIBLE
// button3 : make textView1 INVISIBLE
this is the code freom main_activity :
package com.example.ismail.app_test_1;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
Button button_aff;
Button button_hide;
Button button_hide_hw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_aff = (Button) findViewById(R.id.button);
button_hide = (Button) findViewById(R.id.button2);
button_hide_hw = (Button) findViewById(R.id.button3);
button_aff.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Show("hello world again");
}
});
button_hide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide();
}
});
button_hide_hw.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Hide_hw();
}
});
}
public void Show(String str)
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.VISIBLE);
text.setText(str);
setContentView(text);
}
public void Hide()
{
TextView text;
text = (TextView) findViewById(R.id.textView2);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
public void Hide_hw()
{
TextView text;
text = (TextView) findViewById(R.id.textView);
text.setVisibility(View.INVISIBLE);
setContentView(text);
}
}
after getting it on my phone , when i touch any button : "Unfortunatly,app_test_1 has stopped !
can someone help me ?
Edit : i removed the setContentView and it works , thanks a lot guys. if someone has a good tuto that will help me to improve my android programming skills i'm a taker
Do not use setContentView() method outside of the onCreate()
just delete it in show and hide functions. Should work probably
Remove setContentView(text); and it will work. You don't need to call it.

string array changing onClick

I wish to have questions that change to the next question in the sequence onClick but I'm having trouble creating the array of questions and writing the onClick code for that portion.
*Too add more context to the app it will be a questionaire app with a question above an editText field and the users inputs will show up in its respective box at the top of the screen.
package com.example.greg;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class menu extends Activity {
Button mButton;
EditText mEdit;
int questionNumber = 0;
String [] questions;
int numberOfQuestions = 2;
#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);
questions=new String[numberOfQuestions];
questions[0]="This is first question?";
questions[1]="This is second question?";
final TextView [] myTexts = new TextView[2];
myTexts[0]=(TextView)findViewById(R.id.varATextView);
myTexts[1]=(TextView)findViewById(R.id.varBTextView);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
myTexts[questionNumber].setText(mEdit.getText().toString());
mEdit.setText(null);
questionNumber++;
if(questionNumber < numberOfQuestions)
questionTextViewHolder.setText(questions[questionNumber]);
else
Toast.makeText(menu.this,"No more questions!",Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}

Using textView arrays for questionnaire app

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

Error when setting OnClickListener in android

I'm beginner in android and I wrote the following simple program that just shows the text of the button I press on a TextView (I always get a runtime error):
package com.hmz.secondapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AppPageActivity extends Activity {
Button btnPlus;
Button btnMinus;
Button btnMul;
Button btnDiv;
TextView resultTextView;
View.OnClickListener operations = new View.OnClickListener() {
public void onClick(View v) {
Button pressedButton = (Button) v;
String buttonLabel = pressedButton.getText().toString();
resultTextView.setText(buttonLabel);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_page);
btnPlus.setOnClickListener(operations);
btnMinus.setOnClickListener(operations);
btnMul.setOnClickListener(operations);
btnDiv.setOnClickListener(operations);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.app_page, menu);
return true;
}
}
Your btnPlus object is null. You need to instantiate it in the onCreate method like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_page);
btnPlus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMinus = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnMul = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnDiv = (Button)findViewById(R.id.YOUR_BUTTON_ID);
btnPlus.setOnClickListener(operations);
btnMinus.setOnClickListener(operations);
btnMul.setOnClickListener(operations);
btnDiv.setOnClickListener(operations);
}
Also instantiate the resultTextView object before you use setText method.
From your code the problem is seems that you are not initializing the buttons (btnPlus,btnMinus,btnMul,btnDiv) and the TextView resultTextView.
btnPlus = (Button)findViewById(Button Id);
btnMinus = (Button)findViewById(Button Id);
btnMul = (Button)findViewById(Button Id);
btnDiv = (Button)findViewById(Button Id);
resultTextView = (Button)findViewById(TextView Id);
First initialize these Views then you can apply any operation on these views like Click Events and setting the text.

Syntax error, insert ";" to complete Statement [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
This is the .java file for a small application I am writing in Android through Eclipse, and I am having a minor error or syntax glitch.
at the end bracket marked by asterisks, eclipse is reporting the error 'Syntax error, insert ";" to complete Statement'. I have searched the code, and find nothing unaccounted for or out of place. Could someone please identify or tell me how to fix this? If you need other files, tell me in the comments. Thanks in advance :)!
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
}
}
The compiler is right. You need to replace
**}** // Here is where the error occurs
with
});
to finish the btnAdd.setOnClickListener method call.
This is a statement in the onCreate(xx) and its an anonymous class, because an anonymous class is a statement you MUST terminate the statement:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
**}** // Here is where the error occurs
TO:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
Since you are using an annonymous inner class, you need to end the setOnClickListener method call with ");" as follows:
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}
You started a function call: setOnClickListener(). You need to finish it. Like so:
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
If you indented properly, maybe you'd see it, but you're missing an extra parenthesis and semicolon:
package org.example.knittingframe;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class KnittingFrame extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textview = (TextView)findViewById(R.id.textview);
final EditText op1 = (EditText)findViewById(R.id.NumBox1);
final EditText op2 = (EditText)findViewById(R.id.NumBox2);
final Button btnAdd = (Button)findViewById(R.id.addBox);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a, b;
a = Integer.parseInt(op1.getText().toString());
b = Integer.parseInt(op2.getText().toString());
int sum = a + b;
textview.setText(String.valueOf(sum));
}
});
}
}

Categories

Resources