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
Related
I created an activity on android studio and I have put there something like 20 ImageButtons. I want to use it as on each click on an image it will move to a new activity. All of the Image Buttons are working on the same principle, my app is a game, and each image represents a level. I want to build one function that will be used on all buttons and will move the user to a new activity according to the data(the properties of the image button) and use that data on the new activity. Every level has its own activity and the main activity is the menu of the game.
Below is my code:
public ImageButton beatsCall; public void Beats(){ beatsCall=(ImageButton)findViewById(R.id.beats); beatsCall.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { Intent toy = new Intent(Levels.this,Beats.class); startActivity(toy); } }); }
You need to provide more information and code. However, you may want to try set a distinct onClickListener and then set all the imageButtons to that listener that will perform an action depending on the button clicked. For example, say you have 4 imageButtons and you want to perform a different action (in your case, start a new activity) for each different button click.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//Start activity 1 here, for example
Intent intent = new Intent(this, YourNewActivity1.class);
String message = v.getId().toString;
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
break;
case R.id.textView2:
//Start activity 2 here
break;
case R.id.textView3:
//Start activity 3 here
break;
case R.id.textView4:
//Start activity 4 here
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
This is assuming you have the imageButtons set up in your layout file and you have them initialized in your activity.
In your new activity, you can get the message as such:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if (some condition with message){
do something
}
You may also check out this documentation for further information regarding intents.
Something like this? In your xml make your images clickable and give them ID's like this...
<ImageView
android:id="#+id/level_1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
/>
Then call a function like this in your Activity's onCreate
private void setupButtons() {
findViewById(R.id.level_1_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelOne.class));
}
});
findViewById(R.id.level_2_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelTwo.class));
}
});
}
You could assign a tag via android:tag to each of your views and then use your single listener to switch on the view's tag to branch the behavior you want.
I have used an example code from the following site:
http://www.journaldev.com/10096/android-viewpager-example-tutorial
It works but I need to add another page where there is a textview one can click to go to one of the pages (for eg. BLUE page). I have put the TextView in the xml file with the 2 lines:
android:clickable="true"
android:onClick="click"
I have also added the following incomplete method in the MainActivity.
public void click(View view) {
switch (view.getId()) {
case R.id.change_page:
//what do I need to add in here?
}
}
I am not sure I am setting this up the right way. Any advice will be helpful.
case R.id.change_page :
mViewPager.setCurrentPage(yourpageindex);
break;
case: R.id.another_text view:
//go to another page
create another activity, and separate layout file, and use an explicit intent in the main activity,
in the new layout file , use the desired ViewPager
probably this may work .
as #hash-set suggested. setCurrentPage(yourpageindex); will do the trick.
all you need to know is what is index value of your BLUE page. and set it in place of yourpageindex.
for Example : let's assume index of Your Blue page is "1".
public void click(View view) {
switch (view.getId()) {
case R.id.change_page:
viewPager.setCurrentItem(1);
}
}
Can someone help me with the code for
-if i click on button it should display a full screen image then again if i click on button on 2nd page it should do the same.
i want to know how to connect different pages/activities,when you click a button it should display some image and then again clickin on that page will display another image and so on,
images should be full screen
In your activity after setting content view use this code to start your image activity.
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getActivity(),
ImageActivity.class);
startActivity(intent);
}
});
Now put a ImageSwitcher or Image view and set a touch or click listener to change Images as per your logic.
Let's say you have two activities : MyActivity1 and My Activity2
In MyActivity1 class,create a button and on Button's click listener call MyActivity2.class using startActivity(new Intent(MyActivity1.this,MyActivity.class))
MyActivity2 class will contain one ImageView that will have its width and height as "fillparent" value in xml.
You can continue this process for as many activities you want.
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);
}
});
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?