top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to create button and checkbox in android?

0 votes
633 views

I am newbie to the android, can someone share sample code for creating buttons and checkbox in Android. Any reference will also be helpful.

posted Mar 11, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Open “res/layout/main.xml” file, add 3 “CheckBox” and a button, inside the LinearLayout, following is the sample code -

File : res/layout/main.xml

Markup

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/chkIos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_ios" />

    <CheckBox
        android:id="@+id/chkAndroid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_android"
        android:checked="true" />

    <CheckBox
        android:id="@+id/chkWindows"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_windows" />

    <Button
        android:id="@+id/btnDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_display" />

</LinearLayout>
answer Mar 11, 2016 by Ashish Kumar Khanna
+1 vote

Some additional way of doing things...
There are basically two ways to create a control in android (or any platform)
- Thru editor or Layout XML
- Thru code / Runtime / Dynamic

For creating in Layout XML, @Ashish Kumer Khanna's answer should help you.

For creating in Runtime, you could code something like this...

Button button1 = new Button(this);
button1.setText("I am Button");

LinearLayout layout = (LinearLayout)findViewById(R.id.mylayout);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.addView(button1, layoutParams);

Hope this helps!

answer Jun 3, 2016 by Vinod Kumar K V
Similar Questions
+3 votes

I want to make my FAB button look like a circle or any particular shape? how to achieve this?

+2 votes

I have added a floating action button in my layout but using backgroundtilt, it doesn't change the color of background.

Here my code segment

   <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/AddNewButton"
        android:background="@drawable/add"
        android:layout_margin="15dp"
        app:rippleColor="@android:color/white"
        app:fabSize="normal"
        android:clickable="true"
        app:layout_anchor="@+id/HeaderSection"
        app:layout_anchorGravity="bottom|right|end"/>
+1 vote

The default setup wizard setting is using the back fingerprint sensor, how to change it to the front one in Setup Wizard?

+1 vote

How to create an application like button savior that display a button or a group of buttons on the screen when we turn them on PERMANENTLY. I tried to found solution for it but i failed. Please provide the solution for it or any example

+1 vote

My app cashes when i click submit, it doesnt verify the fields as it should.
It works the way i want to but if i click on submit without entering anything it crashes.

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button addBtn = (Button) findViewById(R.id.addBtn);
        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText amoutGiven = (EditText) findViewById(R.id.amountG);
                EditText manyPeople = (EditText) findViewById(R.id.mPeople);
                EditText moenyGiven = (EditText) findViewById(R.id.moneyG);
                TextView changeTxt  = (TextView) findViewById(R.id.changeTxt);
                Button nxtBtn     = (Button) findViewById(R.id.nxtBtn);
                Button tryBtn     = (Button) findViewById(R.id.tryBtn);
                android.view.inputmethod.InputMethodManager imm = (android.view.inputmethod.InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);

                if (amoutGiven.getText().length()==0){
                    amoutGiven.setError("This field is required");
                }

                if (manyPeople.getText().length()==0){
                    manyPeople.setError("This field is required");
                }

                if (moenyGiven.getText().length()==0){
                    moenyGiven.setError("This field is required");
                }

                int num1 = Integer.parseInt(amoutGiven.getText().toString());
                int num2 = Integer.parseInt(manyPeople.getText().toString());
                int num3 = Integer.parseInt(moenyGiven.getText().toString());
                int total = num1 * num2;
                int change = num3 - total;

                changeTxt.setVisibility((changeTxt.getVisibility() == View.VISIBLE)
                        ? View.INVISIBLE : View.VISIBLE);

                if (change < 0 ){
                    changeTxt.setText("Your numbers dont add up");
                    tryBtn.setVisibility((tryBtn.getVisibility() == View.VISIBLE)
                            ? View.INVISIBLE : View.VISIBLE);
                    tryBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(MainActivity.this, MainActivity.class));

                        }
                    });

                }else {

                    changeTxt.setText( "Give Back Change oF: R" + change +"" );
                    nxtBtn.setVisibility((nxtBtn.getVisibility() == View.VISIBLE)
                            ? View.INVISIBLE : View.VISIBLE);
                    nxtBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(MainActivity.this, MainActivity.class));    
                        }
                    });    
                }    
            }
        });    
    }
}
...