Display image after button click - android

I am making a really simple android application that will display a picture after a button is clicked. I have searched for days and tried everything I can think of but cant seem to get it to work. I have been able to host the picture online and link to it but I want the content available offline too. Please help, sorry for the stupid question.
Update:
It is a fixed image and in my drawable resource. Here is the current code I am using to display the image from a URL. What changes should I make to to display that same image from my drawable resource?
JAVA
public class StandingOrders extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void buttonClick (View image)
{
Uri uri = Uri.parse("my url");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
}
}
XML
<Button android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="170px"
android:text="The Button"
android:layout_gravity="center"
android:clickable="true"
android:onClick="buttonClick">
</Button>

If it's a fixed image, include it as a drawable resource and use an ImageView to display it. If it's an image to download, you can do that in a separate thread and store it in a file or an SQLite data base, then again use an ImageView to see it.

Add an ImageView to your xml layout and call setImageResource from onClick.
setImageResource(R.drawable.yourImage);
Or you could set the image in your layout and make the ImageView hidden until you click the button. See setVisibility(View.GONE)

Check out this answer: https://stackoverflow.com/a/4896272/458968
In your case, the uri should be
Uri.parse("android.resource://com.company.app/"+R.drawable.my_image);

You can try to implement the method, which I am posting below. I have done something more or less similar to your requirement.
Method:
//method to zoom images
public void zoomImage(String imageUrl)
{
AlertDialog.Builder builder;
Context mContext = ExamActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
//to close dialog
ImageView close_dialog = (ImageView) layout.findViewById(R.id.imageView_custom_dialog_close);
close_dialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
alertDialog.dismiss();
}
});
//to show image
WebView wv=(WebView) layout.findViewById(R.id.show_image_webView);
wv.getSettings().setBuiltInZoomControls(true);
wv.setInitialScale(200);
wv.loadUrl(imageUrl);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
the imageUrl is the url to the webpage containing the image. If you want to show a local image then just create a simple html page containing the image and use the local url.

Try this:-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageToDisplay = (ImageView) findViewById(R.id.imageToDisplay);
Button btnShowImage = (Button) findViewById(R.id.btnShowImage);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imageToDisplay.setVisibility(View.VISIBLE);
imageToDisplay.setImageResource(R.drawable.imagelogo);
}
});
}
}
In layout imageToDisplay's visibility is gone.

try following example
you can download "android-query-0.22.10.jar" file from following url.
http://www.java2s.com/Code/Jar/a/Downloadandroidquery02210jar.htm
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.addbuttondynamic.AddButtonDynamic" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
and Main.java
import com.androidquery.AQuery;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity {
Button btnLoad;
ImageView ivImage;
AQuery aQuery;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLoad=(Button)findViewById(R.id.button1);
ivImage=(ImageView)findViewById(R.id.imageView1);
aQuery=new AQuery(this);
btnLoad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//load image from /res folder.
ivImage.setImageResource(R.drawable.ic_launcher);
//load image from url.
aQuery.id(ivImage).image("you URL");
// if ivImage is not working in aQuery.id() then use R.id.imageView1.
}
});
}
}

activity_main.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"
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="com.example.imagechange.MainActivity"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:text="#string/khilan"
android:onClick="btnOnClick"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="800px"
android:layout_height="500px"
android:layout_marginTop="70dp"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnOnClick (View view)
{
String imagename = "khilan";
int res = getResources().getIdentifier(imagename, "drawable", getPackageName());
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(res);
}
}

Create a layout with image and button, make the image hidden and onClick set the visibility to VISIBLE.
Here is what the XML should look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:src="#drawable/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Image" />
</RelativeLayout>
and the following is the Java code
ImageView image = findViewById(R.id.image);
Button button = findViewById(R.id.got_it_button);
image.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setVisibility(View.VISIBLE);
}
});
Good luck :)

Related

Adding a OnclickListener to ImageViews inside a HorizontalScrollView

This question may have been asked before but I seem to not make much progress.
Basically I want to add an onclicklistener to my HorizontalScrollView that expands the image when pressed.
Here's the current code in the XML file:
<HorizontalScrollView
android:id="#+id/firstscrollview"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="left"
android:orientation="horizontal"
android:layout_marginTop="50dp">
<LinearLayout
android:id="#+id/firstlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/Cercie_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="#drawable/image"/>
I've been trying out different things but I can't seem to get it to work, if anyone knows or can find a good example it would be very much appreciated!
Try this .
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ImageView Cercie_button = findViewById(R.id.Cercie_button);
Cercie_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something here
}
});
}
if i am not getting it wrong then what so difficult in it you have to just do this in your on create method
ImageView btn = findViewById(R.id.Cercie_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//write what you want to do
}
});

why doesnt this open the webviewer when i press the button?

So i have this button that I would like it to open a webviewer when the button is pressed.
Here is the xml where the button is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="#drawable/signin"
android:textAlignment="center" />
</LinearLayout>
Here the class code for the click button
public class SignIn extends Activity {
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
img = (ImageView) findViewById(R.id.signIn);
signIn();
}
public void signIn() {
img.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SignIn.this, WebViewActivity.class);
startActivity(intent);
}
});
}
}
here is where i have the webviwer.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
And here is the webviewer class
public class WebViewActivity extends Activity{
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
When click nothing happens, I also edited the manifest to have internet access
Make a small correction. In XML you are using "ImageButton" but in java file your are using "ImageView".
<ImageView
android:id="#+id/signIn"
android:layout_width="match_parent"
android:layout_height="61dp"
android:src="#drawable/signin"
android:textAlignment="center" />
I think it is wrong to write in XML ImageButton and in java file to search for an ImageView by id.
Use an ImageView or an ImageButton!
if you want to use an imageView don forget to make it clicable.

Android - Button under Relative layout wont initiate OnClickListener when clicked

Hello this is my xml file
<RelativeLayout
android:id="#+id/tutorialBox"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
i have made an on click listener for it
final Button closeBt = (Button) findViewById(R.id.closeBen);
closeBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
closeBt.setText("Im a button");
}
});
for some reason when i click this button nothing happens it doesnt look like it has been clicked.
when i took the button out of the realtive layout everything worked fine
any suggestions?
Instead of this add onClick attribute to the button tag in xml.
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:onClick = "close_clicked"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
Then in Main Activity just make a new method like this.
public void close_clicked (View v){
// Your code
}
No need to add on click listner.
Your RelativeLayout does not look good, is it your main container? Maybe try it this way
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<Button
android:id="#+id/closeBen"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/CloseBenny"
android:layout_alignBottom="#+id/bennybox"
android:layout_alignEnd="#+id/chatbub" />
</RelativeLayout>
And a good practice is to declare your widgets globally then instantiate them in theOnCreate
public class Foo extends AppCompatActivity {
private Button closeBenny;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
closeBenny = (Button)findViewById(R.id.closeBen);
closeBenny.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
closeBenny.setText("Im a button");
}
});
}
}

Changong the visibilty/invisibiloty of a TextView Using onClick Method

I am trying to make two textviews appear and disappear on the same click and then on the next click vica versa appear and disappear. I have read some posts on this site, Make Textview Visible by Pressing a Button and Changing the visibility of a textview in a listview, but the solutions in these examples does not work for me. However, I have borrowed some of their ideas.
package com.mycompany.screenchangeapplication;
import android.app.*;
import android.graphics.drawable.ColorDrawable;
import android.os.*;
import android.view.*;
import android.widget.*;
public class ScreenActivity extends Activity {
public RelativeLayout container;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
container = (RelativeLayout) findViewById(R.id.ScreenActivity);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
changeScreen(v);
}
});
}
public void changeScreen(View v) {
ColorDrawable cd = (ColorDrawable) this.container.getBackground();
TextView ON = (TextView) findViewById(R.id.ON);
TextView OFF = (TextView) findViewById(R.id.OFF);
if (cd != null && cd.getColor() == getResources().getColor(R.color.WHITE)) {
container.setBackgroundColor(getResources().getColor(R.color.BLACK));
OFF.setVisibility(View.VISIBLE);
ON.setVisibility(View.INVISIBLE);
} else {
container.setBackgroundColor(getResources().getColor(R.color.WHITE));
OFF.setVisibility(View.INVISIBLE);
ON.setVisibility(View.VISIBLE);
}
}
}
and
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:id="#+id/ScreenActivity"
android:clickable="true"
tools:context=".ScreenActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="ON"
android:id="#+id/ON"
android:visibility="visible"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="150dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="OFF"
android:id="#+id/OFF"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textSize="150dp"
android:textColor="#ffffff"
android:visibility="invisible" />
</RelativeLayout>
When I put the app in the emulator it crashes, so something is badly wrong.
I am not sure how View v should be passed to onClick and to changeScreen. I would hope that the View passed to onClick would also be passed to changeScreen, but I am unsure how exactly this would work.
In Android Studio, all the text seems to be fine (its not though).
Step1) First you add color.xml file into the values folder.(you can use any name for color)
Step2) Use like this into color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="White">#FFFFFF</color>
<!-- White-->
<color name="Black">#000000 </color>
<!-- BLACK -->
</resources>
Step3) Change a little changeScreen() method:
public void changeScreen(View v) {
ColorDrawable cd = (ColorDrawable) this.container.getBackground();
TextView ON = (TextView) findViewById(R.id.ON);
TextView OFF = (TextView) findViewById(R.id.OFF);
if (cd != null && cd.getColor() == getResources().getColor(R.color.Black)) {
container.setBackgroundColor(getResources().getColor(R.color.White));
OFF.setVisibility(View.INVISIBLE);
ON.setVisibility(View.VISIBLE);
} else {
container.setBackgroundColor(getResources().getColor(R.color.Black));
OFF.setVisibility(View.VISIBLE);
ON.setVisibility(View.INVISIBLE);
}
}
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
textview.setVisibility(textview.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
});

App crashes when changing intent?

I set an imageview to change my activity onClick, for some reason it crashes each time I click the imageview on my device. Any ideas? I feel as if this is a very general answer that I am missing. Also if you have any reading material you could link related to my mistake it would help me greatly. Thank you!
Main Activity
package com.gcomcode.oakgang;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends ActionBarActivity {
ImageView bioicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView musicicon = (ImageView) findViewById(R.drawable.musicicon);
ImageView galleryicon = (ImageView) findViewById(R.drawable.gallery);
ImageView connecticon = (ImageView) findViewById (R.drawable.connecticon);
ImageView contacticon = (ImageView) findViewById (R.drawable.contacticon);
ImageView eventsicon = (ImageView) findViewById (R.drawable.eventsicon);
}
public void bioClick() {
bioicon = (ImageView) findViewById(R.drawable.bioicon);
bioicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent bioIntent = new Intent(MainActivity.this, BioActivity.class);
startActivity(bioIntent);
}
});
}
}
and here is the corresponding layout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context="com.gcomcode.oakgang.MainActivity"
tools:ignore="MergeRootFrame" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/black"
android:src="#drawable/oakganglogo"
/>
<android.support.v7.widget.Space
android:id="#+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="144dp"
android:layout_marginTop="216dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp" >
<ImageView
android:id="#+id/bioicon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/bioicon"
android:onClick="bioClick" />
<android.support.v7.widget.Space
android:id="#+id/space3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/musicicon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/musicicon" />
<ImageView
android:id="#+id/galleryicon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/gallery" />
<android.support.v7.widget.Space
android:id="#+id/space2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="85dp"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/eventsicon"
android:layout_width="100dp"
android:layout_height="85dp"
android:src="#drawable/eventsicon" />
<ImageView
android:id="#+id/contacticon"
android:layout_width="100dp"
android:layout_height="85dp"
android:src="#drawable/contacticon" />
<ImageView
android:id="#+id/connecticon"
android:layout_width="100dp"
android:layout_height="85dp"
android:src="#drawable/connecticon" />
</LinearLayout>
This is the activity I am trying to open
package com.gcomcode.oakgang;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
public class BioActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bio_activity);
}
}
Change this:
public void bioClick() {
to this:
public void bioClick(View view) {
because in order for an onClick method to be called from the xml (layout file), that onClick method has to take in exactly one parameter, a View parameter. (The view that gets passed into the method is whichever view called it - whichever view was pressed. This is useful if you want more than one view to call the same onClick method, because then you can use the view variable to figure out which view called it, and respond accordingly.)
Now, another problem with your code:
If you make that change I wrote, then it will probably stop crashing, because now it will be able to find the right onclick method. But the first time you click on bioicon, it won't take you to a new activity. You will find that you have to click it TWICE to make it go to a new activity. That is because the first time you click it, it runs the bioClick method. But the only thing that method does is to change the onClick method for the ImageView from "bioClick" to the new internal method. If you want the activity to change the FIRST time you click the bioicon ImageView, change your method to look like this:
public void bioClick() {
Intent bioIntent = new Intent(MainActivity.this, BioActivity.class);
startActivity(bioIntent);
}
This part of the code:
bioicon = (ImageView) findViewById(R.drawable.bioicon);
bioicon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Intent bioIntent = new Intent(MainActivity.this, BioActivity.class);
startActivity(bioIntent);
}
});
is how you set an onClickListener programmatically, in code. If you set an onClickListener in your xml layout file (by using android:onClick="bioClick"), then you do not need to set your onClickListener programmatically as well. Do one or the other, but not both.
While I am at it, I will also point out that unless you have more code in your onCreate method of your first activity than you showed here, there is absolutely no point to doing this:
ImageView musicicon = (ImageView) findViewById(R.drawable.musicicon);
ImageView galleryicon = (ImageView) findViewById(R.drawable.gallery);
ImageView connecticon = (ImageView) findViewById (R.drawable.connecticon);
ImageView contacticon = (ImageView) findViewById (R.drawable.contacticon);
ImageView eventsicon = (ImageView) findViewById (R.drawable.eventsicon);
You only need to "get" your views into variables if you plan on doing something with them afterward. (Like setting Listeners on them, changing text, or whatever.)

Categories

Resources