Reputation: 741
I am new to android and I tried to implement onclicklistener
on image view. But it's not working.. Please help. When I click on the image it dose not responds.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
ImageView ad = (ImageView) findViewById(R.id.imageView1);
ad.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, ads.class));
}
});
return true;
}
}
this is my code...
Upvotes: 1
Views: 1539
Reputation: 3131
When I had a similar issue, I got it fixed it by adding android:layout_width="44dp"
to make sure user would have enough area to click on. Then, you can align image content the way you wannt. In my case:
<ImageView
android:id="@+id/locationImageView"
android:layout_width="44dp"
android:layout_height="16dp"
android:layout_centerVertical="true"
android:layout_marginStart="4dp"
android:layout_toEndOf="@id/locationText"
android:adjustViewBounds="true"
android:scaleType="fitStart"
android:src="@drawable/icn_edit_small_white"/>
Upvotes: 0
Reputation: 23638
I think you should declare your ImageView
in onCreate()
as below:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView ad = (ImageView) findViewById(R.id.imageView1); ad.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(MainActivity.this, ads.class)); } }); }
Upvotes: 1
Reputation: 18440
You have two problems
First:
startActivity(new Intent(MainActivity.this, ads.class));
The second argument should be an activity
Second:
In your case, the ImageView
and the listener should be in your onCreate()
Upvotes: 0