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()));
}
});
Related
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.
I have a MainActivity with 4 imageviews placed in 4 different card views.
What I would like to is, when clicking on one of these cardviews/imageviews it shows the image in fullscreen in a new activity.
I guess I could create 4 new activities and place a fullscreen imageview in each one of those, referencing to the selected image in MainActivity. But this approach does not seem so smooth.
I would prefer one "imageActivity" and then pass the selected imageview. Could this be done with passing the parameter for the resource id?
There can be a good solution than what I am going to offer but as even I'm learning and if it's just about 4 cardviews, I would've done the following.
Create a public static variable in MainActivity
public static Drawable resId;
set onClickListener for every Imageview like this.
ImageView image1 = (ImageView) findViewById(R.id.image1);
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image1;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
ImageView image2 = (ImageView) findViewById(R.id.image2);
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image2;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
ImageView image3 = (ImageView) findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image3;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
ImageView image4 = (ImageView) findViewById(R.id.image4);
image4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
resId = R.drawable.image4;
Intent intent = new Intent(MainActivity.this,your_fullscreen_activity_name.class);
startActivity(intent);
}
});
or on the cardviews, see here or here.
Create a fullscreenactivity and put its name in those intents.
Then in the java file of this fullscreenactivity, get the reference of layout/imageview and do this.
ViewGroup viewGroup = findViewById(R.id.fullscreenLayout);
viewGroup.setBackgroundDrawable(MainActivity.resId);
OR
ImageView imageView = findViewById(R.id.fullscreenImageview);
imageView .setBackgroundDrawable(MainActivity.resId);
You can use either the imageView or the viewGroup.
And yes, Don't forget to add id of layout in XML file of fullscreenactivity or create a imagview with size same as parent layout.
If anything is confusing/not helpful, I'll help.
I have two java class and two layout for both the class.
Each layout is having one button in it.
Both classes are extending Activity.
Now in first layout I used include tag like this
<include
android:id="#+id/clicked"
layout="#layout/activity_main" />
I can now see two buttons but the second button is not working.
First You have to declare and initialise the include view and then decalre and initialise both buttons using view.findViewById() method as follows:
View includeView = (View)findViewById(R.id.clicked);
Button button1 = (Button)includeView.findViewById(R.id.button1ID); //decalre button like this
Button button2 = (Button)includeView.findViewById(R.id.button2ID);
And then set their onClickListeners
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//code whatever you want to do here
}
});
** EDIT **
Fixed the typo. Should be includeView on the findViewById.
Good explanation though!
I am trying to display a button on my android app but everytime i run the app it crashes. i realise this is because i use setContentView multiple times? I dont understand how it works, and dont understand how i can fix this problem so my button will display. my code is below.
public class MainActivity extends Activity {
Draw draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
draw = new Draw(this);
draw.setBackgroundColor(Color.BLUE);
setContentView(draw);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
setContentView(l);
l.addView(new Draw(this));
//setContentView(R.layout.activity_main);
setUpBlockBtn();
}
private void setUpBlockBtn(){
setContentView(R.layout.activity_main);
Button addBlockButton = (Button)findViewById(R.id.btnBlock);
addBlockButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "you clicked the button");
//finish();
}
});
}
You try to access Button from android xml layout but you do not set this layout in Activity.
Put you button activity_main.xml and use this button in your activity.
Thanks
You can create one more layout and add Draw and Linear layout to that layout.
Something like this.
LinearLayout l1=new LinearLayout(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.addView(draw);
l1.addView(l2) // your linearLayout.
setContentView(l1)
Remember you can't use setContentView more than one time.
There should be top layout which includes subview and other layouts and then you can add that layout to your activity.
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) {
}*/
}
});
}