0

i want to remove the space between TextView and Button i tried to many options but no one worked for now . thanks in advance and any help will be appreciated . the xml code and preview

this is the code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.myapplication.MainActivity">
<TextView
    android:layout_margin="0dp"
    android:gravity="center"
    android:textColor="#000000"
    android:textSize="33sp"
    android:text="hello"
    android:background="#cc3"
    android:layout_width = "match_parent"
    android:layout_height = "0dp"
    android:layout_weight = "3"/>
<Button
    android:layout_margin="0dp"
    android:layout_width = "match_parent"
    android:layout_weight = "1"
    android:layout_height = "0dp"
    android:text="Click"/>

</LinearLayout>
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • Try some of the things discussed here: http://stackoverflow.com/questions/17960599/how-to-remove-padding-around-buttons-in-android – C0D3LIC1OU5 Sep 07 '16 at 21:08

3 Answers3

0

Use negative margin.

android:margin=-2dp

EDIT: If you want to avoid negative margins, you must create your own (button) view because the standard button view comes with that margin, so you can't remove it

techfly
  • 1,826
  • 3
  • 25
  • 31
0

You could always just swap the Button for another TextView and add an OnClickListener to it?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.myapplication.MainActivity">
    <TextView
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="33sp"
        android:text="hello"
        android:background="#cc3"
        android:layout_width = "match_parent"
        android:layout_height = "0dp"
        android:layout_weight = "3"/>
    <TextView
        android:id="@+id/tv_button"
        android:layout_width = "match_parent"
        android:layout_weight = "1"
        android:layout_height = "0dp"
        android:gravity="center"
        android:textAllCaps="true"
        android:text="Click"/>

</LinearLayout>

Then in your Activity:

TextView button = (TextView) findViewById(R.id.tv_button);
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Do some stuff here
        }
    });
Isaac
  • 1,442
  • 17
  • 26
0

Add a background color to the button and change the text color accordingly. That way the space will be removed.

<Button
        android:layout_margin="0dp"
        android:layout_width = "match_parent"
        android:layout_height = "wrap_content"
        android:text="Click"
        android:background="@color/common_google_signin_btn_text_dark_focused"
        android:layout_below="@+id/a"/>

Any custom color will do.

enter image description here

driftking9987
  • 1,673
  • 1
  • 32
  • 63