2

There must be something wrong with my code that changes the Buttons background color to gray with black text and also changes their size when running the app for testing. I can't put my finger on what is wrong. please help. It could be a conflict between my code and AdMob, or maybe the strocture of the code.

package com.example.kakurosidekick;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

import java.util.Arrays;
public class MainActivity extends Activity implements View.OnClickListener {

ListView list;
    String[] kakNmbrs_25_5 = {"1+2+5+8+9", "1+2+6+7+9", "1+3+4+8+9", "1+3+5+7+9", "1+3+6+7+8", "1+4+5+6+9", "1+4+5+7+8",
            "2+3+4+7+9", "2+3+5+6+9", "2+3+5+7+8", "2+4+5+6+8", "3+4+5+6+7"};
    String mstrButtn = String.valueOf(0);

        private AdView mAdView;

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn3 = (Button) findViewById(R.id.button3);
            Button btn4 = (Button) findViewById(R.id.button4);
           Button btn25 = (Button) findViewById(R.id.button25);
            Button dbtn2 = (Button) findViewById(R.id.DigiB2);
           btn3.setOnClickListener(this); // calling onClick() method
            btn4.setOnClickListener((View.OnClickListener) this);
            btn25.setOnClickListener(this);
            dbtn2.setOnClickListener((View.OnClickListener) this);
            list = findViewById(R.id.myList);
            Arrays.sort(kakNmbrs_25_5);
            
            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                
                public void goToAnActivity(View view) {

                }
                public void onInitializationComplete(InitializationStatus initializationStatus) {
                }
            });

            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
        }

    @Override
    public void onClick(View v) {
        Button btn03 = (Button) findViewById(R.id.button3);
        Button btn04 = (Button) findViewById(R.id.button4);
        Button btn05 = (Button) findViewById(R.id.button5);
        Button btn06 = (Button) findViewById(R.id.button6);

        Button[] buttonArray = {btn05, btn04, btn03};

        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.mytextsize, android.R.id.text1, kakNmbrs_25_5);

        switch (v.getId()) {

            case R.id.button3:
                mstrButtn = "3";
                Log.i(mstrButtn, "I just clicked on");
                break;

                       
            default:
                break;}}}

This is what it looks like:

enter image description here

and this is what it suppose to look like

enter image description here

my activity_main.xml looks like that:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@android:color/black"
    tools:context=".MainActivity">
<Button
        android:id="@+id/button3"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        android:text="3"
        android:textSize="24sp"
        android:backgroundTint="#8C8A8A"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/divider3" />
<Button
        android:id="@+id/DigiB3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="70dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:padding="0dp"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        android:text="3"
        android:textSize="30sp"
        app:backgroundTint="#009688"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/divider3" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginTop="8dp"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/divider6"
        app:layout_constraintVertical_bias="0.0">

    </com.google.android.gms.ads.AdView>
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51

3 Answers3

0

Mb you use tools:backgroundTint="@color/yourColor" in your xml code? If I am right, then just replace tools attribute with android, like this:

android:backgroundTint="@color/yourColor"

UPDATE AFTER AUTHOR ADDED XML PART.

Just replace app:backgroundTint="#009688" to android:backgroundTint="#009688"

Alesh17
  • 366
  • 1
  • 7
0

Refer to this answer: What is the difference between background, backgroundTint, backgroundTintMode attributes in android layout xml?

Basically, BackgroundTint gets applied to the Background, and because you don't have any Background attribute it does nothing.

EDIT: Seems like buttons already have a background, testing your xml isolated shows the button with your desired color, maybe something in your adapter?

0

Use android:background instead of android:backgroundTint. backgroundTint only works on API >= 21.

<Button
        android:id="@+id/DigiB3"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="70dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:padding="0dp"
        android:paddingLeft="0dp"
        android:paddingTop="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        android:text="3"
        android:textSize="30sp"
        app:background="#009688"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/divider3" />
Taha Malik
  • 2,188
  • 1
  • 17
  • 28