Showing posts with label single selection. Show all posts
Showing posts with label single selection. Show all posts

Tuesday, December 13, 2016

Menu,Context Menu, Option Menu, Popup Menu, Main Menu

Menu is use to select single option from number of listed choice. In the android three type of menu used to select option. This are-
  1. Option Menu - It will be shown when user click on three dots(Top right corner).
  2. Context Menu - It will be shown when user long click on registered object.
  3. Pop up Menu - It will be shown when user long click on any object which is registered to open popup menu.




 
Steps to use Menu in your project.
  1. Crate a project in android studio.
     
  2. Open your main_activity.xml and update it-

    <?xml version="1.0" encoding="utf-8"?>
    
    <android.support.design.widget.CoordinatorLayout 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"
        android:fitsSystemWindows="true"
        tools:context="com.pankaj.menudemo.MainActivity">
    
        <!--This is an action bar configuration if you want to customization in it then you can here.-->
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
        <!--Main Content-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.pankaj.menudemo.MainActivity">
    
            <!--Button to open context menu-->
            <Button
                android:id="@+id/btnContaxtMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Context Menu" />
    
            <!--Button to open popup menu-->
            <Button
                android:id="@+id/btnPopupMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="openPopupMenu"
                android:text="Popup Menu" />
    
        </LinearLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    
     
  3. Create a menu file in menu directory(main_menu.xml).

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <!--I am using three element in menu you can update it according to your need.-->
        <!--In the option menu first item will be show with image icon and other two option will be show in the form of listing.-->
        <!--In the context and popup menu all three item will be show in listing form.-->
    
        <item
            android:id="@+id/mPointOfAndroid"
            android:icon="@android:drawable/ic_dialog_info"
            android:orderInCategory="1"
            android:title="Point of Android"
            app:showAsAction="ifRoom" />
    
        <item
            android:id="@+id/mSettings"
            android:orderInCategory="2"
            android:title="Settings" />
    
        <item
            android:id="@+id/mLogout"
            android:orderInCategory="3"
            android:title="Logout" />
    </menu>
    
    
    

  4. Open your MainActivity.JAVA class and update it -

    package com.pankaj.menudemo;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.PopupMenu;
    import android.support.v7.widget.Toolbar;
    import android.view.ContextMenu;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        //_________create objects of buttons
        private Button btnPopupMenu, btnContaxtMenu;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //_________create toolbar object
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    
            //_________add toolbar
            setSupportActionBar(toolbar);
    
            //_________initialize buttons
            btnPopupMenu = (Button) findViewById(R.id.btnPopupMenu);
            btnContaxtMenu = (Button) findViewById(R.id.btnContaxtMenu);
    
            //_________register button for context menu
            registerForContextMenu(btnContaxtMenu);
        }
    
        //_________override method to create context menu
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            super.onCreateContextMenu(menu, v, menuInfo);
    
            //________add title of the menu, if is it.
            menu.setHeaderTitle("This is a Context Menu");
    
            //_________add icon of the menu, if is it.
            menu.setHeaderIcon(getResources().getDrawable(R.mipmap.ic_launcher));
    
            //_____________inflate main_menu.xml file to show option in context menu
            getMenuInflater().inflate(R.menu.menu_main, menu);
        }
    
        //_________override method to process selected context menu option.
        @Override
        public boolean onContextItemSelected(MenuItem item) {
    
            //_________Message will be shown, When option selected from context menu.
            Toast.makeText(this, "Context Menu : " + item.getTitle(), Toast.LENGTH_SHORT).show();
            return super.onContextItemSelected(item);
        }
    
        //_________override method to create options menu
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
    
            // Inflate the menu; this adds items to the action bar if it is present.
            //_____________inflate main_menu.xml file to show option in popup menu
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        //_________override method to process selected options menu option.
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            //_________Message will be shown, When option selected from options menu.
            Toast.makeText(this, "Option Menu : " + item.getTitle(), Toast.LENGTH_SHORT).show();
            return super.onOptionsItemSelected(item);
        }
    
        //_________override method to create popup menu.
        public void openPopupMenu(View view) {
    
            //_________create popup menu object
            PopupMenu popupMenu = new PopupMenu(this, btnPopupMenu);
    
            //_____________inflate main_menu.xml file to show option in popup menu
            popupMenu.getMenuInflater().inflate(R.menu.menu_main, popupMenu.getMenu());
    
            //__________set listener to call back on item selection
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    
                //_________override method to process selected popup menu option.
                @Override
                public boolean onMenuItemClick(MenuItem item) {
    
                    //_________Message will be shown, When option selected from popup menu.
                    Toast.makeText(MainActivity.this, "Popup Menu : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                    return false;
                }
            });
            popupMenu.show();
        }
    }
    
    
    

  5. Now all the development steps for menu has completed, Please run the application.




  6. Now click on option menu(three dots on top right corner), this will be open the option menu, then select any option from here. See the result on screen- 





  7. Now click on "Context Menu", this will be open the context menu, then select any option from here. See the result on screen-

     


  8. Now click on "Popup Menu", this will be open the popup menu, then select any option from here. See the result on screen-




  9.  Good bye, Thanks to read this blog.




Wednesday, November 30, 2016

Radio Group/Radio Button


In Android, Radio Group/Radio Button is use to select single option from number of choice. For the better user interference this is very important to customize the radio button according to need of theme. In this tutorial i am covering some points of customization.
 
 
 
<RadioGroup
    android:id="@+id/radioGroupSimple"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/activity_vertical_margin">
   
 <RadioButton
        android:id="@+id/radioButton1Simple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="radioButton1Simple" />
   
 <RadioButton
        android:id="@+id/radioButton2Simple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="radioButton2Simple" />
   
 <RadioButton
        android:id="@+id/radioButton3Simple"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="radioButton3Simple" />
 
</RadioGroup>



Steps to use Radio Group/Radio Button in your project.
  1. Crate a project in android studio.
     
  2.  Open your main_activity.xml and update it-
     <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout 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:gravity="center"
        android:orientation="vertical"
        tools:context="com.pankaj.radio.group_button.MainActivity">
    
        <!--_____________________________________________________-->
        <!--                                                                                                          -->
        <!--                      create simple radio button group                                -->
        <!--_____________________________________________________-->
    
        <RadioGroup
            android:id="@+id/radioGroupSimple"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_vertical_margin">
    
            <RadioButton
                android:id="@+id/radioButton1Simple"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="radioButton1Simple" />
    
            <RadioButton
                android:id="@+id/radioButton2Simple"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="radioButton2Simple" />
    
            <RadioButton
                android:id="@+id/radioButton3Simple"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="radioButton3Simple" />
    
        </RadioGroup>
    
        <!--_____________________________________________________-->
        <!--                                                                                                          -->
        <!--                             create simple radio button group                         -->
        <!--                                   and add custom drawable                              -->
        <!--_____________________________________________________-->
    
        <RadioGroup
            android:id="@+id/radioGroupCustom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/activity_vertical_margin">
    
            <RadioButton
                android:id="@+id/radioButton1Custom"
                style="@style/radio_button_custom_style"
                android:text="radioButton1Custom" />
    
            <RadioButton
                android:id="@+id/radioButton2Custom"
                style="@style/radio_button_custom_style"
                android:text="radioButton2Custom" />
    
            <RadioButton
                android:id="@+id/radioButton3Custom"
                style="@style/radio_button_custom_style"
                android:text="radioButton3Custom" />
        </RadioGroup>
    
    </LinearLayout>
    
    
    
    
      
  3. In the above main_activity.xml, i am using two types of radio buttons. First one radio group have simple radio buttons with default theme. Second radio group having custom theme which is defined in style.xml. So open the style.xml and add new style to it.

    <style name="radio_button_custom_style" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@drawable/bg_radio_buton</item>
        <item name="android:drawablePadding">50dp</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#16a085</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_margin">5dp</item>
        <item name="android:layout_width">wrap_content</item>
    </style>
      

  4. Now your style.xml should be like this-

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
    
        </style>
    
      <!-- Custom radio button theme. -->
        <style name="radio_button_custom_style" parent="@android:style/Widget.CompoundButton.RadioButton">
            <item name="android:button">@drawable/bg_radio_buton</item>
            <item name="android:drawablePadding">50dp</item>
            <item name="android:textSize">12sp</item>
            <item name="android:textColor">#16a085</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:layout_margin">5dp</item>
            <item name="android:layout_width">wrap_content</item>
        </style>
    
    </resources>
    
    
    

     
  5. Now see in your style, there are a custom drawable is used
     
    <item name="android:button">@drawable/bg_radio_buton</item> 
  6. So go to your resource directory and select your drawable directory and create new drawable resource file(drawable/bg_radio_buton.xml).

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/check" android:state_checked="true" android:state_window_focused="false" />
        <item android:drawable="@drawable/uncheck" android:state_checked="false" android:state_window_focused="false" />
        <item android:drawable="@drawable/check" android:state_checked="true" android:state_pressed="true" />
        <item android:drawable="@drawable/uncheck" android:state_checked="false" android:state_pressed="true" />
        <item android:drawable="@drawable/check" android:state_checked="true" android:state_focused="true" />
        <item android:drawable="@drawable/uncheck" android:state_checked="false" android:state_focused="true" />
        <item android:drawable="@drawable/uncheck" android:state_checked="false" />
        <item android:drawable="@drawable/check" android:state_checked="true" />
    </selector>
    
    
    
     
  7. Open your MainActivity.JAVA class and update it for change text at run time-

    package com.pankaj.radio.group_button;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    
        //create objects of radio group
        private RadioGroup radioGroupSimple, radioGroupCustom;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //initialize radiogroup objects
            radioGroupSimple = (RadioGroup) findViewById(R.id.radioGroupSimple);
            radioGroupCustom = (RadioGroup) findViewById(R.id.radioGroupCustom);
    
            //add listener
            radioGroupSimple.setOnCheckedChangeListener(this);
            radioGroupCustom.setOnCheckedChangeListener(this);
        }
    
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
    
            //find id of the selected radio button
            int selectedId = radioGroup.getCheckedRadioButtonId();
    
            // find the radiobutton by returned id
            RadioButton radioButton = (RadioButton) findViewById(selectedId);
    
            //show toast message for selecteded radio button
            Toast.makeText(this, radioButton.getText(), Toast.LENGTH_SHORT).show();
    
        }
    }
    
    

  8. Now all the development process for Radio group/Radio button has completed, Please run the application.



  9. Now select any radio button from first radio group and see the result on screen-
     

  10. Also select any radio button from second radio group and see the result on screen-
     
     


  11.  Good bye, Thanks to read this blog.