Reputation: 19
I'm working on an offline story android app. but it' has a very big problem. whenever I add a paragraph in the story but all the text is mixing. I mean paragraph is not showing. see the image in the description to understand properly.
Example XML Code:
<root>
<story_id>1</story_id>
<story_title>Half of the Profit</story_title>
<story_date>25/07/2017</story_date>
<story_des>A rich man wanted to give a great feast to his friends. He got all kinds of dishes prepared but he could not get fish. He offered a reward to the man who would bring it. After some time a fisherman brought a big fish.
But the gate keeper would not let him in till he had promised to give him half the reward.
The fisherman agreed. The rich man was highly pleased and wanted to give him a lot of money, but the fisherman refused to take it. Instead, he demanded a hundred lashes on his back. All were surprised. At last the rich man ordered a servant to give him a hundred lashes.
When the fisherman had received fifty, he asked them to stop as he had a partner in the business.
It was the gatekeeper. The rich man understood the whole thing. He was given the remaining fifty lashes dismissed from the service. The rich man gave the fisherman a handsome reward.
</story_des>
</story>
</root>
Output:
StoryXMLHandler.java
package com.example.util;
import com.example.item.ItemStory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
public class StoryXMLHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = "";
ItemStory item = null;
private ArrayList<ItemStory> itemsList = new ArrayList<>();
public ArrayList<ItemStory> getItemsList() {
return itemsList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
currentValue = "";
if (localName.equals("story")) {
item = new ItemStory();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
if (localName.equalsIgnoreCase("story_id"))
item.setId(currentValue);
else if (localName.equalsIgnoreCase("story_title"))
item.setStoryTitle(currentValue);
else if (localName.equalsIgnoreCase("story_date"))
item.setStoryDate(currentValue);
else if (localName.equalsIgnoreCase("story_des"))
item.setStoryDescription(currentValue);
else if (localName.equalsIgnoreCase("story"))
itemsList.add(item);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = currentValue + new String(ch, start, length);
}
}
}
StoryDetailsActivity.java
Intent i = getIntent();
Id = i.getStringExtra("Id");
Title = i.getStringExtra("Title");
Description = i.getStringExtra("Desc");
Date = i.getStringExtra("Date");
private void setResult() {
txtName.setText(Title);
txtDate.setText(Date);
String mimeType = "text/html";
String encoding = "utf-8";
String htmlText = Description;
boolean isRTL = Boolean.parseBoolean(getResources().getString(R.string.isRTL));
String direction = isRTL ? "rtl" : "ltr";
String text = "<html dir=" + direction + "><head>"
+ "<style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/custom.ttf\")}body{font-family: MyFont;color: #999999;text-align:justify;font-size:14px;margin-left:0px}"
+ "</style></head>"
+ "<body>"
+ htmlText
+ "</body></html>";
webView.loadDataWithBaseURL(null, text, mimeType, encoding, null);
}
Upvotes: 0
Views: 101
Reputation: 1448
Use <p>
tags to create a new paragraph, like this:
<root>
<story_id>1</story_id>
<story_title>Half of the Profit</story_title>
<story_date>25/07/2017</story_date>
<story_des>
<p>A rich man wanted to give a great feast to his friends. He got all kinds of dishes prepared but he could not get fish. He offered a reward to the man who would bring it. After some time a fisherman brought a big fish.</p>
<p>But the gatekeeper would not let him in till he had promised to give him half the reward.</p>
<p>The fisherman agreed. The rich man was highly pleased and wanted to give him a lot of money, but the fisherman refused to take it. Instead, he demanded a hundred lashes on his back. All were surprised. At last the rich man ordered a servant to give him a hundred lashes.</p>
<p>When the fisherman had received fifty, he asked them to stop as he had a partner in the business.</p>
<p>It was the gatekeeper. The rich man understood the whole thing. He was given the remaining fifty lashes dismissed from the service. The rich man gave the fisherman a handsome reward.</p>
</story_des>
</story>
</root>
Upvotes: 1