Alex
Alex

Reputation: 1936

My toolbar doesn't show up in CardView

I want to make a CardView recycler adapter to display 2 things(like first image).i have 2 xml layouts one with the toolbar and the other with the CardView.The problem is that the CardView is being displayed at the beginning of the screen and no toolbar is been displayed (only the arrow but still...).

The way i want it

This is what I get:

The way i have it

Here is my first xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/constr"
    android:layout_height="match_parent"
    tools:context=".SettingsAct.SettingsActiv">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarSettings"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorAccent"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/todayToolbarSettingsBtn"
        android:layout_width="39dp"
        android:layout_height="35dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@drawable/ic_arrow_back_black_24dp"
        app:layout_constraintEnd_toEndOf="@+id/toolbarSettings"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/toolbarSettings" />

    <TextView
        android:id="@+id/settingsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="230dp"
        android:layout_marginTop="8dp"
        android:text="@string/settingsText"
        android:textColor="@color/colorWhite"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@+id/toolbarSettings" />


</android.support.constraint.ConstraintLayout>

Here is my second xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="71dp"
            android:layout_marginTop="8dp"
            android:gravity="left"
            android:paddingBottom="4dp"
            android:paddingLeft="16dp"
            android:text="TextView"
            android:textColor="#000"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/desc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="20dp"
            android:layout_marginTop="42dp"
            android:gravity="bottom"
            android:text="TextView"
            android:textSize="18sp"
            android:textStyle="bold" />

        <View
            android:layout_width="match_parent"
            android:layout_height=".5dp"
            android:background="@color/cardview_dark_background" />

    </RelativeLayout>
</android.support.v7.widget.CardView>

And here is my main activity

class SettingsActiv : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_settings)


        val constraintLayout = findViewById(R.id.constr) as ConstraintLayout
        val recyclerView =  RecyclerView(this)
        val series = ArrayList<SeriesModel>()
        series.add(SeriesModel("Unit of length","Meter"))
        series.add(SeriesModel("Unit of temperature","Celcius"))
        val adapter = MyAdapter(series)
        recyclerView.setHasFixedSize(true)
        recyclerView.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
        recyclerView.adapter = adapter
        constraintLayout.addView(recyclerView)

    }
}

Upvotes: 0

Views: 327

Answers (2)

user8959091
user8959091

Reputation:

Replace the constraint layout with a LinearLayout, set its orientation to vertical and use this code to add the recyclerview:

val linearLayout = findViewById(R.id.constr) as LinearLayout
recyclerView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)
linearLayout.addView(recyclerView, 1)

Upvotes: 1

Mbuodile Obiosio
Mbuodile Obiosio

Reputation: 1543

setSupportActionBar(findViewById(R.id.toolbarSettings))
supportActionBar?.setDisplayHomeAsUpEnabled(true)

Upvotes: 1

Related Questions