OnClick of bitmap - java

can you do an onClick of a Bitmap picture that you have created in the code or do I need to make my "zombie" the background of a texview etc. in order to preform the action on click?

You need an ImageButton like this
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:onClick="functionToCall"
android:src="#drawable/to_top_button"
/>
And then define the function
public void functionToCall(View v) {
...
}

Related

How to add onClick method to ImageView inside a grid layout?

I'm creating a tic-tac-toe like game which contains some circular balls that are dropped when user taps on the rooms where he wants to deploy. I have added those images inside a grid layout but I'm not able to run a dedicated method when a user taps on them. I have assigned the method to onclick property of imageview but the java code isn't being executed.
EDIT: I tried to log something when the grid layout was click and it was logged. So the conclusion is that the grid layout is blocking the click to be registered to the imageview.
Here's my Java code:
public void dropIn(View view) {
ImageView counter = (ImageView) view;
counter.setTranslationY(1000f);
counter.setImageResource(R.drawable.yellow);
counter.animate().translationYBy(1000f).setDuration(300);
}
This is the XML code for the image view:
<ImageView
android:id="#+id/imageView5"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="10dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="20dp"
android:onClick="dropIn"
app:layout_column="1"
app:layout_row="1" />
Here is the XML code for the grid:
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="185dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="186dp"
android:background="#drawable/board"
app:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:rowCount="3">
You can try to do it programmatically instead of assigning the method via XML view.
In first place you need to create a new ImageView and then get the view rendered with findViewById() method. Then, assign the onClickListener with the "dropIn" logic:
ImageView counter = findViewById(R.id.imageView5);
counter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter.setTranslationY(1000f);
counter.setImageResource(R.drawable.yellow);
counter.animate().translationYBy(1000f).setDuration(300);
}
});

imagebutton becomes very large when I choose my avatar

I have an imagebutton that is fixed to the top right of the navigation menu and when you click on it, you could select an image from your android library and make it as your avatar.
However, once you select your picture, the imagebutton instead becomes very large.
Here is how the button is initally: Before Choosing avatar
here is how the button is after choosing the avatar: After choosing avatar
I inserted this piece of code inside the onCreate() method in NavDrawerActivity class
imgButton = (ImageButton) navigationHeaderView.findViewById(R.id.imageButton1);
imgButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, IMAGE_UPLOAD_REQUEST);
}
});
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), getThumbnail("desiredFilename.png"));
roundedBitmapDrawable.setCircular(true);
imgButton.setImageDrawable(roundedBitmapDrawable);
Here is my XML:
<ImageButton
android:layout_gravity="right"
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_button"
android:scaleType="fitXY"
app:srcCompat="#android:drawable/ic_menu_gallery"/>
Try to add this into XML to set fixed size:
android:maxWidth="42dp"
android:maxHeight="42dp"
42dp is just example, set whatever you want
Judging by what you've given us to work with, the image size of the avatar is much larger than the original icon. If you want it to be the exact same size as the icon, you could either downscale the image, or change the size of the ImageButton from wrap_content to a fixed value. :)
Change this:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
To this:
android:layout_width="THE WIDTH OF THE ICON"
android:layout_height="THE HEIGHT OF THE ICON"
EDIT:
Bit of an abstract thought, but may as well try it.
Change this:
<ImageButton
android:layout_gravity="right"
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_button"
android:scaleType="fitXY"
app:srcCompat="#android:drawable/ic_menu_gallery"/>
To this:
<ImageButton
android:layout_gravity="right"
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_button"
app:srcCompat="#android:drawable/ic_menu_gallery"/>

How do I use a custom icon at a PinItButton of their SDK?

I want to use a custom icon for my pinIt button. I keep getting the old layout overwritten when I run my app while in the preview the icon that I prefer is shown.
<com.pinterest.pinit.PinItButton
android:id="#+id/car_info_comment_pintrest"
android:layout_width="#dimen/car_info_button_width"
android:layout_height="wrap_content"
android:background="#drawable/ic_comment_pintrest"
android:drawableTop="#drawable/ic_comment_pintrest"
android:text="100"
android:textColor="#fff" />
I've fixed it with a hacky solution. I've added another button and called the OnClickEvent of the PinIt button and made that buttons visibility: gone.
holder.mPinterestButton = ((PinItButton) holder.content.findViewById(R.id.pin_it));
holder.mPinterestButton.setImageUrl("http://placekitten.com/400/300");
holder.mPinterestButton.setUrl("http://placekitten.com"); // optional
holder.mPinterestButton.setDescription("A place kitten!"); // optional
holder.pinterestButton = ((Button) holder.content.findViewById(R.id.car_info_comment_pinterest));
holder.pinterestButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.mPinterestButton.performClick();
}
});
XML:
<Button
android:id="#+id/car_info_comment_pinterest"
android:layout_width="#dimen/car_info_button_width"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_comment_pinterest"
android:background="#null"
android:text="100"
android:textColor="#fff" />
<com.pinterest.pinit.PinItButton
android:id="#+id/pin_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>

Android, Button don't working at all

This code represent my real issue...
I'm working on an U.I. more complexe.
I have a custom view that need to have a button out of his leftSide. Why? Because i have four of this custom view aside . And those button's view to create some intercalary view !
I have a simple layout that contains a button.
I had to make him out of his layout's parent, with the property clipChildren="false"
But the button don't response to the onClickListener.
I certainly miss something, but what?
The animation click isn't played at all...
Even the Android's click's song isn't played...
The java code have no effect there..
The button's id button2 work.
The button's id button don't work..
Here is my xml code.
<LinearLayout
android:orientation="vertical"
android:background="#0000FF"
android:clipChildren="false"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#FF0"
android:layout_marginLeft="-20dp"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_marginLeft="-80dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:id="#+id/button2"
android:layout_width="80dp"
android:layout_height="80dp" />
</LinearLayout>
and the onCreate Methode:
Button buton1 = (Button) rootView.findViewById(R.id.button);
buton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.wtf("-----", "Click");
Toast.makeText(getActivity(), "button1", Toast.LENGTH_LONG).show();
}
});
Button buton2 = (Button) rootView.findViewById(R.id.button2);
buton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "button2", Toast.LENGTH_LONG).show();
}
});
Try putting the button back inside its parent.
If it works there, have a very long hard think about why you want this button to not live inside its parent. This is a bad idea almost all of the time as it breaks things (both practically and conceptually).
I would suspect the issue is that something else which has a better claim to the space the button is occupying is consuming the touch event.

When user clicks a button weird text appears in inputbox

Here is my xml code for a sample button.
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="25dp"
android:layout_weight="0.166"
android:text="1"
android:onClick="ProcessInput" />
Now in mainActivity.java i wrote this code
public void ProcessInput(View v)
{
Button btn = (Button) v;
inputText.append(btn.toString());
}
The output is shown in the screenshot
You simply convert to string not getting value of button so change from
inputText.append(btn.toString());
to
inputText.append(btn.getText().toString());
You need to use .getText() to get button text. btn.toString() will return id of btn.
Try this
inputText.append(btn.getText().toString());

Categories