Customized Toast

Web Hosting
In this tutorial, I will show you how to customized Toast in Android. See output below.


Next is add this image in your drawable folder.

Next is add this custom_toast.xml in your drawable folder.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_main"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:background="@drawable/toast_corner">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="70dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/warning_icon" />

    <TextView
        android:id="@+id/tv_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/iv_icon"
        android:paddingRight="16dp"
        android:paddingBottom="5dp"
        android:text="Toast"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

</RelativeLayout>

Next is add this toast_corner.xml to your drawable folder for the radius of your toast.

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <solid
        android:color="#000000" >
    </solid>

    <stroke
        android:width="2dp"
        android:color="#000000" >
    </stroke>

    <padding
        android:left="5dp"
        android:top="5dp"
        android:right="5dp"
        android:bottom="5dp"    >
    </padding>

    <corners
        android:radius="11dp"   >
    </corners>

</shape>

Next is your main_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Show Toast" />

</RelativeLayout>

And lastly is your MainActivity.java

package com.client.apps.views.toast;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener{
 
Button btn_toast;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
btn_toast = (Button) findViewById(R.id.btn_toast);
btn_toast.setOnClickListener(this);
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn_toast:
Toast("Invalid Entry, Please Check. Just Give me a reason!");
break;
}
}

public void Toast(String toast){
//get the LayoutInflater and inflate the custom_toast layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup)
findViewById(R.id.rl_main));
 
//get the TextView from the custom_toast layout
TextView text = (TextView) layout.findViewById(R.id.tv_toast);
text.setText(toast);
 
//create the toast object, set display duration,
//set the view as layout that's inflated above and then call show()
Toast t = new Toast(getApplicationContext());
t.setDuration(Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL, 0, 0);
t.setView(layout);
t.show();
}
 
}


Web Hosting
That's it. Hope this simple post may help you.

Customized Checkbox

Web Hosting
In this tutorial, I will show you a customized checkbox. See output below.


 Next is add this 2 image below in your drawable folder.



Next is add this xml named custom_checkbox_design.xml

<?xml version="1.0" encoding="utf-8"?>
     
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
     
         <item android:state_checked="true" android:drawable="@drawable/checked" />
         <item android:state_checked="false" android:drawable="@drawable/unchecked" />
     

    </selector>

Next is your main_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hobbies:"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <CheckBox
        android:id="@+id/chk_chc1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_title"
        android:button="@drawable/custom_checkbox_design"
        android:text="Basketball" />

    <CheckBox
        android:id="@+id/chk_chc2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/chk_chc1"
        android:layout_below="@+id/chk_chc1"
        android:button="@drawable/custom_checkbox_design"
        android:text="Playing Computer" />

    <CheckBox
        android:id="@+id/chk_chc3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/chk_chc2"
        android:layout_below="@+id/chk_chc2"
        android:button="@drawable/custom_checkbox_design"
        android:text="Reading Books" />

    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/chk_chc3"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="Check" />


</RelativeLayout>

And lastly is your MainActivity.java

package com.client.apps.views.checkbox;

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

public class MainActivity extends Activity implements OnClickListener{

Toast msg;
Button check;
CheckBox chk1, chk2, chk3;


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

check = (Button)findViewById(R.id.btn_check);
chk1 = (CheckBox)findViewById(R.id.chk_chc1);
chk2 = (CheckBox)findViewById(R.id.chk_chc2);
chk3 = (CheckBox)findViewById(R.id.chk_chc3);
check.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

switch(v.getId()){
case R.id.btn_check:
if (chk1.isChecked()==true && chk2.isChecked()==true && chk3.isChecked()==true)
             {
                             msg = Toast.makeText(MainActivity.this,
                                             "BasketBall, PLaying Computer, Reading Books", Toast.LENGTH_SHORT);
                              msg.show();
             }
else if (chk1.isChecked()==true && chk2.isChecked()==false && chk3.isChecked()==false)
                             {
                             msg = Toast.makeText(MainActivity.this,
                                             "BasketBall", Toast.LENGTH_SHORT);
                                              msg.show();
                             }
else if (chk1.isChecked()==false && chk2.isChecked()==true && chk3.isChecked()==false)
             {
             msg = Toast.makeText(MainActivity.this,
                             "Playing Computers", Toast.LENGTH_SHORT);
                              msg.show();
             }  
else if (chk1.isChecked()==false && chk2.isChecked()==false && chk3.isChecked()==true)
             {
             msg = Toast.makeText(MainActivity.this,
                             "Reading Books", Toast.LENGTH_SHORT);
                              msg.show();
             } 
else if (chk1.isChecked()==true && chk2.isChecked()==true && chk3.isChecked()==false)
             {
             msg = Toast.makeText(MainActivity.this,
                             "BasketBall, Playing Computers", Toast.LENGTH_SHORT);
                              msg.show();
             } 
else if (chk1.isChecked()==false && chk2.isChecked()==true && chk3.isChecked()==true)
             {
             msg = Toast.makeText(MainActivity.this,
                             "Playing Computers, Reading Books", Toast.LENGTH_SHORT);
                              msg.show();
             } 
else if (chk1.isChecked()==true && chk2.isChecked()==false && chk3.isChecked()==true)
             {
             msg = Toast.makeText(MainActivity.this,
                             "BasketBall, Reading Books", Toast.LENGTH_SHORT);
                              msg.show();
             } 
else
             {
             msg = Toast.makeText(MainActivity.this,
                             "Nothing Selected", Toast.LENGTH_SHORT);
                              msg.show();
             }
break;
}
}




}


Web Hosting
That's it, hope this may help you. Happy Coding!


Customized Radio Button

Web Hosting
In this tutorial, I will show you customized radio button. See output below.


In your drawable folder, add this xml named custom_radio_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item android:state_checked="true" android:drawable="@drawable/checkedradiobutton" />
    <item android:state_checked="false" android:drawable="@drawable/unchekedradiobutton" />

</selector>


Next is add this 2 images in drawable folder.




Next is your layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/rdg_ask"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/rb_chc1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/custom_radio_button"
            android:checked="true"
            android:textColor="#9D9D9D"
            android:text="Yes" />

        <RadioButton
            android:id="@+id/rb_chc2"
            android:layout_width="wrap_content"
            android:button="@drawable/custom_radio_button"
            android:layout_height="wrap_content"
            android:textColor="#9D9D9D"
            android:text="No" />

        <RadioButton
            android:id="@+id/rb_chc3"
            android:layout_width="wrap_content"
            android:button="@drawable/custom_radio_button"
            android:layout_height="wrap_content"
            android:textColor="#9D9D9D"
            android:text="Maybe" />
    </RadioGroup>

    <Button
        android:id="@+id/btn_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Check" />


</RelativeLayout>

And lastly is your MainActivity.java

package com.client.apps.views.radiobutton;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity implements OnClickListener {

Button btn_check;
RadioGroup radioGroup;
RadioButton get_val;
String radioButtonSelected = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.rdg_ask);
btn_check = (Button) findViewById(R.id.btn_check);
btn_check.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int selectedId = radioGroup.getCheckedRadioButtonId();
get_val = (RadioButton) findViewById(selectedId);
// find the radiobutton by returned id
switch(v.getId()) {
 
case R.id.btn_check:
Log.e("val", get_val.getText().toString());
break;
}
}

}


Web Hosting
That's it, hope this may help you. Happy Coding!