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...
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));
}
});
}
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()
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"/>
Related
Please bear with me on this I'm new to android programming. I was trying to show an image on a button click in android but unfortunately my emulator is not showing the required output. Here is my layout.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:text="Button" />
</RelativeLayout>
main_activity.java
package com.example.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
ImageView image;
#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);
return true;
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
image.setImageResource(R.drawable.optimus);
}
});
}
}
I have included the image file under /res/drawable-mdpi. When I try running on emulator it does not show image on button click. Is there any problem with the code?
In your onCreate() method write this please :
Button theButton = (Button)findViewById(R.id.button1);
theButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
theButton.setBackgroundResource(R.drawable.optimus);
}
Assuming that you have an ImageView called imageView1,
You have to assign it, before using it.
So change this:
#Override
public void onClick(View arg0)
{
image.setImageResource(R.drawable.optimus);
}
to this
#Override
public void onClick(View arg0)
{
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(R.drawable.optimus);
}
You have to write onclick method like this code and also you need to call addListenerOnButton method in the onCreate() method
package com.example.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
#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);
return true;
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
button.setBackgroundResource(R.drawable.optimus);
}
});
}
}
Try This:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/background" <!-- you're adding this here -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:onClick="showImage" <!-- you're adding this here -->
android:text="Button" />
</RelativeLayout>
Then in the .java file...
Package com.example.app1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.ImageView;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
ImageView image;
#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);
return true;
}
public void showImage(View view) {
RelativeLayout background = (RelativeLayout) findViewById(R.id.background);
background.setBackgroundResource(R.drawable.optimus);
} /* This is a whole new section of code to replace the onClick listener */
}
Since you're already using the XML Layouts, try to use the built in onClick listener whenever possible. You can do this by naming a function in your .java file called yourFunctionName with a single argument of (View view).
Then, in your XML, you need to point to it by adding android:onClick="yourFunctionName" for the view that needs the onClick method.
Lastly, you never had the image you were referring to actually pointing to anything. You simply said that the object "image" now used "R.drawable.optimus" but that Image was never used! So instead, I just pointed to placing it in the background of your RelativeView. If instead you wanted it in a separate image, you'll have to add an ImageView to the XML file and set the background resource for the ImageView the same way.
Best of luck on learning Android!
I am creating an app in android.
In this app i want to show google Map after click on button.
I have declared button and i declared listener.
But my problem is i want to call another activity class in the action listener and show google map.
But it is not working, I changed emulator from google to Android it work but google API is not called and shows me error.
for this i used this code But my Map is not show and my Application is Terminated .
//----------------Main Activity ---------------//
import java.security.PublicKey;
import com.google.android.maps.MapView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
EditText TextFeild;
Button button1;
Context context=this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addOnListenerAction();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void addOnListenerAction()
{
TextFeild=(EditText) findViewById(R.id.editText1);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(context,MapViwer.class);
}
});
}
#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);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
//_-------------------MapViwer Class------------//
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class MapViwer extends MapActivity
{
MapView map;
public void onCreate(Bundle setInstanceState)
{
super.onCreate(setInstanceState);
setContentView(R.layout.activity_main);
map=(MapView) findViewById(R.id.mapView);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
So you're saying, you have a class named (MapViwer) that defines a layout and the class (MapViwer) contains information for the map to work. To deploy a class from your MainActivity.class, use the following method.
in MainActivity, under the public class add this
private Button button1;
then add this function -
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), MapViwer.class);
startActivityForResult(i, 100);
}
});
Use it like this..It worked for me..
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,MapViewer.class);
startActivity(intent);
}
});
and in the Mapviewer class just call the mapviewer.xml
Now, the mapviewer.xml should look like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="34dp" />
</LinearLayout>
Try it out..
Replace context with Mainactivity.this in the code
Intent intent=new Intent(context,MapViwer.class);
You just only defined intent but didn't call the Mapviewr activity
Correct code:
public void addOnListenerAction()
{
TextFeild=(EditText) findViewById(R.id.editText1);
button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Mainactivity.this,MapViwer.class);
startActivity(intent);
}
});
}
I can't find a solution for my problem! I keep getting the error:
onClickListener cannot be resolved to a
type
Please Help!! Here is my code
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements onClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#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);
return true;
}
Try adding this method according to your aim:
#Override
public void onClick(View view)
{
//....
//....
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
onClickListener should be like this OnClickListener
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.button1);
send.setOnClickListener(this);
}
#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);
return true;
}
#Override
public void onClick(View v) {
}
}
Create a method like addOnClickListener();
and work on it like:
public void addOnActionListener(){
Button send=(Button)findViewById(R.id.button1);
send.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//...
//...
So what I've done in Eclipse, in layouts I have: activity_main.xml and activity_main2.xml. What I tried is to create a button in activity_main.xml and on click to go on screen of activity_main2.xml
so in com.example.myfirstapp I have
MainActivity.Java:
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
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.activity_main, menu);
return true;
}
public void click1(View v) {
Log.i("clicks","You Clicked B1");
Intent i=new Intent(
MainActivity.this,
MainActivity2.class);
startActivity(i);
}
}
MainActivity2.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
public class MainActivity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Write below code in your MainActivity.java file instead of your code.
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mBtn1 = (Button) findViewById(R.id.mBtn1);
mBtn1.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v) {
Log.i("clicks","You Clicked B1");
Intent i=new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
}
}
And Declare MainActivity2 into your Androidmanifest.xml file using below code.
<activity
android:name=".MainActivity2"
android:label="#string/title_activity_main">
</activity>
Change your FirstyActivity to:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn_go=(Button)findViewById(R.id.YOUR_BUTTON_ID);
btn_go.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("clicks","You Clicked B1");
Intent i=new Intent(
MainActivity.this,
MainActivity2.class);
startActivity(i);
}
}
});
}
Hope it will help you.
There is more than one way to do this.
Here is a good resource straight from Google:
http://developer.android.com/training/basics/firstapp/starting-activity.html
At developer.android.com, they have numerous tutorials explaining just about everything you need to know about android. They even provide detailed API for each class.
If that doesn't help, there are NUMEROUS different resources that can help you with this question and other android questions.
I have a problem with my android project. I cannot get access to my button. I have attach my class code==http://pastebin.com/A5ZTBkhd. Please anyone help me.
Here is my code -
package com.droid.androiddoctor;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidDoctorMainActivity extends Activity implements OnClickListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_android_doctor_main, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.btnSubmit:
String e="hello";
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
break;
}
}
}
.Here is my code.when i push submit nothing happens. and the xml file is====
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnSubmit"
androi:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="#+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exit" />
</LinearLayout>
You did not set the listener for your widgets at all. Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
button1.setOnClickListener(this)
}
I would suggest you to go through Android's UI Guide.
I wouldn't implement OnClickListener like that, try to add it like this in your class:
public class AndroidDoctorMainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_android_doctor_main);
Button button1 = (Button) findViewById(R.id.btnSubmit);
Button button2 = (Button) findViewById(R.id.btnExit);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for click on this button
}});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//your code for this button
//or if you just want to exit from activity, just call:
finish();
}});
#Override
public boolean onCreateOptionsMenu(Menu menu) {
}
}