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);
Related
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;
}
}
});
Just wondering can anyone solve this problem about adding listeners to imagebuttons at runtime. I'm presuming it has got something to do with the "this" parameter passed to setOnClickListener as we are already in a onClickListener.
My fragment implements onclicklistener. The onClick methods work for imagebuttons known at compile time, just not for the ones that are defined after inflating a prompt view. What seems to happen is the prompt layout seems to be recreated and added onto the back stack.
Basically the onclicklistener for mWhatsappshare,mEmail share does not react as I might expect. I put a stackoverflow error in the onclick method previously and when I clicked these imagebuttons my application did not crash. This means that the listener is in fact not registered (or at least not correctly) at
mwhatsapp.setonclicklistener(this)
By the way, I do not want to set separate listener like mWhatsapp.setonclicklistner(new View.onClickListener()){ for each imagebutton as it is too cumbersome and I want each listener to be handled in onclick().
Thank you
mOtherShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeColorFilters();
mOtherShare.setImageResource(R.drawable.other_pill_pressed);
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.share_prompt,null);
mWhatsappShare = (ImageButton) dialoglayout.findViewById(R.id.ib_whatsapp_share);
mEmailShare = (ImageButton) dialoglayout.findViewById(R.id.ib_email_share);
mFlickrShare = (ImageButton) dialoglayout.findViewById(R.id.ib_flickr_share);
mTumblrShare = (ImageButton) dialoglayout.findViewById(R.id.ib_tumblr_share);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout);
dlg = builder.show();
mWhatsappShare.setOnClickListener(this);
mEmailShare.setOnClickListener(this);
mFlickrShare.setOnClickListener(this);
mTumblrShare.setOnClickListener(this);
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ib_whatsapp_share:
sendIntent("com.whatsapp");
break;
case R.id.ib_email_share:
sendIntent("android.email");
break;
case R.id.ib_flickr_share:
sendIntent("com.yahoo.mobile.client.android.flickr");
break;
case R.id.ib_tumblr_share:
sendIntent("com.tumblr");
break;
}
}
``
On this block of code:
mWhatsappShare.setOnClickListener(this);
mEmailShare.setOnClickListener(this);
mFlickrShare.setOnClickListener(this);
mTumblrShare.setOnClickListener(this);
this is referring to the new OnClickListener you are creating - mOtherShare.setOnClickListener(new View.OnClickListener() and NOT your fragment's OnClickListener
To use the outer onClick, change this to YourFragment.this
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;
});
I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }
I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).
All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.
Hope that all makes sense.
UPDATE:
I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);
//set the title of the popup
TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
titletext.setText(activityName);
if (activityName.equals("Root"))
{
switch (arg0.getId())
{
case R.id.color:
//Rename the string so to get the value from the string xml
String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
break;
case R.id.polarity:
//Rename the string so to get the value from the string xml
String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
//polarityDesc.setText(activityName);
polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
break;
}
}
I think
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
should be
Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
Button btnOpenPopupSecond = (Button) findViewById(R.id.color);
you should declare different different button for diffrerent findviewbyid
also in my eclipse it is not accepting
btnOpenPopup.setOnClickListener(new Button.OnClickListener()
instead it works with
btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
and you need to provide more clear view of what you want to perform
new thoughts,try doing this:
btnOpenPopupFirst.setOnClickListener(this);
btnOpenPopupSecond.setOnClickListener(this);
then option will come on both the above code lines
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
choose this
let MainActivity implement OnClickListener
then this option will come
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
choose
add unimplemented method
now
#Override
public void onClick(View v)
will be created
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.polarity:
Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
break;
case R.id.color:
Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
default:
break;
}
}
And post your views after implementing this way.
int[] id={R.id.button1,R.id.button2};
Button b=(Button)findViewById(id[i]);
The onClick method in Button.OnClickListener has a View parameter... you can call getId() on that view to get the id of that button that was clicked on.
It doesn't make too much sense to me. If what you really want is this:
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
All you need to do is set your value in the onClick(View view) method of your OnClickListener
public void onClick(View view) {
btnOpenPopup = (Button)view;
}
I am trying to change the background image of a button that is clicked. The button whose image I am trying to toggle is the same button that is clicked. I ultimately want the program to test the current background image and change it to the other picture given the result of the test.
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (testButton.equals(getResources().getDrawable(R.drawable.fakepicture))) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
}
}//end void onClick
});//end test button on click listener
try
testButton.getBackground().equals(getResources().getDrawable(R.drawable.fakepicture));
However ToggleButton might suit your case better.
As others have said, your equals method is comparing the button itself with the image, but you need to compare the background drawables.
I recommend loading the images drawables you want to use and then using their references later to make things more clear, something like this:
final Drawable first = getResources().getDrawable(
android.R.drawable.arrow_up_float);
final Drawable second = getResources().getDrawable(
android.R.drawable.arrow_down_float);
final Button testButton = (Button) findViewById(R.id.toggleButton);
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (testButton.getBackground().equals(first)) {
testButton.setBackgroundDrawable(second);
} else {
testButton.setBackgroundDrawable(first);
}
}
});
as the other friends answered , it is preferable to use the ToggleButton in Android ,
and in your case, if you want to keep your code , so your method should be like this :
final Button testButton = (Button) findViewById(R.id.buttonTestButton);
int status = 0;//GLOBAL VARIABLE : the status of the Button ( 0 or 1 )
testButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//toggle picture
if (status == 0) {
testButton.setBackgroundResource(R.drawable.alternatepicture);
status=1 ; // change the status to 1 so the at the second clic , the else will be executed
}
else {
testButton.setBackgroundResource(R.drawable.fakpicture);
status =0;//change the status to 0 so the at the second clic , the if will be executed
}
}//end void onClick
});//end test button on click listener
You can simply use ToggleButton: Android ToggleButton and use StateList for the changing of the background: StateList using the check attribute.
You can use Buttons or Image buttons..
private ImageButton mod1,mod2;
mod1 = (ImageButton) findViewById(R.id.mod1);
mod2 = (ImageButton) findViewById(R.id.mod2);
mod1.setOnClickListener(this);
mod2.setOnClickListener(this);
public void onClick(View v) {
mod1.getDrawable().clearColorFilter();
mod2.getDrawable().clearColorFilter();
switch (v.getId()) {
case R.id.mod1:
mod1.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
case R.id.mod2:
mod2.getDrawable().setColorFilter(0xfff47521,PorterDuff.Mode.SRC_ATOP);
break;
}
}