I have an activity where the user can select their character. I have three image buttons on the screen and when the user clicks the character they want, I want the game to load another activity that will then just show an image of the character they selected in the previous screen. Basically this is just the forerunner for what I am going to do later. I found some examples on this website for how to accomplish this but I haven't quite gotten it all ironed out.
This is what I have in my character selection activity:
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
intent.putExtra("#drawable/archer", pathToImage);
startActivity(intent);
finish();
}
});
The pathToImage line is throwing an error. What exactly am I supposed to put here?
The LevelOne activity which is supposed to just display the image chosen has this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
String path = getIntent().getStringExtra("imagePath");
Drawable image = Drawable.createFromPath(path);
Character_Chosen.setImageDrawable(image);
}
I'm a bit confused on this section as well. The Character_Chosen entry is the name of the imageview which should house the selected image.
I'm also confused on this line of code:
String path = getIntent().getStringExtra("imagePath");
Does this mean I have to manually enter the image path every time? What if they choose a different image?
Does anyone have a link to an actual working example? Pseudo code doesn't really help me very much when I'm a novice and don't know what needs to stay and what needs to go.
I would take an alternative approach, instead of passing the path to every activity you want to show the image(or say avatar).
I would save it in Global Application Object(by extending Application) either a bitmap loaded into memory or path to image and just access or change it in one place and access that value in all the activities.
First of all convert the resource into id
Button archerButton = (Button) findViewById(R.id.Button_Archer);
archerButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id =getResources().getIdentifier("imagename", "drawable", getPackageName());
Intent intent = new Intent(SelectCharacterActivity.this, LevelOneActivity.class);
intent.putExtra("image_id", id);
startActivity(intent);
finish();
}
});
LevelOne.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.level_one);
int imageId=getIntent().getIntExtra("image_id", 0);
Character_Chosen.setImageResource(imageId)
}
Same way can be done for all the remaining buttons.
Happy Coding :)
I made it running by doing a different approach.
MainActivity.java
What I did is I pass the R.drawable reference in the intent instead using the string path.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnArcher = (Button)findViewById(R.id.button_archer);
btnArcher.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,LevelOne.class);
intent.putExtra("archer_drawable", R.drawable.archer);
startActivity(intent);
}
});
}
On levelOne.java is I used the getIntExtra using the name that I specified in the MainActivity.java to get the R.drawable resource reference of the archer image( you can assign a name whatever you want ). Finally use the integer value that you got from getIntExtra and use it on the the image view by calling the method setImageResource(int resId).
ImageView img;
int drwResource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level_one);
//find the ImageView fron level_one.xml
img = (ImageView)findViewById(R.id.character_image);
drwResource = getIntent().getIntExtra("archer_drawable", -1);
img.setImageResource(drwResource);
}
level_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/character_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
I hope this helps. :)
Related
I'm new to Android development, and to this site!
I have done a few tutorials etc and am working on a project at the moment, and had a good look through other answers to similar questions, but haven't been able to find quite what i'm looking for (but loads of good suggestions!)
I am trying to get buttons on my main screen linking to individual pages. I am using my phone instead of an emulator, but every time i click on a button, the app dies... can you help me?
This is my main Screen code for button1:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//calling the page1 function
page1(view);
}
});
This is the page1 function:
public void page1(View view) {
Intent intent = new Intent(this, Page1.class);
startActivity(intent);
}
Here is the code for the Page1 class file:
public class Page1 extends ActionBarActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_1);
}
}
This is the code for the layout file: (page_1.xml)
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="shannon.white.finalyear.DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
If you need anything else, let me know
Any ideas?
Thanks :)
Your coding looks to be correct.
The next thing to check would be to look inside your AndroidManifest.xml file to ensure you have added the activity to it so the android OS knows it exists. You add it like so:
<activity android:name="Page1" />
If your activity resides in a different package then the one declared inside the manifest file, then you need to specify the full package inside the "name" like so:
<activity android:name="some.other.package.name.Page1" />
Thats about all i can say from the provided code. If you are simply starting another activity which is Page1.class then your code looks correct and you might be missing the manifest declaration as i stated above.
Try moving this following code
// Declaring and defining the buttons used
Button student1 = (Button) findViewById(R.id.button1);
// Setting the onClickListener for button1
student1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
}
underneath your page1 function so your code looks like this:
public void page1(View view) {
Button student1 = (Button) findViewById(R.id.button1); // Declaring and defining the buttons used
student1.setOnClickListener(new View.OnClickListener() { // Setting the onClickListener for button1
#Override
public void onClick(View v) {
startActivity(intent);
Intent intent = new Intent(this, Page1.class);
}
}
}
and your onCreate look like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page1(view); // NOTE I'm now declaring it on the onCreate instead of onClick
}
If that doesn't help, well your code still looks cleaner. Could just be my OCD though...
This is a very good tutorial by Mkyong on how to achieve what you are trying to do. If no other answers help, restarting using this tutorial will likely help you succeed. On multiple occasions I've tested his code and its worked.
I'm creating an camera app here. I've managed to click an image and display in an ImageView. I have a ImageButton on whose click takes you to another page. On the other page I want to display that image with a set of scrollable images like on Instagram. But I cant load that image on the other xml file.
Help Please.
Code:
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_select);
// Image clicked...
ImageView ivPic = (ImageView) findViewById (R.id.ivPic);
// Image button to go to next page...
ImageButton ib = (ImageButton) findViewById(R.id.ibNext);
// Not sure if this is right.....
Drawable iv = ivPic.getDrawable();
ib.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.rating.bar.EFFECTS"));
}
});
}}
In the other class I created this classes variable but I cant use iv(which holds the drawable of the image view). It only accepts integers I think. What should I do?
Read the URI/Path of the image in your imagebutton and add to the extras of the intent calling the next activity and read the extras in the next activity.
I managed to have a button and then when I click on it, I go to a new activity that is called "TUTORIALONE"
and then I want to display some text in this new activity
so I have something like this
Button b = (Button) findViewById(R.id.tutorial1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("my.android.TUTORIALONE"));
TextView tv = (TextView)findViewById(R.id.tutorial1);
tv.setText("this is some text);
}
});
the problem is that it first displays the text on my button, and then it shows me the new activity, how would I achieve displaying the text on the new activity?
thanks in advance
In your TUTORIALONE activity you probably have an associated xml file for displaying content. Perhaps it iss set something like this
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.id.TUTORIALONE);
}
In the layout xml file for TUTORIALONE just add something like this
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
EDIT
To change the text of this TextView, do something like this in your TUTORIALONE activity.
protected void onStart()
{
super.onStart();
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("this string is set dynamically from java code");
}
Note that the id here (R.id.text) is the same as in the xml file ("#+id/text")
In the onCreate() of the new Activity, you can create a Button, set the text and then call setContentView() in order to show it.
In case you want to show to this Activity a string from the current Activity, you can pass this String an an Intent extra and then recover it to the new Activity.
Hope this helps!
I'm new to android and am making a simple app. I'm trying to change the image (in an imageview) on a button click.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frown = (ImageView)findViewById(R.id.imageView1);
}
public void action(View view)
{
Toast.makeText(getApplicationContext(), buttontest, Toast.LENGTH_SHORT).show();
frown.setImageResource(R.drawable.chimpy);
}
"action" is being called via XML with the "android:onClick"[insert method here]" for my button
The button works fine and I get my toast, but the image stays the same.
Try changing the drawable to something standard e.g. android.R.drawable.btn_default.
Does it chnage Now?
I am sure you are having some issues with R.drawable.chimpy.
You must use .png image and you can go with the following code snippet:
frown.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
frown.setBackgroundDrawable(R.id.chimpy);
}
});
If it is not working, just tell me...!
Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView