Emulator not displaying image on button click android - android

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!

Related

I am having slideshow code in ImageView, But in addition to that I need to navigate to next next image while swiping

package cppandi.apjquotes;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;
public class quotes extends AppCompatActivity {
int mFlipping = 0 ; // Initially flipping is off
Button mButton ; // Reference to button available in the layout to start and stop the flipper
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quotes);
/** Click event handler for button */
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
ViewFlipper flipper = (ViewFlipper) findViewById(R.id.flipper1);
if (mFlipping == 0) {
/** Start Flipping */
flipper.startFlipping();
mFlipping = 1;
mButton.setText(R.string.str_btn_stop);
} else {
/** Stop Flipping */
flipper.stopFlipping();
mFlipping = 0;
mButton.setText(R.string.str_btn_start);
}
}
};
/** Getting a reference to the button available in the resource */
mButton = (Button) findViewById(R.id.btn);
/** Setting click event listner for the button */
mButton.setOnClickListener(listener);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
I need to navigate to next corresponding image when user swipe the screen. Then what code should I use?
Here is my xml page with ViewFlipper and Imageview.
<?xml version="1.0" encoding="utf-8"?>
<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:background="#drawable/background">
<Button
android:id="#+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/str_btn_start" />
<ViewFlipper
android:id="#+id/flipper1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:flipInterval="3000"
android:inAnimation="#anim/slide_in_right"
android:outAnimation="#anim/slide_out_left"
android:layout_centerInParent="true" >
//25 Images in imageview
</ViewFlipper>
</RelativeLayout>
Then I need to know the usage of inAnimation and outAnimation.

How to send text from one Activity to another Activity?

calculated.java: (this has to command to show the calculated.xml layout)
public class Calculated extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculated);
}
}
older:
main class:
package com.barth.appie;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
Button button1;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button1 = (Button) findViewById(R.id.buttoncalculate);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText)findViewById(R.id.editText1);
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(), Calculated.class);
startActivityForResult(myIntent, 0);
}
});
}
#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;
}
}
calculated.xml: (this is what it has to show after the button press)
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:text="#string/text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Furthermore I have <string name="text">Value</string> at strings.xml, and in the other class called calculated.class, I have it so that it outputs the calculated.xml ( which works fine)
What I want: I want to display text in calculated.xml, which is a string made in main.class, and i want the string to be the text filled in textfield called "editText1"
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.EditText;
public class MainActivity extends Activity {
Button button1;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button1 = (Button) findViewById(R.id.buttoncalculate);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText editText = (EditText)findViewById(R.id.editText1);
String text = editText.getText().toString();
Intent myIntent = new Intent(view.getContext(),Calculated.class);
myIntent.putExtra("mytext",text);
startActivity(myIntent);
}
});
}
#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;
}
}
Calculated.java
public class Calculated extends Activity {
TextView mTextview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculated);
mTextview = (TextView)findViewById(R.id.textView1);
mTextview.setText(getIntent().getStringExtra("mytext"));
}
calculated.xml
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="42dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
To send the data from one activity to another activity
MainActivity.java is first activity from where you want to send data to another activity.
Intent myIntent = new Intent(view.getContext(), Calculated.class);
myIntent.putExtra("text", text);
startActivity(myIntent);
Calculated.java is second activity which receive the intent data whatever you pass from MainActivity.java
String text = myIntent.getStringExtra("text");
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(text);

Android ImageView onclicklistenener not working

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"/>

Im trying to use my HTC camera on my pc Eclipse but im getting errors:

This is the content of the file: MainActivity.xml
package com.example.camera_test;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
#SuppressLint("NewApi") #Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
int i = Camera.getNumberOfCameras();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(i);
startActivityForResult( intent, 0 );
}
});
}
#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;
}
}
It was working good I clicked the button and it opened the camera application on my device !
But as soon as I added this lines:
int i = Camera.getNumberOfCameras();
TextView age = (TextView) findViewById(R.id.textView1);
age.setText(i);
Im getting the error on my device say nned to force close.
I tried also instead age.setText(I); this:
age.setText(Integer.toString(i));
I tried instead of id.textView1 also id.button1 but not working.
This is the content of activity_main.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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="45dp"
android:layout_marginTop="62dp"
android:text="Activate The Camera" />
</RelativeLayout>
This is the only two files I did changes.
Solution. Now when I click the button I see the camera then take a photo click on Done then I see the image I took on a small window in my device !
This is the MainActivity.Java fileL:
package com.example.camera_test;
import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.hardware.Camera;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 1337;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener()
{
#SuppressLint("NewApi") #Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult( intent, CAMERA_PIC_REQUEST );
}
});
}
#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;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
// do something
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(thumbnail);
}
}
}
And added to the activity_main.xml file imageView1 in the bottom:
<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"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="45dp"
android:layout_marginTop="62dp"
android:text="Activate The Camera" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Now it's all working ! Thanks.

Droidparts InjectView not working

I'm trying to use droidParts, specifically the DI part. I'm testing 2 injections, InjectResource and InjectView, but the second one doesn;t seem to be working.
I have a simple default layout with a textView:
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
and here's my sample code:
import org.droidparts.annotation.inject.InjectResource;
import org.droidparts.annotation.inject.InjectView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends org.droidparts.activity.Activity {
#InjectView(R.id.text1) private TextView txtView;
#InjectResource(R.string.app_name) private String appName;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("testLog", appName);
txtView.setText("Test");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
The InjectResource works properly and logs the String, but when it gets to the setText("Test"); line I get a nullPointerException. Is there anything else that needs to be done to use InjectView?
The content view needs to be set in preInject() as onCreate() is called right after the injection has been performed. And Views can't be found without layout information.
So the code should look like this:
#Override
public void onPreInject() {
setContentView(R.layout.activity_main);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txtView.setText("Test");
}

Categories

Resources