Issue
It moves to the activity but I get an empty page
MainActivit.kt
This is the block I use to move
binding.fab2.setOnClickListener {
val intent = Intent(this, InfoActivity::class.java)
startActivity(intent)
}
The other activity
I removed the package
package ...
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.m.anwer.astudy.databinding.ActivityInfoBinding
public class InfoActivity : AppCompatActivity() {
private var _binding: ActivityInfoBinding? = null
private val binding: ActivityInfoBinding
get() = checkNotNull(_binding) { "Activity has been destroyed" }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityInfoBinding.inflate(layoutInflater)
setContentView(binding.root)
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
activity_info.xml
All seems good
<?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"
tools:context=".InfoActivity">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.MaterialComponents.Light" />
<TextView
android:id="@+id/tv1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Welcome"
android:textSize="18sp"
android:textStyle="bold"
android:layout_margin="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</com.google.android.material.appbar.AppBarLayout>
</androidx.constraintlayout.widget.ConstraintLayou
t>
Manifest
I added the another activity manually
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@drawable/graduate_hat"
android:roundIcon="@drawable/graduate_hat"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InfoActivity" />
</application>
</manifest>
All seems good but I don't know what is wrong
I have tried it without binding and the same problem. Tried to move to MainActivity from the same Activity and worked
Solution
Looks like the activity_info.xml
nesting might be the problem.
You have
ConstraintLayout
AppBarLayout
MaterialToolBar
TextView
Your Textview
has constraint layout attributes but isn't in the constraint layout. The AppBarLayout
doesn't have constraint layout attributes.
I think you're looking for something like
CoordinatorLayout
AppBarLayout
MaterialToolbar
ConstraintLayout
TextView (or other content)
AppBarLayout
should cooperate with the CoordinatorLayout
(for expand/collapse etc.) and the TextView
should be in the main content area (the ConstraintLayout
)
For example:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2023 Google LLC.
SPDX-License-Identifier: Apache-2.0
-->
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".InfoActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/tv1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Welcome"
android:textSize="18sp"
android:textStyle="bold"
android:layout_margin="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Answered By - Scott Stanchfield
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.