How to set the image button in the nested activity? - android

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

Related

Base Activity for custom ListView menu

I'm new in Android programming. I'm working on an application that have multiple activities. I've created a custom menu with ListView. I would like to put this menu in a base activity to be available in all activities. How should I do this?
Till now, I have something like this:
This is for the button to toggle the menu
menuToggelIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Hide layouts if VISIBLE
if(menuLayout.getVisibility() == View.VISIBLE)
{
menuLayout.setVisibility(View.GONE);
}
// Show layouts if they're not VISIBLE
else
{
menuLayout.setVisibility(View.VISIBLE);
}
}
});
And this is for the menu
menuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String name = menuArray[position];
Context context = getApplicationContext();
switch (name) {
case "CASE1":
Intent case1Intent = new Intent(context, Activity1.class);
startActivity(case1Intent);
break;
case "CASE2":
Intent case2Intent = new Intent(context, Activity2.class);
startActivity(case2Intent);
break;
case "CASE3":
Intent case3Intent = new Intent(context, Activity3.class);
startActivity(case3Intent);
break;
case "CASE4":
Intent case4Intent = new Intent(context, Activity4.class);
startActivity(case4Intent);
break;
case "CASE5":
Intent case5Intent = new Intent(context, Activity5.class);
startActivity(case5Intent);
break;
case "CASE6":
Intent case6Intent = new Intent(context, Activity6.class);
startActivity(case6Intent);
break;
case "CASE7":
Intent case7Intent = new Intent(context, Activity7.class);
startActivity(case7Intent);
break;
default:
break;
}
}
});
Android custom menu
make one BaseActivity class and all activity extends by BasyActivity class.
BaseActivity class define your main things that show all the screen like menu and other thing. for example
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.manu_file_name, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.icon) {
Toast.makeText(getApplicationContext(), "Hello World", 0).show();
}
return super.onOptionsItemSelected(item);
}
}
and this activity extends all other activity.

Why are my buttons in Android not working?

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);

One Result Activity with many Activities

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 :)

Why the application crashes when I implements View.OnClickListener on the detail page of a list item?

I am using a list view when I click on each item it shows a detail page. In the detail page I want to have some buttons and also an action-bar for implementing the menu access but the app crashes when I use: implements View.OnClickListener this is the code, can you please tell me what is the problem ?.
This is my Class AgendaDetail :
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
public class AgendaDetail extends ActionBarActivity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agenda_detail);
View btnClick = findViewById(R.id.emergencycall);
btnClick.setOnClickListener(AgendaDetail.this);
View btnClick1 = findViewById(R.id.cancel);
btnClick1.setOnClickListener(AgendaDetail.this);
String selected_Device = getIntent().getStringExtra("device_name");
// do whatever on this behalf of selected_Device String
}
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);
}
#Override
public void onClick(View arg0) {
if (arg0.getId() == R.id.emergencycall) {
//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.cancel) {
//define a new Intent for the second Activity
Intent intent = new Intent(this, Destination.class);
//start the second Activity
this.startActivity(intent);
}
}
}
Try
Intent intent = new Intent(AgendaDetail.this, EmergencyCall.class);
startActivity(intent);
instead
Intent intent = new Intent(this, EmergencyCall.class);
this.startActivity(intent);

Multiple Activities and Up Button

I have an activity that can be asked to run after clicking buttons on many different activities and hence it does not have a "single parent". Therefore in the android manifest I cannot define its parent so I cant get the "Up" button to function properly.
Is there a way I can have the "up" button return to the activity that called it?
You can pass ComponentName of starting activity as an extra
intent = new Intent(this, UpButtonActivity.class);
intent.putExtra(EXTRA_PARENT_COMPONENT_NAME, new ComponentName(this, ThisActivity.class));
startActivity(intent);
The Activity with up button
private ComponentName parent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parent = getIntent().getParcelable(EXTRA_PARENT_COMPONENT_NAME);
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getId()) {
case android.R.id.home:
if (parent != null) {
final Intent parentIntent = new Intent();
parentIntent.setComponentName(parent);
startActivity(parentIntent);
finish();
return true;
} else {
return super.onMenuItemSelected(featureId, item);
}
//...
}
}

Categories

Resources