I rarely worked on image views in android. I want to display image view after some interval every time as I want to display ads on it from server. I want to pass Image view in a function as image view. But I don't know how to pass image view from oncreate as I worked on adapter class and where integer array of drawable images does the work properly.
please suggest me how to pass image view as I tried to pass and tried lot of conversions but failed.
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here i want to take image and pass it on via loadphoto
loadPhoto(R.drawable.ic_launcher, 400, 100);
}
private void loadPhoto(ImageView imageView, int width, int height) {
ImageView tempImageView = imageView;
AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_main,
(ViewGroup) findViewById(R.id.layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.fullimage);
image.setImageDrawable(tempImageView.getDrawable());
imageDialog.setView(layout);
imageDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
imageDialog.create();
imageDialog.show();
}
}
actvity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10dp" >
<ImageView
android:id="#+id/fullimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageView>
<TextView
android:id="#+id/custom_fullimage_placename"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" >
</TextView>
</LinearLayout>
Thanks in advance
Display image view after some interval every time as I want to display ads on it from server
It means you should use (need) a Timer task (for simple) or a schedule thread to make it runnable.
in new Runnable() {
// Your work here:
// Load the image
}
Also notice : update the image is excuted on UI thread.
Oh sorry, may be too far with your question.
If you already had your images (in your drawable/ in raw/ folder), i suggest you parse an interger, for example loadPhoto(Integer imageSourceID, Context mContext) and load it, using getResource()
If you get it from server (download), then asynctask would be a good choice. Prepare your UI in onPreExcute, do your work in onDoinBackground() and update in the onPostExcute()
I hope these will help you
4.
Related
this is my code:
final ImageView imageView1 = (ImageView) findViewById(R.id.imageView8);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
}
});
by this code when i click on my ImageView i can see it in fullScreen.
Now i have a question:
when I see imageView in fullscreen I want when I press Back, this imageView close and back to previous situation and my app doesn't back to previous activity
Rather then opening an image on full screen you can use a custom dialog to open that image which is called a Full Image Dialog, which works exactly as AlertDialog, for that you just need to design a custom layout including width and height of image you are opening. It is easy to maintain as on clicking anywhere on screen dialog automatically closes and your activity will resume.
If you want then I can give you more idea?
Step 1: Make a class FullImageDialog having this method:
public static void showImage(Context context, String strImagePath) {
AlertDialog.Builder imageDialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_fullimage_dialog, null);
ImageView image = (ImageView) layout.findViewById(R.id.fullImage);
Glide.with(context)
.load(strImagePath)
.placeholder(R.drawable.default_user_image)
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(image);
//image.setImageDrawable(tempImageView.getDrawable());
imageDialog.setView(layout);
final AlertDialog alert= imageDialog.create();
alert.getWindow().getAttributes().windowAnimations=R.style.FadeInTheme;
alert.show();
}
Step 2: create xml of full_image_dialog as:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/fullImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="#drawable/default_user_image"
android:scaleType="fitXY" />
</LinearLayout>
step 3: call method where you need to open this dialog:
final ImageView imageView1 = (ImageView) findViewById(R.id.imageView8);
imageView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//call FullImageDialog class by giving parameter of imageUrl
FullImageDialog.showImage(imageUrl);
}
I know that this question might be a duplicate of another question but i tried to follow what was suggested in solutions to similar questions without any success. I am relatively new to android so please kindly point me in the right direction.
Scenario:
I have a list view that has a custom layout file containing a small image and a text view. The list view displays all items as intended but what I want to do is as follows:
When a button is clicked, a condition is evaluated and if the result is true, i want to change the default image to another one. I am aware that I have to perform the image switching in the get view method that I have to override.
public View getView(int position, View convertView, ViewGroup parent) {
View itemView=convertView;
if (itemView==null) {
itemView=getLayoutInflater().inflate(R.layout.check_list_view, parent, false);
}
MailObject currentMailObject=mailList.get(position);
ImageView ivCurrentMail=(ImageView)itemView.findViewById(R.id.ivMailIcon);
if (mailList.get(position).isScanned()) {
ivCurrentMail.setImageResource(R.drawable.ic_mail_scanned);
}
return itemView;
}
The code for the button is:
btnSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), scanResult, Toast.LENGTH_LONG).show();
if(!scannedMails.contains(scanResult))
{
scannedMails.add(scanResult);
for (int i = 0; i < mailList.size(); i++) {
if (mailList.get(i).getCode().equalsIgnoreCase(scanResult)) {
int position=i;
mailList.get(i).setScanned(true);
ImageView ivCurrent = (ImageView) lvMails.getAdapter().getView(position, null, null).findViewById(R.id.ivMailIcon);
ivCurrent.setImageResource(R.drawable.ic_mail_scanned);
lvMails.invalidateViews();
}
}
}
}
});
The XML file for a list view item is as follows:
<ImageView
android:id="#+id/ivMailIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_mail_not_scanned" />
<TextView
android:id="#+id/tvMailItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_toRightOf="#+id/ivMailIcon"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
I did test if the condition evaluates as it should by displaying a Toast message on list view item click and the results were positive but the image just does not change. Please let me know where I went wrong or missed something. Thanks
You're setting the same image twice. In both cases, you use the setImageResource() method for the ImageView that points to R.id.ivMailIcon to set the drawable as R.drawable.ic_mail_scanned.
You are probably "setting" to image again correctly on a click, but since it's the same image, it won't appear to change.
I have a need to show a minimally-intrusive non-blocking notification which is not tied to the activity it was shown in (like a Toast) and which is clickable. Anyone have any idea whether or not this is possible? Unfortunately, it appears that Toast notifications (custom or otherwise) are not clickable (i.e. setting an OnClickListener on its views has no effect). All the alternatives that I'm aware of (i.e. AlertDialog, PopupWindow and Crouton) seem to show a notification which is tied to the activity it was shown in (i.e. they won't continue showing when the activity finishes). Any suggestions?
You can use PopupWindow, add an onClickListener and add a handler to auto cancel it after n times (just like the behavior of a toast). Something like this:
public static void showToast(Activity a, String title, String message) {
// inflate your xml layout
LayoutInflater inflater = a.getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) a.findViewById(R.id.toast_layout_root));
// set the custom display
((TextView) layout.findViewById(R.id.title)).setText(title);
((TextView) layout.findViewById(R.id.message)).setText(message);
// initialize your popupWindow and use your custom layout as the view
final PopupWindow pw = new PopupWindow(layout,
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, true);
// set windowType to TYPE_TOAST (requires API 23 above)
// this will make popupWindow still appear even the activity was closed
pw.setWindowLayoutType(WindowManager.LayoutParams.TYPE_TOAST);
pw.showAtLocation(layout, Gravity.CENTER | Gravity.TOP, 0, 500);
// handle popupWindow click event
layout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do anything when popupWindow was clicked
pw.dismiss(); // dismiss the window
}
});
// dismiss the popup window after 3sec
new Handler().postDelayed(new Runnable() {
public void run() {
pw.dismiss();
}
}, 3000);
}
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical"
android:elevation="10dp"
android:padding="20dp">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"/>
</LinearLayout>
You are right, a Toast object has no way to be interacted with, but there are many libraries out there that will give you the same look and feel as a toast, but with some interactivity. The one I use is https://github.com/JohnPersano/SuperToasts
I think what you need is in fact a PopupWindowwhich can be seen here "http://developer.android.com/reference/android/widget/PopupWindow.html".
Toasts have a very specific task, which is to inform the user, without any input from them. So instead of trying to extend the purpose of the Toast, use the PopupWindow which can be interacted with by the user.
A 'Dialog' type of activity will probably be your best bet.
In manifest:
<activity android:name=".ToastLikeActivity"
android:theme="#android:style/Theme.Dialog"
android:label="#string/label"
></activity>
And timeout the activity within the onCreate():
class ToastLikeActivity extends Activity {
#Override
public void onCreate(Bundle state)
// auto-kill activity after X seconds <-------------------------
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
ToastLikeActivity.this.finish(); // kill after X seconds
}
}
}, VisibleTimeSecs*1000);
}
To display the dialog start it as with any other activity:
Intent i = new Intent(this, ToastLikeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
And it will show up and automatically go away after X seconds.
Such a popup will not be tied to the caller activity. In fact - it will not even require a caller activity. You
can activate it (bad idea, but possible) even from a service.
You can implement basically any kind of sensitive (i.e. accepting user's clicks) interface you want to
the ToastLikeActivity. Especially: you can make its exteriors transparent, giving it a dialog-likke looks.
For an Android app...I have a button on an Activity that calls a custom ListActivity. This ListActivity has two lines of text and a checkbox. When invoked, the ListActivity opens up an XML file on the device (local.xml) . This XML file contains a list of target XML files on the web. If the file exists on the device, the checkbox on the ListActivity is checked, otherwise it isn't.
When the ListItem is pressed, it checks to see if the target file exists on the device-if it does, it displays a dialog box asking if they want to overwrite. If the file doesn't exist, or if they chose to overwrite, a progress dialog is displayed as it goes to the internet and grabs a set of files (the target XML file contains a list of JPegs to gather).
After downloading the JPegs, I change the message on the progress to show whether all the JPegs downloaded or not. It sleeps for a few seconds, then disapears.
All of the above works.
My questions are:
After completion, how do I set the checkbox associated with the pressed item, based on whether all of the JPegs downloaded or not?
I'd really like a tri-state indicator instead of a checkbox, which is binary, unless I could change the color to yellow. Is there a better widget I should be using here?
Relvant code follows (let me know if you need to see more)
Initial Activity:
public class RRS_Preferences extends Activity {
onCreate(yadda, yadda) {
}
public void Button_Listener(View view) {
/* open up the ListView Activity */
Intent myIntent = new Intent();
myIntent.setClassName("com.sinisterlabs.mypackage", "com.sinisterlabs.mypackage.Download_List");
startActivity(myIntent);
}
}
Custom List Activity:
public class Download_List extends ListActivity {
List<Location>loc_list = new ArrayList<Location>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new RRSList_ArrayAdapter(this));
selection = (TextView)findViewById(R.id.tableRow1);
/* Open the "local.xml" file, pull from it the list of files that need to go
onto the ListActivity. For each file, I add it to the List. */
loc_list.add(new Location(stringLocalFilename, stringURL, booleanIsPresent));
}
protected void onListItemClick(final ListView parent, final View v, int position, long href) {
if (fileLocalFile.exists) {
subDownloadJPegs(fileLocalFile);
} else { // Ask to download or not?
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setMessage("Are you sure you want to OverWrite this file and all of its image files?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
subDownloadJPegs(fileLocalFile);
}
});
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(), "OverWrite Operation Cancelled...", Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
}
}
private void subDownloadJPegs(fileLocalFile) {
progDialog = new ProgressDialog(this);
progDialog.setCancelable(true);
progDialog.setMessage("Downloading files for " + fileLocalFile.toString() + "...");
progDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progDialog.setProgress(0);
/* open up this file and count the number of JPegs to be downloaded */
progDialog.setMax(intMax);
progDialog.setMessage("Downloading Sign Files for " + RuleSetName + "...");
progDialog.show();
/* background thread to update progress bar */
Thread background = new Thread (new Runnable() {
#Overide
public void run() {
/* Inside a loop, download each file, increment the progress bar as we do */
progressHandler.sendMessage(progressHandler.obtainMessage());
}
background.start();
}
Handler progressHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
progDialog.incrementProgressBy(1);
}
}
List Item Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="false"
android:focusable="false"
android:gravity="center" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:textSize="18dp"></TextView>
<TextView android:id="#+id/text2"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:textSize="13dp"></TextView>
</LinearLayout>
</LinearLayout>
Thanks!!!
OK, I got it. the problem was where I placed the call to dismiss the dialog box. It ended up inside a catch statement and was never executing. In fixing this, I also parameterized my calls to the handler, which made things clearer.
Wheh!
:-)
Can anyone tell me what's wrong with this implementation? All I want to do here is have two overlapping views that swap places when you tap the screen. Unless I'm just using it wrong, View.bringToFront() does nothing?
Below is all the code in my app. Note that I added padding to the 'backView' just to make sure the two were actually overlapping. Indeed I could see both on the screen. While tapping the top view does indeed trigger the onClick method, nothing visibly changes in response to the calls to bringToFront.
public class MainActivity extends Activity implements OnClickListener {
private ImageView frontView;
private ImageView backView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
backView = (ImageView) findViewById(com.example.R.id.BackView);
frontView.setOnClickListener(this);
backView.setOnClickListener(this);
backView.setPadding(10,0,0,0);
}
private boolean flag;
public void onClick(View v) {
if (!flag) {
backView.bringToFront();
}
else {
frontView.bringToFront();
}
flag = !flag;
}
}
and the corresponding layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/FrontView"
android:src="#drawable/front"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/BackView"
android:src="#drawable/back"
/>
</RelativeLayout>
Maybe it's the layout I'm using? I'm not sure... I've tried FrameLayout and LinearLayout as well.
I would try swapping content views instead of ImageViews.
Put each imageView in a different layout and then it is easy:
public void onClick(View v) {
if (!flag) {
setContentView(R.layout.main_front);
frontView = (ImageView) findViewById(com.example.R.id.FrontView);
frontView.setOnClickListener(this);
}
else {
setContentView(R.layout.main_back);
backView = (ImageView) findViewById(com.example.R.id.BackView);
backView.setOnClickListener(this);
}
flag = !flag;
}
There are a couple of Components that you can use that do this for you.
ViewAnimator, ViewFlipper and ViewSwitcher. You can set the animations you require etc and they hand the rest.
here's one example.
http://www.androidpeople.com/android-viewflipper-example/
Given your example, do you have to call invalidate() on the parent after you've called bringToFront() ?