1

In my Activity A , I have checkbox, spinner and editText. If checkbox is checked, it will show editText and hide spinner and textView .Otherwise, it will show them.

   public void addListenerOnChk() // for checkbox
    {


        checkBox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked())
                {
                    typeProject.setVisibility(View.VISIBLE);
                    project.setVisibility(View.GONE);
                    ProjectName.setVisibility(View.GONE);
                }
                else
                {
                    typeProject.setVisibility(View.GONE);
                    project.setVisibility(View.VISIBLE);
                    ProjectName.setVisibility(View.VISIBLE);
                }

            }

        });

So this is my layout

Before checkbox is checked

enter image description here

After

enter image description here

If the checkbox did not checked, how can I move the progress bar below the checkbox(Currently it has a space for editText)?

If checkbox checked, how to move up the checkbox and hide the spinner and text ? Thanks.

Here my layout...sorry, it a bit long (but the things I want to change is id/ProjectName, id/checkbox2 and id/editText). id/checkbox2 and id/editText is in the last two paragraph.

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">


<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    android:background="@mipmap/background_work_details">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ProjectName"
        android:padding="10dp"
        android:text="Project/Service/Training"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="14dp"
        android:layout_y="11dp" />

    <Spinner
        android:layout_width="322dp"
        android:layout_height="41dp"
        android:layout_margin="10dp"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:paddingLeft="20dp"
        android:layout_x="15dp"
        android:layout_y="47dp"
        android:id="@+id/SpinnerProject"
        android:spinnerMode="dropdown" />

    <EditText
        android:layout_width="339dp"
        android:layout_height="156dp"
        android:layout_margin="10dp"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:paddingLeft="20dp"
        android:paddingBottom="80dp"
        android:layout_x="14dp"
        android:layout_y="730dp"
        android:id="@+id/editTextWorkDescription" />

    <SeekBar
        android:layout_width="206dp"
        android:layout_height="wrap_content"
        android:id="@+id/seekBarPercentage"
        android:layout_x="30dp"
        android:layout_y="189dp"
        android:indeterminate="false" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView5"
        android:padding="10dp"
        android:text="Time In"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="16dp"
        android:layout_y="225dp" />

    <EditText
        android:layout_width="80dp"
        android:layout_height="34dp"
        android:layout_margin="10dp"
        android:focusableInTouchMode="false"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:paddingLeft="20dp"
        android:layout_x="86dp"
        android:layout_y="225dp"
        android:id="@+id/TimeIn" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView4"
        android:padding="10dp"
        android:text="Time Out"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="172dp"
        android:layout_y="225dp" />

    <EditText
        android:layout_width="87dp"
        android:layout_height="34dp"
        android:layout_margin="10dp"
        android:focusableInTouchMode="false"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:paddingLeft="20dp"
        android:layout_x="246dp"
        android:layout_y="225dp"
        android:id="@+id/TimeOut" />

    <Button
        android:layout_width="127dp"
        android:layout_height="63dp"
        android:text="save"
        android:drawableRight="@mipmap/save"
        android:id="@+id/save"
        android:layout_x="217dp"
        android:layout_y="891dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textProgress"
        android:layout_x="257dp"
        android:textColor="@color/red"
        android:layout_y="194dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView7"
        android:padding="10dp"
        android:text="Travel From"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="14dp"
        android:layout_y="262dp" />

    <EditText
        android:layout_width="223dp"
        android:layout_height="85dp"
        android:layout_margin="10dp"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:paddingLeft="20dp"
        android:layout_x="14dp"
        android:layout_y="300dp"
        android:id="@+id/travelFrom" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView15"
        android:padding="10dp"
        android:text="Travel To"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="14dp"
        android:layout_y="390dp" />

    <EditText
        android:layout_width="97dp"
        android:layout_height="35dp"
        android:layout_margin="10dp"
        android:inputType="numberDecimal"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:paddingLeft="15dp"
        android:layout_x="255dp"
        android:layout_y="470dp"
        android:id="@+id/mileage" />

    <EditText
        android:layout_width="222dp"
        android:layout_height="85dp"
        android:layout_margin="10dp"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:paddingLeft="20dp"
        android:layout_x="14dp"
        android:layout_y="423dp"
        android:id="@+id/travelTo" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView16"
        android:padding="10dp"
        android:inputType="numberDecimal"
        android:text="Hotel accomm"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="14dp"
        android:layout_y="550dp"
        android:allowUndo="true" />

    <EditText
        android:layout_width="92dp"
        android:layout_height="34dp"
        android:layout_margin="10dp"
        android:inputType="numberDecimal"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:hint="RM"
        android:paddingLeft="15dp"
        android:layout_x="131dp"
        android:layout_y="554dp"
        android:id="@+id/hotel" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView17"
        android:padding="10dp"
        android:text="Toll"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="222dp"
        android:layout_y="550dp" />

    <EditText
        android:layout_width="187dp"
        android:layout_height="34dp"
        android:layout_margin="10dp"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:hint="RM"
        android:inputType="numberDecimal"
        android:paddingLeft="15dp"
        android:layout_x="163dp"
        android:layout_y="620dp"
        android:id="@+id/business" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView18"
        android:padding="10dp"
        android:text="Business Expenses"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="14dp"
        android:layout_y="620dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView19"
        android:padding="10dp"
        android:text="Mileage"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="268dp"
        android:layout_y="430dp" />

    <EditText
        android:layout_width="92dp"
        android:layout_height="34dp"
        android:layout_margin="10dp"
        android:background="@drawable/round_corner_square"
        android:layout_weight="0.07"
        android:hint="RM"
        android:inputType="numberDecimal"
        android:paddingLeft="15dp"
        android:layout_x="263dp"
        android:layout_y="554dp"
        android:id="@+id/toll" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView20"
        android:padding="10dp"
        android:text="Work Description"
        android:textColor="@color/peru"
        android:layout_alignParentTop="true"
        android:layout_x="14dp"
        android:layout_y="683dp" />

    <CheckBox
        android:layout_width="112dp"
        android:layout_height="53dp"
        android:text="outstation"
        android:id="@+id/checkBox"
        android:textColor="@color/peru"
        android:layout_x="232dp"
        android:layout_y="662dp"
        android:checked="false" />

    <CheckBox
        android:layout_width="112dp"
        android:layout_height="45dp"
        android:text="Others?"
        android:id="@+id/checkBox2"
        android:layout_x="24dp"
        android:textColor="@color/peru"
        android:layout_y="92dp"
        android:checked="false" />

    <EditText
        android:layout_width="296dp"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:visibility="gone"
        android:layout_x="27dp"
        android:layout_y="133dp" />

</AbsoluteLayout>

</ScrollView>

3 Answers3

0

You should use OnCheckedChangeListener, rather than OnClickListener

Do this

checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        if(isChecked)
        {
            typeProject.setVisibility(View.VISIBLE);
            project.setVisibility(View.GONE);
            ProjectName.setVisibility(View.GONE);
        }
        else
        {
            typeProject.setVisibility(View.GONE);
            project.setVisibility(View.VISIBLE);
            ProjectName.setVisibility(View.VISIBLE);
        }
   }
});
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • no difference.....when I check the check box, how can I move up the check box and replaces the spinner ? –  Feb 15 '16 at 14:49
  • @Seng you tell me they still there after trying this?!! how come? – Muhammed Refaat Feb 15 '16 at 14:53
  • @Seng: what exactly are you trying to do. I thought you view is not hiding/unhiding. – Rohit5k2 Feb 15 '16 at 14:58
  • sir please check my post again. When check box not checked, it show a space above progress bar(for editText when checked). When checkbox checked, the spinner is hiding but I want the check box move up and replace the spinner..is it possible??? –  Feb 15 '16 at 15:03
  • same..if check box is checked,it hide the spinner but how to move up the checkbox? –  Feb 15 '16 at 15:12
  • Looks like your checkbox position is fixed somehow in xml. Please post the layout xml. – Rohit5k2 Feb 15 '16 at 15:14
0

Try with OnCheckedChangeListener:

checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if(isChecked){
                    typeProject.setVisibility(View.VISIBLE);
                    project.setVisibility(View.GONE);
                    ProjectName.setVisibility(View.GONE);
            }
           else{
                    typeProject.setVisibility(View.GONE);
                    project.setVisibility(View.VISIBLE);
                    ProjectName.setVisibility(View.VISIBLE);
           }
        });
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
0

Your problem is because you are using absolute_layout with layout_x and layout_y, so all views will be at your predefined positions of x and y, no matter what elements will you remove or what elements will you leave.

So, you have to change the layout parameters for the elements not just set it's visibility to VISIBLE or GONE, so the complete solution will be:

    checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(isChecked){
                typeProject.setVisibility(View.VISIBLE);
                project.setVisibility(View.GONE);
                ProjectName.setVisibility(View.GONE);

                // now settings the new parameters
                AbsoluteLayout.LayoutParams params = ((AbsoluteLayout.LayoutParams) typeProject.getLayoutParams());
                params.x = 100; // the new value
                params.y = 100; // the new value
                typeProject.setLayoutParams(params);
             }
            else{
                typeProject.setVisibility(View.GONE);
                project.setVisibility(View.VISIBLE);
                ProjectName.setVisibility(View.VISIBLE);

                // use the same way here with 'project' and 'ProjectName'
            }
    });
Community
  • 1
  • 1
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118