Show/Hide Text using Radiobutton in Android


                 This is small demo for how to show and hide Text using radiobutton in  android .
In MainActivity we have to use this below code.In this project i used validation ,after submit  the button it will show toast....

Tv.setVisibility(view.VISIBLE);
Tv.setVisibility(view.INVISIBLE);
Tv.setVisibility(view.GONE);
              
In Xml 
we have to use 
   android:visibility="gone"
   android:visibility="visible"
   android:visibility="invisible"

MainActivity.java
First Create MainActivity.java in src

package com.exampl.hide;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

Button Send;
public static String no;
public static String  conname;
public static String typ1;
public static String ass;
public static String savenotice;
Typeface font;
String cno;
RadioButton rb1;
EditText edit;
 TextView ts;
RadioButton rb2;
TextView tv1;
TextView tv2;
EditText edit1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
font = Typeface.createFromAsset(getAssets(), "Avvaiyar.ttf");  

TextView tview=(TextView)findViewById(R.id.savenotice);
   tv1=(TextView)findViewById(R.id.assno);
tv1.setTypeface(font);         
tv1.setVisibility(View.VISIBLE);
  
tv2=(TextView)findViewById(R.id.cno);
tv2.setTypeface(font); 
tv2.setVisibility(View.GONE);
         rb1=(RadioButton)findViewById(R.id.rb1);
             
         rb1.setChecked(true);
         rb2=(RadioButton)findViewById(R.id.rb2);
        
       
         Controls();
        
}
         public String onRadioButtonClicked(View view) {
             // Is the button now checked?
             boolean checked = ((RadioButton) view).isChecked();
            
             switch(view.getId()) {
                 case R.id.rb1:
                     if (checked)
                         tv1.setVisibility(View.VISIBLE);
                         tv2.setVisibility(View.GONE);
                         edit1 = (EditText) findViewById(R.id.edit12);
                         edit1.setText("");
                     break;
                 case R.id.rb2:
                     if (checked)
                      tv2.setVisibility(View.VISIBLE);
                         tv1.setVisibility(View.GONE);
                          edit1 = (EditText) findViewById(R.id.edit12);
                         edit1.setText("");
                     break;
                    
             }return null;
         }
         
         public void Controls(){
        final EditText edit1 = (EditText) findViewById(R.id.edit12);
             Send = (Button) findViewById(R.id.button1);
            
  Send.setOnClickListener(new OnClickListener() {
      @Override
      
      
  public void onClick(View v) {
       no=edit1.getText().toString();
 
  if(edit1.getText().toString().equalsIgnoreCase(""))
  {
  Toast.makeText(getBaseContext(), "Please Enter No", Toast.LENGTH_LONG).show();
  }
 
  else if(edit1.getText().toString().equalsIgnoreCase("0"))
  {
  Toast.makeText(getBaseContext(), "Invalid No", Toast.LENGTH_LONG).show();
  }
  else
  {
  Toast.makeText(getApplicationContext(), "You have Enter "+no, Toast.LENGTH_LONG).show();
 
 
  }}
      });

}
   
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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:background="#99CCFF"
   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=".SaveMobileNumber" >

            <TextView
                android:id="@+id/savenotice"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               android:textColor="#009999"
                android:layout_centerHorizontal="true"
                android:text="@string/h1"
                android:textAppearance="?android:attr/textAppearanceMedium"
               />
      
<TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <RadioGroup
                 tools:ignore="UselessParent"
                android:id="@+id/radiogr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/rb1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                     android:onClick="onRadioButtonClicked"
                    android:text="@string/r1" />

                <RadioButton
                    android:id="@+id/rb2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                     android:onClick="onRadioButtonClicked"
                    android:text="@string/r2" />
            </RadioGroup>
        </TableRow>

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TableRow
                 tools:ignore="UselessParent"
                android:id="@+id/tableRow4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/assno"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#0000FF"
                    android:layout_marginRight="30dp"
                    android:text="@string/n3" />
                  
                 <TextView
                    android:id="@+id/cno"
                    android:layout_width="56dp"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                     android:textColor="#0000FF"
                    android:text="@string/n4" />
                  
                
                <EditText
                    android:id="@+id/edit12"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:background="@android:drawable/editbox_dropdown_light_frame"
                    android:textColor="#106BE3" />

            </TableRow>
        </TableLayout>

</TableLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/savenotice"
        android:layout_below="@+id/tableLayout1"
        android:layout_marginLeft="14dp"
        android:layout_marginTop="20dp"
        android:text="@string/Send" />
   
</RelativeLayout>


String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="app_name">Hide</string>
 <string name="action_settings">Settings</string>
 <string name="hello_world">Hello world!</string>
<string name="r1">Books</string>
<string name="r2">Games</string>
<string name="n3">v©</string>
<string name="n4">bga®</string>
<string name="Send">Submit</string>
<string name="h1">Demo Show/hide Text</string>
</resources>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exampl.hide"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.exampl.hide.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Result:

Choosing Books it will show the text is Number .then choosing games it will show the text is name and after giving submit its showing result in Toast............















 ................Thank you.......................

1 comment: