Communicating information between html file and android [closed] - android

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an HTML file being utilized by webview in an activity. The HTML file makes up a large table that is scrollable in the webview. I want to add some javascript to the HTML file to float an image over the table. I also want this image to change positions on a time interval (every five seconds). I was hoping to accomplish such by using a random number generator to change the coordinates of the floated image every five seconds.
Firstly, is this implementation possible? I am new to javascript, but this doesn't seem like too far-fetched of an idea.
Secondly, I want the floated image to be clickable. And, upon clicking I want information to be placed in a string. My other question is, is there a way to call the string built by the HTML file into another activity in android?
I know my intentions may be unclear to some. If clarification is necessary, please ask. Thanks for any and all help :D

prob 1 : need an image to float
prob 2 : need the image to change direction at fixed time interval
first of all I would suggest you create a gif image which changes direction at every 5 seconds
thus your prob 2 is solved
now coming to first prob ,
use this
String HtmlFileString = "<html style=\"height:90%;width:95%\"><body>" +
"<img src=\""+**gif_char**+".gif"+"\" " +
"style=\"height:100%;width:100%\"></body></html>";
webview.loadDataWithBaseURL("file:///android_asset/",HtmlFileString,"text/html", "UTF-8","");
don't forget to keep the gif in asset folder
and also this gif variale you pass by yourself ,
it simply changes the name of your gif so you can show any image anytime you want
just call this line again with diff param for variable gif_char
if you dont want to use the GIF image , you can use a simple image and change the image by loading webview again and again with diff image passed every time
let me try to explain further
step 1 : read the html file using buffered reader ,
step 2 : store the file content in a string
step 3 : hange the image resource in html file string
step 4 : pass this html file string to method webview.loadDataWithBaseURL()
And also , Webview doesn't support onclick event but support ontouch event

To do this you will need a combination of Java and Javascript.
Inserting the image and moving it around will easiest in javascript similar to what is shown above.
To get the return string back out the android application you should look at using
addJavascriptInterface (Object object, String name)
This will allow the javascript to call public functions in the java object that you passed to this function.
This object could then transfer the string to a new activity using the extras bundle in the intent that is used to start the activity

Related

How to display an image in android ImageView from the <img> tag

I have an application which fetches data from a MySQL database. That data contains image urls which I have managed to implement an imageLoader class to display the image using the images url. I have however hit a dead end when it came to displaying an image which is contained in the text fetched and is wrapped in the <img> tag. Kindly assist me in displaying an image contained in the post content. Below is a sample of the data fetched.
This is a sample post, you may choose to continue reading or not. <a href='http://website.com/post'><img src=' http://website.com/post.jpg'></a>
This is a sample post, you may choose to continue reading or not
From the above text, How do I display http://website.com/post.jpg in an android imageview?
If all you want to do is extract the image url from the text returned from the database something like the following will suffice:
int start = s.indexOf("<img src='")+10;
int end = s.indexOf("'",start);
String url = s.substring(start, end));
where s is the string returned from the database
I finally got a perfect solution for my problem. Using jsoup, I am able to get the HTML tags in my string and also I am able to replace the entire HTML segment with whatever content I want. Thanks for your input and hope this answer will help someone sometime.

How to get JSON data from reddit after the first page

So I am currently making an app to work with and learn more about JSON (using GSON) and decided to go with a reddit browsing app. I am currently able to get the JSON data by using "http://www.reddit.com/.json" and this will get me the JSON for the first 25 posts. I am trying to get the information for the next posts by using
"http://www.reddit.com/.json?count=25&after=" + name_of_last_post
which name_of_last_post is the name field from my GSON for the last post. This however just brings up the first page's posts again with their numbering starting at 25, I get that the numbering starts at 25 because count is set to 25. What I am asking is what is the format of the string I can create to get that next set of JSON items. you can try this your self by going to reddit.com/.json and taking that into a json editor and formatting it nicely than going to the next page of reddit and adding .json before the ?count=25&after=, you will see that the text following after= does not seem to appear on the first JSON file anywhere. If there are any parts of this question that are still unclear please leave a comment and I will check in on it later. Thanks and have a great day.
There are a couple of fields that you can use in order to fetch the next page on JSON.
You could either retrieve the 'name' of the last post, or instead of parsing the whole block, you can retrieve the last couple of fields from the JSON that reddit gives you. There are a couple of fields that you can easily use to navigate pages forwards and backwards, they're conveniently called 'after' and 'before'
Having that value, you can mount your URL with it, which in this case is http://www.reddit.com/.json?count=25&after=t3_3gi42o
This will get you a new JSON, with different 'after' and 'before' fields, pictured below:

Need a way to save a meteor.collection as a text file on a mobile

a newbie to Meteor, but I have created under localhost a simple passenger counter I intend to use on an android app for a passenger survey (holding a laptop in an airport isn't a particularly wise idea). It works, but there is no export function. Basically uses meteor.collection as simple rows of data - one row been one passenger with a date/time stamp, two buttons - Add Passenger and Reset. I have tried looking everywhere for a simple answer, but what I want to do is now add to the client side (on browser - but ultimately on the mobile) a button called export, and when clicked, takes each row in the meteor.collection and export it to a text file - line by line. Very simple. Alas right now I haven't got a clue how to proceed. Seen some references to FS.collection, but don't think there is what I want. This is ultimately for a mobile application.
Thanks.
JSON.stringify each object into a larger object of pure text, say bytes
Create a blob from that text object, ex: blob = new Blob([bytes]);
Use the filesaver package to save to a file locally.

Searching a string from large text file of 10 Mb [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a large file of about 10 MB, I want to search a specific string, and this specific string may be used a lot of times in 10 Mb text file. I need results where this specific string is used. I want to do search like Google. For example when i write a string then google comes with matching Patterns . Your suggestions will be appreciated.
file formate
he is going to school.
we should do best deeds.
we should work hard.
.
.
.
.
Always speak truth.
i have search edit field in my application.
user write "should" in search edit field.and press search button.
a list should be opened in which searched words come with it's complete line.
for example result should be
we should do best deeds.
we should work hard.
A simple way to search a file and get a match "with context" is to use grep. For example, to match every line with "hello", and print one line before and three lines after, you would do
grep -b1 -a3 'hello' myBigFile.txt
You can use grep -E to allow for a wide range of PCRE regex syntax.
Without more detail it would be hard to give you a better answer.
EDIT 2
Now that you have explained your problem more clearly, here is a possible approach:
InputStream fileIn;
BufferedReader bufRd;
String line, pattern;
pattern = "should"; // get the pattern from the user, do not hard code. Example only
fileIn = new FileInputStream("myBigTextfile.txt");
bufRd = new BufferedReader(new InputStreamReader(fis, Charset.forName("UTF-8")));
while ((line = bufRd.readLine()) != null) {
if(line.contains(pattern)) {
System.out.println(line); // echo matching line to output
}
}
// Done with the file
br.close();
If you need to match with wildcards, then you might replace the line.contains with something that is a little more "hard core regex" - for example
matchPattern = Pattern.compile("/should.+not/");
(only need to do that once - after getting input, and before opening file) and change the condition to
if (matchPattern.matcher(line).find())
Note - code adapted from / inspired by https://stackoverflow.com/a/7413900/1967396 but not tested.
Note there are no for loops... maybe the boss will be happy now.
By the way - if you edit your original question with all the information you provided in the comments (both to this answer and to the original question) I think the question can be re-opened.
If you expect the user to do many searches it may be faster to read the entire file into memory once. But that's outside of the scope of your question, I think.

String and character sequence error (force close) Android

I am facing a problem in solving this issue.
I have 30 smiley images. In a grid view I display all the 30 images. So when I select the image based on the position of the image, the corresponding special character will be displayed from SQLite DB. Before I send message to service it is like "hi :)". What I need is to replace the ":)" with the corresponding smiley image in the local folder of my project (eg. R.drawable.facea.png). I am able to fetch the data and replace ":)" with "aaaaa"(example). But when I try to replace the ":)" with Char Sequence (Image), I get 'force close error'. Where am I going wrong?
I have a function to draw image and I get image as in Character Sequence. How can I use the value in this function globally. I also tried to create a global variable and use the CS value globally. But I still get 'force close error.
You need to use a span (specifically an ImageSpan) to be able to embed bitmap in a string. I have written about this on my blog which also covers some common pitfalls.
If you still cannot get it to work I would suggest posting the precise details of what is going wrong: i.e. your code, and the stacktrace that is getting dumped to logcat.

Categories

Resources