Friday, 6 December 2013

Custom Dialog

So here we are with a simple method to create a own designed dialog box.

Create a button to activate the dialog box.

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Click Button"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Button" />


Now create a new layout file named "custom_dialog.xml" to design your dialog box.

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    />

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#DC143C"
    android:layout_toRightOf="@+id/image"
    />
   
<Button
    android:id="@+id/dialogButtonOK"
    android:layout_width="100px"
    android:layout_height="wrap_content"
    android:text="OK"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:layout_below="@+id/image"
    />


Now make changes in the main activity file.

package com.codingredefined.customdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity
{
Button button;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
button=(Button)findViewById(R.id.buttonShowCustomDialog);
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
final Dialog dialog=new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text=(TextView)dialog.findViewById(R.id.text);
text.setText("Android Custom Dialog");
ImageView image=(ImageView)dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton=(Button)dialog.findViewById(R.id.dialogButtonOK);
dialogButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
}
});
dialog.show();
}
});
}

}


Following output will be obtained.



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.