Roma Kap
Roma Kap

Reputation: 636

Delete Button programmatically by other button

I have a delete Button (btnDelete). When I press it, I want to delete other "selected" button. I don't know, how I can select something on touchscreen or determine, which UI-Element should be delete now. Here is my .xml-File on for my Activity:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/overViewLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:columnCount="6"
    android:orientation="vertical"
    tools:context="com.example.myexpenses.OverviewActivity" >

    <TextView
         android:id="@+id/tvOverViewTitel"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentTop="true"
         android:layout_centerHorizontal="true"
         android:layout_marginTop="28dp"
         android:text="Übersicht"
         android:textColor="#ffffffff"
         android:textSize="24dp"
         android:textStyle="bold" />


    <ScrollView android:layout_width="fill_parent"  android:layout_height="195dp" android:scrollbars="vertical"
                android:layout_marginTop="90dp">
    <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content">
        <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">
        <Button
            android:id="@+id/test11"
            android:layout_width="60dp"
            android:layout_height="60dp"            
            android:background="@drawable/selected_category"
            android:text="rechts1" />

        <Button
            android:id="@+id/test18831"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="right2" />
        </LinearLayout>
        <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">     
        <Button
            android:id="@+id/test141"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        <Button
            android:id="@+id/test533"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        </LinearLayout>
        <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="fill_parent">      

        <Button
            android:id="@+id/test121"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        <Button
            android:id="@+id/test23"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        </LinearLayout>
        <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">     

        <Button
            android:id="@+id/test1212212"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        <Button
            android:id="@+id/test2324"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        </LinearLayout>
        <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">     

        <Button
            android:id="@+id/test121222112"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        <Button
            android:id="@+id/test2233"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        </LinearLayout>
        <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">     

        <Button
            android:id="@+id/test12199"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        <Button
            android:id="@+id/test23232"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        </LinearLayout>
        <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal" android:layout_height="wrap_content">     

        <Button
            android:id="@+id/test1111"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        <Button
            android:id="@+id/test23222"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@drawable/selected_category"
            android:text="links" />
        </LinearLayout>
     </LinearLayout>
     </ScrollView>

     <Button
        android:id="@+id/btnDelete"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignBaseline="@+id/btnAdd"
        android:layout_alignBottom="@+id/btnAdd"
        android:layout_alignParentRight="true"
        android:layout_marginRight="32dp"
        android:background="@drawable/delete" />

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="46dp"
        android:layout_marginRight="24dp"
        android:layout_toLeftOf="@+id/tvTitel"
        android:background="@drawable/add" />  


</RelativeLayout>

Upvotes: 1

Views: 110

Answers (2)

Tan Tran
Tan Tran

Reputation: 176

You set the height of your ScrollView is 195dp, I think that is the main reason that you just see only one button. In your case, you can use "match_parent" or "fill_parent" instead

 <ScrollView
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">

Upvotes: 0

bbb
bbb

Reputation: 1489

I don't really understand your explanation of your problem but judging by the title I think this is what you want to do in your Java code.

Button btn = (Button)findViewById(R.layout.button)
btn.setVisibility(btn.GONE)

To remove it from the layout. Or alternatively

btn.setVisibility(btn.INVISIBLE)

Which will hide it so it will still take up space

Upvotes: 1

Related Questions