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?
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 a Activity in android that has 4 buttons.
The first 3 buttons fetches a json data from a weather API for 1 day, next 5 days and next 10 days respectively.
I have a 4th button placed at the bottom of the screen, which takes user to second activity.
I want to restrict the entry of user to second Activity if no button from top 3 is clicked.
If the data is fetched, I mean any one of the top 3 buttons have been clicked, allow him to go to second activity on 4th button click else show a message.
How can i check on click of 4th button if any of the top 3 buttons have been clicked before?
Thanks
Put a boolean field in your activity, name it clicked and set it to false on the onCreate method of your first activity, then in the onClick method of your 3 buttons, set it to true,
and in the onClick method of your 4th button check it, if it's true go startActivity, else launch a Toast
You can make the 4th button look disable in "OnCreate" with the function "setEnabled"(may be wrong),
and then just set "setOnClickListener" for the 4th button when you click any of the others.
ps.
Can provide code example if needed.
Why don't you use if statement? You can keep the clicked count data under the first three buttons. Like this;
import java.util.stream.*;
int[] btnMemory = new int[4];
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
btnMemory[0] = 1;
// your code
}
});
after, you can check it with if statement under 4th button;
button4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int sum = IntStream.of(btnMemory).sum();
if(sum >= 3)
// your code
}
});
I have the following problem:
I have a TableLayout along with several TableRows, which are dynamically created.
On the right side of every row I create a button, which should call another activity.
Now I want to pass some information with intent.putExtra(). In this case I want so pass the row number, which is also the first information in the row.
Here is a picture of the current state:
This is how I create the buttons during run-time (in a loop):
Button b1 = new Button (this, null, android.R.attr.buttonStyleSmall);
b1.setId(1000+grButtonId);
b1.setText("Request GR");
b1.setLayoutParams(params);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Some code, taken out for clarity
// See next code snippet
}
});
grButtonId++;
tr.addView(b1);
My idea so far is to use the id of the button (of course), and get the line number by the value of grButtonId.
Now comes my problem, let's have a detailed look at my onClickmethod:
#Override
public void onClick(View view) {
// finished is true, as soon as GRRequest has recieved the data
if(!finished & !dataRequested){
new GRRequest().execute(getIntent().getLongExtra("poNr", 0),(long)view.getId());
b1.setText("Show GR");
Log.d("DataList", detailList.toString());
dataRequested=true;
}
else{
if (dataRequested){
b1.setText("Show GR");
}
Intent intent = new Intent(DataTableCreater.this, GRTableCreater.class);
intent.putExtra("lineNr",view.getId());
intent.putExtra("dataList", detailList);
startActivity(intent);
}
}
When I request my data, the button I clicked on gets set to "Show GR" , as intended. The other buttons stay on "Request GR", this is also fine. But now I want these Buttons to Change to "Show GR" when tapped first and on second tap start the activity.
By now, the buttons change to "Show GR" and directly start the activity.
What would be a solution, to make this work?
Create a boolean Array clickedOnce[] = new boolean[grButtonId+1] one field for every Button.
Then have this
public void onClick(View view) {
if(!finished){
new GRRequest().execute(getIntent().getLongExtra("poNr", 0),(long)view.getId());
b1.setText("Show GR");
Log.d("DataList", detailList.toString());
clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]=true; //sets the clickedOnce for this button to true, substring(1,4) is needed to cancle the leading 1 from the id
}
else{
//Checks, if the button was clicked once
if (!clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]){
b1.setText("Show GR");
clickedOnce[Integer.parseInt(String.valueOf(view.getId()).substring(1,4))]=true;
}
else{
Intent intent = new Intent(DataTableCreater.this, GRTableCreater.class);
intent.putExtra("lineNr",view.getId());
intent.putExtra("dataList", detailList);
startActivity(intent);
}
}
}
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);
}
});