ok so now what i got is this as shown below . cant seem to solve it still . a little help please for android developer eclipse application for my final year project .wacthed alot of videos about it on youtube and google but non manage to help fixed the syntax error i've got here .
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class Category extends Activity {
#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);
}
};
Check your onClick() methods bro...
switchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Intent intent = new Intent(Category.this, Cake.class);
startActivity(intent);
}
}); // This was missing.
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);
}
});
Take the semicolon away at the end of this inner onClick method
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
Replace
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
};
with
public void onClick(View v) {
Intent intent = new Intent(Category.this, Cookie.class);
startActivity(intent);
}
You did not close the scope properly.
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.lge.cptool.R;
public class Category extends Activity {
#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);
}
});
}
}
Nothing more than syntax error. Use this
public class Category extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
Button switchButton = (Button) findViewById(R.id.button1);
switchButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
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);
}
}
Related
I am trying to learn Android. I want to build a simple letter counting app for counting random name, that is my code l have problem with onclickListerner. Thank you in advance Counting Themed Game
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
TextView nameView = (TextView)findViewById(R.id.nameView);
nameView.setText("saya");
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
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_refresh) {
TextView nameView = (TextView)findViewById(R.id.nameView);
nameView.setText("van");
}
return super.onOptionsItemSelected(item);
}
}
});
}
Replace this:
b.setOnClickListener() ;
With this:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Your code here
}
});
And remove this:
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stu
}
Since you're extending the onclicklistener, you should call b.setOnClickListener(this);
I have a first activity ("Home"), with two buttons: one is called About and leads to activity About and the second is named List and leads to the activity List.
Manifest.xml should be fine, but I get a load of tiny petty errors I can't fix up by myself, regrettably.
Home.class is the following
Public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button AboutButton = (Button)findViewById(R.id.About);
AboutButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view); {
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}
Button ListButton = (Button)findViewById(R.id.List);
ListButton.setOnClickListener(new View.onClickListener());{
#Override
public void onClick(View view); {
Intent openList = new Intent(Home.this, List.class);
startActivity(openList);
}
}
}
#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_home, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
while About.class is like this
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
public void onClick(View view); {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#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_about, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and List is like this:
public class List extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#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_list, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I get lot of red light bulbs, saying, for instance that ")" or ";" is expected or (worse) onClickListener cannot be resolved
Last but not least: I copied this code online and I was wondering why after "View" there is a "view"; what does it mean?
I copied your code and I see fails everywhere... let me explain you what's going on ...
HOME CLASS
1.- You have to remove the ")"
2.- You don't have to ";" when you call onClick()
3.- When you are don on your onClick() NOW you have to close it, you missed the ");"
AboutButton.setOnClickListener(new View.OnClickListener()){ //<-- Just remove one
#Override
public void onClick(View view); { //<-- Remove this ";"
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}//Here goes ");"
4.- The ListButton has the same issues so just fix it as you will fix the first one.
ABOUT CLASS
1.-On this case you have the setOnClickListener() ok, BUT why you have two onClick(View view)? It's not necessary just remove one of them.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button) findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
});
}
LIST CLASS
1.-Well in your List class you have made the same error as the first one... Your onClickListener() it's wrong.
2.-Once again you included an unnecessary ")" on new View.OnClickListener() just remove it,
3.-Another fail that I'm seeing is that you are trying to make an Intent but you are refering that you are on About.this and you are NOT. You are on List class so you have ot put List.this because the first parameter refers :
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
More information about Intents
4.- You need to close again the setOnClickListener()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){ //<-- remove one ")"
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class); //<-- Remove About.this and put List.this
startActivity(returnhome);
}
}//<-- Close the setOnClickListener() with ");"
}
It's okay guy, this is your first question and I'll answer it, but NOW as I've made the favor to take my time and explain to you what was wrong on your code take your time to :
How do I as a question on StackOverflow
Learn some Android basics
And the most IMPORTANT THING
DO NOT COPY PASTE AN INTERNET CODE if you don't know the basics, I mean you can copy paste the code, but you'll face with this problem every time you do this, so first of all read the tutorial, make an examples, and you'll improve every day.
I have created a menu activity with 3 buttons (main,history,logout).
I do not know why all of buttons are not working and there's logout function.
Anyway i have no idea about logout function, is it right function or wrong function?
I am new to Android, any help will be appreciated.
Here is my code:
public class MenuActivity extends ActionBarActivity implements View.OnClickListener {
Button mainBtn,historyBtn,logoutBtn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
mainBtn = (Button)findViewById(R.id.button1);
historyBtn = (Button)findViewById(R.id.button2);
logoutBtn = (Button)findViewById(R.id.button3);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent main = new Intent (this, MainActivity2.class);
startActivity(main);
break;
case R.id.button2:
Intent history = new Intent(this, HistoryActivity.class);
startActivity(history);
break;
case R.id.button3:
Intent logout = new Intent(this, LoginActivity.class);
keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logout);
this.finish();
}
}
#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_menu, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Have you done this?
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
i.e. Register the onClickListener for the buttons, either in code or within the xml file.
Set listner to all buttons like..
mainBtn.setOnClickListener(this);
You will need to register the buttons with the click listener. Replace your code with the following..
public class MenuActivity extends ActionBarActivity implements View.OnClickListener {
Button mainBtn,historyBtn,logoutBtn ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
mainBtn = (Button)findViewById(R.id.button1);
historyBtn = (Button)findViewById(R.id.button2);
logoutBtn = (Button)findViewById(R.id.button3);
// Register your buttons with the click listener
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
Intent main = new Intent (this, MainActivity2.class);
startActivity(main);
break;
case R.id.button2:
Intent history = new Intent(this, HistoryActivity.class);
startActivity(history);
break;
case R.id.button3:
Intent logout = new Intent(this, LoginActivity.class);
keluar.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logout);
this.finish();
}
}
#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_menu, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You didn't set a listener to your buttons.
mainBtn = (Button)findViewById(R.id.button1);
historyBtn = (Button)findViewById(R.id.button2);
logoutBtn = (Button)findViewById(R.id.button3);
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
Good Luck With your work. :)
mainBtn.setOnClickListener(this);
historyBtn.setOnClickListener(this);
logoutBtn.setOnClickListener(this);
I have many activities which have 1 resultant activity. Does anyone know how to separate it, the result will display one result from many activity. If i run it it will display result fruitQuestion and AnimalQuestion.
Test.java:
public class Test extends Activity {
Button btnFruitQ,btnAnimalQ;
ToggleButton tb;
public static boolean tbflag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
btnFruitQ = (Button) findViewById(R.id.btnFruitQ);
btnAnimalQ = (Button) findViewById(R.id.btnAnimalQ);
tb = (ToggleButton) findViewById(R.id.ToggleButton);
btnFruitQ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tbflag=tb.isChecked();
startActivity(new Intent(Test.this, FruitQuestion.class));
}
});
btnAnimalQ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tbflag=tb.isChecked();
startActivity(new Intent(Test.this, AnimalQuestion.class));
}
});
}
#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_test, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
FruitQuestion.java:
public class FruitQuestion extends Activity {
TextView tv;
Button btnNext;
RadioGroup rg;
RadioButton bt1,bt2,bt3;
String Question[]={"HAHA","HAHA2","HAHA3"};
String ans[]={"i1","12","i3"};
String opt[]={"i1","lali","uu","12","hehe","oo","i3","jj","cc"};
int flag=0;
public static int mark,correct,wrong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fruit_question);
tv=(TextView) findViewById(R.id.tvque);
btnNext=(Button) findViewById(R.id.btnNext);
rg = (RadioGroup) findViewById(R.id.rg);
bt1 = (RadioButton) findViewById(R.id.btn1);
bt2 = (RadioButton) findViewById(R.id.btn2);
bt3 = (RadioButton) findViewById(R.id.btn3);
tv.setText(Question[flag]);
bt1.setText(opt[0]);
bt2.setText(opt[1]);
bt3.setText(opt[2]);
Toast.makeText(this,"Negative Mark : "+Test.tbflag,1000).show();
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RadioButton uans=(RadioButton)findViewById(rg.getCheckedRadioButtonId());
String ansText= uans.getText().toString();
if (ansText.equalsIgnoreCase(ans[flag]))
{
correct++;
}
else
{
wrong++;
}
flag++;
if(flag<Question.length)
{
tv.setText(Question[flag]);
bt1.setText(opt[flag*3]);
bt2.setText(opt[(flag*3)+1]);
bt3.setText(opt[(flag*3)+2]);
}
else
{
if(Test.tbflag)
{
mark=correct-wrong;
}
else
{
mark=correct;
}
Intent in=new Intent(getApplicationContext(),ResultActivity.class);
startActivity(in);
}
}
});
}
#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_fruit_question, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ResultActivity.java:
public class ResultActivity extends Activity {
TextView tv;
Button btnRestart;
StringBuffer sb=new StringBuffer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
tv=(TextView)findViewById(R.id.tvres);
btnRestart=(Button)findViewById(R.id.btnRestart);
sb.append("Correct Answer : " + FruitQuestion.correct);
sb.append("\nWrong Answer : " + FruitQuestion.wrong);
sb.append("\nFinal Score : " + FruitQuestion.mark);
tv.setText(sb);
FruitQuestion.correct = 0;
FruitQuestion.wrong = 0;
sb.append("Correct Answer : "+AnimalQuestion.correct);
sb.append("\nWrong Answer : "+AnimalQuestion.wrong);
sb.append("\nFinal Score : " + AnimalQuestion.mark);
tv.setText(sb);
AnimalQuestion.correct=0;
AnimalQuestion.wrong=0;
btnRestart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in=new Intent(getApplicationContext(),Test.class);
startActivity(in);
}
});
}
#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_result, 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.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
You can use Intent Extras to tell ResultActivity which Activity called it, and display the corresponding results.
Note that you should also re-factor your code so that you're not using static variables in the Activities to store data, you could use SharedPreferences instead.
Just to get you started though, here is what you could do in the short term:
In FruitQuestion, you would have:
Intent in=new Intent(getApplicationContext(),ResultActivity.class);
in.putExtra("question", "fruit");
startActivity(in);
And in AnimalQuestion, you would have:
Intent in=new Intent(getApplicationContext(),ResultActivity.class);
in.putExtra("question", "animal");
startActivity(in);
Then, in ResultActivity, capture the Intent Extra, and show corresponding result:
Bundle extras = this.getIntent().getExtras();
if (extras != null){
String question = extras.getString("question");
if (question.equals("fruit")) {
sb.append("Correct Answer : " + FruitQuestion.correct);
sb.append("\nWrong Answer : " + FruitQuestion.wrong);
sb.append("\nFinal Score : " + FruitQuestion.mark);
tv.setText(sb);
FruitQuestion.correct = 0;
FruitQuestion.wrong = 0;
}
else if (question.equals("animal")) {
sb.append("Correct Answer : " + AnimalQuestion.correct);
sb.append("\nWrong Answer : " + AnimalQuestion.wrong);
sb.append("\nFinal Score : " + AnimalQuestion.mark);
tv.setText(sb);
AnimalQuestion.correct = 0;
AnimalQuestion.wrong = 0;
}
}
Firstly, give your Intent instance a different name. Other than in. You can put extra in intent object and send it to ResultActivity
Like this
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("activityName", "FruitQuestion");
startActivity(intent);
In ResultActivity
Intent intent = getIntent();
String activityName = intent.getStringExtra("activityName");
Check your activityName value and show its results.
I think I understand you, you want to pass some values from A-activity to B-activity.
To pass data between activities, you can use the same intent you are sending.
Here an example:
Intent i = new Intent(this, ActivityA.class);
i.putExtra("key1", value1);
i.putExtra("key2", value2);
startActivity(i);
putExtra method allow pass multiples kinds of value, like boolean, string, integer...
Then, in the another activity, in onCreate method, you can get this value this way:
String save = getIntent().getExtras().getString("key1");
Or
Bundle bundle = getIntent().getExtras();
boolean check = bundle.getBoolean("key2");
I hope it help you :)
I have a menu and when clicked on a menu, it goes inside another menu.
for example, mainmenu-->click on airport guide --> menu2
but when i am in menu2 and press on the back button, the app is closing instead of going back to mainmenu . i am not able to figure out the problem here. i am new to android development
package com.shashank.sharjahinternationalairport;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity {
ImageButton flightInfoButton;
ImageButton airportGuideButton;
ImageButton visitorInfoButton;
ImageButton saaDcaButton;
ImageButton cargoButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flightInfoButton = (ImageButton) findViewById(R.id.flightInfo);
airportGuideButton = (ImageButton) findViewById(R.id.airportGuide);
visitorInfoButton = (ImageButton) findViewById(R.id.visitorInfo);
saaDcaButton = (ImageButton) findViewById(R.id.saaDca);
cargoButton = (ImageButton) findViewById(R.id.cargo);
airportGuideButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
setContentView(R.layout.airport_guide);
}
});
visitorInfoButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
setContentView(R.layout.visitor_info);
}
});
saaDcaButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
setContentView(R.layout.saa_dca);
}
});
}
#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;
}
You are using setContentView, so you are just changing the view layout. When you press the backbutton, the Android calls onBackPressed, which the default implementation is to call finnish() method, that closes the activity.
You can override the onBackPressed method to set the other view.
#Override
public void onBackPressed()
{
//setContentView the previous view
}
Hope it helps!
you can always override the onBackPressed method of Activity, you'll have something like this
public class MainActivity extends Activity {
ImageButton flightInfoButton;
ImageButton airportGuideButton;
ImageButton visitorInfoButton;
ImageButton saaDcaButton;
ImageButton cargoButton;
private enum VIEWS {
VIEW_1, VIEW_2, VIEW_3
};
private VIEWS mSelectedView = VIEWS.VIEW_1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flightInfoButton = (ImageButton) findViewById(R.id.flightInfo);
airportGuideButton = (ImageButton) findViewById(R.id.airportGuide);
visitorInfoButton = (ImageButton) findViewById(R.id.visitorInfo);
saaDcaButton = (ImageButton) findViewById(R.id.saaDca);
cargoButton = (ImageButton) findViewById(R.id.cargo);
airportGuideButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View V) {
showView1();
}
});
visitorInfoButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View V) {
showView2();
}
});
saaDcaButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View V) {
showView3();
}
});
}
private void showView1() {
setContentView(R.layout.airport_guide);
mSelectedView = VIEWS.VIEW_1;
}
private void showView2() {
setContentView(R.layout.visitor_info);
mSelectedView = VIEWS.VIEW_2;
}
private void showView3() {
setContentView(R.layout.saa_dca);
mSelectedView = VIEWS.VIEW_3;
}
#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;
}
#Override
public void onBackPressed() {
switch (mSelectedView) {
case VIEW_1:
super.onBackPressed();
break;
case VIEW_2:
showView1();
break;
case VIEW_3:
showView2();
break;
}
}
}
All you is doing that you are changing the Content View of the class MainActivity you need to create sperate classes for your difreent layouts and you can call it by INTENT
This is happennig because you are using only one activity, and on click just changing the content view. Even though this appears like a multi page activity, it is only one screen. Hence onBackPressed() it closes. Yo will have to create different activiies for each of your menu options and start them using Intents on Button Click.
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
You are using only one activity, if it is many activity mean you can track back to previously loaded activity.
else you must needed only one activity mean please store the layouts in some array, then in the onBackPressed() you can it get LIFO manner
#Override
public void onBackPressed() {
}