my problem with new intent , the problem is with this: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.test.test1/com.test.test1.facebook}; have you declared this activity in your AndroidManifest.xml?
`package com.test.test1;
public class SampleActivity extends Activity implements OnItemSelectedListener,
OnItemClickListener, OnRotationFinishedListener, OnCenterClickListener {
public static final String ARG_LAYOUT = "layout";
private TextView selectedTextView;
#Override
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set content view by passed extra
Bundle extras = getIntent().getExtras();
int layoutId = extras.getInt(ARG_LAYOUT);
setContentView(R.layout.sample_with_background);
// Set listeners
CircleLayout circleMenu = (CircleLayout) findViewById(R.id.main_circle_layout);
circleMenu.setOnItemSelectedListener(this);
circleMenu.setOnItemClickListener(this);
circleMenu.setOnRotationFinishedListener(this);
circleMenu.setOnCenterClickListener(this);
selectedTextView = (TextView) findViewById(R.id.main_selected_textView);
selectedTextView.setText(((CircleImageView) circleMenu
.getSelectedItem()).getName());
}
#Override
public void onItemSelected(View view, String name) {
selectedTextView.setText(name);
switch (view.getId()) {
case R.id.main_calendar_image:
// Handle calendar selection
break;
case R.id.main_cloud_image:
// Handle cloud selection
break;
case R.id.main_facebook_image:
// Handle facebook selection
break;
case R.id.main_key_image:
// Handle key selection
break;
case R.id.main_profile_image:
// Handle profile selection
break;
case R.id.main_tap_image:
// Handle tap selection
break;
}
}
#Override
public void onItemClick(View view, String name) {
Toast.makeText(getApplicationContext(),
getResources().getString(R.string.start_app) + " " + name,
Toast.LENGTH_SHORT).show();
switch (view.getId()) {
case R.id.main_calendar_image:
// Here is my problem i cant start a new intent why ?
Intent myIntent = new Intent(view.getContext(), facebook.class);
startActivityForResult(myIntent, 0);
break;
case R.id.main_cloud_image:
// Handle cloud click
break;
case R.id.main_facebook_image:
// Handle facebook click
break;}
}
#Override
public void onRotationFinished(View view, String name) {
Animation animation = new RotateAnimation(0, 360, view.getWidth() / 2,
view.getHeight() / 2);
animation.setDuration(250);
view.startAnimation(animation);
}
#Override
public void onCenterClick() {
Toast.makeText(getApplicationContext(), R.string.center_click,
Toast.LENGTH_SHORT).show();
}
}
`
change this line in your code.
Intent myIntent = new Intent(SampleActivity.this, facebook.class);
you have declare Facebook activity in your manifestfile.xml
I guess the reason why you can't start the new intent is because you are using lowercase instead of uppercase for that facebook class.
Intent myIntent = new Intent(this, Facebook.class);
I think in your manifest have not facebook activity if have declared it but still can not start intent then you can check R.id.main_calendar_image view really get clicked
Instead of using view.getContext() use the full Activity.this way.
Intent myIntent = new Intent(SampleActivity.this, Facebook.class);
Oh and do ensure that it is declared in your manifest
Related
This question already has answers here:
How to open a different activity on recyclerView item onclick
(10 answers)
Closed 7 years ago.
I need to start an activity based on the item a user clicks on a RecyclerView. The code below has the position as a reference. Does anyone knows how to get this done? I need something like Intent intent = new Intent (MainActivity.this, Target.class). The target class changes depending on the item clicked of course.
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
Intent intent = new Intent(MainActivity.this, ???);
startActivity(intent);
}
})
);
Well, how about just putting the proper OnClickListener on each item's View in the RecyclerView? Each OnClickListener could store the target Class that you need for handling the navigation. You can handle this in the binding phase of the RecyclerView's adapter, there's not magic to it.
Select the target class via position:
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View view, int position) {
switch(position){
case 0:
startActivity(new Intent(MainActivity.this, A.class));
break;
case 1:
startActivity(new Intent(MainActivity.this, B.class));
break;
default:
break;
}
}
})
);
Of course, you have to define the mapping from position to target class.
You have collection of objects (probably ArrayList),try to add Object which has field of Class type, and then get it like this:
Intent intent = new Intent(MainActivity.this, objects.get(position).getClassField());
startActivity(intent);
You just need to put a onclicklistener to your viewholder(contain views).
private MainActivity mAct;
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAct.animateActivity(anything);
}
});
public void animateActivity(anything any) {
Intent i = new Intent(this, AssetDescription.class);
//Some anitmation if you want
startActivity(i);
}
I want to ask a question about optimizing my code. I am working on a project and i have code working for onItemClick listner and button click handler the problem is that i have 9 different activities and i have to copy and paste the same code in all my activities the issue i am having is too much of same code copy pasted into each activity
The Highlighted Section of the sliding menu are same in all activies all i have to do is register their click listners again and again to make them working and copy the same code in all activities. i want it to be generic i.e. code written in one place should be working for all the activities.
This app is in final launch mode and i cannot shift to sliding menu using navigation drawer that was the main reason i used this approach and the top menu also has different buttons which clicks need to be managed dynamically. i tried making this static but it didnt worked.
Thanking You for your time and replies.
That sounds like the perfect use for a fragment. Place the views and the related code in a fragment, and include the fragment in each activity.
What Gabe mentions would be the perfect way to go. However, if you do want to continue with multiple activities, you could create a class extending Activity with all the code for the sliding menu inside it. Then make sure that all other activities extend the new class you created.
visit Android Sliding Menu using Navigation Drawer tutorial for using navigation drawer...
this may help you..
This is the Solution to this Problem
public class SuperActivity extends Activity implements OnClickListener,
OnItemClickListener {
protected static Button btn_logout;
protected static ListView lv_SlidingMenu;
protected static FlyOutContainer rootView;
protected static TextView tv_userName;
protected static TextView tv_memberSince;
protected static ImageView iv_userImage;
protected static ImageView iv_top_home;
protected static TextView tv_top_home;
protected ImageView iv_slidingmenu;
protected static SlidingMenuAdapter slidingMenuAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
switch(parent.getId()){
case R.id.list:
switch(position){
case 0:
GeneralDataModel.actionIntent = new Intent(this,
ActivityTheGreatControversy.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 1:
break;
case 2:
GeneralDataModel.actionIntent = new Intent(this,
AtlastActivity.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 3:
GeneralDataModel.actionIntent = new Intent(this,
MediaActivity.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 4:
GeneralDataModel.actionIntent = new Intent(this,
TimeLineActivity.class);
this.startActivity(GeneralDataModel.actionIntent);
break;
case 6:
GeneralDataModel.actionIntent = new Intent(getApplicationContext(),
ActivityNotes.class);
this.startActivity(GeneralDataModel.actionIntent);
GeneralDataModel.actionIntent = null;
break;
case 10:
GeneralDataModel.actionIntent = new Intent(this,
ActivitySettings.class);
this.startActivity(GeneralDataModel.actionIntent);
rootView.toggleMenu();
break;
default:
rootView.toggleMenu();
break;
}
break;
}
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "CLICKEDiy", Toast.LENGTH_LONG)
.show();
switch (v.getId()) {
case R.id.btn_sliding_logout:
GeneralDataModel.actionIntent = new Intent(getApplicationContext(),
LoginSignup.class);
GeneralDataModel.actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(GeneralDataModel.actionIntent);
GeneralDataModel.actionIntent = null;
new SessionManager(this).logoutUser();
this.finish();
break;
case R.id.iv_home_slidingmenu:
rootView.toggleMenu();
break;
}
}
protected void fillSlidingMenu() {
tv_userName.setText(UserInformation.getFirstName() + " "
+ UserInformation.getLastName());
tv_memberSince.setText(UserInformation.getMemberSince());
lv_SlidingMenu.setAdapter(slidingMenuAdapter);
}
}
and derive your all activities from This Class
Then in their onClickListners just simply call super.onclick(v);
I have a Switch button, from which I want to pass different values (1 or 2) to next activity for calculations. But I always get 0. Any help?
Switch mySwitch = (Switch) findViewById(R.id.edit_home);
// set the switch to ON
mySwitch.setChecked(true);
// attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
double input5 = 2;
Intent intentx=new Intent(MainActivity.this,Main2Activity.class);
intentx.putExtra("IFValue",input5);
startActivity(intentx);
Toast.makeText(getApplicationContext(), "Home field is important advantage",
Toast.LENGTH_SHORT).show();
} else {
double input5 = 1;
Intent intenty=new Intent(MainActivity.this,Main2Activity.class);
intenty.putExtra("IFValue",input5);
startActivity(intentx);
Toast.makeText(getApplicationContext(),
"Home field is not advantage", Toast.LENGTH_SHORT).show();
}
}
});
In next activity:
Bundle bundle = getIntent().getExtras();
double input5 = bundle.getDouble("IFValue");
EDITED: I want to send values (input5) to the next activity, but if I add startActivity(intentx) inside of switch; then goes there imediately and not when I press next Button (buttonForward) as I want to.
Rest of the code:
Switch mySwitch = (Switch) findViewById(R.id.edit_home);
// set the switch to ON
mySwitch.setChecked(true);
// attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new Switch.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
double input5 = 2;
Intent intentx=new Intent(MainActivity.this,Main2Activity.class);
intentx.putExtra("IFValue",input5);
startActivity(intentx);
Toast.makeText(getApplicationContext(), "Home field is important advantage",
Toast.LENGTH_SHORT).show();
} else {
double input5 = 1;
Intent intenty=new Intent(MainActivity.this,Main2Activity.class);
intenty.putExtra("IFValue",input5);
startActivity(intenty);
Toast.makeText(getApplicationContext(),
"Home field is not advantage", Toast.LENGTH_SHORT).show();
}
}
});
Button buttonForward = (Button) findViewById(R.id.buttonToMain2);
buttonForward.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
EditText edit_team = (EditText) findViewById(R.id.edit_team);
EditText edit_form = (EditText) findViewById(R.id.edit_form);
EditText edit_import = (EditText) findViewById(R.id.edit_import);
String ekipa1 = edit_team.getText().toString();
final double input2 = Double.valueOf(qualityControl.getProgress());
final double input3 = Double.valueOf(edit_form.getText().toString());
final double input4 = Double.valueOf(ratingBar.getRating());
final double input6 = Double.valueOf(edit_import.getText().toString());
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
intent.putExtra("Value",input2);
intent.putExtra("Value1",input3);
intent.putExtra("Value2",input4);
intent.putExtra("Value4",input6);
intent.putExtra("team1", ekipa1);
startActivity(intent);
}
});
You are creating a local instance of the Intent (intentx & intenty) inside the listener (I do not see you calling startActivity() inside the listener). The passed Extras only apply to the local copy not to the one you are using outside the listener with startActivity().
Update 1
Character case matters when using Extra keys:
You are using intentx.putExtra("IFvalue",input5);
while in the next Activity you are using double input5 = bundle.getDouble("IFValue"); which is a different key.
Update 2
I want to send values (input5) to the next activity, but if I add
startActivity(intentx) inside of switch; then goes there imediately
and not when I press next Button (buttonForward) as I want to.
Then, you will need to have only one Intent instance in the whole Activity (define it before onCreate() and then do not start the Activity from the switch listener, but add the Extra for the single Intent instance:
Intent intent = new Intent(MainActivity.this,Main2Activity.class); // Class level variable
then change the following in the switch listener:
// Intent intentx=new Intent(MainActivity.this,Main2Activity.class); remove this
intent.putExtra("IFValue",input5);
and in the buttonForward click listener:
//Intent intent=new Intent(MainActivity.this,Main2Activity.class); remove this
intent.putExtra("Value",input2);
intent.putExtra("Value1",input3);
intent.putExtra("Value2",input4);
intent.putExtra("Value4",input6);
intent.putExtra("team1", ekipa1);
startActivity(intent);
I know this has been asked a million times but none have led me to solving my problem. The onclicklistener will not activate the code for any of the buttons. Here are the different sections that apply to the five buttons.
Button btnGuysMax;
Button btnGuysMedium;
Button btnEven;
Button btnGirlsMedium;
Button btnGirlsMax;
....
private void init()
{
datasource = new BarsDataSource(this);
datasource.open();
Intent intent = getIntent();
long id = intent.getLongExtra("bar_id",0);
bar = datasource.getBarById(id);
title = (TextView)findViewById(R.id.title);
btnGuysMax = (Button)findViewById(R.id.btnGuysMax);
btnGuysMedium = (Button)findViewById(R.id.btnGuysMedium);
btnEven = (Button)findViewById(R.id.btnEven);
btnGirlsMedium = (Button)findViewById(R.id.btnGirlsMedium);
btnGirlsMax = (Button)findViewById(R.id.btnGirlsMax);
......
btnGuysMax.setOnClickListener(this);
btnGuysMedium.setOnClickListener(this);
btnEven.setOnClickListener(this);
btnGirlsMedium.setOnClickListener(this);
btnGirlsMax.setOnClickListener(this);
.....
#Override
public void onClick(View view)
{
//resetButtons();
switch (view.getId()) {
case R.id.btnGuysMax:
//bar.setSexRatio(-2);
//btnGuysMax.setBackgroundColor(guysMaxColor);
Toast.makeText(this,"Max clicked!",Toast.LENGTH_LONG);
break;
case R.id.btnGuysMedium:
bar.setSexRatio(-1);
Toast.makeText(this,"Medium clicked!",Toast.LENGTH_LONG);
//btnGuysMedium.setBackgroundColor(guysMediumColor);
break;
case R.id.btnEven:
bar.setSexRatio(0);
//Toast.makeText(this,"Medium clicked!",Toast.LENGTH_LONG);
break;
case R.id.btnGirlsMedium:
bar.setSexRatio(1);
//btnGirlsMedium.setBackgroundColor(girlsMediumColor);
break;
case R.id.btnGirlsMax:
bar.setSexRatio(2);
break;
.....
To display a toast you need to call show method.
Try:
Toast.makeText(this,"message",Toast.LENGTH_LONG).show();
try doing:
#Override
public void onClickListener(View view)
{
instead of:
#Override
public void onClick(View view)
{
GOAL 1: When click the button, if there isn't any radiobutton checked, it will warning user by Toast; if a radiobutton checked, it will take user to new activity (or do smt up on you).
First
public class hanh1_2 extends Activity{
public static int ButID;
#Override
Second, set the button action:
final Button ok2 = (Button) findViewById(R.id.ok2);
ok2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Set int of ButID = checkedradiobuttonID
//If ButID = -1 --> there isn't bt checked
int ButID = tieng.getCheckedRadioButtonId();
if (ButID == -1){
Toast.makeText(hanh1_2.this, "Check a butt pls", Toast.LENGTH_SHORT).show();
}
else {
Intent intent2 = new Intent(hanh1_2.this,hanh1_3.class);
startActivity(intent2);
}
}
});
Meaningless to advanced, but may helpful for some newbie like me :)
Have a look at the Form stuff tutorial on the Android dev site. You can supply an OnClickListener to all RadioButtons and keep track of the one selected (if any).
private OnClickListener radio_listener = new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
RadioButton rb = (RadioButton) v;
Toast.makeText(HelloFormStuff.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
};
Alternatively, you can potentially use the RadioGroup's getCheckedRadioButtonId() method.
As illustrated in one of the other answers: pass the int value as an extra to the Intent you use to launch your second Activity:
// In first activity
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putInt("selected_index", selectedIndex);
startActivity(i);
// In second activity
int selectedIndex = getIntent().getExtras().getInt("selected_index");
Take all your RadioButton and RadioGroup to class level.
initialize them inside onCreate()
now inside onClick() get id of checked RadioButton and compare like this:
public void onClick(View v) {
int checked = tieng.getCheckedRadioButtonId(); // tieng is your RadioGroup
switch(checked)
{
case R.id.tieng1:
Toast.makeText(hanh1_2.this, "First is selected", Toast.LENGTH_SHORT).show();
break;
case R.id.tieng1:
Toast.makeText(hanh1_2.this, "Second is selected", Toast.LENGTH_SHORT).show();
break;
case R.id.tieng1:
Toast.makeText(hanh1_2.this, "Third is selected", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(hanh1_2.this, "pleas check any button", Toast.LENGTH_SHORT).show();
break;
}
}
put extra along with intent :
else {
Intent intent2 = new Intent(hanh1_2.this,hanh1_3.class);
intent2.putInt(Index1, index1);
startActivity(intent2);
}
now inside second activity onCreate() read this extra :
{
int Index1 = getIntent().getExtras().getInt("Index1");
//do stuff here
}