Showing posts with label Android Studio. Show all posts
Showing posts with label Android Studio. Show all posts

Radio Button Android Part 1 :- Check and Unchecked

Description From https://developer.android.com/guide/topics/ui/controls/radiobutton.html

Radio Buttons


Radio buttons allow the user to select one option from a set. You should use radio buttons for optional sets that are mutually exclusive if you think that the user needs to see all available options side-by-side. If it's not necessary to show all options side-by-side, use a spinner instead.

To create each radio button option, create a RadioButton in your layout. However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup. By grouping them together, the system ensures that only one radio button can be selected at a time.

1. Make a   XML Layout


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.avadhesh.radiobutton.MainActivity">


    <RadioButton        android:id="@+id/radioButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="RadioButton" />
</RelativeLayout>
2. Coding
Within the Activity that hosts this layout, the following method handles the click event for  radio buttons: by which you can Check and Unchecked the radio button.
@Overridepublic void onClick(View v) {

    switch (v.getId())
    {
        case R.id.radioButton:
            if(radioButton.isSelected())
            {
                radioButton.setSelected(false);
                radioButton.setChecked(false);
            }else {
                radioButton.setSelected(true);
                radioButton.setChecked(true);
            }

            break;
    }

}

3. Output

 When radio button is not check.

When radio button is check.





Android Custom Dialog

This post will show the android custom dialog with custom layout.



In the xml file you can create any type of layout.

XML

    <<
    ? xml version = "1.0"
encoding = "utf-8" ? >
    <
    RelativeLayout xmlns : android = "http://schemas.android.com/apk/res/android"
android: layout_width = "match_parent"
android: layout_height = "match_parent" >


    <
    LinearLayout
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: layout_margin = "10dp"
android: layout_below = "@+id/editText11"
android: background = "@android:color/transparent"
android: orientation = "horizontal"
android: id = "@+id/linearLayout2" >

    <
    Button android: id = "@+id/ok"
android: layout_width = "match_parent"
android: layout_height = "50dp"
android: layout_weight = "1"
android: background = "@color/colorPrimary"
android: padding = "5dp"
android: text = "ok"
android: textColor = "@android:color/white"
android: textSize = "15dp"
android: textStyle = "bold" / >

    <
    Button android: id = "@+id/gallery"
android: layout_width = "match_parent"
android: layout_height = "50dp"
android: layout_marginLeft = "5dp"
android: layout_weight = "1"
android: background = "@color/colorPrimary"
android: padding = "5dp"
android: text = "Gallery"
android: visibility = "invisible"
android: textColor = "@android:color/white"
android: textSize = "15dp"
android: textStyle = "bold" / >

    <
    Button android: id = "@+id/cancel"
android: layout_width = "match_parent"
android: layout_height = "50dp"
android: layout_marginLeft = "5dp"
android: layout_weight = "1"
android: background = "@color/colorPrimary"
android: padding = "5dp"
android: text = "Cancel"
android: textColor = "@android:color/white"
android: textSize = "15dp"
android: textStyle = "bold" / >
    <
    /LinearLayout>

    <
    EditText android: layout_width = "match_parent"
android: textColor = "#000"
android: gravity = "center"
android: layout_height = "wrap_content"
android: inputType = "textPersonName"
android: hint = "Enter video link"
android: singleLine = "true"
android: ems = "10"
android: layout_centerHorizontal = "true"
android: id = "@+id/editText11" / >
    < /RelativeLayout>


Perfect Picker Date Android

DatePickerDialog mDatePickerDialog = null;
               final Calendar mCalendar = Calendar.getInstance();
               if (mDatePickerDialog == null) {
                   mDatePickerDialog = new DatePickerDialog(getActivity(),
                           new DatePickerDialog.OnDateSetListener() {
                               @Override
                               public void onDateSet(DatePicker view, int year,
                                                     int monthOfYear, int dayOfMonth) {
                                   // TODO Auto-generated method stub
                                     /*  edit_gi_age.setText(String.format(
                                               "%04d-%02d-%02d", year, monthOfYear + 1,
                                               dayOfMonth));*/
                                   edit_gi_age.setText(String.valueOf(getAge1(year, monthOfYear, dayOfMonth)));
                                   txt_ov_age.setText(String.valueOf(getAge1(year, monthOfYear, dayOfMonth)));
                                   mCalendar.set(Calendar.YEAR, year);
                                   mCalendar.set(Calendar.MONTH, monthOfYear);
                                   mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                               }
                           }, 1970, 0, 1);
                   mCalendar.set(Calendar.YEAR, 1970);
                   mCalendar.set(Calendar.MONTH, 0);
                   mCalendar.set(Calendar.DAY_OF_MONTH, 1);
                   mDatePickerDialog.setCanceledOnTouchOutside(true);
                   mDatePickerDialog
                           .setTitle(getString(R.string.app_name));
                   mDatePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
                   mDatePickerDialog.getDatePicker().setMinDate(mCalendar.getTimeInMillis());
               }
               mDatePickerDialog.show();