OnClicklistener not working - java

I load the image programmatically and set onclicklistener but it's not working if I click the image.
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.one);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
How to do this?

First figure out if the imageView is registered to listen to onClick events. Put a toast or a Log.d message to find out if the control is going to the onClick method. If you still have issues, refer to the below link.
To set an ImageView programatically, follow this:
Adding image programmatically to RelativeLayout

You need to set View.onClick.... so replace this code
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
with below
ImgBook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TEST","in onclick");
}
});

try this code..
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.ic_launcher);
LinearLayout lyt=new LinearLayout(this);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Params.setMargins(6, 0, 6, 0);
lyt.addView(ImgBook);
setContentView(lyt);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});

Related

Image button onclick change image

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.

Click on Imageview and open fullscreen-image in new Activity

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.

Only click Checkbox in clickable Relative Layout

I'm trying to design a layout with a checkbox inside of a relative layout. The relative layout is clickable and works as it is supposed to, but when the checkbox is click, it runs the checkbox code, and the relative view code.
Is there anyway to ignore the relative layout's OnClickListener while running the checkbox?
RelativeLayout holder = new RelativeLayout(context);
contactHolder.setId(View.generateViewId());
TextView name = new TextView(context);
final CheckBox checkBox = new CheckBox(context);
checkBox.setId(View.generateViewId());
name.setText(contact.getName());
name.setId(View.generateViewId());
holder.addView(name);
holder.addView(checkBox);
checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
holder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!checkBox.isChecked()) {
checkBox.setChecked(true);
}
else if (checkBox.isChecked()){
checkBox.setChecked(false);
}
}
});
userView.addView(holder);
}
use this in Relativelayout
android:clickable="true"
Remove the listener from the check box. Then change the logic in RelativeLayout's listener to act based on the state of the check box.

Toast.. on android

Hello can you please help me.. I would like to add toast on my code.. But I dont know where to put it.. I just want toast to appear after pressing button..
public class Question1 extends Fragment{
RadioButton q1a2;
Button btn1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.question1, null);
return v;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q1a2 = (RadioButton)getView().findViewById(R.id.q1a2);
btn1 = (Button)getView().findViewById(R.id.btnq1);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
if (q1a2.isChecked()){
editor.putInt("answer_value", 1);
} else {
editor.putInt("answer_value", 0);
}
editor.commit();
}
});
}
}
Inside
onClick(View v){
// some else
// showing toast
Toast.makeText(getApplicationContext(), "Message", Toast.LENGTH_SHORT).show();
}
On click display toast
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Toast.makeText(getActivity(),"Button pressed",Toast.LENGHT_SHORT).show();
...//rest of the code
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
To Display Toast
Do like this on your onClick of the button listener
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
/* here ->*/ Toast.makeText(getApplicationContext(),"clicked",Toast.LENGTH_LONG).show();
}
You can very easily add a Toast inside the OnClickListener. When the listener is invoked, the code inside onClick() is executed, so you could modify this to include your toast:
btn1.setOnClickListener(new OnClickListener(){
public void onClick(View v){
SharedPreferences.Editor editor = app_preferences.edit();
Toast.makeText(getActivity(), "onClick() executed", Toast.LENGTH_SHORT).show();
...
}
});
Bear in mind that Toast.makeText() simply creates a new Toast object. Invoking show() on that object will cause it to appear. The duration of the toast can be controlled using Toast.LENGTH_SHORT or Toast.LENGTH_LONG.
I'd suggest getting a little more acquainted with Android by understanding how events and listeners work.

how to give id for Button By Dynamic Creations in Android?

I am trying to create a button by Dynamically and also i created .Now i want to write the function for click Event So i need the id for Button.I Dont know How to Create id for button Dynamically.Guide me
Thanks in Advance
Here My coding
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
sv.addView(ll);
Button main=new Button(this);
CharSequence value="Main +";
main.setText(value);
ll.addView(main);
}
this.setContentView(sv);
}
You can set id of any controls by
btn.setId(integer value) at runtime.
If you don't want to set id then there is no issue
Also when you create new view then you have to set its layout parameters(Height, Width)
for example
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
So whole process is like
Button btn = new Button(Context);
btn.setId(1);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setText("Dynamic button");
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(),"Dynamic button is clicked", 3000).show();
}
});
Try this code.
Button text = new Button(this);
text.setId(1);
text.setText("text here");
ll.addView(text);
text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId(); // you get ID of your dynamic button
Toast.makeText(getApplicationContext(), "Dynamic textview!", Toast.LENGTH_SHORT).show();
}
});
In that "ll" is a Layout and that add button.
after that you use anytime for click execute that clickListener code.
you can simply use this
Button main= new Button(this);
main.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
and there is main.setId(pass here int value); to set id but i think you will not need it

Categories