Title: Issue with Android Check Box Visibility?
Tags: android;eclipse;checkbox
Question: In my application I have two Check boxes one is PayByCredit and one more is PaybySMS For Both the check boxes are not visible what was the issue ? To display the Check box i gave code like this android:visibility="visible" but still no use . Can any one help me out of this please ?
My XML Code is :
<?xml version="1.0" encoding="utf-8"?&gt;
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/homescreeen2"
android:orientation="vertical" &gt;
<RelativeLayout
android:id="@+id/toplayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" &gt;
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:fontFamily="Optima"
android:gravity="center"
android:text="@string/palmtreeimg"
android:textColor="#ffffff"
android:textStyle="bold" /&gt;
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:background="@null"
android:contentDescription="@string/backbt"
android:src="@drawable/backbt" /&gt;
</RelativeLayout&gt;
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toplayout"
android:paddingBottom="10dp"
android:scrollbars="none" &gt;
<RelativeLayout
android:id="@+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" &gt;
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_marginLeft="10dp"
android:fontFamily="@+fonts/MontereyFLF"
android:text="@string/realname"
android:textColor="#ff0000" /&gt;
<LinearLayout
android:id="@+id/mainlayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_margin="15dp"
android:background="@drawable/layout_bg"
android:orientation="vertical"
android:paddingBottom="5dp" &gt;
<LinearLayout
android:id="@+id/getaplague"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" &gt;
<include
android:id="@+id/cmpndbtnName"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_marginStart="@+id/treesplanted"
layout="@layout/compound_button" /&gt;
</LinearLayout&gt;
<View
android:id="@+id/border"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88"
android:visibility="gone" /&gt;
<LinearLayout
android:id="@+id/entername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:alpha="0.8"
android:orientation="horizontal"
android:visibility="gone" &gt;
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="#ff0000" /&gt;
</LinearLayout&gt;
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:background="@null"
android:contentDescription="@string/btn_save"
android:src="@drawable/savesmall"
android:visibility="gone" /&gt;
</LinearLayout&gt;
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mainlayout2"
android:layout_margin="15dp"
android:background="@drawable/layout_bg"
android:orientation="vertical" &gt;
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" &gt;
<include
android:id="@+id/cmpndbtnfrnname"
android:layout_marginStart="@+id/treesplanted"
layout="@layout/compound_buttonfrnname" /&gt;
</LinearLayout&gt;
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" /&gt;
<LinearLayout
android:id="@+id/enterfrnname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:alpha="0.8"
android:orientation="horizontal"
android:visibility="gone" &gt;
</LinearLayout&gt;
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" /&gt;
<LinearLayout
android:id="@+id/enterfrnmailid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal"
android:visibility="gone" &gt;
<EditText
android:id="@+id/editTextfrnmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="@null"
android:hint="@string/enterfrnmailid"
android:inputType="text"
android:textSize="12sp"
android:paddingLeft="5dp"
android:textColor="#000000"
android:textColorHint="#999898" &gt;
</EditText&gt;
</LinearLayout&gt;
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" /&gt;
<LinearLayout
android:id="@+id/enterfrnphoneno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal"
android:visibility="gone" &gt;
</EditText&gt;
</LinearLayout&gt;
</LinearLayout&gt;
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" /&gt;
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" /&gt;
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1"
android:layout_margin="15dp"
android:layout="@+id/amntlayout"
android:orientation="horizontal" &gt;
</LinearLayout&gt;
<LinearLayout
android:id="@+id/mainlayoutpay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout2"
android:layout_margin="15dp"
android:background="@drawable/layout_bg"
android:orientation="vertical"
android:paddingBottom="5dp" &gt;
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" &gt;
<TextView
android:id="@+id/txpaycredit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fontFamily="Optima"
android:text="@string/paycredit"
android:textColor="#ff0000" /&gt;
<TextView
android:id="@+id/dum"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fontFamily="Optima"
android:text=""
/&gt;
<CheckBox
android:id="@+id/paycredit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="@null"
android:visibility="visible"
android:textColor="#000000" /&gt;
</LinearLayout&gt;
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" /&gt;
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="0dp"
android:orientation="horizontal" &gt;
<TextView
android:id="@+id/paybysms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/paysms"
android:textColor="#ff0000" /&gt;
<TextView
android:id="@+id/dummy"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text=""
android:textColor="#ff0000" /&gt;
<CheckBox
android:id="@+id/paysms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="@null"
android:visibility="visible"
android:layout_marginLeft="10dp"
android:fontFamily="@+fonts/MontereyFLF"
android:textColor="#000000" /&gt;
</LinearLayout&gt;
</LinearLayout&gt;
</RelativeLayout&gt;
</ScrollView&gt;
</RelativeLayout&gt;
And My .java Code is here:
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;
public class PalmTreeActivity extends Activity {
//Used for payment
double currencyAftconversion;
public String treeValue;
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
private static final String CONFIG_CLIENT_ID = "AWg-FRC9GM1CdolC2XnKnYBEgpi01jDlKi6IEXFcEnu4QIfer6cVE0iH9YNI";
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
.merchantName("Isaaf")
.merchantPrivacyPolicyUri(
Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(
Uri.parse("https://www.example.com/legal"));
String AmountOfTree = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
String price = null;
//call currency conversion method
new DownloadCurrencyConversion().execute();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_palmtree);
ImageButton btnNext = (ImageButton) findViewById(R.id.imageButton2);
ImageButton btBack = (ImageButton) findViewById(R.id.imageButton1);
final CheckBox paybycard = (CheckBox) findViewById(R.id.paycredit);
//paycredit.setVisibility(View.VISIBLE);
final CheckBox paybysms = (CheckBox) findViewById(R.id.paysms);
final  CompoundButton   switchrealname= (CompoundButton) findViewById(R.id.enabled);
final  CompoundButton switchfrnname=(CompoundButton) findViewById(R.id.enabledfrnname);
final LinearLayout friendName=(LinearLayout) findViewById(R.id.enterfrnname);
final LinearLayout friendMailId=(LinearLayout) findViewById(R.id.enterfrnmailid);
final LinearLayout friendPhoneno=(LinearLayout) findViewById(R.id.enterfrnphoneno);
final LinearLayout Name=(LinearLayout) findViewById(R.id.entername);
final EditText username=(EditText)findViewById(R.id.editText1);
final EditText frnName=(EditText)findViewById(R.id.editTextfrnName);
final EditText frnmail=(EditText)findViewById(R.id.editTextfrnmail);
final EditText frnphoneno=(EditText)findViewById(R.id.editTextfrnphoneno);
//       final ImageButton savename=(ImageButton)findViewById(R.id.imageButton3);
//       final ImageButton savefrnname=(ImageButton) findViewById(R.id.imageButton4);
if (getIntent().getExtras() != null &amp;&amp; !TextUtils.isEmpty("price"))
{
price = getIntent().getExtras().getString("price");
TextView amount = (TextView) findViewById(R.id.amountvalue);
amount.setGravity(Gravity.CENTER);
String Pricedouble = price.substring(price.indexOf('('));
Pricedouble = Pricedouble.replaceAll("\\D+", "");
amount.setText(Pricedouble.toString()); // Get Number
//EditText amounttext = (EditText) findViewById(R.id.editText1);
//amounttext.setText(Pricedouble);
AmountOfTree = Pricedouble.toString();
Intent intentActivity = new Intent(this, PayPalService.class);
intentActivity.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intentActivity);
}
//button next click
btnNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (paybycard.isChecked()) {
Bundle bundle=new Bundle();
if(!TextUtils.isEmpty(username.getText().toString()))
bundle.putString("name", username.getText().toString());
if(!TextUtils.isEmpty(frnName.getText().toString()))
bundle.putString("frnname", frnName.getText().toString());
if(!TextUtils.isEmpty(frnmail.getText().toString()))
bundle.putString("frnmailid", frnmail.getText().toString());
if(!TextUtils.isEmpty(frnphoneno.getText().toString()))
bundle.putString("frnphoneno", frnphoneno.getText().toString());
PayPalPayment payment = new PayPalPayment(new BigDecimal(
currencyAftconversion), "USD", "Tree",
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(getApplicationContext(),
PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, 0);
}
else if(paybysms.isChecked())
{
StringBuilder tmp=new StringBuilder();
if(!TextUtils.isEmpty(username.getText().toString()));
tmp.append("User details are :"+"\n");
tmp.append("UserName is:"+username.getText().toString());
tmp.append("\n");
//tmp.append(username.getText().toString());
if(!TextUtils.isEmpty(frnName.getText().toString()))
tmp.append("Friend Name is:"+frnName.getText().toString());
tmp.append("\n");
if(!TextUtils.isEmpty(frnmail.getText().toString()))
tmp.append("Friend Email id is:"+frnmail.getText().toString());
tmp.append("\n");
if(!TextUtils.isEmpty(frnphoneno.getText().toString()))
tmp.append("Friend phone number is:"+frnphoneno.getText().toString());
PayByCash senddetailstoadmin=new PayByCash();
if(!TextUtils.isEmpty(tmp.toString()))
senddetailstoadmin.execute(tmp.toString());
Toast paymessage=Toast.makeText(getApplicationContext(), "Our customer care people will contact you shortly..!!!!", Toast.LENGTH_SHORT);
paymessage.show();
Intent intent=new Intent( getApplicationContext(),PaycashthanksActivity.class);
startActivity(intent);
}
else
{
Toast paywarning=Toast.makeText(getApplicationContext(), "Please pay using card or pay by cash", Toast.LENGTH_SHORT);
paywarning.show();
}
}
});
btBack.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),
FindmarkedLocationActivity.class);
startActivity(intent);
}
});
//switchrealname toggle
switchrealname.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if(switchrealname.isChecked())
{
Name.setVisibility(View.VISIBLE);  //Set the layout visible
//                  savename.setVisibility(View.VISIBLE);
}
else
{
Name.setVisibility(View.GONE);
//                  savename.setVisibility(View.GONE);
}
}
});
//Switch friend details switch toggle checked event
switchfrnname.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if(switchfrnname.isChecked())
{
//Set the layout visible
friendName.setVisibility(View.VISIBLE);
friendMailId.setVisibility(View.VISIBLE);
friendPhoneno.setVisibility(View.VISIBLE);
// savefrnname.setVisibility(View.VISIBLE);
}
else
{
//Set the layout visible
friendName.setVisibility(View.GONE);
friendMailId.setVisibility(View.GONE);
friendPhoneno.setVisibility(View.GONE);
//                  savefrnname.setVisibility(View.GONE);
}
}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm = data
.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.i("paymentExample", confirm.toJSONObject().toString(4));
// TODO: send 'confirm' to your server for verification or
// consent
// completion.
// see
// https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
// for more details.
Intent intent = new Intent(getApplicationContext(),
CongratulationActivity.class);
startActivity(intent);
}
catch (JSONException e)
{
sorryActivity();
Log.e("paymentExample",
"an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("paymentExample", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID)
{
sorryActivity();
Log.i("paymentExample",
"An invalid Payment was submitted. Please see the docs.");
}
}
//back button click
public String getJson(String url) throws ClientProtocolException,
IOException {
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
content));
String con;
while ((con = reader.readLine()) != null) {
build.append(con);
return build.toString();
public void sorryActivity()
Intent intent = new Intent(getApplicationContext(),
SorryPaymentActivity.class);
startActivity(intent);
@Override
public void onDestroy() {
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
public class DownloadCurrencyConversion extends
AsyncTask<Void, Integer, String&gt; {
@Override
protected String doInBackground(Void... params) {
String s;
String theResult = "";
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22BHDUSD%22%29&amp;format=json&amp;env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&amp;callback=");
JSONObject jObj;
jObj = new JSONObject(s);
theResult = jObj.getJSONObject("query")
.getJSONObject("results").getJSONObject("rate")
.getString("Rate");
System.out.println(theResult);
catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return theResult;
@Override
protected void onPostExecute(String theResult) {
super.onPostExecute(theResult);
System.out.println("theResult:" + theResult);
if (!TextUtils.isEmpty(theResult) &amp;&amp;!TextUtils.isEmpty(AmountOfTree) )
currencyAftconversion = (Double.parseDouble(AmountOfTree))
* (Double.parseDouble(theResult));
Please help me out of this issue ??
Comment: What Layout do you use for XML?

Comment: Could you please put the whole layout? I don't see where do you set text Value for checkbox?

Comment: it s possible that the mistake is in your `layout-xml` (you should post it as well). f.e. checkboxes are below an layout which height is `fill_parent`. so set you checkboxes a green background and check if you see it

Comment: @Mike , I am using Relativelayout.

Here is the accepted answer: Remove android:background="@null"
Comment for this answer: @user3744932 without the exact code it is hard to tell what could be the issue. Since you are using "OnCheckedChangedListener" everytime you are changing it, it reverts back to original stage.
Comment for this answer: ,Hi I have one more issue with same check box ,i.e the Both check box are selecting at same time . But what i want only one check box  should able to select ? I tried like this 'if (paybycard.isChecked()) {
paybysms.setChecked(false);' But it doesn't work for me ?

Here is another answer: These layouts work for me:
<CheckBox
android:id="@+id/paysms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@null"
android:visibility="visible"
android:text="pay with sms"
android:layout_marginLeft="10dp"
android:textColor="#000000" /&gt;
<CheckBox
android:id="@+id/paycredit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/paysms"
android:background="@null"
android:text="pay credit"
android:visibility="visible"
android:layout_marginLeft="10dp"
android:textColor="#000000" /&gt;
Comment for this answer: Maybe it is better to use RadioGroup and RadioButton? http://developer.android.com/reference/android/widget/RadioGroup.html
Comment for this answer: please update your question with the code of your checkboxes or create a new one.
Comment for this answer: Hi I have one more issue with same check box ,i.e the Both check box are selecting at same time . But what i want only one check box should able to select ? I tried like this 'if (paybycard.isChecked()) { paybysms.setChecked(false);' But it doesn't work for me ?
Comment for this answer: Sorry , i want use check box only .Thanks

Here is another answer: In your code try this:
paybycard.setVisibility(0);
paybysms.setVisibility(0);
Hope this help, thanks

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:07/17/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<android><eclipse><checkbox>', 'Title': 'Issue with Android Check Box Visibility?', 'ViewCount': 171, 'id': 24797194, 'user_AboutMe': '', 'user_AccountId': 4619431.0, 'user_DisplayName': 'user3744932', 'user_Reputation': 89.0, 'user_Views': 22.0, 'user_id': 3744932.0}}