ImageView setImageResource not working - android

I have a custom class that inherits from RelativeLayout. I have an ImageButton called control_btn that's defined and added to the layout in an init() method. On the onDraw, I have the following code (among other things):
control_btn.setImageResource(R.drawable.play_new);
control_btn.getLayoutParams().width = getPixels(20);
control_btn.getLayoutParams().height = getPixels(20);
control_btn.setX(getPixels(0));
control_btn.setY(getPixels(10));
control_btn.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
control_btn.setAdjustViewBounds(true);
control_btn.setPadding(0, 0, 0, 0);
control_btn.setBackgroundColor(Color.TRANSPARENT);
control_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
control_btn.setImageResource(R.drawable.pause);
Log.d("WTH", "clicked");
}
});
However, the image never changes, despite getting the log entry.
Further, I have another ImageButton with exactly the same code whose image DOES change (also inside the custom class).
Any ideas on how to fix this?

put setImageResource into init() method, then problem will be fixed.

Related

How to load a layout inside another one on click and change button function?

I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}

how to add onClickListener to buttons inside CardLayout

I am using Gabrielemariotti's Cardslib library to implement card layout in my android application. I am using a custom layout for my cards. Below is the code for creating custom cards:
Card card = new Card(getActivity().getApplicationContext(), R.layout.status_card);
card.setTitle("sample title");
I have three buttons at the bottom of my card (like buttons in Facebook android app). I want to set onClickListener for these buttons. But I am not sure how to do that.
Please help me here.
Thanks,
You have to define your layout.
Then create a Card with this layout, and override the setupInnerViewElements method.
Inside this method you can define your OnClickListener on your buttons, and you can access to all card's values.
public class CustomCard extends Card {
/**
* Constructor with a custom inner layout
*
* #param context
*/
public CustomCard(Context context) {
super(context, R.layout.carddemo_mycard_inner_content);
}
#Override
public void setupInnerViewElements(ViewGroup parent, View view) {
//Retrieve button
Button myButton = (Button) view.findViewById(R.id.myButton);
if (myButton != null) {
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Click Listener card=" + getId(),
Toast.LENGTH_LONG).show();
}
});
}
}
}
I have an easy solution for this.
So another way to add onClick listeners, which is a bit easier, is through the XML.
In the xml for the button, you add this line:
android:onClick="methodName"
Where 'methodName' is obviously the name of a method. This will call the method whenever the button is clicked. The next step is obvious - just go into your java activity and create the method that you want called, making sure to take the View as a parameter. So you'd have something like this in your activity class:
public void methodName(View view) {
Log.v("appTag","BUTTON WAS PRESSED");
//whatever you want to do here
}
This is a shortcut to creating a whole onClickListener.
Hope that helps. Good luck :)
EDIT:
Remember, you're passing in a view here, so you can get whatever you want off of that view. Since you commented that you need to get the text off of your card, I'll show you how to do that.
Here is your method for this case:
public void methodName(View view) {
Log.v("appTag","BUTTON WAS PRESSED");
TextView textFromCard = view.findViewById(R.id.THE_ID_YOU_GAVE_YOUR_TEXTVIEW_IN_THE_XML);
String textFromTextView = textFromCard.getText().toString();
//do whatever you want with the string here
}

Detect which child is clicked on click of the parent

I have a layout which contains lots of images. What I have to do is when an image is clicked, I have to show its details. But I don't want to have onClickListeners for all the images. How can I achieve this?
You don't have to have different handlers for all the images. Instead use one handler for all the images. This would make your code cleaner, manageable and solve your problem too.
public void onCreate(Bundle bundle) {
//...
OnClickListener mHandler = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.img1:
//..
break;
case R.id.img2:
//....
break;
}
}
};
ImageButton btn1 = (ImageButton)findViewById(R.id.img1);
ImageButton btn2 = (ImageButton)findViewById(R.id.img2);
//...
btn1.SetOnClickListener(mHandler);
btn2.SetOnClickListener(mHandler);
//...
}
One Listener to rule them all.
Implement onClick() on an object, register it as listener
In onClick(), examine the View object passed as parameter to determine which of the images was clicked. You can do anything from getId() to casting it to (ImageView) and getting the actual image out.
Once you know which image was clicked, do what you will with it.
If you're looking to implement custom behavior for an ImageView (or whatever), and then have multiple instances of that type of view, you should subclass the ImageView and put your listener in there. Then you've got an encapsulated View that implements the custom behavior you want, and if you decide later that you want more or less or them, or to put them in another place, it's easy to move the View and its behavior without ripping apart your Activity.

Perform Click ImageView Android

For an ordinary button I can do this:
myButton.performClick();
and the system understands, that the button was clicked.
Now I have an ImageView. What's the alternative of this function for ImageViews?
Thanks
You can still assign an onClickListener to an image view, since the listener assignment method is a View based method. Now once the listener is added to the ImageView, you may call the onClick(ImageView) method in the listener when ever. Besides that, ImageView also has access to the performClick method that the ButtonView does. You can use the same code across views so long as you have a Listener.
Do you mean something like..
ImageView img = (ImageView) findViewById(R.id.myImageId);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
}
});
Or
In xml:
<ImageView
android:clickable="true"
android:onClick="imageClick"
android:src="#drawable/myImage">
</ImageView>
In code
public class Test extends Activity {
........
........
public void imageClick(View view) {
//Implement image click function
}
You can setOnClickListener() to an ImageView as well. You have to make sure you programmatically imageView.setClickable(true) first, or in XML define android:clickable="true" to your ImageVIew..

Linearlayout >> empty eventlistener method neccessarry to get focus?

I created a drawable (background_row.xml) with a state_focused and a default item.
Now i want to use this drawable to color a linearlayout when selected.
Linearlayout row = new LinearLayout(this);
row.setFocusableInTouchMode(true);
row.setBackgroundResource(R.drawable.background_row);
This doesn't work.
I tryed a lot and finally i found out that it works, when i implement an empty setOnClickListener like this:
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Sorry.. but that makes no sense for me..
Why do i have to implement an empty OnClickListener and why is it working when i do so?

Categories

Resources