C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android Custom CheckBoxAndroid provides facility to customize the UI of view elements rather than default. You are able to create custom CheckBox in android. So, you can add some different images of checkbox on the layout. Example of Custom CheckBoxIn this example, we create both default as well as custom checkbox. Add the following code in activity_main.xml file. activity_main.xmlFile: activity_main.xml Now implement a selector in another file (checkbox.xml) under drawable folder which customizes the checkbox. checkbox.xmlFile: checkbox.xml Activity classFile: MainActivity.java
package example.TheDeveloperBlog.com.customcheckbox;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox cb1,cb2;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1=(CheckBox)findViewById(R.id.checkBox3);
cb2=(CheckBox)findViewById(R.id.checkBox4);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuilder sb=new StringBuilder("");
if(cb1.isChecked()){
String s1=cb1.getText().toString();
sb.append(s1);
}
if(cb2.isChecked()){
String s2=cb2.getText().toString();
sb.append("\n"+s2);
}
if(sb!=null && !sb.toString().equals("")){
Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),"Nothing Selected", Toast.LENGTH_LONG).show();
}
}
});
}
}
Output ![]()
Next TopicAndroid RadioButton Example
|