I have made one screen with two images and I would like to add a button lower on the page which will navigate to a second page when I click it. Do you know how to code this? I know how to create a button but I don't know how to connect the two screens!
This task is accomplished with the startActivity(); method using Intents.
Intent i = new Intent(FromActivity.this, ToActivity.class);
startActivity(i);
In this case the Intent uses your current Activity as the Context in the first parameter, and the destination Activity in the second parameter.
Make sure that you add your second Activity to the manifest also (it resides in the tag)!
<activity android:name=".ToActivity"
android:label="#string/app_name">
</activity>
To sum it up:
ImageView myImage = (ImageView) findViewById(R.id.image);
myImage.setOnClickListener(new OnClickListener() {
#Override
onClick(View v) {
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}
}
);
Intent intent = new Intent(currentActivity.this,nextActivity.class);
this.finish();
startActivity(intent);
Button start_button=(Button)findViewById(R.id.btnsend);
start_button.setonClickListener(new onClickListener( ){
#override
onClick(View view){
Intent i = new Intent(MainActivity.this, NewActivity.class);
startActivity(i);
}
}
);
Lets break the answer in two parts, XML & JAVA part as every activity has each of these two. Assuming we are having only two activity, 'Activity1' being the one with the button which would redirect user to 'Activity2'.
As we have 2 activity, we would be having 4 files related for these 2 activity.
XML
so lets first do the easy way, as soon as you open the .xml file of Activity1, you should shift to design tab.
After reaching the design part you can insert a button from pallet, now you can select button which is inside your layout. After selection you could see the properties of button in right section of your screen where you can effectly change multiple properties of the button.
Here you shall find the "onClick" option, fill the box next to it with anything very simple, or something which you can remember. For example enter "nextAct"
or
Hard way would be entering the onClick property manually by typing follwing line in button code in XML
android:onClick="nextAct"
This is all on XML part.
JAVA
Open the .java file of Activity1, here you have to make a new method. This method should be named same as in the "onClick" property of button. Over here i would be taking "nextAct" as that is what i had used in XML. You can place this new method anywhere inside the class of the java file, i prefer keeping it at the end of the class as i could easily locate it if any issue in future.
Now you have to write the body of nextAct method. This can be sumed up in these two lines
public void nextAct(View v){
Intent i = new Intent(this, Activity2.class);
startActivity(i);
}
After this both should be connected and working fine.
give id to your button and mention it in your MainActivity.class.Then you can call OnClickListener to listen to your click.
Button mButton = (Button)findViewById(R.id.buttonid);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//you can use anything in place of i
Intent i = new Intent(MainActivity.this, NextActivity.class);
startActivity(i);
}
});
Related
I Have a button with a top drawable image, onclicking the button a intent is created i want to pass this drawable to the intent. How to do it pls help me out thanks in advance..
Button
Image to be showed on next activity
Assuming that your buttons are all atrological signs, and that you know what image resource you are using for each, why don't you just assign all your buttons the same onClick listener, and use a switch case to determine which sign it is.
public void onClick(View v) {
Intent i = new Intent(this, yourSecondActivity.class);
switch(v.getId()){
case R.id.ariesBtn:
i.putExra("sign", "aries");
break;
case R.id.cancerBtn:
i.putExtra("sign", "cancer");
break;
}
startActivity(i);
}
Then in your other activity check for extras "sign" and act accordingly
Try this:
#drawable/filename
or post the code to better undertastand
Hi i am creating a simple educational game in which user has to press three images in order to proceed to the next level and in next level user has to press 5 images.
i have gone through the on click but i using onIntent intent = new Intent(this.getApplicationContext(), Activity.class);
this.startActivity(intent, 0); i am only able to start new activity on single button pressed but i wanted to start new activity when user has finished pressing three image Buttons.
Thanks in advanced.
you can use global int variable and increase it every button click and if its more than your button number open new activity
public int btn = 0;
MyButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(btn >= 2) {
//open your activity
}
else{
btn++;
}
}
});
Based on my understanding of your problem,I have provided a possible simple implementation.
Use a simple data structure like queue or stack. When a image is tapped, add information about the image to the data structure. After adding the information to data structure see if number of items in data structure is equal to 3 ? if yes check data structure has info about the required three images and not just info about the same image (happens if user taps on same image more than once). If condition is met then call startActivity(). Generalize this so that you can reuse the logic in different activities, irrespective of the number of images.
Hi i finally achieved my desired activity. now with this after checking two check box app will start new activity.
`private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
checkBoxListener =new OnClickListener() {
public void onClick(View v) {
if(chkIos.isChecked()&&chkAndroid.isChecked()) {
Intent i1=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
if(chkAndroid.isChecked()) {
Intent i2=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
}
}
}
};
chkIos.setOnClickListener(checkBoxListener);
chkAndroid.setOnClickListener(checkBoxListener);
}
}
`
thanks for all your answers.
I have this clickable LinearLayout view, im trying to have it change Activity when clicked by every time i click the object i get a error.
final LinearLayout lindet = (LinearLayout) findViewById(R.id.detials);
lindet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(SellingActivity.this, DetailsActivity.class);
startActivity(i);
finish();
}
});
Did you remember to add the DetailsActivity to the AndroidManifest?
Supporting Macarse I would add that use SellingActivity.this.finish() instead of finish()
I believe this will prove to be your problem solver.
I am Having 8 Screenns.I have prepared 8 Activities for that.
In First Activity I have given this code
To Switch from Ist Activity to IInd
On Image Button gives On Clickpublic void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), Activity2.class);
v.getContext().startActivity(myIntent);
});
What to do to Switch on 2nd Activity to 3rd Activity ,
3rd Activity to 4th Activity , and so on.
Pls help me in regard.
Here's 1 way you could do it below. In this example, you'd put 3 buttons on the screen. These are buttons I defined and laid out in my XML file. Click on any of the 3 different buttons, and it takes you to the corresponding activity.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Here is code to go grab and layout the Buttons, they're named b1, b2, etc. and identified as such.
Button b1 =(Button)findViewById(R.id.b1);
Button b2 =(Button)findViewById(R.id.b2);
Button b3 =(Button)findViewById(R.id.b3);
// Setup the listeners for the buttons, and the button handler
b1.setOnClickListener(buttonhandler);
b2.setOnClickListener(buttonhandler);
b3.setOnClickListener(buttonhandler);
}
View.OnClickListener buttonhandler=new View.OnClickListener() {
// Now I need to determine which button was clicked, and which intent or activity to launch.
public void onClick(View v) {
switch(v.getId()) {
// Now, which button did they press, and take me to that class/activity
case R.id.b1: //<<---- notice end line with colon, not a semicolon
Intent myIntent1 = new Intent(yourAppNamehere.this, theNextActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent1);
break;
case R.id.b2: //<<---- notice end line with colon, not a semicolon
Intent myIntent2 = new Intent(yourMainAppNamehere.this, AnotherActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent2);
break;
case R.id.b3:
Intent myIntent3 = new Intent(yourMainAppNamehere.this, a3rdActivtyIwant.class);
YourAppNameHere.this.startActivity(myIntent3);
break;
}
}
};
}
Basically we're doing several things to set it up. Identify the buttons and pull them in from the XML layout. See how each has an id name assigned to it. r.id.b1 by example is my first button.
Then we set up a handler, which listens for clicks on my buttons. Next, need to know which button was pushed. The switch / case is like an "if then". If they press button b1, the code takes us to what we assigned to that button click. Press on b1 (Button 1), and we go to that "intent" or activity we've assigned to it.
Hope this helps a little Don't forget to vote up the answer if it's of any use. I'm just getting started on this stuff myself.
Thanks,
let's try to use the code snippet from below url and go through flags from developer guide.
Android; How can I initialise state in one activity, then have another refresh that?
I am creating a project in which i need to take in some numbers, makes some calculations and then on a new screen create show the answers. I am using an Intent object to go to the new screen:
final Button button = (Button) findViewById(R.id.save);
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent();
myIntent.setClass(HelloAndroid.this, screen2.class);
myIntent.putExtra("eFiber", Double.toString(E_fiber));
startActivity(myIntent);
}
});
but when i do this it crashes when i click the button. If i use the same xml file as i do in the first screen then it works just fine, its when i use a different xml file that i have the problems.
Have you registered your second Activity as an Activity in the android-manifest xml?
Under the <application> node, something to the effect of:
<activity android:name=".my.screen2" android:label="#string/app_name"></activity>
With your specific Activity information in place of ".my.screen2"