SameerKhan1406 SameerKhan1406 - 26 days ago 12
Android Question

Enforcing terms and condition Agreement in Android?

How to enforce terms and condition agreement in Android? I want to do it like this http://www.droidbin.com/. You can observe in the given link that upload appears only when checkbox is ticked else it invisible and initially it is invisible.

My login XML:

<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="@drawable/startbg"
android:padding="0dip"
tools:context=".LoginActivity">

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingLeft="40dp"
android:paddingRight="40dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Enter your number"
android:textAllCaps="true"
android:id="@+id/textView" android:layout_gravity="center_horizontal"
android:textColor="@color/sinch_purple" android:textSize="20sp"/>
<EditText
android:id="@+id/loginNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:inputType="phone"
android:digits="0123456789"
android:maxLength="10"
android:textSize="32sp"
android:padding="10dp"
android:textColor="@color/sinch_purple"
android:background="@drawable/inputbox">


<requestFocus/>
</EditText>

</LinearLayout>

<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/button_login"
android:minHeight="56dp"
android:minWidth="132dp"
android:textColor="@color/off_white"
android:textSize="22sp"
android:visibility="invisible"
android:textStyle="bold" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>

<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/sinch_purple"
android:layout_toRightOf="@id/checkBox1"
android:layout_alignTop="@id/checkBox1"
android:textSize="16sp"/>

</RelativeLayout>


Login Java file

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBox = (CheckBox)findViewById(R.id.checkBox1);
setContentView(R.layout.login);
String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>";

mBox.setText(Html.fromHtml(checkBoxText));
mBox.setMovementMethod(LinkMovementMethod.getInstance());

//terms=(TextView)findViewById(R.id.terms);
mLoginNo = (EditText) findViewById(R.id.loginNo);

mLoginButton = (Button) findViewById(R.id.loginButton);
mLoginButton.setEnabled(false);
mLoginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
loginClicked();
}
});
mBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if ( mBox.isChecked() )
{
mLoginButton.setVisibility(buttonView.VISIBLE);
}
else{
mLoginButton.setVisibility(buttonView.GONE);
// terms.setText("Please Agree to Terms And Condition");
}
}
});
}

Answer

You need to use a CheckBox. Simply add the Checkbox in your layout's .xml file using XML tag .

Then add an OnCheckedChangeListener to the CheckBox:

CheckBox checkBox = null;//get the view using findViewById
    final View invisibleView = null;//do the same
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                invisibleView.setVisibility(View.VISIBLE);
            } else
                invisibleView.setVisibility(View.GONE);
        }
    });