Skip to content

Instantly share code, notes, and snippets.

@kumar-aakash86
Last active December 15, 2018 15:05
Show Gist options
  • Save kumar-aakash86/646dbd586dd62273af98e9cb4f9efe59 to your computer and use it in GitHub Desktop.
Save kumar-aakash86/646dbd586dd62273af98e9cb4f9efe59 to your computer and use it in GitHub Desktop.
Customizing html string with your custom html tags in Android
package r.systems.demo1.utils;
import android.util.Log;
// jsoup library is required for this
// implementation 'org.jsoup:jsoup:1.11.3'
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class HtmlCustomizer {
private String htmlBody;
private static String TAG = "HtmlCustomizer";
public HtmlCustomizer(String body){
this.htmlBody = body;
}
public void setHtmlBody(String body){
this.htmlBody = body;
}
public String getOriginalHtmlBody(){
return this.htmlBody;
}
public String putTextAtParagraphLocation(int position, String htmlToAppend){
String tempHtml = this.htmlBody;
Document doc = Jsoup.parse(tempHtml);
Elements newsHeadlines = doc.select("p");
position--;
if(newsHeadlines.size()>= position){
int index = tempHtml.indexOf(newsHeadlines.get(position).toString());
tempHtml = (new StringBuffer(tempHtml).insert(index+newsHeadlines.get(position).toString().length(), htmlToAppend)).toString();
}
return tempHtml;
}
public String putTextAtParagraphOccurance(int position, String htmlToAppend){
String tempHtml = this.htmlBody;
if(position <= 1){
Log.e(TAG, "Position must be more than one");
return tempHtml;
}
Document doc = Jsoup.parse(tempHtml);
Elements newsHeadlines = doc.select("p");
if(newsHeadlines.size()>= 0){
int startIndex = 0;
int newsIndex = 0;
for (Element headline : newsHeadlines) {
newsIndex++;
if(newsIndex%position == 0) {
int index = tempHtml.indexOf(headline.toString(), startIndex);
if(index >= 0) {
tempHtml = (new StringBuffer(tempHtml).insert(index+headline.toString().length(), htmlToAppend)).toString();
startIndex = index+ headline.toString().length() + htmlToAppend.length();
}
}
}
}
return tempHtml;
}
public ArrayList<String> splitInTwo() {
ArrayList<String> splittedHtml = new ArrayList<>();
String fullHtml = Jsoup.clean(this.htmlBody, Whitelist.basic());
Document doc = Jsoup.parse("<div>" + fullHtml + "</div>");
Elements newsHeadlines = doc.select("div > *");
if (newsHeadlines.size() > 0) {
if (newsHeadlines.get(0).toString().startsWith("<p><img")) {
newsHeadlines.remove(0);
}
int position = Math.round((float) newsHeadlines.size() / 2);
String firstHtml = "", secondHtml = "";
for (int i = 0; i < position; i++) {
firstHtml = firstHtml + newsHeadlines.get(i).toString();
}
for (int i = position; i < newsHeadlines.size(); i++) {
secondHtml = secondHtml + newsHeadlines.get(i).toString();
}
splittedHtml.add(firstHtml);
// splittedHtml.add("<h1>HELLO BROTHER</h1>"); FOR TESTING PURPOSE
splittedHtml.add(secondHtml);
return splittedHtml;
} else {
return new ArrayList<>();
}
}
}
public class WebViewActivity extends AppCompatActivity {
WebView wv_test;
HtmlCustomizer htmlCustomizer;
String html = <your html>;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
wv_test = (WebView) findViewById(R.id.wv_test);
htmlCustomizer = new HtmlCustomizer(html);
// TO SET THE HTML TAG AFTER A SPECIFIC <P> TAG LOCATION
// wv_test.loadData(htmlCustomizer.putTextAtParagraphLocation(1, "<h1>THIS IS MY CUSTOM HTML</h1>"), "text/html", null);
// TO REPEAT THE HTML TAG AFTER EVERY SPECIFIC <P> TAG LOCATIONS
// MUST BE MORE THAN 1
wv_test.loadData(htmlCustomizer.putTextAtParagraphOccurance(2, "<h1>THIS IS MY CUSTOM HTML</h1>"), "text/html", null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment