Monday, November 17, 2014

Formatting Text with Html.fromHtml() In Android

Sample screen showing some formattings
Sample screen showing some formattings
You probably are going to use bold or italics the most, but there are many more supported.
Here is the list of all supported tags. You can find it in the source ofandroid.text.Html‘s inner class HtmlToSpannedConverter:
Supported HTML-Tags
TagsFormat
b, strongBold
i, em, cite, dfnItalics
uUnderline
subSubtext
supSupertext
bigBig
smallSmall
ttMonospace
h1 … h6Headlines
imgImage
fontFont face and color
blockquoteFor longer quotes
aLink
div, pParagraph
brLinefeed

Formatting text from your strings.xml

If you want to support text formatting from within your strings.xml file, you have to escape the tags – or use a CDATA section . Otherwise Android simply ignores them when reading the resource file.
To escape the tags you just need to replace all "<" characters. Luckily you do not have to escape the ">" characters as well. That way the HTML structure is still at least kind of readable. But only kind of.
If you use many HTML tags a CDATA section is better. For the sample above it looks like this:
1&lt;![CDATA[
2<p>Text with markup for <strong>bold</strong>
3and <em>italic</em> text.</p>
4<p>There is also support for a
5<tt>teletype-style</tt> font.
6But no use for the <code>code</code>
7tag!</p>
8]]&gt;
Even if you can add a lot of HTML tags, you are better off using only minor styling as mixing too much styles makes your text look uneasy instead of being more striking.
The following snippet shows how to use this string from within your Java code:
1TextView view = (TextView)findViewById(R.id.sampleText);
2String formattedText = getString(R.string.htmlFormattedText);
3Spanned result = Html.fromHtml(formattedText);
4view.setText(result);

Alternatives to consider

For longer texts that use HTML tags, I recommend to use raw files instead.
For more complicated formatting a WebView probably would be better.

Tuesday, August 5, 2014

Where to use SQL Server Cursor

                Cursor can be used when you need to manipulate data in a set on a row-by-row basis, however you can also use T-SQL WHILE loop, CASE expression and some system defined stored procedure like sp_MSforeachdbsp_MSforeachdb etc.
IN SQL Server the cursor can be implemented by 6 step process as:
  1. Declare cursor
DECLARE ins_cursor CURSOR
FOR
    Select statement…………….
  1. Open cursor
OPEN ins_cursor
  1. Fetch row from the cursor
FETCH NEXT FROM ins_cursor 
INTO @fileno,……….variable list
  1. Process fetched row
WHILE @@FETCH_STATUS = 0
BEGIN
….
…..
FETCH NEXT FROM ins_cursor 
INTO @fileno@fileno,……….variable list
END
  1. Close cursor
CLOSE ins_cursor
  1. Deallocate cursor
DEALLOCATE ins_cursor;

I have used cursor first time when I need to insert data from one table of one database into tables of another database. So I am explaining same example here

--declare variables to use in logic
DECLARE @RID INT
DECLARE @FILENO VARCHAR(50),@ISSUEDATE DATETIME,@REQDATE DATETIME,@RCNVARCHAR(50),@FILEREMARKS VARCHAR(200)
--decalare cusrsor
DECLARE INS_CURSOR CURSOR
    FOR
    --select statement
    SELECT FILENO,REQCONTROLNO,CONVERT(DATETIME,REPLACE(REQDATE,'-','/'),103),CONVERT(DATETIME,REPLACE(ISSUEDATE,'-','/'),103) ,REMARKS FROM ISSUE WHERERECEIVEDDATE IS NULL
    --open cursor
OPEN INS_CURSOR
--Fetch row from the cursor
FETCH NEXT FROM INS_CURSOR
INTO @FILENO,@RCN,@REQDATE,@ISSUEDATE,@FILEREMARKS
--process fetched row
WHILE @@FETCH_STATUS = 0
BEGIN
--insert into first table
INSERT INTO FISS1.DBO.FILEREQUEST(REQDATE,REQSTATUS,RCN,CORDSTATUS,FILEREMARKS)
VALUES (@REQDATE,1,@RCN,1,@FILEREMARKS)
----assigning primary key(identity) value
SELECT @RID=@@IDENTITY
--insert into 2nd table with foreign key @rid
INSERT INTO FISS1.DBO.ISSUE(REQID,FILENO,PRIORITY,PURPOSE,ISSUEDATE,STAUS)
VALUES(@RID,@FILENO,1,'GENERAL',@ISSUEDATE,1)
FETCH NEXT FROM INS_CURSOR INTO @FILENO,@RCN,@REQDATE,@ISSUEDATE,@FILEREMARKS
END
--close cursor
CLOSE INS_CURSOR;
DEALLOCATE INS_CURSOR;

Cursor Recommendations:
·         It is better to avoid using cursor because they consume memory for execution, so performance is less.
·         If you using cursor, then you should always close cursor after using it.

Further Reading

Thanks:queryingsql.com