Search code examples
javaandroidxmlkotlinbutton

Why are the borders not showing on the buttons?


I am building an app in Android Studio and I am trying to put a border around my buttons.

Specifically here is a button I am trying to add it to:

<Button
        android:id="@+id/activitiesButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        style="@style/ButtonStyle"
        android:text="@string/activities"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/healthBeautyButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mbCardButton"
        tools:ignore="VisualLintButtonSize" />

Here is the code for the entire file actvity_home.xml:

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

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    style="@style/ButtonStyle"
    tools:ignore="SpeakableTextPresentCheck">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"

    tools:context=".HomeActivity"
    tools:ignore="Overdraw" android:layout_height="wrap_content">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/virtual_card"
        android:textColor="#FEBE0F"
        android:textSize="27sp"
        android:textStyle="bold"
        android:gravity="center_horizontal"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="20sp"
        app:autoSizeMaxTextSize="25sp"
        app:autoSizeStepGranularity="1sp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        app:layout_constraintBottom_toTopOf="@+id/mbCardButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="TextSizeCheck" />

    <ImageView
        android:id="@+id/mbCardButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="@string/mane_benefits_logo"
        app:layout_constraintBottom_toTopOf="@+id/activitiesButton"
        app:layout_constraintDimensionRatio="W,2:1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:srcCompat="@drawable/mb_card"
        tools:ignore="SpeakableTextPresentCheck,DuplicateSpeakableTextCheck,ImageContrastCheck" />

    <Button
        android:id="@+id/activitiesButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        style="@style/ButtonStyle"
        android:text="@string/activities"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/healthBeautyButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/mbCardButton"
        tools:ignore="VisualLintButtonSize" />


    <Button
        android:id="@+id/healthBeautyButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        style="@style/ButtonStyle"
        android:text="@string/health_beauty"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/restaurantsButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/activitiesButton"
        tools:ignore="TextSizeCheck,VisualLintButtonSize" />

    <Button
        android:id="@+id/restaurantsButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        style="@style/ButtonStyle"
        android:text="@string/restaurants"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/retailButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/healthBeautyButton"
        tools:ignore="VisualLintButtonSize" />

    <Button
        android:id="@+id/retailButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        style="@style/ButtonStyle"
        android:text="@string/retail"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/servicesButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/restaurantsButton"
        tools:ignore="VisualLintButtonSize" />

    <Button
        android:id="@+id/servicesButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        style="@style/ButtonStyle"
        android:text="@string/services"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/sevenPointsButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/retailButton"
        tools:ignore="VisualLintButtonSize" />

    <Button
        android:id="@+id/sevenPointsButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        style="@style/ButtonStyle"
        android:text="@string/_7_points"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/mbLogo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/servicesButton"
        tools:ignore="VisualLintButtonSize" />

    <ImageView
        android:id="@+id/ffcuLogo"
        android:layout_width="64dp"
        android:layout_height="74dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/ffcu_logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/mbLogo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton"
        app:srcCompat="@drawable/ffcu_logo" />

    <ImageView
        android:id="@+id/mbLogo"
        android:layout_width="64dp"
        android:layout_height="74dp"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/mane_benefits_logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/fbButton"
        app:layout_constraintStart_toEndOf="@+id/ffcuLogo"
        app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton"
        app:srcCompat="@drawable/mb_logo_new" />

    <ImageButton
        android:id="@+id/fbButton"
        android:layout_width="51dp"
        android:layout_height="76dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="0dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/mb_fb_logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/instaButton"
        app:layout_constraintStart_toEndOf="@+id/mbLogo"
        app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton"
        app:srcCompat="@drawable/mb_fb"
        tools:ignore="SpeakableTextPresentCheck" />

    <ImageButton
        android:id="@+id/instaButton"
        android:layout_width="64dp"
        android:layout_height="68dp"
        android:layout_marginStart="0dp"
        android:layout_marginTop="0dp"
        android:layout_marginEnd="20dp"
        android:layout_marginBottom="20dp"
        android:contentDescription="@string/mb_insta_logo"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/fbButton"
        app:layout_constraintTop_toBottomOf="@+id/sevenPointsButton"
        app:srcCompat="@drawable/mb_insta"
        tools:ignore="SpeakableTextPresentCheck,ImageContrastCheck" />

</androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>

Here is the styles.xml:

<resources>
    <style name="ButtonStyle" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/border</item> <!-- Reference the correct drawable name -->
        <item name="android:textColor">#FEBE0F</item> <!-- Text color -->
        <item name="android:textSize">18sp</item> <!-- Example text size -->
        <item name="android:padding">10dp</item> <!-- Example padding -->
    </style>
</resources>

Here is the border.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#632994" /> <!-- Background color -->
    <stroke android:width="5dp" android:color="#FEBE0F" /> <!-- Border color and width -->
    <corners android:radius="5dp" /> <!-- Rounded corners -->
</shape>

in the Design view I see the border when I am in the border.xml it looks like this: The Border Image

But in my home_activity.xml it just looks like this: home_activity.xml

I have tried making so the background of the button is the border like

 <Button
        android:id="@+id/sevenPointsButton"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:text="@string/_7_points"
        android:background="@drawable/border"
        android:textColor="#FEBE0F"
        app:autoSizeTextType="uniform"
        app:autoSizeMinTextSize="5sp"
        app:autoSizeMaxTextSize="30sp"
        app:autoSizeStepGranularity="1sp"
        app:layout_constraintBottom_toTopOf="@+id/mbLogo"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/servicesButton"
        tools:ignore="VisualLintButtonSize" />

I even put it around the entire view to see if it would even work, as you can see from the picture above it does.

Nothing else affects these buttons except the setting them up to go to different views in the HomeActivity.java:

public class HomeActivity extends AppCompatActivity {

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

    Button transitionButton = findViewById(R.id.activitiesButton);
        transitionButton.setOnClickListener(v -> {
            Intent intent = new Intent(HomeActivity.this,ActivitiesActivity.class);
            startActivity(intent);
        });

        transitionButton = findViewById(R.id.healthBeautyButton);
        transitionButton.setOnClickListener(v -> {
            Intent intent = new Intent(HomeActivity.this,HealthBeautyActivity.class);
            startActivity(intent);
        });

        transitionButton = findViewById(R.id.restaurantsButton);
        transitionButton.setOnClickListener(v -> {
            Intent intent = new Intent(HomeActivity.this,RestaurantsActivity.class);
            startActivity(intent);
        });

        transitionButton = findViewById(R.id.retailButton);
        transitionButton.setOnClickListener(v -> {
            Intent intent = new Intent(HomeActivity.this,RetailActivity.class);
            startActivity(intent);
        });

        transitionButton = findViewById(R.id.servicesButton);
        transitionButton.setOnClickListener(v -> {
            Intent intent = new Intent(HomeActivity.this,ServicesActivity.class);
            startActivity(intent);
        });

        transitionButton = findViewById(R.id.sevenPointsButton);
        transitionButton.setOnClickListener(v -> {
            Intent intent = new Intent(HomeActivity.this,SevenPointsActivity.class);
            startActivity(intent);
        });

    }
}

Solution

  • try using a different button type as these buttons are quite hard to customize

       <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/sevenPointsButton"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_marginStart="20dp"
            android:layout_marginEnd="20dp"
            android:text="@string/_7_points"
            android:background="@drawable/border"
            android:textColor="#FEBE0F"
            app:autoSizeTextType="uniform"
            app:autoSizeMinTextSize="5sp"
            app:autoSizeMaxTextSize="30sp"
            app:autoSizeStepGranularity="1sp"
            app:layout_constraintBottom_toTopOf="@+id/mbLogo"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/servicesButton"
            tools:ignore="VisualLintButtonSize" />
    

    try this

    you can also try material buttons to they are the easiest to customize according to me