how do i edit HTML inside string JAVA CODE easily - java

i developed android app and i used HTML code to Print the result in formatted form.
the code will be like this :
String adress = editText1.getText().toString();
String phone = editText2.getText().toString();
String licenseNo = editText3.getText().toString();
"\n" +
" <div class=\"down\">\n" +
" <div class=\"EASY\">\n" +
" <img src=\"" + image + "\" alt=\"QR\" >\n" +
" </div>\n" +
" <table class=\" info_table\">\n" +
" <tbody>\n" +
" <tr>\n" +
" <td class=\"info\">" + phone + "</td>\n" +
" <td class=\"info\"> " + licenseNo + "</td>\n" +
" </tr>\n" +
" <tr>\n" +
" <td class=\"info_A\" colspan=\"2\"> " + adress + "</td>\n" +
" </tr>\n" +
" </tbody>\n" +
" </table>\n" +
" </div>";
as you see it should contain \n and + because its inside a string object contained variable values from user input.
i faced some difficulty when i need to Edit this Piece of code cose i will test it inside the application.
my question is what is the best way to get this code to edit and preview it outside android studio and reinsert it again.
i used find and replace but it make some problems.
hope i explained enough

You can create a XML file in the resources folder, read it and then use format in order to change the values there.

Related

How can I add a newline character to a String in Java?

In a Java application, I am creating a String like below (by concatenation):
String notaCorrente = dataOdierna + " - " + testoNotaCorrente;
My problem is that I want to add also something like an HTML newline character at the end of this String (that will be shown into an HTML page).
How can I implement it?
The newline character in Java is "\n" which will look like this:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "\n";
However, this will not display as you expect on your HTML page. You can try adding an html break tag, or add the
(Line Feed) and 
 (Carriage Return) HTML entities:
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
or
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "
&#10";
For a newline that will result in a line break in HTML, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>";
For a newline that will result in a line break in your text editor, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + System.lineSeparator();
And for both, use
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br>" + System.lineSeparator();
Why not \n?
\n is specific to certain operating systems, while others use \r\n. System.lineSeparator() will get you the one that is relevant to the system where you are executing your application. See the documentation for more info on this function, and Wikipedia for more info on newlines in general.
Simply, need to add <br/> (break line tag of HTML).
String notaCorrente = dataOdierna + " - " + testoNotaCorrente + "<br/>";
so, while you are going to display this content, <br/> tag will rendered on HTML page in form of new line.

How to Call an Html file in Java Code?

I am working on Java RestFul WebProjects. In my Project I have one util package that uses JavaMail.java class. In this project I am sending mails to our requirement for that purpose I am adding HTML tags at my String body variable and passing my html code.
My doubt is i need to place html code at one file in my localhost and I need to give the html file path to that string body variable. Is it possible and can anybody help me?
Here i am placing my JavaMail.Java class code:
public static void sendEmailForProfileActivation(int activationCode, String to) throws AddressException, MessagingException {
/**
* Sender's credentials
* */
String from = "helloworld#gmail.com";
String password = "888888";
String sub = "Activate Your Profile using activation code";
//String body = "Activate Profile Using the active Code: <b>" + activationCode + "</b>.";
String body="<html>"
+ "<body>"
+ "<table width=\"100%"+"\" cellpadding=\"0"+"\" cellspacing=\"0"+"\" bgcolor=\"e4e4e4"+"\">"
+ "<tr>"
+ "<td>"
+ "<table id=\"top-message"+"\" cellpadding=\"20"+"\" cellspacing=\"0"+"\" width=\"600"+"\" align=\"center"+"\">"
+ "<tr>"
+ "<td align=\"center"+"\">"
+ "<p>Trouble viewing this email? View in Browser</p>"
+ "</td>"
+ "</tr>"
+ "</table><!-- top message -->"
+ "<table id=\"main"+"\" width=\"570"+"\" align=\"center"+"\" cellpadding=\"0"+"\" cellspacing=\"15"+"\" bgcolor=\"ffffff"+"\">"
+ "<tr>"
+ "<td>"
+ "<table id=\"header"+"\" cellpadding=\"10"+"\" cellspacing=\"0"+"\" align=\"center"+"\" bgcolor=\"8fb3e9"+"\">"
+ "<tr>"
+ "<td width=\"570"+"\" bgcolor=\"#7EB646"+"\">"
+ "<h1><font color=\"white"+"\">HelloWorld Services 15 July 2015</font></h1>"
+ "</td>"
+ "</tr>"
+ "<tr>"
+ "</tr>"
+ "</table><!-- header -->"
+ "</td>"
+ "</tr><!-- header -->"
+ " <!--tr>"
+ "<td height=\"30"+"\">"
+ "<img src=\"http://dummyimage.com/570x30/fff/fff"+"\" />"
+ "</td>"
+ "</tr For spacing pupose one image to another where you want space you add this image-->"
+ "<tr>"
+ "<td>"
+ "<table id=\"content-6"+"\" cellpadding=\"0"+"\" cellspacing=\"0"+"\" align=\"center"+"\">"
+ "<p align=\"center"+"\">Activate Profile Using the active Code: <b>" + activationCode + "</b>.</p>"
+ "<p align=\"center"+"\">Activate Your Account</p>"
+ "</table>"
+ "</td>"
+ "</tr>"
+ "</table><!-- main -->"
+ "<table id=\"bottom-message"+"\" cellpadding=\"20"+"\" cellspacing=\"0"+"\" width=\"600"+"\" align=\"center"+"\">"
+ "<tr>"
+ "<td align=\"center"+"\">"
+ "<p>You are receiving this email because you signed up for updates</p>"
+ "<p>Unsubscribe instantly | Forward to a friend | View in Browser</p>"
+ "<p> <img src=\"file:///home/yavat6/Downloads/manDoctor.png"+"\" style=\"width:50px;float:left;height:35px;"+"\"/>If you have any queries contact to us via mail 24*7CONTACT US</p>"
+ "</td>"
+ "</tr>"
+ "</table><!-- bottom message -->"
+ "</table><!-- 100% -->"
+ "</body>"
+ "</html>";
sendMessage(from, password, to,sub, body);
}
Yes you can send the HTML file in your java code. You can make use of getResourceAsStream() to read your .html file and then get the content of this .html file in a String and send this content as a message like :-
InputStream is = Sum.class.getClassLoader().getResourceAsStream("test.html"); // read file as stream, add this html file in yur class path.
BufferedReader br = new BufferedReader(new InputStreamReader(is)) ;
StringBuilder sb = new StringBuilder();
String str = null;
while((str=br.readLine()) != null)
sb.append(str);
String send_to = "test#gmail.com"; // add sender here
Message msg = new MimeMessage(mailSession);
try{
msg.setSubject("Test Email");
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(send_to, false));
String message = sb.toString(); // your html message as string
msg.setContent(message, "text/html; charset=utf-8");
msg.setSentDate(new Date());
Transport.send(msg);
}catch(MessagingException me){
// catch exception here
}

How Html works to create a jLable in java

Since it is an array it repeats till the end of the loop. But Room 1 should not be repeated . it should be place at the top
first[ind] = new JLabel("<html>"
+ "<body>"
+ "<div id=r12style=border: 3px solid orange; margin-bottom: 5px;>"
+ " <h2>"
+ " Room 1"
+ " </h2>"
+ "<img src=" + icon + " width=\"95\" height=\"105\"></img>"
+ "</div>"
+ "</body>"
+ "</html>");
Try this (I haven't tested it):
IMM[ind] = new JLabel("<html><style >#aa {margin-left:25px;}</style>"
+ "<div id=\"aa\"></font><font color=\"rgb(0,0,0)\"size=\"5\">"
+ bed_no + "<font color=\"rgb(255, 204, 204, 150)\"size=\"1\">.</div></font></html>"
, "<html><img src=" + icon + "></html>", JLabel.LEFT_ALIGNMENT);
If you want the text under the icon:
IMM[ind].setHorizontalTextPosition(JLabel.CENTER);
IMM[ind].setVerticalTextPosition(JLabel.BOTTOM);
Edit - It's probably a good idea to break up the JLabel text by using a String variable. Because it looks almost unreadable.

Create HTML in java with background image

I have created an html string in which I need to set the background image.
protected void onPostExecute(HashMap<String,String> hPlaceDetails){
String backgroundImage = ??????
String name = hPlaceDetails.get("name");
String icon = hPlaceDetails.get("icon");
String vicinity = hPlaceDetails.get("vicinity");
String lat = hPlaceDetails.get("lat");
String lng = hPlaceDetails.get("lng");
String formatted_address = hPlaceDetails.get("formatted_address");
String formatted_phone = hPlaceDetails.get("formatted_phone");
String website = hPlaceDetails.get("website");
String rating = hPlaceDetails.get("rating");
String international_phone_number = hPlaceDetails.get("international_phone_number");
String url = hPlaceDetails.get("url");
String mimeType = "text/html";
String encoding = "utf-8";
String data = "<html>"+
"<body background="+backgroundImage+"><img style='float:left' src="+icon+" /><h1><center>"+name+"</center></h1>" +
"<br style='clear:both' />" +
"<hr />"+
"<p>Vicinity : " + vicinity + "</p>" +
"<p>Location : " + lat + "," + lng + "</p>" +
"<p>Address : " + formatted_address + "</p>" +
"<p>Phone : " + formatted_phone + "</p>" +
"<p>Website : " + website + "</p>" +
"<p>Rating : " + rating + "</p>" +
"<p>International Phone : " + international_phone_number + "</p>" +
"<p>URL : <a href='" + url + "'>" + url + "</p>" +
"</body></html>";
// Setting the data in WebView
mWvPlaceDetails.loadDataWithBaseURL("", data, mimeType, encoding, "");
}
Note: I have my background image (background9.png) in the location MyProject/res/drawable-xhdpi. Please suggest how I need to set
<body background="+backgroundImage+">
Instead of using \res\drawable-xhdp\background9.png as the path as you mentioned in your comment, you should use:
<body background='file:///android_res/drawable/background9.png'>
I've just tested this and it works perfectly.
Please add a tag for android to clarify your question.
Anyway, you need to get a reference for your parent layout, by using findViewById(R.id.yourActivitysParentLayout)
You need then to set it's background using a ResourceManager. From the top of my head, the method name you will need is yourparent.setBackgroungDrawable(drawable). The drawable can be obtained from a resource manager instance.
This can all be avoided by setting it's background in the xml if possible by using android:background="#drawable/background9" //sorry not sure if .png is needed. xhdp is not though.

Meeting request timing mismatch with full VS lite outlook version

I am creating meeting request with java code and sending it to full as well as lite version of outlook as shown below:
final SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
final long uid =System.currentTimeMillis();
iCalendarDateFormat.setTimeZone(TimeZone.getTimeZone(MRBSConstants.TIMEZONE));
final String calendarContent = "BEGIN:VCALENDAR\n"
+ "METHOD:REQUEST\n"
+ "PRODID: BCP - Meeting\n"
+ "VERSION:2.0\n"
+ "BEGIN:VEVENT\n"
+ "DTSTAMP:"+ iCalendarDateFormat.format(meetingEndTime) + "\n"
+ "DTSTART:" + iCalendarDateFormat.format(meetingStartTime) + "\n"
+ "DTEND:"+ iCalendarDateFormat.format(meetingEndTime) + "\n"
+ "SUMMARY:test request\n"
+ "UID:" + uid + "\n"
+ "ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:" + Arrays.toString(recipetList) + "\n"
+ "ORGANIZER:MAILTO:" + from+ "\n"
+ "LOCATION:" +loc + "\n"
+ "DESCRIPTION:" + body+ "\n"
+ "SEQUENCE:0\n" + "PRIORITY:5\n" + "CLASS:PUBLIC\n" + "STATUS:CONFIRMED\n" + "TRANSP:OPAQUE\n" + "BEGIN:VALARM\n"
+ "ACTION:DISPLAY\n" + "DESCRIPTION:REMINDER\n" + "TRIGGER;RELATED=START:-PT00H15M00S\n" + "END:VALARM\n" + "END:VEVENT\n" + "END:VCALENDAR";
And it is working fine with lite version of outlook means it giving correct time in lite version but in full version of outlook it showing different timings.?why
Here MRBSConstants.TIMEZONE value is GMT-5:30.
I have also tried adding VTIMEZONE COMPONENT but in that case outlook cant recognize the ics file as correct.
Do we have any general ics object which can work on both version?

Categories