I have a button and I want to pro grammatically add a picture of a green tick to the top right button after it has been selected, how would one do that?
First call the relative layout family and using LayoutPram update the position of the picture
ImageView imageView = (ImageView)findViewById(R.id.theImage);
Button myBtn = (Button)findViewById(R.id.myButton);
myBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
imageView.setLayoutParams(params);
}
});
Try this!
Use ImageButton and onClick swap out the image
Its a vague answer but your question is also vague
Related
Add View dynamically in LinearLayout and after adding View change any View background on the click button.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
ImageView img1 = new ImageView(this);
img1.setLayoutParams(layoutParams);
img1.setImageBitmap(icon);
llLayout.addView(img1);
ImageView img2 = new ImageView(this);
img2.setLayoutParams(layoutParams);
img2.setImageBitmap(icon);
llLayout.addView(img2);
ImageView img3 = new ImageView(this);
img3.setLayoutParams(layoutParams);
img3.setImageBitmap(icon);
llLayout.addView(img3);
On button, click change all imageview background or particular ImageView.
Note : llLayout is my linear layout this layout adding in XML
when you are adding imageView into linear layout, at that time you are setImageBitmap to imageView.
if you want to reset Image to Imageview, you should use img1.setImageResource
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//use anyone as your requirement
img1.setBackgroundResource(); // for set background resource from drawable folder(for src)
img1.setBackground();// for set background(Drawable)(for background)
img1.setBackgroundColor(); //for set background color(for background)
}
});
I am not sure if I understand the question correctly, but If you mean change specific image background with onClick event on a button .. If you have the image reference change it .. but if you mean you are adding the image view dynamically like inside for loop and you don't have the reference you can create Arraylist and add the added images into it then loop on this arraylist to change all the images background or filter the specific image you want to change
case R.id.btn_sound:
if (sp.getBoolean("mute", false)) {
ed.putBoolean("mute", false);
mp.setVolume(0.2f, 0.2f);
((Button) findViewById(R.id.btn_sound)).setText(getString(R.string.btn_mute));
} else {
ed.putBoolean("mute", true);
mp.setVolume(0, 0);
((Button) findViewById(R.id.btn_sound)).setText(getString(R.string.btn_sound));
}
ed.commit();
break;
Instead of text
setText(getString(R.string.btn_sound))
I want to show image when I click on a button
Use button and set your image as backgroundResource with this method : Button.setBackgroundResource(yourResID)
Use ImageButton and set your image as ImageResource with this method :
ImageButton.setImageResource(yourResID)
Note : Even you can use right or left drawable for your button beside your text if you want keep both text and image in your button.
Just try setBackground() method. Or use ImageButton and then you can use setImageResource() method.
I want to add an ImageView within the onClick() of the Button. But the ImageView should be aligned to the centre of the button.
How can I do this pragmatically?
I'm creating a new instance of the ImageView to be added on top of the View that called it and it's position is equivalent of the View that called it.
How can I get the X and Y coordinates?
EDIT:
#Override
public void onClick(final View v) {
if(v.getWidth() > v.getHeight()) size = v.getHeight();
if(v.getWidth() < v.getHeight()) size = v.getWidth();
RelativeLayout.LayoutParams button = new RelativeLayout.LayoutParams(size,size);
buttonc = new ImageView(getApplicationContext());
buttonc.setBackgroundResource(R.drawable.round);
layout.addView(buttonc,button);
I'm using the above code to attempt to pragmatically set the size of the ImageView to that of the button, and it's working.
As for the coordinates, I still haven't figured it out.
To add clarity: I'm attempting to create a rounded ripple effect over the view that calls it.
I suggest a better solution create an image view before hand place it in the xml as you see fit,at first set its visibility to false .
On click switch its visibilty to true, and the image will appear
This solution is easier than what you are searching.
Feel free to ask questions
to add Imageview
#Override
public void onClick(final View v) {
//LinearLayout
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView
ImageView imageView = new ImageView(this);
//image resource
imageView.setImageResource(R.drawable.specs);
//image position
imageView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
}
I'm having a problem with this code. I need to dynamically add buttons to my layout. This code works fine, with one exception. The second button sits on top of the first. This must have something to do with LayoutParams, but I'm not sure what.
private void buttonmaker (Button button)
{
RelativeLayout.LayoutParams rlayout = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rlayout.addRule(RelativeLayout.CENTER_VERTICAL);
rlayout.addRule(RelativeLayout.ALIGN_LEFT);
rlayout.width = 100;
button.setId(Atom.count);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int id = v.getId();
atoms[id].getname();
TextView textview = (TextView)findViewById(R.id.textView2);
textview.setText(textview.getText()+String.valueOf(atoms[id].getname()));
}
});
if (Atom.count > 1) rlayout.addRule(RelativeLayout.RIGHT_OF,Atom.count-1); else rlayout.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
button.setLayoutParams(rlayout);
RelativeLayout v = (RelativeLayout) findViewById(R.id.rlayout);
v.addView(button);
}
the problem is you set de button in the relativelayout, in this component the objects are being added one above the other, you try create linearlayout global with orientation vertical or horizontal depending on what you want and added buttons, and its all
I'm trying to implement a ExpandableListView in java that when click deployment space elements inside, not the typical wish list allows choosing an option. What I try to do is basically something like this
One can see that the spinner "wraps" to have items and does not have a list, Would I could help a little?
You can use ExpandableListView .. check this tutorial http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/
You can easily create one on your own. Just create a Button and assign a Layout below. When the Button gets clicked set the View of you Layout to Visible and otherwise to Invisible. Additionaly you can set up and down arrows to the right of your Button.
final Button button = (Button) findViewById(R.id.button);
final LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout);
linearLayout.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (linearLayout.getVisibility() == View.VISIBLE) {
linearLayout.setVisibility(View.GONE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.expand, 0);
} else {
linearLayout.setVisibility(View.VISIBLE);
button.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.collapse, 0);
}
}
});