This is the strangest thing. I have an Arraylist being called into an imageview. The image returned is in the position it should be when the image is clicked but when I use a Log, the image returned is the next image, instead of the image in the position.
Ion.with(getApplicationContext())
.load(mImages.get(position))
.noCache()
.progressBar(progressBar)
.progressHandler(new ProgressCallback() {
#Override
public void onProgress(long downloaded, long total) {
progressBar.setVisibility(View.VISIBLE);
}
})
.intoImageView(imageView)
.setCallback(new FutureCallback<ImageView>() {
#Override
public void onCompleted(Exception e, ImageView imageView) {
progressBar.setVisibility(View.GONE);
}
});
And the log right underneath:
Log.d("position of url = ", (mImages.get(position));
This is all placed in a PagerAdapter with the position of the viewpager sourced from a gridview. What could be wrong?
I figured this out.
position=viewPager.getCurrentItem();
and it works fine now!
Related
I am using https://github.com/Chivorns/SmartMaterialSpinner Library for spinner.
When i first click the spinner no adapter is attached to it or spinner items shows empty list..
like
but when i close it and again select it.. data are shown so I believe that .. due to time consuming data fetching .. spinner show empty dialog.. Now I need to handle that.. How can i only show spinner when data is populated or available.. I have tried async task and handler but not get working.. any hint would be appreciated.. Thank you
Edited ...
I have call api on spinner onTouch event to populate data
{
private void ProvinceSpinnerCode(boolean IsEditCase) {
if (IsEditCase) {
//edit case condition
} else {
provinceSpinner.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
mService = RetrofitManager.getApiClient().create(RetrofitInstance.class);
Call<List<SelectModel>> provinceCall = mService.GetProvinces();
provinceCall.enqueue(new Callback<List<SelectModel>>() {
#Override
public void onResponse(Call<List<SelectModel>> call, Response<List<SelectModel>> response) {
if (response.body().size() >= 0) {
provinceSpinner.setAdapter(new SpinnerAdapter(response.body(),AddCustomerActivity.this));
}
}
#Override
public void onFailure(Call<List<SelectModel>> call, Throwable t) {
}
});
return false;
}
});
}
provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(AddCustomerActivity.this, "Id is : " + id, Toast.LENGTH_SHORT).show();
provinceId = (int) id;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
You are making an API call inside on touch event which will be fired when you will touch the spinner so to solve the problem just do Api call out side that event (may be inside Oncreate).
Right now provinceSpinner has a touch listerner so when you are touching it to open for the first time it is Fetching data so when you are clicking it again the alert is showing data (the data you fetched when you clicked first time)
So, I have an activity, lets say a PetDetailActivity that shows a carousel with some Bitmaps in it (I use com.synnapps:carouselview:0.1.5 to handle my carousel). The problem is that the PetDetailActivity loaded with 0 sized carousel, which maybe the images is still being processed by a thread. How to wait Picasso to finish processing URLs, and then show it up in the new Activity?
This is the code of PetDetailActivity:
import ...
public class PetDetailActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pet_detail);
Intent i = getIntent();
Pet targetPet = (Pet)i.getSerializableExtra("PetObject");
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null) actionBar.setDisplayHomeAsUpEnabled(true);
//Creating a BitmapHandler object to download image from URL to Bitmap object using picasso.
BitmapHandler bitmapHandler = new BitmapHandler(targetPet.getCarouselImageUrl());
final ArrayList<Bitmap> petCarouselBitmaps = bitmapHandler.getProcessedBitmap();
//The bitmap is being downloaded in other thread, so the activity is up and
//CarouselView is still empty (petCarouselBitmaps.size() == 0)
//So how to wait the bitmaps is processed, like show a loading screen on the UI?
CarouselView petCarousel = findViewById(R.id.petCarousel);
petCarousel.setPageCount(petCarouselBitmaps.size());
petCarousel.setImageListener(new ImageListener() {
#Override
public void setImageForPosition(int position, ImageView imageView) {
imageView.setImageBitmap(petCarouselBitmaps.get(position));
}
});
}
...
}
And Here is the BitmapHandler Class that downloads image from URL to a Bitmap using picasso:
public class BitmapHandler extends Thread {
ArrayList<String> urlList;
private ArrayList<Bitmap> loadedBitmap;
public BitmapHandler(ArrayList<String> list){
this.urlList = list;
this.loadedBitmap = new ArrayList<>();
}
public ArrayList<Bitmap> getProcessedBitmap(){
this.run();
//Returning the loaded bitmap as a ArrayList<Bitmap> Object.
return loadedBitmap;
}
#Override
public void run() {
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
loadedBitmap.add(bitmap);
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
for (String url : urlList) {
Picasso.get().load(url).into(target);
}
}
}
Thank you for any helps!
Problem: . How to wait Picasso to finish processing URLs
Solution:
I think you can go with Target callback:
private Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
#Override
public void onBitmapFailed() {
}
}
And while loading image, you need to write:
Picasso.with(this).load(myURL).into(target);
Just for the information:
onBitmapLoaded() mostly used to perform image operations before we load into the view actually.
Picasso.LoadedFrom describes where the image was loaded from, whether it's MEMORY, DISK or NETWORK.
I think you can use placeholder & then the image is loaded it will show in Image View.
And if you want to delay use can use Thread.sleep(5000).
I am trying to make an android app from my website using webview such that I want a certain links in my website to open in landscape mode and others in portrait mode inside my webview. For this purpose, I have implemented below code inside the overridden function shouldOverrideUrlLoading which does the work, but not up to the mark. What this code does is, on clicking the landscape links the first time, it changes orientation of the present page to landscape mode but does not go to the website link and if I click it a second time when the current page is already in landscape mode, then it goes to the website link in landscape mode. This does the work, but it is very irritating because the link positions get changed in landscape mode and user will have to again search the link by scrolling and if the user clicks a portrait mode link during the second time, the present page turns to portrait mode but does not go to the website link. In short, the activity restarts due to orientation change, but the weird thing is this happens irrespective of the order of loadUrl and setRequestedOrientation functions and makes no difference on placing sleep ( for say 5 seconds ) before or after setRequestedOrientation, in either cases it sleeps ( for say 5 seconds ) first and then changes the orientation on first click and on second click, it sleeps first ( for say 5 seconds ) and then loads the url link and I am unable to understand the reason behind this strange behaviour ( Same feeling which quantum mechanics scientists experience ). I have tried everything like giving below code in AndroidManifest file :
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
And also tried overriding below functions in MainActivity file :
#Override
protected void onSaveInstanceState(Bundle outState )
{
super.onSaveInstanceState(outState);
mywebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
mywebView.restoreState(savedInstanceState);
}
#Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
Experts please help in resolving this orientation change on first click only.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
/* try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
if(url.contains("<<some keywords>>")){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
return true;
}
Please try this code ! i have implemented shouldOverrideUrlLoading,onPageStarted ,onPageFinished .Screen Orientation is changed in onPageStarted if any condition satisfy .
public class WebViewActivity extends AppCompatActivity
{
private WebView mywebView;
private ProgressBar myprogressBar;
private static final String TAG = "WebViewActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
myprogressBar = findViewById(R.id.myProgressBar);
myprogressBar.setVisibility(View.GONE);
mywebView = findViewById(R.id.myWebView);
mywebView.getSettings().setLoadsImagesAutomatically(true);
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mywebView.setWebViewClient(new MyBrowser());
mywebView.loadUrl("https://stackoverflow.com/");
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mywebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mywebView.restoreState(savedInstanceState);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private class MyBrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "shouldOverrideUrlLoading: loading ");
myprogressBar.setVisibility(View.VISIBLE);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
Log.d(TAG, "onPageStarted: started");
myprogressBar.setVisibility(View.VISIBLE);
/*
here you have to include the your keywords instead of tags [hardcoded string]
*/
if (url.contains("tags")) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
myprogressBar.setVisibility(View.GONE);
Log.d(TAG, "onPageFinished: finished");
super.onPageFinished(view, url);
}
}
}
I have a fragment that i'm calling again and again it has Picasso in it to load images from url when I pop back same fragment with different url to load image from, the previous image shown until new image loaded, how can i solve this problem, here is my code:
Picasso.with(context)
.load(url)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(ivParallex, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
getProductDetail(productId);
} else {
}
#Override
public void onError() {
}
});
So, here's my problem? I have it where it will slide out and then it will stay in that position and then you should be able to click on it again and it will slide back to the original location.
But instead I have to click on where it FIRST was at. Even when it's slided out. I want to make it after the animation I can click it any where, that it slided out.
Here's my code
public void sideBar()
{
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
if(out == 0)
{
mSlideInRight = AnimationUtils.loadAnimation(this, R.anim.slide_in_right);
mSlideInRight.setFillAfter(true);
sidebar.startAnimation(mSlideInRight);
out= 1;
}
else if(out == 1)
{
mSlideInLeft = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
sidebar.startAnimation(mSlideInLeft);
out=0;
}
}
and this is the code for when you click on it
public void onClick(View v) {
ImageView sidebar = (ImageView)findViewById(R.id.sidebar);
ImageView popup = (ImageView)findViewById(R.id.popup);
ImageView popup2 = (ImageView)findViewById(R.id.popup2);
switch(v.getId())
{
case R.id.sidebar:
sideBar();
break;
}
}
Animations only affect how your sidebar ImageView is drawn, not its actual position. After your animation completes, you should update the sidebar's layout parameters to your desired position. You can do this in an AnimationListener if you want the position to be updated as the animation runs or after it is complete, or right after your call to startAnimation if you want the position change to happen as the animation starts.
mSlideInRight.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation anim) {};
#Override
public void onAnimationRepeat(Animation anim) {};
#Override
public void onAnimationEnd(Animation anim) {
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(...);
// Set properties of layoutParams here
sidebar.setLayoutParams(layoutParams);
};
});
Replace RelativeLayout with whatever layout your ImageView is a child of.