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);
Related
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 want to know how I can make my ImageButton to go to another activity in Android Studio 1.2.2?
I tried to make it using the way that was made for buttons.
This is my Java code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_01);
}
#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_01, 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 first need to create a ImageButton on the layout.xml of your current activity:
<ImageButton
android:id="#+id/your_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Than, in your current activity, you need to create a variable ImageButton:
ImageButton myButton = (ImageButton) findViewById(R.id.the_button_you_created_on_the_layout.xml_file);
Then, you need to set a click listener to this button:
*The click listener will "wait" for the click in your button.
myButton.setOnClickListener(new View.OnClickListner(){
// When the button is pressed/clicked, it will run the code below
#Override
public void onClick(){
// Intent is what you use to start another activity
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
}
});
After that, your app should start the other activity with no problem.
Here is some sample code for you to work with. The idea is to set an OnClickListener for your ImageButton, and in that OnClickListener create an Intent to go to your new Activity, then call startActivity(intent).
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_01);
ImageButton button = (ImageButton) findViewById(R.id.my_button);
final Context context = this;
button.setOnClickListener(new View.OnClickListner(){
#Override
public void onClick(View v){
Intent intent = new Intent(context, NewActivity.class);
startActivity(intent);
}
});
}
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 page with 6 image buttons when the user clicks on each of them will go to the detail page which contains the image of that icon with extra information. The question is how can I set the image in the detail page corresponds to the image button which the user clicked? for example when user clicked on image button 1 in the detail page see the image button1 when clicked on image button 2 in the detail page we have image of button2, how can I define this property in the activity page code?
here is my code corresponding to 6 image buttons:
//implement the OnClickListener interface
public class Destination extends ActionBarActivity
implements View.OnClickListener
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.destination);
//get the Button reference
//Button is a subclass of View
//buttonClick if from main.xml "#+id/buttonClick"
View btnClick = findViewById(R.id.image1);
View btnClick2 = findViewById(R.id.image2);
View btnClick3 = findViewById(R.id.image3);
View btnClick4 = findViewById(R.id.image4);
View btnClick5 = findViewById(R.id.image5);
View btnClick6 = findViewById(R.id.image6);
btnClick.setOnClickListener(this);
btnClick2.setOnClickListener(this);
btnClick3.setOnClickListener(this);
btnClick4.setOnClickListener(this);
btnClick5.setOnClickListener(this);
btnClick6.setOnClickListener(this);
}
//override the OnClickListener interface method
#Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.image1) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, DestinationTherapy.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image2) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image3) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image4) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image5) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.image6) {
//define a new Intent for the second Activity
Intent intent = new Intent(Destination.this, DestinationDetail.class);
//start the second Activity
this.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.detail, 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) {
startActivity(new Intent(this, Setting.class));
return true;
} else if (id == R.id.action_menu) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
Destination Detail activity:
public class DestinationDetail extends ActionBarActivity
implements View.OnClickListener
{
View btnClick = findViewById(R.id.emergency4);
btnClick.setOnClickListener(DestinationDetail.this);
View btnClick1 = findViewById(R.id.back2);
btnClick1.setOnClickListener(DestinationDetail.this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.detail, 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_settings) {
startActivity(new Intent(this, SettingActivity.class));
return true;
}
if (id == R.id.action_menu) {
startActivity(new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick(View arg0) {
if (arg0.getId() == R.id.emergency4) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, EmergencyCall.class);
//start the second Activity
this.startActivity(intent);
} else if (arg0.getId() == R.id.back2) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, Destination.class);
//start the second Activity
this.startActivity(intent);
}
}
}
Pass the res id of the image in the Intent
In Destination activity, in onClick() write something like:
public void onClick(View view) {
int id = view.getId();
// ...
if (id == R.id.image1) {
Intent intent = new Intent(this, DestinationTherapy.class);
intent.putExtra("res_id", R.drawable.image1);
startActivity(intent);
} else if ( //...
}
Change R.drawable.image1 to the appropriate res id. In your case it will be different for each image, so you need a kind of if/switch construct.
In DestinationDetail get a handle of the image and set it to that
setContentView(...);
ImageView iv = (ImageView) findViewById(R.id.my_detail_image_view);
int resId = getIntent().getIntExtra("res_id", 0);
iv.setImageResource(resId);
You must change R.id.my_detail_image_view with the appropriate view id of your ImageView in DestinationDetail
Clarification (view id vs res id): a view id is the one you define like android:id="#+id/my_view_id" in xml. A res id for an image is automatically generated for you when you put the image in the drawable folders. You can use it in java with R.id.image_name_without_extension or in xml with #drawable/image_name_without_extension
when i use this same code for menu item in other class then menu is shown. but when i implement in this class then don't show , can anyone tell me is there any problem with menu item if we use implement OnClickListener
public class MainPage extends Activity implements OnClickListener {
Context context;
ImageButton countingbutton, maximymbutton, minButton, compareButton,
statsbutton, review, memorygmae;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newmainpage);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
countingbutton = (ImageButton) findViewById(R.id.countingbutton);
maximymbutton = (ImageButton) findViewById(R.id.maximumbutton);
minButton = (ImageButton) findViewById(R.id.minimunbutton);
compareButton = (ImageButton) findViewById(R.id.comparenum);
statsbutton = (ImageButton) findViewById(R.id.ibStats);
review = (ImageButton) findViewById(R.id.review);
countingbutton.setOnClickListener(this);
maximymbutton.setOnClickListener(this);
minButton.setOnClickListener(this);
compareButton.setOnClickListener(this);
statsbutton.setOnClickListener(this);
review.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate themenu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cool_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub private static final String
switch (item.getItemId())
{
case R.id.aboutUs:
Intent about = new Intent(this, AboutUs.class);
startActivity(about);
break;
case R.id.exit:
super.finish();
System.exit(0);
break;
case R.id.mainmenu: Intent mainmenu = new Intent(this, MainPage.class);
startActivity(mainmenu);
break;
}
return false;
}