Tuesday, April 29, 2014

Custom DatePicker

  • Custom_Date_Picker.java

public class Custom_Date_Picker extends Activity implements ViewFactory {

               private Button btnPicDateTime;
               private TextSwitcher DaySwitcher, MonthSwitcher, YearSwitcher;
               private ImageView dayP, dayM, monthP, monthM, yearP, yearM;

               private int prevMonth;
               private Calendar cal;
               private static int year = 1819710;
               private static int day = 1819710;
               private static int month = 1819710;

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

                              btnPicDateTime = (Button) findViewById(R.id.btnPicDateTime);
                              btnPicDateTime.setOnClickListener(new OnClickListener() {

                                             @Override
                                             public void onClick(View v) {

                                                            int month = cal.get(Calendar.MONTH) + 1;
                                                            Toast.makeText(
                                                                                          getApplicationContext(),
                                                                                          cal.get(Calendar.YEAR) + "/" + cal.get(Calendar.MONTH)
                                                                                                                        + "/" + cal.get(Calendar.DAY_OF_MONTH),
                                                                                          Toast.LENGTH_LONG).show();                                                     finish();
                                             }
                              });

                              DaySwitcher = (TextSwitcher) findViewById(R.id.DaySwitcher);
                              DaySwitcher.setFactory(this);

                              MonthSwitcher = (TextSwitcher) findViewById(R.id.MonthSwitcher);
                              MonthSwitcher.setFactory(this);

                              YearSwitcher = (TextSwitcher) findViewById(R.id.YearSwitcher);
                              YearSwitcher.setFactory(this);

                              setAnimation(DaySwitcher);
                              setAnimation(MonthSwitcher);
                              setAnimation(YearSwitcher);

                              cal = Calendar.getInstance();

                              if (year != 1819710) {
                                             cal.set(Calendar.YEAR, year);
                              }
                              if (month != 1819710) {
                                             cal.set(Calendar.MONTH, month);
                              }
                              if (day != 1819710) {
                                             cal.set(Calendar.DAY_OF_MONTH, day);
                              }

                              dayP = (ImageView) findViewById(R.id.dayP);
                              dayM = (ImageView) findViewById(R.id.dayM);

                              monthP = (ImageView) findViewById(R.id.monthP);
                              monthM = (ImageView) findViewById(R.id.monthM);

                              yearP = (ImageView) findViewById(R.id.yearP);
                              yearM = (ImageView) findViewById(R.id.yearM);

                              getClickEvents(dayP, Calendar.DAY_OF_YEAR, 1);
                              getClickEvents(dayM, Calendar.DAY_OF_YEAR, -1);
                              getClickEvents(monthP, Calendar.MONTH, 1);
                              getClickEvents(monthM, Calendar.MONTH, -1);
                              getClickEvents(yearP, Calendar.YEAR, 1);
                              getClickEvents(yearM, Calendar.YEAR, -1);

                              update();
               }

               private void getClickEvents(ImageView imageView, final int field,
                                             final int value) {

                              imageView.setOnClickListener(new View.OnClickListener() {
                                             public void onClick(View v) {
                                                            cal.roll(field, value);
                                                            update();
                                             }
                              });
               }

               private void setAnimation(TextSwitcher switcher) {
                              Animation in = AnimationUtils.loadAnimation(getApplicationContext(),
                                                            android.R.anim.fade_in);
                              Animation out = AnimationUtils.loadAnimation(getApplicationContext(),
                                                            android.R.anim.fade_out);

                              switcher.setInAnimation(out);
                              switcher.setOutAnimation(in);

               }

               public void update() {

                              DaySwitcher.setText(String.valueOf(cal.get(Calendar.DAY_OF_MONTH)));
                              MonthSwitcher.setText(sort(cal.get(Calendar.MONTH)));
                              YearSwitcher.setText(String.valueOf(cal.get(Calendar.YEAR)));

               }

               private CharSequence sort(int i) {

                              String retrn = null;

                              if (i == 0) {
                                             retrn = "Jan";
                                             prevMonth = i;
                              }
                              if (i == 1) {
                                             retrn = "Feb";
                              }
                              if (i == 2) {
                                             retrn = "Mar";
                              }
                              if (i == 3) {
                                             retrn = "Apr";
                              }
                              if (i == 4) {
                                             retrn = "May";
                              }
                              if (i == 5) {
                                             retrn = "Jun";
                              }
                              if (i == 6) {
                                             retrn = "Jul";
                              }
                              if (i == 7) {
                                             retrn = "Aug";
                              }
                              if (i == 8) {
                                             retrn = "Sep";
                              }
                              if (i == 9) {
                                             retrn = "Oct";
                              }
                              if (i == 10) {
                                             retrn = "Nov";
                              }
                              if (i == 11) {
                                             retrn = "Dec";
                                             prevMonth = i;
                              }

                              if (i == 12) {
                                             if (prevMonth == 0) {
                                                            cal.roll(Calendar.MONTH, -1);
                                             } else {
                                                            cal.roll(Calendar.MONTH, 1);
                                             }
                                             update();
                              }

                              return retrn;
               }

               public View makeView() {
                              TextView t = new TextView(this);
                              t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                              t.setTextAppearance(getApplicationContext(), R.style.BlackText);

                              return t;
               }
              
}
____________________________________________________________________

  • custom_date_picker.xml
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="25dp"
        android:background="@color/Header_BG"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:padding="0.70dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/White"
            android:baselineAligned="false"
            android:orientation="horizontal" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="5dp" >

                <ImageView
                    android:id="@+id/dayP"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:contentDescription="@string/Strnull"
                    android:src="@drawable/arrowup" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/Blue" />

                <TextSwitcher
                    android:id="@+id/DaySwitcher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp" >
                </TextSwitcher>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/Blue" />

                <ImageView
                    android:id="@+id/dayM"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:contentDescription="@string/Strnull"
                    android:src="@drawable/arrowdown" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="5dp" >

                <ImageView
                    android:id="@+id/monthP"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:contentDescription="@string/Strnull"
                    android:src="@drawable/arrowup" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/Blue" />

                <TextSwitcher
                    android:id="@+id/MonthSwitcher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp" >
                </TextSwitcher>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/Blue" />

                <ImageView
                    android:id="@+id/monthM"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:contentDescription="@string/Strnull"
                    android:src="@drawable/arrowdown" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="vertical"
                android:paddingBottom="5dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:paddingTop="5dp" >

                <ImageView
                    android:id="@+id/yearP"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:contentDescription="@string/Strnull"
                    android:src="@drawable/arrowup" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/Blue" />

                <TextSwitcher
                    android:id="@+id/YearSwitcher"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp" >
                </TextSwitcher>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/Blue" />

                <ImageView
                    android:id="@+id/yearM"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:contentDescription="@string/Strnull"
                    android:src="@drawable/arrowdown" />
            </LinearLayout>
        </LinearLayout>

        <Button
            android:id="@+id/btnPicDateTime"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="@string/Ok" />
    </LinearLayout>

____________________________________________________________________
  • Add bellow image in drawable.

 
____________________________________________________________________

 OUTPUT :

Sunday, April 20, 2014

Custom Toast

Custom Toast :

LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custom_toast,
                                               (ViewGroup) findViewById(R.id.
LL_Parent));
   
                TextView text = (TextView) layout.findViewById(R.id.txtShow);


                text.setText("This is My Custom Toast.");

                Toast toast = new Toast(getApplicationContext());


                // Display in center

                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);

                toast.show();


__________________________________________________________



custom_toast.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LL_Parent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAA"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF" />


</LinearLayout>

__________________________________________________________


customized toast without creating xml layout :


                    // create a LinearLayout and Views

                    LinearLayout layout1 = new LinearLayout(this);
                    layout1.setBackgroundResource(android.R.color.darker_gray);

                    // create a TextView

                    TextView tv = new TextView(this);
                    tv.setTextColor(Color.RED);
                    tv.setTextSize(15);
                    tv.setGravity(Gravity.CENTER_VERTICAL);
                    tv.setText("This is My Custom Toast.");

                    // create a ImageView

                    ImageView img = new ImageView(this);
                    img.setImageResource(R.drawable.ic_launcher);

                    // TextView and ImageView are add in LinearLayout 

                    layout1.addView(img);
                    layout1.addView(tv);

                    // Toast 

                    Toast toast1 = new Toast(this);
                    toast1.setView(layout1);
                    toast1.setGravity(Gravity.BOTTOM, 0, 50);
                    toast1.show();

Saturday, April 19, 2014

Background And Borders (Drawable - layer-list)



  • Rounded Corners


<?xml version="1.0" encoding="utf-8"?>

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

    android:padding="10dp"

    android:shape="rectangle" >



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



    <corners

        android:bottomLeftRadius="10dp"

        android:bottomRightRadius="10dp"
        android:topLeftRadius="10dp"
        android:topRightRadius="10dp" />

</shape>

__________________________________________________________________

  • Rectangle frame


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

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

    <stroke
        android:width="1dp"
        android:color="#CCCCCC" />

</shape>

__________________________________________________________________




   Use of <layer-list> in drawable (xml)   

  • Rounded Corners with Shadow



<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Shadow At Bottom-->


    <item>

        <shape android:shape="rectangle" >
            <solid android:color="#d8d8d8" />

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

        </shape>
    </item>

    <!-- White Color At Top -->


    <item android:bottom="3px">

        <shape android:shape="rectangle" >
            <solid android:color="#FFFFFF" />

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

        </shape>
    </item>


</layer-list>

__________________________________________________________________



  • Rectangle frame with Shadow



<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- Drop Shadow Stack -->
    <item>
        <shape>
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="#00CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="#10CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="#20CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="#30CCCCCC" />
        </shape>
    </item>
    <item>
        <shape>
            <padding
                android:bottom="5dp"
                android:left="5dp"
                android:right="5dp"
                android:top="5dp" />

            <solid android:color="#50CCCCCC" />
        </shape>
    </item>

    <!-- Background -->
    <item>
        <shape>
            <solid android:color="@color/White" />

            <corners android:radius="3dp" />
        </shape>
    </item>

</layer-list>

Change padding in all items and see the difference.

e.g. 

 <padding
                android:bottom="1dp"
                android:left="1dp"
                android:right="1dp"

                android:top="1dp" />


__________________________________________________________________



<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <!-- dropshadow -->
    <item>
        <shape>
            <solid android:color="#10CCCCCC" />

            <padding
                android:bottom="3dp"
                android:left="3dp"
                android:right="0dp"
                android:top="0dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#20CCCCCC" />

            <padding
                android:bottom="3dp"
                android:left="3dp"
                android:right="0dp"
                android:top="0dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#40CCCCCC" />

            <padding
                android:bottom="3dp"
                android:left="3dp"
                android:right="0dp"
                android:top="0dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#50CCCCCC" />

            <padding
                android:bottom="3dp"
                android:left="3dp"
                android:right="0dp"
                android:top="0dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid android:color="#60CCCCCC" />

            <padding
                android:bottom="3dp"
                android:left="3dp"
                android:right="0dp"
                android:top="0dp" />
        </shape>
    </item>

    <!-- content background -->
    <item>
        <shape>
            <solid android:color="#FDC650" />
        </shape>
    </item>

</layer-list>




Transparent Color Code


Transparent Color Code :


Color hexadecimal notation is like following: #AARRGGBB
  • A : alpha
  • R : red
  • G : green
  • B : blue
____________________________________________


E.g. for 50% white you'd use
            #80FFFFFF.
            #(Transparent  hex value)(Any Color code)
____________________________________________

  • Normal opaque black hex- "#000000"
  • Fully transparent - "#00000000"
  • Fully opaque - "#FF000000"
  • 50% transparent - "#80000000"
____________________________________________
Percentages to hex values. 
  • 100% — FF
  • 95% — F2
  • 90% — E6
  • 85% — D9
  • 80% — CC
  • 75% — BF
  • 70% — B3
  • 65% — A6
  • 60% — 99
  • 55% — 8C
  • 50% — 80
  • 45% — 73
  • 40% — 66
  • 35% — 59
  • 30% — 4D
  • 25% — 40
  • 20% — 33
  • 15% — 26
  • 10% — 1A
  • 5% — 0D
  • 0% — 00