Why do Images in Android aren't being shown on a TableView? - android

I'm working on Titanium 2.1.3 and deploying for both iOS and Android.
I'm having trouble displaying images on ImageView on Android in a TableView, if I do something like clicking on the search bar and show the keyboard then the images are shown or if I scroll the TableView the images appear and disappear with no apparent reason. It seems that unless I do something that forces a layout refresh on the TableView, the images aren't rendered.
The images are being loaded the same way in both Android and iOS, which is like this:
var itemIconHolder = Titanium.UI.createView({
width : '100dp',
left : '55dp',
height : "100%"
});
var itemIcon = Titanium.UI.createImageView({
left : '0dp',
height : '100%',
});
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "thumb-" + filename);
itemIcon.image = f;
itemIconHolder.add(itemIcon);
This problem doesn't happen in iOS, on iOS devices/simulator the behaviour is normal and flawless, but on Android devices/emulator it happens.
Do I have to load the images differently in Android? Am I missing something? I hope someone could help me in this one.
Thanks in advance.
EDIT
I tried these approaches:
itemIcon.image = f.read();
and
itemIcon.image = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, "thumb-" + filename);
but the images still aren't rendered until I make something that causes the TableView to refresh in some way.

I found a workaround for this particular issue on Android.
Since it takes a layout refresh for the images to be rendered properly, what I did was to animate the table, moving it 1dp in a direction and at the completition of said animation I animated it again to return it to its original position. This is the code I used:
var table_bottom = '-1dp'
var tableAnimation = Ti.UI.createAnimation({
bottom : table_bottom,
duration : 100
});
tableAnimation.addEventListener('complete', function(e){
var table_bottom = '50dp';
if (osname === 'android')
{
table_bottom = '0dp'
}
table.animate({
bottom : table_bottom,
duration : 100
});
});
I've encountered this kind of problem with both Titanium SDK 2.1.3 and Titanium 3.0.

Related

Titanium Flip animation for Android

I have superview (flashcardView ), i added the two subviews(frontView and backView) to this view. If user tap the any one of the view it flip and shows the other view. I am using the following code for achieve this functionality.
frontView.addEventListener('click', function(e) {
var animation = require('alloy/animation');
animation.flip( frontView,backView, 'horizontal',500, function(){
});
});
backView.addEventListener('click', function(e) {
var animation = require('alloy/animation');
animation.flip( backView,frontView, 'horizontal',500, function(){
});
This code is only working in iOS devices. It is not working in Android devices. Pls suggest me to do the flip animation for Android.
In Titanium available animations depend on the system (iOS or Android) you use. Therefore the code you use is only working for iOS. As far as I know there is no built-in flip animation for Android. But there is a solution by another user he gave in this answer.
Here is the code he uses:
var anim_minimize = Titanium.UI.createAnimation({width:0,duration:500});
var anim_maximize = Titanium.UI.createAnimation({width:320,duration:500});
tabGroup.animate(anim_minimize);
setTimeout(function(){
tabGroup.animate(anim_maximize);
},500);
Basically he creates the views next to each other and uses another animation which should look the same as a flip animation.
Another option you could try is using a module such as Flipium.

Can not display Android Remote Images Titanium SDK

I have an image view and try to load remote image but it's can not show up.
I'm already search and try any suggestion on this forum but not work. I'm using SDK 3.2.2 on Mac OS X Maverick
My code is like here, and it's running good on iOS.
var iconImage = Ti.UI.createImageView({
width:40,
height:40,
left:5,
hires:true,
defaultImage:'/images/default.jpg',
image:urltoimage
});
sample image
this is a bug or something wrong with my code?any suggestion how to do remote image? Please help..
Use this
exports.LoadRemoteImage = function (obj,url) {
var xhr = Titanium.Network.createHTTPClient();
xhr.onload = function()
{
Ti.API.info('image data='+this.responseData);
obj.image=this.responseData;
};
// open the client
xhr.open('GET',url);
// send the data
xhr.send();
};
save on new file and named "ImageLoader.js"
I'm forget where I found that code, but I'm used that to handle any image from internet.
usage:
var ImageLoader = require('ImageLoader');
var imageView = Ti.UI.createImageView({
width:100,
height:100
})
ImageLoader.LoadRemoteImage(imageView,"http://i1314.photobucket.com/albums/t567/vademahendra/sightseeing_zpsd91fa049.png?t=1401680320");
I hope this can help you
I've run application with this code:
var urltoimage = 'http://i1314.photobucket.com/albums/t567/vademahendra/sightseeing_zpsd91fa049.png?t=1401680320';
var win = Ti.UI.createWindow();
var iconImage = Ti.UI.createImageView({
width: 40,
height: 40,
left: 5,
hires: true,
defaultImage: '/images/default.jpg',
image: urltoimage
});
win.add(iconImage);
win.open();
All worked properly, so the issue has to be on your device or simulator.
Check network connection and if airplane mode is enabled.
Does it show up on the device and just not on the emulator, or does it not show up at all?
Sometimes Android can be temperamental about remote images. CDNs usually work really well which is probably why daniula's code worked. Things to try are:
Change the pixel dimensions of your remote image to be smaller - test
Change the image file size of course using different compressions - test
Try different file types
Try using the same image url daniula used to see narrow it down to the image or the code: if the images shows up, then it's a problem with the serving of the images; if the image does not show, it may be an issue with your code.

Phonegap breaks jQuery Mobile pages when reloaded - visiting the same page again

I have been working on a project using PhoneGap and jQuery Mobile. My setup uses multiple pages inside a single html file.
I am facing a problem and I haven't found anything similar anywhere:
When I revisit a page, which means I visited it, then navigated to another page, and now returned to the first page, there is some padding between the header and the content, and also between the footer and the content of the page.
As screenshots show below:
http://i.imgur.com/neBwZYx.png
Below you can see the padding added, red background, when returned to the page above afterwards (this happens with every page)
http://i.imgur.com/u1whW9b.png
The code is very large to post here so if anyone has a suggestion please tell me how to fix this or where to look for the problem.
It should be noted that the problem exists only if the app runs on Android tablets, and not when viewed through the browser on my laptop.
Thank you
You can force correct content height with this function:
function getRealContentHeight() {
var header = $.mobile.activePage.find("div[data-role='header']:visible");
var footer = $.mobile.activePage.find("div[data-role='footer']:visible");
var content = $.mobile.activePage.find("div[data-role='content']:visible:visible");
var viewport_height = $(window).height();
var content_height = viewport_height - header.outerHeight() - footer.outerHeight();
if((content.outerHeight() - header.outerHeight() - footer.outerHeight()) <= viewport_height) {
content_height -= (content.outerHeight() - content.height());
}
return content_height;
}
It must be activated during the pageshow event because only at that point page height is correct:
$(document).on('pageshow', '#index', function(){
$.mobile.activePage.find('.ui-content').height(getRealContentHeight());
});
Working example: http://jsfiddle.net/Gajotres/nVs9J/
If you want to find out more about this function read my other article: https://stackoverflow.com/a/14550417/1848600

Android Browser Triggers jQuery $(window).resize() on scrolling

I have recently come across something quite wierd, I'm not sure if it's maybe me just missing something but I can't understand why this is happening.
I have a site that has the following jQuery snippet running on it:
$(window).resize(function(){
alert("Resize fired!");
});
When I go to the site on an Android phone browser, and simply scroll up and down the site, I can see the alert.
The Android browsers scroll bars (which fade in and out) are overlayed ontop of the entire site and don't seem to cause any resizing of the window, so I'm guessing this event isn't being fired by them.
Does anyone know why the Android browser is firing this event on scrolling?
Any information will be greatly appreciated.
EDIT:
I have tried setting CSS for body, setting overflow-y to scroll to see if that was a viable solution but the event is still being fired on scrolling on Android.
EDIT #2:
I am using the following metatag in my HTML:
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
I was having the same problem, my solution was to check if the window size actually changed, for doing it I needed to store the past window width somewhere in my app. The code could be something like this:
$(window).resize(function() {
clearTimeout(app.resize.timer)
app.resize.timer = setTimeout(function(){
var window_changed = $(window).width() != app.size.window_width
if(window_changed) console.log('Window size changed! resize site')
}, 500)
})
I did not count on the window height because my Android browser hides and shows the address textbox when I scroll down the site making the window height change on vertical scroll
#john-mccollum is correct in the comments. It appears to be the disappearing browser interface causing a change in height that triggers the resize event. So check for change in width specifically in your function if you are doing responsive design stuff where you want to check if the width has been resized.
$(window).resize(function(){
var w = $(window).width();
if (typeof checkw == 'undefined') checkw = w;
if (w!=checkw) {
console.log("The width changed from "+checkw+" to "+w);
// do your responsive magic!
checkw = w;
}
});
Not required to make this work, but this pairs well with the Paul Irish / John Hann "smartresize" method.
i'm having the same problem too!
the problem is true because the height of the browser in Android will change when the url bar hide and show. So, we have to make the browser reload only happens when the width size changes.
i saw this question in Stackoverflow show me how to do this. And this is the jsfiddle.
var doit;
function resizedw(appwidth){
var window_changed = $(window).width() != appwidth;
if ($(window).width() != appwidth){
("body").append("did it"+appwidth+" ");
}
past_width = $(window).width();
}
var past_width = $(window).width();
window.onresize = function() {
clearTimeout(doit);
doit = setTimeout(function() {
resizedw(past_width);
}, 100);
};

Titanium Not Loading all HTTPClient XML Requests

I have an application that has 5 windows, and when clicking on the tabs above them passes the window the URL of the RSS to load. It's maddening, and it's about to make me jump ship from Appcelerator to PhoneGap.
This is a last ditch effort before jumping ship..........please tell me I'm doing something silly. It works fine if a bit sluggish on the emulator, but on my Dell Streak 5 it's maddeningly inconsistent. Sometimes the XML renders (it always returns with a 4 readystate), somethings it just hangs with the activity indicator spinning. If I rebuild without the activity indicator many times just nothing appears in the windows. No errors to speak of that I can see.
HEre is the offending code. Please tell me I'm doing something horribly wrong to make me happy.
data = [];
//load RSS Feed
Ti.API.info('>>>> loading RSS feed '+url);
//Show Loading Animation
navActInd.show();
var xhr = Ti.Network.createHTTPClient();
xhr.timeout = 10000;
xhr.open('GET', url);
xhr.onload = function()
{
var xml1 = xhr.responseXML;
var items = xml1.documentElement.getElementsByTagName("item");
//Loop Through XML and Build Rows
for (var i = 0; i < items.length; i++) {
this_post_title = items.item(i).getElementsByTagName("title").item(0).text;
this_post_merchant = items.item(i).getElementsByTagName("category").item(0).text;
post_cid = items.item(i).getElementsByTagName("source").item(0).text;
var row = Ti.UI.createTableViewRow();
var post_title = Ti.UI.createLabel({text: this_post_title});
row.add(post_title);
row.thisSource = post_cid;
row.thisMerchant = this_post_merchant;
data.push(row);
}
navActInd.hide();
var tv = Titanium.UI.createTableView({
data: data,
top: 0,
width: 'auto',
height: 'auto'
});
//Add Table to Window
window.add(tv);
navActInd.hide();
first, you shouldn't hide the navActInd twice. you also should add an onerror function to print the status code.
without taking a look at your xml file it's hard to form an opinion.
i understand your frustration about titanium. there are a lot frustration things about it since it's quite complicated to build such a framework. i build a really comprehensive app with titanium and have had a lot of such moments but nether the less i found a solution for all problems;)
I have seen similar issues when the xml is not what I have expected, you should put a try catch block around your xmlparsing block and see if an error is thrown
you should add an onError handler to you code.
from a complexity perspective, you do not need a label to set the title of the row; these lines can be edited
var row = Ti.UI.createTableViewRow();
var post_title = Ti.UI.createLabel({text: this_post_title});
row.add(post_title);
with this line, also use rowClassName whenever possible
var row = Ti.UI.createTableViewRow({title: this_post_title, className:'#row'});
row.add(post_title);
I would also suggest you take a look at the kitchen Sink demo for a complete example. https://github.com/appcelerator/KitchenSink/tree/master/KitchenSink

Categories

Resources