Tuesday, 3 December 2013

Hyperlink in a Text View

Now we will learn to embed Hyperlinks in a TextView.

First make a text view in the layout file.

<TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/link1" />


Now open the "strings.xml" file under the "values" folder. And add the following code to it.

<string name="link1"><![CDATA[This is Link to <a href="http://www.google.com/">Google</a>]]></string>


Finally make changes in the activity file.

package com.codingredefined.linkinatextview;

import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView tv=(TextView)findViewById(R.id.tv);
tv.setText(Html.fromHtml(getString(R.string.link1)));
Linkify.addLinks(tv, Linkify.ALL);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}

}


The following output will be obtained.


Clicking on Google will automatically open google.com in your default browser.

Custom Toast

So back to blogging after a long time with a lot new stuff.

This time we are making a CUSTOM toast designed completely by you.
This is what will appear as a toast when you will click the button.


First create a button in the native layout file to trigger the toast.

<Button
        android:id="@+id/buttonShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Show Custom Toast" />


After that create a new layout file in the same folder named "custom_toast.xml". And create an image view and a text view in that layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        />
 
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textColor="#DC143C"
        />

</RelativeLayout>


Now the final step is to make changes in the activity file.

package com.codingredefined.customtoast;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity
{
Button button;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.buttonShow);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
LayoutInflater inflater=getLayoutInflater();
View layout=inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.toast_layout_root));
ImageView image=(ImageView)layout.findViewById(R.id.image);
image.setImageResource(R.raw.reebok);
TextView text=(TextView)layout.findViewById(R.id.text);
Toast toast=new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}

}


The following output should be observed.


Saturday, 12 October 2013

Android Progress Dialog

In this exercise we will learn to create a Progress Dialog.

Lets create a button to trigger the progress dialog.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Start" />


Now to trigger the progress dialog

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
Button st;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
st=(Button)findViewById(R.id.button1);
st.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
dialog=ProgressDialog.show(MainActivity.this,"Progress Dialog","Loading...",true);
Thread t=new Thread()
{
public void run()
{
try
{
Thread.sleep(5000);
dialog.cancel();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
}
}
};
t.start();
}
});
}
}


The program has been coded to show the progress dialog for 5 seconds.