OnClick on inflated items - android

In my activity.xml I have a LinearLayout. This LinearLayout contains an inflated layout consisting of four ImageViews. Implementation is something like this:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_template);
View view = getLayoutInflater().inflate(R.layout.inflated_layout, linearLayout, false);
linearLayout.addView(view);
I want to implement an action if the user clicks on the image. However, nothing happens when I click on the image. This is my implementation for onclick event:
v.getId() gives me -1 every time I click an image
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("V.getID", String.valueOf(v.getId()));
Log.v("Image 1 id", String.valueOf(R.id.iv_t_1));
switch (v.getId()){
case R.id.iv_t_1: //image 1 clicked
//action
break;
case R.id.iv_t_2: //image 2 clicked
//action
break;
case R.id.iv_t_3: //image 3 clicked
//action
break;
case R.id.iv_t_4: //image 4 clicked
//action
break;
default:
break;
}
}
});
EDIT: The reason i don't want to set listeners on separate images is because the inflated layout is dynamic. It can contain any number from 1 to 4 images. If I have a layout of two images then the fourth imageView listener would return a nullptr exception

You can find those images and check if it's not null before setting onClickListener.
ImageView img1 = (ImageView) view.findViewById(R.id.imageview1);
if (img1 != null) {
img1.setOnClickListener(...);
}
ImageView img2 = (ImageView) view.findViewById(R.id.imageview2);
if (img2 != null) {
img1.setOnClickListener(...);
}
...

v.getId() is giving -1 because you have not set any ID to Root Element in the Layout inflated_layout. You set an ID to Root Element in "inflated_layout" then v.getId() will give you a valid ID.
View view = getLayoutInflater().inflate(R.layout.inflated_layout, linearLayout, false);
linearLayout.addView(view);
when you add click listener to the view you inflated above, you are adding click listener to the entire layout(inflated_layout) and not to the individual imageviews present inside inflated_layout. To add same click listener to all imageviews present inside inflated_layout do the following.
Create a private class in the class where you are inflating the inflated_layout.
private class ClickListener implements View.OnClickListener{
#Override
public void onClick(View v) {
Log.d("test","View Id="+v.getId());
}
}
Then you get a reference to each imageview and set the click listener to each imageview as below.
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.ll_template);
View view=getLayoutInflater().inflate(R.layout.inflated_layout, null);
linearLayout.addView(view);
ImageView iv1=(ImageView) view.findViewById(R.id.iv_t_1);
ImageView iv2=(ImageView) view.findViewById(R.id.iv_t_2);
ImageView iv3=(ImageView) view.findViewById(R.id.iv_t_3);
ImageView iv4=(ImageView) view.findViewById(R.id.iv_t_4);
ClickListener cl=new ClickListener();
iv1.setOnClickListener(cl);
iv2.setOnClickListener(cl);
iv3.setOnClickListener(cl);
iv4.setOnClickListener(cl);

do this,
ImageView img1 = (ImageView) view.findViewById(R.id.imageview1);
img1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v("V.getID", String.valueOf(v.getId()));
Log.v("Image 1 id", String.valueOf(R.id.iv_t_1));
switch (v.getId()){
case R.id.iv_t_1: //image 1 clicked
//action
break;
case R.id.iv_t_2: //image 2 clicked
//action
break;
case R.id.iv_t_3: //image 3 clicked
//action
break;
case R.id.iv_t_4: //image 4 clicked
//action
break;
default:
break;
}
}
});

Related

switching my imageView between two sources

When I click a button it changes my ImageView from pic1 to pic2, I use this...
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
myImageView.setImageResource(R.drawable.pic2);
and it changes my ImageView to pic2 now I want to be able to click the button again and change it back to pic 1 using...
myImageView.setImageResource(R.drawable.pic1);
but I need some way to make a getImageResource so I can run an if statement on which pic is showing and display the other when the button is clicked. For Example if pic 2 is showing it will check which pic is showing and returns pic2 so it knows when the button is clicked to switch it to pic1
Set a flag to identify which image is set.
By default pic1 is selected, add the flag
boolean flag = true;
Then on imageview click listener check for the flag, if the flag is set then set the imageview with pic2 else pic2.
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(flag) {
imageView.setImageResource(R.drawable.pic2);
} else {
imageView.setImageResource(R.drawable.pic1);
}
flag = !flag;
}
});
Try Below code and see if it works for you:
Write this before onCreate: private boolean imageIs = false;
Write this in onCreate:
btnImageChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!imageIs) {
imageV.setImageResource(R.mipmap.ic_launcher);
imageIs = true;
} else {
imageV.setImageResource(R.mipmap.ic_launcher_round);
imageIs = false;
}
}
});
You can add a variable, which will have a value when you click the button like
int x= R.drawable.pic2;
And when you click the button for changing the image to pic2 your code will be like
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
myImageView.setImageResource(R.drawable.pic2);
x=R.drawable.pic2;
And if you want to add pic1 again by clicking the button you can use if loop like
ImageView myImageView = (ImageView) findViewById(R.id.myImageView );
if(x=R.drawable.pic2){
myImageView.setImageResource(R.drawable.pic1);
}
else if(x=R.drawable.pic1){
myImageView.setImageResource(R.drawable.pic2);
x=R.drawable.pic2;
}
else{
myImageView.setImageResource(R.drawable.pic1);
}

How to create a history of pressing ImageButton

I am a novice developer. Please help to create the code to display the history of pressing ImageButton (on Android). I have three ImageButton (IB1, IB2, IB3) and I have 5 empty ImageView (IV1, IV2, IV3, IV4, IV5).
When the user first time touching one of the three ImageButtons (for example IB2), the ImageView (IV1) displays button image from IB2.
When the user clicks a second time and touching one of three buttons again (for example IB1 this time) image of the first pressed button (image from IB2) is moved from IV1 to the IV2, and IV1 gets the image from button which clicked in second time – from IB1. And so on an unlimited number of times, but history shows only the last 5 clicks. Here is the beginning of my code. Thanks in advance to all.
public class MainActivity extends Activity implements View.OnClickListener {
ImageView IV1, IV2, IV3, IV4, IV5;
ImageButton IB1, IB2, IB3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IB1 = (ImageButton) findViewById(R.id.IB_first);
IB2 = (ImageButton) findViewById(R.id.IB_second);
IB3 = (ImageButton) findViewById(R.id.IB_third);
IV1 = (ImageView)findViewById(R.id.ImageView1);
IV2 = (ImageView)findViewById(R.id.ImageView2);
IV3 = (ImageView)findViewById(R.id.ImageView3);
IV4 = (ImageView)findViewById(R.id.ImageView4);
IV5 = (ImageView)findViewById(R.id.ImageView5);
IB1.setOnClickListener(this);
IB2.setOnClickListener(this);
IB3.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.IB1:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.first_image);
break;
case R.id.IB2:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.second_image);
break;
case R.id.IB3:
IV1.setVisibility(View.VISIBLE);
IV1.setImageResource(R.drawable.third_image);
break;
}
}
}
Inside your clicklistner you should pass the drawables to other imageViews
for example:
IV5.setImageDrawable(IV4.getDrawable());
IV4.setImageDrawable(IV3.getDrawable());
IV3.setImageDrawable(IV2.getDrawable());
IV2.setImageDrawable(IV1.getDrawable());
// set your IV1 resource
Happy Coding ;)

How is it possible to know which View is clicked in a layout OnClickListener?

I have a layout which contains some views. I want to set some actions when a user clicks anywhere on the layout, so I have set an OnClickListener for the layout which works as it should. But how can I know which view was clicked?
I want to assign different actions depending on the view which was clicked; or maybe no view was clicked and it was only the layout itself.
So the problem is if I set OnClickListener for one of the views, both OnCLickListeners related to the layout and the view will get activated, but I want only the view action. Thanks in advance.
Other answers are quite abstract and some are incorrect. You can't Override onClick method for an Activity as it doesn't have one (unless of course you implement an OnClickListener).
Furthermore, if you set the listener for the layout the View argument received by the onClick method will always be the layout.
You can either create your own custom layout that intercepts the clicks and check if you clicked on a view, or you can set listeners for all the views.
A somewhat cleaner solution is using a single listener and checking if it's a view that was clicked.
private final String TAG = "MainActivity";
List<View> views = new ArrayList<View>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
TextView tv1 = (TextView) findViewById(R.id.tv1);
TextView tv2 = (TextView) findViewById(R.id.tv2);
TextView tv3 = (TextView) findViewById(R.id.tv3);
views.add(tv1);
views.add(tv2);
views.add(tv3);
View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isView = false;
for (View view : views) {
if (v.equals(view)) {
isView = true;
break;
}
}
if (isView) {
Log.e(TAG, "Click on view");
} else {
Log.e(TAG, "Click on layout");
}
}
};
tv1.setOnClickListener(clickListener);
tv2.setOnClickListener(clickListener);
tv3.setOnClickListener(clickListener);
rLayout.setOnClickListener(clickListener);
}
If you want different actions for different views, just create a listener for each of them.
For example Layout has two views View1 and View2. I assume that you have already inflated them. So you have to set OnClickListiner for each of them
Layout.setOnClickListener(this);
View1.setOnClickListener(this);
View2.setOnClickListener(this);
Then you have to override method:
public void onClick(View v)
{
switch(v.getId())
{
case R.id.id_for_layout:
// do what you want when user click layout
break;
case R.id.id_for_first_view:
// do what you want when user click first view
break;
case R.id.id_for_second_view:
// do what you want when user click second view
break;
}
}
Remember that the method OnClick have one parameter that indicates which View is clicked!
public void onClick(View v) {
switch(v.getId()) {
case R.id.btn1:
// first button
break;
case R.id.btn2:
// second button
break;
}
}
Assign the id to each of your views by android:id="#+id/v1"
then in
onClick(View v)
check for the view like
if(v == findViewById(R.id.v1))
{
// v1 specific action
}
else if(v == findViewById(R.id.v2))
{
// v2 specific action
}
Ok, it's easy.
You have to add an OnClickListener to each view (button, textView and so on), and to the whole layout as well.
In this case, the onClickListeners that will be launched if you click are the view's onClickListener and the layout's onClickListener. So no need for a whole bunch of classes. One will be enough if you try this (do actions depending on the id and don't forget the layout ;) ):
public void onClick(View v) {
switch(v.getId()) {
case R.id.layout:
layout actions here
break;
case R.id.textview:
textview actions here
break;
this onClickListener is for all of your views ;)
There is View v parameter in onClickListner(View v) method :
#Override
protected void onCreate(Bundle savedInstanceState) {
LinearLayout layout=(LinearLayout)findViewById(R.id.linearLayout);
layout.setOnclickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.linearLayout){
//your logic
}
}
for anyone interested you can also log the currently clicked view:
getActivity()
.getWindow()
.getDecorView()
.findViewById(android.R.id.content)
.setOnTouchListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.v("[onClick]", v.getClass().getCanonicalName());
}
return false;
});

Writing onClick event for textviews inside linear layout

I have two linear layouts present inside relativelayout. Each linear layout contains three text view. I want to write onclick event for all text views present in both linear layouts in general. Please advice.
in all TextView add the following attribute
android:onClick="onClick"
dont forget to set id to all TextView too
Then from your code
public void onClick(View v){
switch (v.getId()) {
case R.id.tv1:
// do somethong
break;
default:
break
}
Have a common click listener for all the text views. From the common click listener handle the click events of all the text views by the ID of the text view.
Sample FYR.
findViewById(R.id.textview1_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview2_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview3_id).setOnClickListener(commonClickListener);
private OnClickListener commonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int selectedItemId = v.getId();
switch (selectedItemId) {
case R.id.textview1_id:
// implement your code here.
break;
case R.id.textview2_id:
// implement your code here.
break;
case textview3_id:
// implement your code here.
break;
}
}`

android imageView

I'd like to do such thing.... There is a Relative layout with splash.PNG as a background.... there are five ImageViews: #drawable\1.png, #drawable\2.png, #drawable\3.png, #drawable\4.png and #drawable\5.png...
1-is clickable and visible;
2-5 - are invisible and clickable="false"
by clicking once Imageview 2 becomes visible but unclickable, and then by clicking all 2-5 appears, then by clicking last fifth time 2-5 again becomes invisible....
As for me, such construction works with one invisible ImageView:
final ImageView iv36 = (ImageView) findViewById(R.id.yabl3skr);
iv36.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v33) {
// TODO Auto-generated method stub
final ImageView iv37 = (ImageView)findViewById(R.id.yab3);
iv37.setVisibility(1);
iv37.setClickable(true);
iv37.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v33) {
// TODO Auto-generated method stub
iv37.setVisibility(View.GONE);
iv37.setClickable(false);
}
});
}
});
Need any ideas how to do it in a good way with five imageViews?
I don't know if I fully get it, but what I think you're saying is that you have five ImageViews that all perform conditional logic when clicked. If that's the case, what I normally do is implement single OnClickListener for all of the images. You can set an initial state on all of your images and then do a switch on the id of the View being passed to enable and show which ever views you want to show.
final ImageView iv33 = (ImageView) findViewById(R.id.iv33);
final ImageView iv34 = (ImageView) findViewById(R.id.iv34);
final ImageView iv35 = (ImageView) findViewById(R.id.iv35);
final ImageView iv36 = (ImageView) findViewById(R.id.iv36);
final ImageView iv37 = (ImageView) findViewById(R.id.iv37);
OnClickListener imageClickListener = new OnClickListener() {
public void onClick(View v) {
// Initial state
iv33.setClickable(false);
iv34.setClickable(false);
iv35.setClickable(false);
iv36.setClickable(false);
iv37.setClickable(false);
iv33.setVisibility(View.GONE);
iv34.setVisibility(View.GONE);
iv35.setVisibility(View.GONE);
iv36.setVisibility(View.GONE);
iv37.setVisibility(View.GONE);
switch(v.getId())
{
case R.id.iv33:
// show and hide what you would like...
break;
case R.id.iv34:
// show and hide what you would like...
break;
case R.id.iv35:
// show and hide what you would like...
break;
case R.id.iv36:
// show and hide what you would like...
break;
case R.id.iv37:
// show and hide what you would like...
break;
}
}
};
iv33.setOnClickListener(imageClickListener);
iv34.setOnClickListener(imageClickListener);
iv35.setOnClickListener(imageClickListener);
iv36.setOnClickListener(imageClickListener);
iv37.setOnClickListener(imageClickListener);

Categories

Resources