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...!
Related
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. :)
I'm a beginner with android.
What I want to do is, loading multiple image icons, like a folder with images needed to be opened. A user will select one and that image should be displayed in full size.
Here is what I've done till now...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.btnChangeImage);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
image.setImageResource(R.drawable.def);
}
});
}
I mean, here the image will be changed, if I add more images in setImageResource, but not like the way I want them to be. What should I do further? Please help...
Merry Christmas and Happy Holidays everyone!
I'm trying to setup a listener on the image icon that appears on the left side of the default title bar, but so far not having any luck.
Here's my Activity's onCreate:
#Override public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_LEFT_ICON);
super.onCreate(savedInstanceState);
findViewById(Window.FEATURE_LEFT_ICON).setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
System.out.println("It works!");
}
});
}
Any suggestions? I'm hoping to not see the answer "it's not possible" :)
There doesn't seem to be an id for the left icon, however for the classic title bar, there is an id available: android.R.id.title Here is a sample Activity using this id. The requestWindowFeature(Window.FEATURE_LEFT_ICON); should force the classic title bar regardless of theme.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
View v = findViewById (android.R.id.title);
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Toast.makeText(MainActivity.this, "Works!", Toast.LENGTH_SHORT).show();
}
});
}
}
Basically, what this does, is it finds the id of the title bar (android.R.id.title) then assigns an onClickListener to it.
This will not work with ActionBars, only classic window title bars.
You should use the ActionBar. Use ActionBarSherlock to have it also for Android versions less than 3.0. To make the Icon clickable, see the ActionBar API Docs (see link below). It's very easy, you just activate the behaviour and then it works like a menu-item with a special item-id.
http://developer.android.com/guide/topics/ui/actionbar.html#Home
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
I want to change the image of a button in my code. I found this can be done in xml:
please check this link
but the image will not stay on after I released the button. I want to click the button
and the button change to a new image. Can this be done?
in onClick method you need to change the Button's image, this way..
public void onClick(View v) {
if(v==buttonName){
buttonName.setBackgroundResource(R.drawable.imageName);
}
}
Assuming an ImageButton...
You can change the background image in an onClick listener, similar to the following:
myButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//set button image
myButton.setImageBitmap(myBitmapFile)
}
});