Search code examples
androidxmlandroid-layoutlayoutandroid-tablayout

Custom Tablayout without viewpager having textview hidden


So, I have a custom tablayout with text and a circular dark dot below it to show the user that they need to click on it. So, My tablayout doesn't have any viewpager. I tried notification badger but it doesn't work in tablayout.

MainClass.java

public class MainActivity extends AppCompatActivity {

        TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabLayout = findViewById(R.id.tablayout);

        View view = (FrameLayout)LayoutInflater.from(this).inflate(R.layout.custom_layout, null);
        TextView tv = (TextView) view.findViewById(R.id.textview);
        tv.setText("This is cool");


        tabLayout.addTab(tabLayout.newTab().setCustomView(view));



    }
}

cusotm_layout.xml

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

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
     android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/textview"
        android:text="SOmetext "

        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimaryDark"
        android:layout_gravity="center" />

    <ImageView
        android:layout_width="100dp"
        android:src="@drawable/bg_card_elements"
        android:id="@+id/imageview"
        android:layout_gravity="bottom|center"
        android:layout_height="100dp">

    </ImageView>

</FrameLayout>

I don't know why my text is not visible. any idea? image without text


Solution

  • Your textview is hidden behind imageview so it's not displaying.

    Switch imageview and textview like this

    cusotm_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <ImageView
            android:layout_width="100dp"
            android:src="@drawable/bg_card_elements"
            android:id="@+id/imageview"
            android:layout_gravity="bottom|center"
            android:layout_height="100dp">
    
    </ImageView>
    
    <TextView
            android:layout_width="wrap_content"
            android:id="@+id/textview"
            android:text="SOmetext "
            android:layout_height="wrap_content"
            android:textColor="@color/colorPrimaryDark"
            android:layout_gravity="center" /></FrameLayout>