Android: Quiz Application using if else statement - java

If Texboxt = " "
nextActivity.show
else msg = "Sorry!"
Can anyone help and tell me how to make this in Android eclipse java?
I have a code here, but I don't know how to correct this.
private EditText inputtxt;
private Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g1);
inputtxt = (EditText) findViewById(R.id.editText1);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View ContentView) {
// TODO Auto-generated method stub
String name;
name=inputtxt.getText().toString();
if (name.contentEquals("Accounting"))
{
Intent myIntent = new Intent (ContentView.getContext(), NextActivity.class);
startActivityForResult(myIntent, 0);
}
else
{ Toast.makeText(getBaseContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.g1, menu);
return true;
}
`

update your code like following it might work..
private EditText inputtxt;
private Button btnNext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_g1);
inputtxt = (EditText) findViewById(R.id.editText1);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View ContentView) {
// TODO Auto-generated method stub
String name;
name=inputtxt.getText().toString();
if (name.equalsIgnoreCase("Accounting"))
{
Intent myIntent = new Intent (QuizActivity.this, NextActivity.class);
startActivity(myIntent);
}
else
{ Toast.makeText(getApplicationContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.g1, menu);
return true;
}

Compare your string with this
if(name.equalsIgnoreCase("Accounting"))

if(name.equalsIgnoreCase("Accounting"))
instead of
if (name.contentEquals("Accounting"))

Using startActivityForResult(intent) will open a new activity and the previous activity waits for a result from the new activity. Use Bundle to share message between the activities and use startActivity(intent) instead of startActivityForResult(intent)

if (value.equals("")) {
Log.e("value equal to zero", value);
} else {
Log.e(value, "value not equal to zero");
startActivity(new Intent(Registernew.this, next.class));
finish();
}

Change To:
if (name.length()==0)
{
Intent myIntent = new Intent (ContentView.getContext(), NextActivity.class);
startActivityForResult(myIntent, 0);
}
else
{
Toast.makeText(getBaseContext(), "Sorry, wrong answer. Try Again!", Toast.LENGTH_SHORT).show();
}

Related

How to fix non-static error

My group is trying to make a contactlist which contacts can be addded too, However they are getting the following error : Non-static method populatelist() cannot be referenced through a static context. However we don't know if this then will actually work. Any help is appreciated
public class Profile extends Activity {
ImageView contactImageImgView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
Button editProfileButton = (Button) findViewById(R.id.BeditProfile);
Button addContactButton = (Button) findViewById(R.id.AdContactButton);
//Text ChangeProfilePictureText = (Text) findViewById(R.id.ChangeProfilePicture);
String Name = getIntent().getStringExtra("Name");
String eMail = getIntent().getStringExtra("Mail");
String Mobile = getIntent().getStringExtra("Mobile");
String Adress = getIntent().getStringExtra("Adress");
String Bio = getIntent().getStringExtra("Bio");
contactImageImgView = (ImageView) findViewById(R.id.imgViewContactImage);
TextView tv_Name = (TextView) findViewById(R.id.Name);
TextView tv_Mail = (TextView) findViewById(R.id.Email);
TextView tv_Mobile = (TextView) findViewById(R.id.Mobile);
TextView tv_Adress = (TextView) findViewById(R.id.Adress);
TextView tv_Bio = (TextView) findViewById(R.id.Bio);
tv_Name.setText(Name);
tv_Mail.setText(eMail);
tv_Mobile.setText(Mobile);
tv_Adress.setText(Adress);
tv_Bio.setText(Bio);
if(Cube.fromUnit){
editProfileButton.setVisibility(View.GONE);
//ChangeProfilePictureText.replaceWholeText("");
tv_Name.setText(Cube.Name);
tv_Mail.setText(Cube.eMail);
tv_Mobile.setText(Cube.Mobile);
tv_Adress.setText(Cube.Adress);
tv_Bio.setText(Cube.Bio);
}
contactImageImgView.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){ // error # View v, cannot resolve symbol v , expected ;
Intent intent = new Intent();
intent.setType("image*/");
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(intent.createChooser(intent, "Select Profile Image"), 1);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_maps, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_home) {
Intent i = new Intent(this, HomeScreen.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.BeditProfile) {
Intent i = new Intent(Profile.this, editProfile.class);
startActivity(i);
}
if (v.getId() == R.id.AdContactButton) {
MainActivity.addContact(Cube.Name, Cube.Adress, "", Cube.Mobile, Cube.eMail);
MainActivity.populateList();
Toast pass = Toast.makeText(this, Cube.Name + " " + "Has been added to your contacts", Toast.LENGTH_LONG);
pass.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 10);
pass.show();
}
}
public void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == RESULT_OK){
if(reqCode == 1)
contactImageImgView.setImageURI(data.getData());
}
}
}
We got a new crash regarding our button "add contact" . Which says that populateList cannot be executed
You cannot refer to MainActivity.populateList(); if populateList declaration is not static.Check JLS (ยง8.5).
You must create an instance of MainActivity
MainActivity ma = new MainActivity(); // or another constructor
ma.populateList(); // valid call of method
Or, if you don't need the instance of MainActivity declare populateList() as follows:
public static void populateList()

Same activity getting stacked

I have an activity that is suppose to finish itself and close the application. Now, in certain cases, which are varying on how the user is navigating to the activity, the activity is getting stacked. When the activity is stacking up, then calling finish() or android.os.Process.killProcess(android.os.Process.myPid()); or both together is only showing up the same activity again.
The Manifest:
<activity
android:name="newActivities.HomeActivity"
android:label="#string/title_activity_home"
android:screenOrientation="portrait">
</activity>
The activity:
public class HomeActivity extends Activity {
private EditText studentNameEdittext;
private Button startYourStoryButton, loginButton, navCollegesButton, settingsButton, search_friends_button, browseStoriesButton;
private TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
// finish();
// saveInPreference("loginStatus", "");
// }
// Set up the action bar
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#009945")));
bar.setTitle("Study Story");
bar.setIcon(R.drawable.statusbar_icon);
int titleId = getResources().getIdentifier("action_bar_title", "id", "android");
TextView yourTextView = (TextView) findViewById(titleId);
yourTextView.setTextColor(getResources().getColor(R.color.white_colour));
yourTextView.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// studentNameEdittext = (EditText)
// findViewById(R.id.studentNameEdittext);
startYourStoryButton = (Button) findViewById(R.id.startYourStoryButton);
// loginButton = (Button) findViewById(R.id.loginButton);
navCollegesButton = (Button) findViewById(R.id.navCollegesButton);
// settingsButton = (Button) findViewById(R.id.settingsButton);
// search_friends_button = (Button)
// findViewById(R.id.search_friends_button);
browseStoriesButton = (Button) findViewById(R.id.browseStoriesButton);
// textView1 = (TextView) findViewById(R.id.textView1);
// Set up font type
// studentNameEdittext.setTypeface(TypeFaceController.generalTextFace(HomeActivity.this));
startYourStoryButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// loginButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
navCollegesButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// settingsButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// search_friends_button.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
browseStoriesButton.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
// textView1.setTypeface(TypeFaceController.titleFace(HomeActivity.this));
startYourStoryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(HomeActivity.this, SignUp.class);
i.putExtra("signUpCaller", "Home");
startActivity(i);
}
});
// loginButton.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View arg0){
// Intent i = new Intent(HomeActivity.this, Login.class);
// startActivity(i);
// }
// });
// search_friends_button.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View v){
// Intent i = new Intent(HomeActivity.this,
// FindStudentBrowseStoryActivity.class);
// i.putExtra("Button", "search_friends_button");
// i.putExtra("searchString", studentNameEdittext.getText().toString());
// startActivity(i);
//
// }
// });
browseStoriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Intent i = new Intent(HomeActivity.this, FindStudentBrowseStoryActivity.class);
i.putExtra("Button", "browseStoriesButton");
startActivity(i);
}
});
navCollegesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
Intent i = new Intent(HomeActivity.this, CollegeListActivity.class);
startActivity(i);
}
});
// settingsButton.setOnClickListener(new View.OnClickListener() {
//
// #Override
// public void onClick(View v){
// Intent i = new Intent(HomeActivity.this, HomeSettingsActivity.class);
// i.putExtra("FromActivity", "HomeSettingsActivity");
// startActivity(i);
// finish();
//
// }
// });
}
// #Override
// protected void onStart(){
// if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
// finish();
// }
// super.onStart();
// }
//
// #Override
// protected void onResume(){
// if (getFromPreference("loginStatus").equalsIgnoreCase("true")) {
// finish();
// }
// super.onResume();
// }
// =========Login button action bar
#Override
public boolean onCreateOptionsMenu(Menu menu){
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_menu, menu);
return true;
}
// =========Login button action bar
#Override
public boolean onOptionsItemSelected(MenuItem item){
// handle item selection
switch (item.getItemId()) {
case R.id.home_login_string:
Intent i = new Intent(HomeActivity.this, Login.class);
// finish();
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
// logic to fix logout
#Override
public void onBackPressed(){
// Intent startMain = new Intent(Intent.ACTION_MAIN);
// startMain.addCategory(Intent.CATEGORY_HOME);
// startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);;
// startActivity(startMain);
//
// int pid = android.os.Process.myPid(); //
// android.os.Process.killProcess(pid); // return; }
android.os.Process.killProcess(android.os.Process.myPid());
finish();
}
// method to save variable in preference
public void saveInPreference(String name, String content){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(name, content);
editor.commit();
}
// getting content from preferences
public String getFromPreference(String variable_name){
String preference_return;
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preference_return = preferences.getString(variable_name, "");
return preference_return;
}
}
Please tell me where am I going wrong? Why is the activity stacking?
P.S: We cannot use single top etc as it causes some transition issues with the existing custom theme!
Take one application class which extends Application and take one arrayList and maintain the references of the activities in the arraylist.
When you click on back button in desired activity then finish the all the activities using arraylist.
Take one base activity. Which is super class of all the activities
public class BaseActviity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
App application = (App) getApplication();
application.addActivity(this);
}
}
Take one application class
public class App extends Application {
public App() {
if (listActivty == null) {
listActivty = new ArrayList<BaseActviity>();
}
}
public ArrayList<BaseActviity> listActivty;
public void addActivity(BaseActviity actviity) {
if (!listActivty.contains(actviity)) {
listActivty.add(actviity);
}
}
}
take 4 samples activity classes like ......
1) public class FirstActivity extends BaseActviity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
}
public void send(View view) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
2) public class SecondActivity extends BaseActviity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
}
public void send(View view) {
Intent intent = new Intent(this, ThirdActivity.class);
startActivity(intent);
}
}
3).........................
4) ............................
in 4 th activity class placed the following code
In desire activity that means final activity,override the onBackPressed
public void onBackPressed() {
super.onBackPressed();
App application = (App) getApplication();
ArrayList<BaseActviity> listActivty = application.listActivty;
for (BaseActviity actviity : listActivty) {
actviity.finish();
}
}
Wherever you are opening the activity which is getting stacked up, use this:
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Check if the Activity, that would be last activity when user clicks on the back button,is visible or not and if visible use
System.exit(0);
At last, the problem is solved. I has to do a little trick:
ParseUser.getCurrentUser();
ParseUser.logOut();
Intent i = new Intent(getActivity(), NewHomeActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(i);
getActivity().finish();
This did not cause any transition issues as well.

eclipse android application syntax error

i just started to use eclipse, and im trying to do my first android application. But apparently something is wrong with my syntax error , and i dont know how can i fix it or where did i do wrongly. so i hope someone here can help me. Thanks :) pardon me for my bad english.
Those underlined in red are errors but i dont know how can i fix them.
starts below here
Here is my .java
public class Category extends Activity { <---(**this sign is underlined in red**)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
Button switchButton = (Button) findViewById(R.id.button2);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.category, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
} ^
(**this sign is underlined in red**)
};
Unfortunately you're trying to initialize your button listener in another button listener. That's wrong.
Corrected:
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
};
});
Button switchButton2 = (Button) findViewById(R.id.button2);
switchButton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
});

Button takes to wrong location

So i am having some trouble with my application, bt3 takes me to bt2 location when i dont want it to, Can someone please explain why?
public class MainActivity extends Activity {
Button bt, bt2, bt3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
bt3 = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
integrator.initiateScan();
}
});
bt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, location1.class);
startActivity(intent);
}
});
bt3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, location2.class);
startActivity(intent);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
//super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
} else if (resultCode == RESULT_CANCELED) {
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
To clarify what each button is suppose to do:
button1 is supose to start a scan,
button2 is supose to take me to location1,
button3 is supose to take me to location2.
You could try implementing the onClick methods for each button in the XML instead, see if that changes anything.

How to retrieve data without using database,it has to display in listview

I am creating one dynamic list view i don't need database to store the data,whatever i am adding the data,it has to display in my list view. Right now its not displaying in my list view.
Projectlistactivity.java
public class Prayers extends ListActivity{
private static final int ACTIVITY_CREATE=0;
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();
/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prayers_list);
fillData();
registerForContextMenu(getListView());
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
//btn.setOnClickListener(listener);
/** Setting the adapter to the ListView */
setListAdapter(adapter);
}
private void fillData() {
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_insert:
createProject();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void createProject() {
Intent i = new Intent(this, PrayersEditActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_prayers_list, menu);
return true;
}
}
This is my projecteditactivity.java
public class PrayersEditActivity extends Activity{
private EditText mTitleText;
private Button mConfirmButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prayers_edit);
mTitleText = (EditText) findViewById(R.id.title);
mConfirmButton = (Button) findViewById(R.id.confirm);
registerButtonListenersAndSetDefaultText();
}
private void registerButtonListenersAndSetDefaultText() {
mConfirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//saveState();
String title = mTitleText.getText().toString();
mTitleText.setText("");
//adapter.notifyDataSetChanged();
setResult(RESULT_OK);
Toast.makeText(PrayersEditActivity.this, getString(R.string.task_saved_message), Toast.LENGTH_SHORT).show();
finish();
}
});
}
/*private void saveState() {
//mTitleText = (EditText) findViewById(R.id.title);
String title = mTitleText.getText().toString();
mTitleText.setText("");
adapter.notifyDataSetChanged();
}*/
}
In my listview if i click the menu button it has to go to the edit page,in their i have to add project after clicking the save button,it has to display in my listview,Right now its not display in my listview.
startActivityForResult(i, 1); and override this method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
list.add(result);
adapter.notifyDataSetChanged();
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
in your projecteditactivity.java change the onClick to following code
#Override
public void onClick(View view) {
String title = mTitleText.getText().toString();
mTitleText.setText("");
Intent returnIntent = new Intent();
returnIntent.putExtra("result",title);
setResult(RESULT_OK,returnIntent);
finish();
}
Why cant you store the data in a file, so that, you can retrive it when the application goes to background and after a few time, when come back, you can retrive it from file if memory lost happend.
I used to keep data in a Json formatted string, Dont know how and from where you get the list data.
you may use the life_cycle function. In addtion, you may transmit the data between Activities. So that you can use the edit data to display.
keep your data with ondestroy, onpause and onresume method.. and second option is to keep it with sharedprefence or sqlite.

Categories

Resources