I'm new to Android studio. I don't know much about Android Studio and Java but I'm trying to learn it.
My question is:
Is there any way to change image of image button and picture at the same time when the image button is clicked like it's in the picture i have attached?
Thank you so much!
(https://i.stack.imgur.com/ogxDs.png)
(https://i.stack.imgur.com/WQCl6.png)
you can do this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setBackgroundResource(R.drawable.btn_image);
mImageView.setImageResource(R.drawable.image);
}
});
You just need to listen for Click Event and change the image using
setImageResource()
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.your_image_change);
}
});
I assume you have a ImageView and a ImageButton. You want to change the image of imageView when you press the button and at the same time change the image of ImageButton.
ImageView myImageView = findViewById(R.id.my_image_view);
ImageButton myImageButton = findViewById(R.id.my_image_button);
myImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myImageView.setImageResource(R.drawable.my_imageview_image);
myImageButton.setImageResource(R.drawable.my_imagebutton_image);
}
});
Set a flag if you don't want to change after clicking second time.
Related
How to solve this problem? When I restart activity the array works anew and picture doesnt changes.I want to change the picture to follow from array when I restart activity. This button restarts activity
Button btnNext = (Button) dialog.findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
dialog.dismiss();
finish();
startActivity(getIntent());
}
});
How to make a textview show after the button click how to hide the textview again by clicking on the same button in android java.Should i have to try if.else structure?
You can use a FLAG of some sort to make this happen
int i = 0;
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(i%2==0){
textView.setVisibility(View.INVISIBLE);
}
else{
textView.setVisibility(View.VISIBLE);
}
i++;
}
}
Initially make the textview disable after clicking the button make it enable,again after clicking the button disable the button.
Use textview.setVisibility() to achieve this.
In button.setOnClickListner() method put this code:
if(textview.getVisibility() == View.GONE){
textview.setVisibility(View.Visible);
}else{
textview.setVisibility(View.GONE);
}
and in layout xml set default visibility of textview as you desire that can be visible or gone or invisible
yourButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(textview.getVisibility() == View.GONE)
{
textview.setVisibility(View.Visible);
}
else
{
textview.setVisibility(View.GONE);
}
}
}
Aftermath of this: Image for ImageButton doesn't change as intended
When ImageButton of "ibChamp" is clicked, it opens up champions.xml. In that layout, there is an Imagebutton called "ibAnnie". What I want that to do when clicked is, to change the image of "ibChamp", and to switch to that layout. What happened here is that after "ibAnnie" is clicked, it switches to "create_build_page.xml", and in that is a changed picture of "ibChamp". But that only happens for a split second before changing back to the original, unchanged picture, "create_build_page.xml" layout. When I click the back button, it goes to the "create_build_page.xml" layout with the changed picture, but the buttons do not work. I would gladly provide any additional information required.
I'm still not entirely clear on what you're trying to achieve... but if my understanding is correct, this should get you closer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.champions);
ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);
ibAnnie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View createView = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(createView);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);
img.setTag(R.drawable.ic_launcher); // so you can retrieve it later
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// this will set the imageView of ibAnnie
ibAnnie.setImageView((Integer)view.getTag());
// this will remove the selection view from your layout
((RelativeLayout) findViewById(R.id.layoutChampions)).removeChild(createView);
}
});
// this opens another Activity... you don't want that, at least not for what you seem to be trying to do.
/*try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {
}*/
}
});
}
I load the image programmatically and set onclicklistener but it's not working if I click the image.
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.one);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
How to do this?
First figure out if the imageView is registered to listen to onClick events. Put a toast or a Log.d message to find out if the control is going to the onClick method. If you still have issues, refer to the below link.
To set an ImageView programatically, follow this:
Adding image programmatically to RelativeLayout
You need to set View.onClick.... so replace this code
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
with below
ImgBook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TEST","in onclick");
}
});
try this code..
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.ic_launcher);
LinearLayout lyt=new LinearLayout(this);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Params.setMargins(6, 0, 6, 0);
lyt.addView(ImgBook);
setContentView(lyt);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});
I have a Linear layout initialized.
While running program, i am using following code to add child in this layout dynamically
ImageView image = new ImageView(this);
image.setImageBitmap(imageBitmap);
image.setId(counterOfReceipts);
myLinearlayout.addView(image);
When some one click on this imageView i need to call a function for each. Please tell me how can i do that.
Best Regards
ImageView image = new ImageView(this);
image.setImageResource(R.drawable.app_icon);
image.setId(counterOfReceipts);
myLinearlayout.addView(image);
image.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Activity", String.valueOf(v.getId()));
}
});