Display image in popout window after button is clicked -- Android/Java - java

I've been searching the internet for a solution to this problem for several hours now and I can't seem to find an answer that works.
I have written an android app that uses the camera emulator in Eclipse to take a photo and save it to the sd card (path is stored using a String called "file"). What I would like to do is display the image, preferably in a sort of pop-out window, with another button that closes it and returns to the app. I expect I'll need to create another class to do this, but I am not sure all that is involved. Will I have to create another XML file to do the layout? How could I access the file and display it? I expect I'll use ImageView if I am to use another class, but other than that I'm not sure what to do.
There's not much code I can really provide that would help, other than the method which is called when the button is clicked. This part of the code works just fine, and it's simply a method that looks like this:
public void viewPhoto(file){ }
As you can see, I have nothing inside the method yet, that is why I am posting here.
I have been trying to figure this out for several hours, so I would greatly appreciate any ideas you folks might have.
EDIT: MORE INFO
Many of the suggestions I have found require the image to be stored in the "src" folder of the project. However, this photo will be taken and used all within the same program, so I'll have to be able to access it using its file path alone. I can't just state its location in the XML. I created a Dialog object within my existing code, but I'm not sure if this will work. The dialog I plan to use should be able to access the photo from the specified path that is stored in the string mentioned before.
I have been referring to the first answer in this thread: Android Image Dialog/Popup
but as you can see it requires the image to be in the src folder and specified in the XML.
SECOND EDIT:
Here is the code I currently have, both for the XML and the Java. But first I want to mention that I have two XML files--one called activity_main.xml which handles all the initial buttons, menu, etc. The second one is only for my photo (called photo_viewer.xml) and it only contains the ImageView.
photo_viewer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pictureViewer"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/viewImage" />
</RelativeLayout>
Java method to generate image:
public void viewPhoto(String file){
ImageView imageView = new ImageView(getApplicationContext());
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
Bitmap image = BitmapFactory.decodeFile(file);
imageView.setImageBitmap(image);
RelativeLayout rl = (RelativeLayout)findViewById(R.id.pictureViewer);
rl.addView(imageView, lp);
}

you need to create a class which would extend Dialog Class.
refer this how you can create your own dialog.
How to create a Custom Dialog box in android?
then for showing image just call DialogObj.show() to show dialog.

Related

How would I go about using an OnClickListener for a GridView of ImageButtons?

I have this GridAdapter that shows my working GridView of at least 100+ buttons, but all of these will need to navigate to a page once clicked. I'm unsure of how to begin or if what I have is a substantial place to start. I know where I'd put an OnClick method and listener but I am unsure of how to ensure every button in the grid is heard separately since they don't have Ids. Guides online have been helpful but I'm still lost. If anyone has some advice as to how to proceed I'd be grateful. Both java files are attached but the array of images is not included in the Adapter screenshot.
Grid_Adapter
Grid_Fragment
You could add android:onClick="[method name here]" within the ImageButtons in your XML layout code, to call the code when each button is clicked. Note that this method has to be public and exist within the current activity.
As an example:
<?xml version="1.0" encoding="utf-8"?>
<!-- truncated -->
<Button android:id="#+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:onClick="buttonClickMethod1" />
<!-- truncated -->
Also note that this isn't the preferred method, and that using onClick listeners or data bindings is a better approach.
Finally, I would question your activity layout of having 100+ buttons from a user perspective - that seems very high.

Android Studio, Java, TextView not reflecting what I coded

Good evening community,
I am currently trying to make a layout and my code dosen't reflect what it should be.
Here is what it's suppose to look like:
Basically the text blend into the tealish bar
and this is what I have:
What to do?
Your Image is clear but your question is not, in the TextView you have to place the text what ever you want Hardcoded or not.
android:text="Your text here"
but if you want your TextView to show different results, you have to place an ID of TextView and initialize it in your Java code

ViewStub - Show view with dynamic content

This may be a dumb question, so my apologies if so; I'm fairly new to Android.
But anyway - I have a working ViewStub, which is replaced by different layouts depending different situations. It's working fine with regards to showing the correct layout when I call the setLayoutResource() method, and then setVisibility to VISIBLE. However, I now need some of the content in this view that is being shows to be dynamic (i.e. I need to set it via code rather than just show a static layout).
Is this even possible? The setLayoutResource() method only takes a static layout-resource ID, but I need that layout XML file to be able to have it's TextViews contain non-static text that comes from some code that I have ready to utilize. How should this be approached if possible? I understand the concept of having a Java class, and inflating the XML to attach itself to it to update the fields, but I can't see how that relates to my code at hand, since it's simply a layout resource int I need to set for the setLayoutResource() method in ViewStub.
I can post existing code if needed, but I'm not sure it do much more than clutter up the post. For reference - All I have is a simple layout XML file with some TextViews, and then my main XML containing the ViewStub, which is part of a custom Dialog. The user is able to instantiate the Dialog and set the layout, which in turn sets the layout of the ViewStub. This is the layout in which I need the dynamic content to be used.
Any advice would be greatly appreciated. Thanks!
Turns out this wasn't too difficult to accomplish. I just needed to use the ID of the TextView layouts after inflating the ViewStub to get a copy of the actual TextViews, then I was easily able to set their text to whatever kind of dynamic/custom text I desired.
I also needed to comment out the code that shows it via the .VISIBLE call, and instead do the following (the .inflate() line of code accomplishes the same thing as setting it to VISIBLE):
View inflatedView = dialog.myStubView.inflate();
TextView myTextView = (TextView) inflatedView.findViewById(R.id.my_text_view);
myTextView.setText("Dynamic/Custom Text");

How do I create custom GUI controls in Eclipse for use in Android app layouts?

OK the header says it.
I want to be able to create my own controls such as buttons, textviews, edittext boxes, spinners, and so on. I can create the images in CS5 but how do I turn those images into functional GUI components? Thanks
You mean you want to create a custom looking version of those controls right?
You could make an ImageButton component for any custom button you made, and use the attribute android:src, like
android:src="#drawable/your_image"
Similarly for EditText you can use the attribute android:background.
Edit:
The android controls are not stored as standard image files anywhere accessible. The standard designs that you mentioned are the default look of the elements.
To change it, you would have to create the background yourself and store it as a .png file in the drawable folder of your project. Make sure you name this file only in lower case and with no special characters like spaces etc.. Copy this file into all the drawable folders(drawable-hdpi,ldpi etc).
Then you have to refer to this file within your box as
<EditText
android:background="#drawable/your_edit_text_graphic_file_name"
android:text="#string/sample_text"
android:layout_width="match_parent"
-- all other attribute declarations --
</EditText>
Similarly, for other elements you would have to use android:src like is mentioned earlier to refer to your own graphic!
Hope this helps!
You just create your CustomView extend View or TextView.... and implement event handle. Android Developer had a training about this Custom Components . When you finish you can find your custom view in GraphicLayout => CustomView in Eclipse and add like normal components.
Images just provide nice appearance not event handle so you can not just make image to component
You don't have to use CSS in case of android you have to use XML file for UI designing.You can create UI in XML statically or dynamically(i.e. Programmatically). You can create button in android in the following manner.
<Button
android:id="#+id/buttonStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your btn Name"/>
You can access this button in your java file by getting id of button and use it.
Hope this helps.
For more info read this post http://developer.android.com/reference/android/widget/Button.html

How do I insert multiple image in Edit Text in android

I am trying to insert multiple images in one editText. I have used following code to attach image to EditText
txt = (EditText)findViewById(R.id.text);
Bitmap bitmap = BitmapFactory.decodeFile(attach);
Drawable drawable = new BitmapDrawable(bitmap);
txt.setBackgroundDrawable(drawable);
But the problem is it only attach one file and show, If i used array then only the last image is only show. at any how it only shows one Image. Is there any way to show multiple images in one Editbox. Thanks in advance.
Because you can only have one background.... if you want more then there is a layer Drawable which you can use and also you can put the button in a frame layout and add a couple of imageViews below/over it for the rest of the images.
But the best solution will probably be instead of having a couple of bitmaps to just make a single one in Photoshop or equivalent photo editing app and place that one.
According to docs: http://developer.android.com/reference/android/view/View.html#setBackgroundDrawable(android.graphics.drawable.Drawable)
You are not able to provide more than one Drawable item in argument
And I don't know if you understand what is .setBackgroundDrawable(d) method purpose, but it is not meant to be used for displaying images inside text but to set Background of EditText View.
So you're not inserting images in EditText but setting its background,
look for some Rich Text Edit components usable for Android
such as http://code.google.com/p/android-richtexteditor/
or others..
You can probably use a LayerDrawable with the constructor LayerDrawable(Drawable[] layers) to chain together several images and display them as an EditText's background, though as Marek said, I suspect you're looking for something other than setting images in the background.

Categories