Friday, 23 February 2018

How to use RadioButton in android, How to set image and text dynamically on radio group, how to set text on radio using method, OnClickListener on RadioButton

Here i am giving one simple example that how to use radio button in android, OnClickListener on RadioButton | Dynamically set image and text to a radioGroup radioButton | How to set text on radio button by using method in android. Here are the following steps to do this:-

Step 1:- Add new project and open activity_main.xml file and write following code:-
<?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:orientation="vertical"    
tools:context="com.example.supriyabharti.radiobuttonexmp.MainActivity">

    <TextView        
android:layout_width="match_parent"        
android:layout_height="wrap_content"        
android:layout_margin="20dp"        
android:gravity="center"        
android:text="Radio Group Example" />

    <RadioGroup        
android:id="@+id/radioGroup"        
android:layout_width="match_parent"        
android:layout_height="30dp"      
  android:orientation="horizontal"      
  android:weightSum="2">

        <RadioButton           
 android:id="@+id/radioChat"           
 android:layout_width="0dp"           
 android:layout_height="30dp"          
  android:layout_marginLeft="20dp"          
  android:layout_weight="1"           
 android:button="@null"            
android:drawableLeft="@mipmap/chatgrey"      
      android:drawablePadding="10dp"           
 android:text="Chat" />

        <RadioButton           
 android:id="@+id/radioVideo"          
  android:layout_width="0dp"           
 android:layout_height="30dp"            
android:layout_weight="1"            
android:button="@null"            
android:drawableLeft="@mipmap/videogrey"            
android:drawablePadding="10dp"            
android:text="video" />
    </RadioGroup>

    <Button        
android:id="@+id/button"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_gravity="center"       
 android:layout_marginTop="30dp"       
 android:background="@color/green"       
 android:padding="10dp"        
android:text="Set text using method" />

</LinearLayout>
Step 2:- colors.xml
<?xml version="1.0" encoding="utf-8"?><resources>
    <color name="green">#FF00AA64</color>
    <color name="inactive">#FF808A8F</color>
</resources>
Step 3:- MainActivity.java:-
public class MainActivity extends AppCompatActivity {
    RadioButton video,Chat;
    Button button;


    @Override    
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        video=(RadioButton)findViewById(R.id.radioVideo);
        Chat=(RadioButton)findViewById(R.id.radioChat);
        button=(Button)findViewById(R.id.button);

        //initially chat button will be activated so dynamically we will give activated image and color to the button        
        Chat.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.chat_green,0,0,0);
        Chat.setText("ChatNow");
        Chat.setTextColor(getResources().getColor(R.color.green));
        Chat.setOnClickListener(new View.OnClickListener() {
            @Override            
                public void onClick(View v) {
                Chat.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.chat_green,0,0,0);
                Chat.setText("ChatNow");
                Chat.setTextColor(getResources().getColor(R.color.green));

                //set inactive to other button..                
                video.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.videogrey,0,0,0);
                video.setTextColor(getResources().getColor(R.color.inactive));
                video.setText("Video");
            }
        });

        //video click...        
           video.setOnClickListener(new View.OnClickListener() {
            @Override            
                public void onClick(View v) {
                video.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.video_green,0,0,0);
                video.setText("VideoNow");
                video.setTextColor(getResources().getColor(R.color.green));

                //set remaning to inactive..                
                Chat.setCompoundDrawablesRelativeWithIntrinsicBounds(R.mipmap.chatgrey,0,0,0);
                Chat.setText("Chat");
                Chat.setTextColor(getResources().getColor(R.color.inactive));
            }
        });

        //button click listner...        
         button.setOnClickListener(new View.OnClickListener() {
            @Override            
                public void onClick(View v) {
                setText(getResources().getString(R.string.Rs)+"250","",Chat);
                setText(getResources().getString(R.string.Rs)+"350","",video);
            }
        });

    }

    //method to set text on radio button..    
private void setText(String text, String defaultText, View view) {
        if (view instanceof TextView) {
            TextView textView = (TextView) view;
            if (text != null && !text.isEmpty()) {
                textView.setText(text);
            } else {
                textView.setText(defaultText);
            }
        }
    }
}
Step 4:- string.xml:-
<resources>
    <string name="app_name">RadioButtonExmp</string>
    <string name="Rs">\u20B9</string>
</resources>
Step 5: ScreenShorts:-



No comments:

Post a Comment