Android - how to switch activity on image click - java

Here I've got this code
switchact.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent act2 = new Intent(view.getContext(), Activity2.class);
startActivity(act2);
}
});
And i've got image on my activity/layout1.
How do I make that when I click on the image it switched to activity 2?

You should set a listener on the image over the onClick event, try something like:
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent act2 = new Intent(view.getContext(), Activity2.class);
startActivity(act2);
}
});

Intent act2 = new Intent(Activity1.this, Activity2.class);
startActivity(act2);

ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent act2 = new Intent(Activity1.this, Activity2.class);
startActivity(act2);
}
});

Related

How to make a second transition through a fragment

I made the first transition from the fragment to the activity, everything works fine, but I can't make the second transition. I'm a beginner, and I understand that this is a stupid mistake.
View v = inflater.inflate(R.layout.activity_physics, null);
Button phys35 = (Button) v.findViewById(R.id.phys35);
phys35.setOnClickListener(new View.OnClickListener() {
Public void onClick(View v) {
Intent intent = new Intent(getContext(), Phys35.class);
StartActivity(intent);
}
});
return v;
Button phys36 = (Button) v.findViewById(R.id.phys36);
phys36.setOnClickListener(new View.OnClickListener() {
Public void onClick(View v) {
Intent intent = new Intent(getContext(), Phys36.class);
StartActivity(intent);
}
});
return v;
Please help, thank you in advance for your help.

How to stop looping in activites and at the same handle the default on screen navigation back?

ImageView Back = findViewById(R.id.imageView11);
Back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Nav.class);
startActivity(intent);
finish();
}
});
Button cash = findViewById(R.id.button11);
cash.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Cash.class);
startActivity(intent);
finish();
}
});
TextView Card = findViewById(R.id.textView26);
Card.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Card.class);
startActivity(intent);
finish();
}
});
Button AddCard = findViewById(R.id.button12);
AddCard.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, AddedCard.class);
startActivity(intent);
finish();
}
});
TextView promo = findViewById(R.id.textView28);
promo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, promocode.class);
startActivity(intent);
finish();
}
});
Button Voucher = findViewById(R.id.button13);
Voucher.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Wallet.this, Vouchers.class);
startActivity(intent);
finish();
}
});
}
}
If i remove finish(); this create loops when I press default navigation back button.
like if I visited promocode class,then voucher class.from vouchar class I want to go back in Main Class by pressing default navigation back button.it will reverse like vouchar to promode then main class.
But I want by default back button will bring me vouchar class to directly main class.
But after adding finish(); whenever i press navigation back button the app quits.
I want a solution that...default navigation back button will act like my every customized back button for every each individuals frames !!
The key is to provide proper navigation.
Here's the Official Documentation to understand the tasks and back stack.
The other way to provide back navigation to the custom activity is to override onBackPressed() method of your activity like :
#Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(YourCurrentActivity.this, MainActivity.class));
finish();
}

images Intent does not open randomly. If I press the images in order intents are working

I have six images. If I press the first image intent is working. But if I press the third image firstly it is not working. it is working in order.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
You are setting other view click listener inside R.id.indoor_activities
In above code, you are saying that assign click listener every time to outdoor_activities if user click on indoor_activities first.
To fix this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.indoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent);
});// click listener for indoor activities
// click listener for outdoor activities when onCreated is called
ImageView imageView = (ImageView) findViewById(R.id.outdoor_activities);
imageView.bringToFront();
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent);
});
I think you should do like this.This will certainly works as your requirement.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView1 = (ImageView) findViewById(R.id.indoor_activities);
imageView1.bringToFront();
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent1 = new Intent (MainActivity.this,indoorActivities.class);
startActivity(myIntent1);
}
});
ImageView imageView2 = (ImageView) findViewById(R.id.outdoor_activities);
imageView2.bringToFront();
imageView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent2 = new Intent(MainActivity.this, outdoorActivities.class);
startActivity(myIntent2); }
});
Hope this helps you.

createDialog for onBackPressed but my app showing error close app

I have created a new layout name dialog_exit for onBackPressed but when I install and open, my application can not open and showing error close app
Please review my whole code and guide me how can I solve this problem
Here is my main activity code
public class MainActivity extends AppCompatActivity {
public Dialog mDialog;
public Button mDialogyes, mDialogno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDialog();
}
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) findViewById(R.id.yes);
mDialogno = (Button) findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
});
}
#Override
public void onBackPressed() {
mDialog.show();
}
}
Here is my layout code as screenshot because
stackoverflow not allow me to add more code that why sharing image
Updated code of createDialog function
private void createDialog() {
mDialog = new Dialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.dialog_exit);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
}
try this code
mDialogyes = (Button)mDialogyes. findViewById(R.id.yes);
mDialogno = (Button)mDialogyes. findViewById(R.id.no);
mDialogyes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
System.exit(0);
}
});
mDialogno.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDialog.dismiss();
}
});
You have to do like this
mDialogyes = (Button) mDialog.findViewById(R.id.yes);
mDialogno = (Button) mDialog.findViewById(R.id.no);

Android OnClickListener, intent and context

I am new to java and android. Here I am trying to set up my onclicklistener so when clicked, it will show another activity, i.e. ActivityB.class. The problem is with Intent i = new Intent(context, ActivityB.class); I am not sure what to put there for context. I tried to use this and context, and both are wrong.
Could you please kindly explain when and why I should use this and when to use other terms for the context?
public class MainActivity extends Activity {
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(context, ActivityB.class);
startActivity(i);
}
});
}
Change the code to.
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
}
});
As you need to pass context while using intent.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, ActivityB.class));
}
});
Fastest method!
Hope it helps! :D
1) Replace context with getApplicationContext()
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ActivityB.class);
startActivity(intent);
}
});
2) Replace context with MainActivity.this
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,ActivityB.class);
startActivity(intent);
}
});
Hope it will help you!
Try this..
If you declare ActivityB.class in manifest this should work.
Intent i = new Intent(MainActivity.this, ActivityB.class);
startActivity(i);
Intent i = new Intent(this, ActivityB.class);
startActivity(i);

Categories

Resources