Thursday, April 28, 2011

Read Text From the Raw Content in Resource Folder.

You can store the raw content in the raw folder and read using the below method..
1. Create a file in and Raw content
2. Create Method called readTextResource() in the util class
3. Use the below code to read the data
StringBuffer strBuffer = new StringBuffer();
InputStream inputStream = null;
try {
inputStream = owner.getResources().openRawResource(resourceId);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String str = br.readLine();
while (str != null) {
strBuffer.append(str);
str = br.readLine();
}
} catch (IOException e) {
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
}
}
4. Return the StringBuffer value.
5. If you need a html format, you can use the html util class
Html.fromHtml(text); - It returns Spanned instance.

Sample Code
-----------
public class MainActivity extends Activity {

private Spanned mText = null;
private TextView mTextView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.sampleText);
if (mText == null) {
String text = readTextResource(R.raw.license, this);
mText = Html.fromHtml(text);
}
mTextView.setText(mText);
}
public String readTextResource(int resourceId, Activity owner) {
StringBuffer strBuffer = new StringBuffer();
InputStream inputStream = null;
try {
inputStream = owner.getResources().openRawResource(resourceId);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String str = br.readLine();
while (str != null) {
strBuffer.append(str);
str = br.readLine();
}
} catch (IOException e) {
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
}
}
return strBuffer.toString();
}
}

Monday, April 25, 2011

Android Tips - Using Layout Param

Layout Param is used to change the layout properties. You can create a layout instance and assign to required view.

LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Sample Code

LinearLayout.LayoutParams mLayoutParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button mButton = new Button(this);
mButton.setLayoutParams(layoutParam);

The above code works fine but this is not a current solution. It should like the below code

Button mButton = new Button(this);
ViewGroup.LayoutParam mLayoutParam= mButton.getLayoutParams();
mLayoutParam.height = ViewGroup.LayoutParams.FILL_PARENT


Thanks to Ashok , I have done the mistake and corrected me

Android Window Leak

The WindowLeaked exception in the log usually happens when you have some sort of async task that is finishing after the activity that began it is destroyed.
Solution for the above issue, we can dismiss the progress dialog before finish the activity.


Issue :

Activity com.sample.ViewLeakIssueActivity has leaked window com.android.internal.policy.impl.TvWindow$DecorView@6bc30788 that was originally added here
E/WindowManager( 1687): at android.view.ViewRoot.(ViewRoot.java:230)
E/WindowManager( 1687): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
-----

Sample Leak Issue
public class ViewLeakIssueActivity extends Activity {
volatile ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pd = ProgressDialog.show(ViewLeakIssueActivity.this, "", "test...", true);

new Thread() {
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("@@@@@@@@@@@"+ViewLeakIssueActivity.this+pd.isShowing());

pd.dismiss();
}
}.start();

finish();

}

}

Solution for the above issue, we can dismiss the progress dialog before finish the activity.

@Override
public void finish() {
if(pd.isShowing()){
pd.dismiss();
}
super.finish();
}