Changing image dynamically in an ImageButton - android

XML
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton1"
android:src="#drawable/image1"
android:onClick="buttonClick"
/>
JAVA
--------------------
public void buttonClick(View v)
{
Button aButton = (Button)v;
aButton.setBackgroundResource(R.drawable.image2);
}
Here's what I've tried so far with no luck...
I want to be able to click the button and change the image to image2, there's also going to be other images i'll change it to based off of other variables. I'm just really stuck.. I'll continue looking at other questions and if I find an answer I'll post it here.

Your buttonClick() needs fixing:
public void buttonClick(View v)
{
ImageButton aButton = (ImageButton)v;
aButton.setImageResource(R.drawable.image2);
}
the View is an ImageButton, not a Button. The src attribute is updated via setImageResource, not setBackgroundResource.

Related

Changing imagebutton image(from gallery) with longclick

I've searched the forums, but not have found any specific or understandable answers for my problem.
I'd like to change my Imagebutton image to a picture, selected from the gallery. Prefferrably the image should stay changed after closing the application.
My XML for the button is here:
<ImageButton
android:id="#+id/eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:adjustViewBounds="true"
android:background="#drawable/eat"
android:clickable="true"
android:longClickable="true"
android:scaleType="fitCenter" />
The java code for playing the sound is here with the OnClick method.
ImageButton eat = (ImageButton) findViewById (R.id.eat);
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
I would like to add the OnLongClick method here too, (since to OnClick is allready taken and the image replacing should be a little different), but havent found the right way. Can You please guide me a little bit?
You need to return true from image's onLongClickListener.
Like this:
eat.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//do something
return true;
}
});
eat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp1.start();
}
});
This will not cause the image's onClickListener to be called as it means that the action has already been handled in longClickListener.

Android - Using Buttons With Single Layout

I have created an Activity that uses the ViewFlipper to Switch between Different elements. Each element represents an Item in a store. I would like to add a "Buy" Button to each View. I am however not sure how to do this, since all the views use the default layout i have created. I have added the information like the Price of the Item etc Programmatically. So i am uncertain how to add a listener to the button, since they will all refer to the same button in the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/credit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="220dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="104dp" />
<TextView
android:id="#+id/credit_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/credit_button"
android:layout_alignTop="#+id/credit_button"
android:layout_marginRight="22dp"
android:layout_marginTop="21dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:typeface="normal" />
<TextView
android:id="#+id/credit_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/credit_button"
android:layout_alignRight="#+id/credit_type"
android:layout_centerVertical="true"
android:layout_marginBottom="30dp"
android:layout_marginLeft="15dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
As you can see, the Button id is "Credit Button". So now to be able to differentiate between the different store items' buttons what would i have to do?
Note, i am adding store items dynamically as well, so i cant simply create all the views separately using xml.
OK HERE IS THE UPDATED ANSWER. I used everyones responses below to fix teh issue. So Thank you all :)
// PerkView
View PerkView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(PerkView);
Button perkButton = (Button)
PerkView.findViewById(R.id.StoreCatItem);
// TitleView
View TitleView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(TitleView);
Button titleButton = (Button)
TitleView.findViewById(R.id.StoreCatItem);
// ProfileView
View ProfileView = View.inflate(this, R.layout.store_category, null);
viewFlipper.addView(ProfileView);
Button profileButton = (Button)
ProfileView.findViewById(R.id.StoreCatItem);
I simply Created Multiple Views Programmatically, and then retrieved the buttons from those views afterwards. I then added the listeners to the buttons as follows:
perkButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Perks.class);
i.putExtra("player", player);
startActivity(i);
}
});
titleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getBaseContext(), Titles.class);
i.putExtra("player", player);
startActivity(i);
}
});
Thanks A lot :)
There are two ways you can handle the button click distinctly,
At the time of inflating layout for each store, add the button listener with onClick() method there only.
Or you can assign some uniqueId or flag to each store and then handle button click of store by this uniqueID.
Usually you solve this by following stack:
fragment -has-> list -has-> adapter -has-> list of items
Normally if you present one item per screen and you want to swipe between you should use ViewPager with FragmentStatePagerAdapter (allows removing items from ViewPager if needed).
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
For more items in one screen use ListView with BaseAdapter as it allows better control over item view and ListView will recycle views as you scroll or fling.
http://developer.android.com/guide/topics/ui/layout/listview.html
Set unique tag for each store button, in code you can differentiate with respect to tags.Tag is just piece of information you want set for any view, u can use it.Implement on click listener in your activity and then set that to all buttons, so all buttons click run through same code where you can easily diffrentiate between ur store buttons with respect to tags.
like below how you can set and get tag
// for setting tag
Button button = (Button) findViewById(R.id.button1);
button.setTag("unique_tag");
// get tag and then differentiate with the unique_tg
button.getTag();
You can easily create your button programmatically like so :
Button b = new Button(this); // where this = your context
b.setText("Buy");
// b."other attribute" = "other value";
b.setTag("Awesome blue shirt");
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Button buy = (Button) v;
String article = b.getTag();
// do some stuff
}
});
container.addView(b);
You can identify the buy button using its tag, as seen here.

achartengine custom zoom buttons

Is there an easy way to use custom images for Zoom Buttons?
I'd like to use default setZoomButtonsVisible functions to manage
show/hide buttons.
How can I override those buttons?
I'd like to use icons from my res/drawable (drawable-hdpi, drawable-ldpi ..)
to make sure images will be good looking on all screens.
You can hide original zoom buttons and enable external zoom:
private XYMultipleSeriesRenderer mRenderer; //or any of other renderer
mRenderer.setZoomButtonsVisible(false);
mRenderer.setExternalZoomEnabled(true);
//then add click events tot he imagebuttons on the view
//mChartView --> private GraphicalView mChartView;
ImageButton btnZoomIn= (ImageButton) findViewById(R.id.btnZoomIn);
btnZoomIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mChartView.zoomIn();
}
});
ImageButton btnZoomOut = (ImageButton) findViewById(R.id.btnZoomOut );
btnZoomOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mChartView.zoomOut();
}
});
Hope it helps.
The only problem is that some strange think is happening on the click. I posted problem here and also as an issue.
Hope somebody will find an answer.
Toni
I'm using almost the same way I think. I've got:
renderer.setZoomEnabled(true);
renderer.setExternalZoomEnabled(true);
renderer.setApplyBackgroundColor(true);
And I'm using .xml file for my buttons layout. I'm not using <ImageButton> in my code. I'm using <Button> with background. And in this way these buttons are images. My code for button:
<Button
android:id="#+id/zoomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#drawable/action_zoomin"
android:hapticFeedbackEnabled="true"
android:layout_marginRight="15dp">
</Button>

How do you incorporate TextSwitcher on top of a Button?

I wanted the text on my buttons to switch every time that I click it. The only way I know how to switch text is Textswitcher, but I can't seem to find a way to use it with the buttons. I'm sure this is a simple answer and appreciate any help!
XML file portion...
Button
android:id="#+id/Button_A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="#dimen/help_text_size"
android:minWidth="10px"
Java:
Button alphaButton = (Button) findViewById(R.id.Button_A);
alphaButton.setText(mGameSettings.getString(GAME_PREFERENCES_RIGHT, ""));
alphaButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
handleAnswerAndShowNextQuestion("a");
}
});
I'm pretty sure you should just be able to call Button#setText() in the onClick listener

How to Change a Button's Icon Programmatically?

I already have the button:
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:drawableLeft="#drawable/empty"
android:id="#+id/buttonMyText"
android:text=" myText"
android:textSize="20px"
android:gravity="left">
</Button>
I have the "empty" icon show on the button when the program starts.
What I want to do is change the button's icon automatically from my code (low, medium and high) based on user inputs
I tried:
Button myButton = bla... bla... bla...
But I cant figure out
myButton.(what?)
If you check the docs, you'll see the code equivalent for each XML attribute.
See here: http://developer.android.com/reference/android/widget/Button.html
Searching for drawableLeft shows:
android:drawableLeft:
setCompoundDrawablesWithIntrinsicBounds(Drawable,Drawable,Drawable,Drawable)
if you want to change icon at button click event then try this code...
buttonMyText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
buttonMyText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ImageNameHere, 0, 0, 0);
buttonMyText.setTextColor(Color.BLACK);
}
});

Categories

Resources