My activity got crashed when I run my application in kotlin - android

I am creating one learning application in which one activity suddenly crashed. everything I did for it, I install application again but not get succussed. Please suggest me any solutions
In this is activity I join variable activity (Variable is name of activity)
package com.example.myapplication.java
import android.annotation.SuppressLint
import android.app.ActivityOptions
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import com.example.myapplication.R
import com.example.myapplication.java.Variable
class Java_Basic : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_java_basic)
val back: ImageView = findViewById(R.id.back)
val variable: Button = findViewById(R.id.variable_btn)
back.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
variable.setOnClickListener{
intent = Intent(this, Variable::class.java)
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle())
}
}
}
This is Variable activity
package com.example.myapplication.java
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ImageView
import com.example.myapplication.R
class Variable : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_variable)
val back: ImageView = findViewById(R.id.back2)
back.setOnClickListener {
onBackPressedDispatcher.onBackPressed()
}
}
}
this is Variable.xml
<?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="#drawable/bg_main" >
<ImageView
android:id="#+id/back2"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="16dp"
android:layout_marginTop="40dp"
android:src="#drawable/ic_baseline_arrow_back_242"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<ImageView
android:id="#+id/imageView"
android:layout_width="49dp"
android:layout_height="49dp"
android:layout_marginStart="84dp"
android:layout_marginTop="28dp"
android:src="#drawable/javalogo"
app:layout_constraintStart_toEndOf="#+id/back2"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/java"
android:layout_width="79dp"
android:layout_height="41dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="104dp"
android:fontFamily="#font/el_messiri_medium"
android:gravity="center"
android:text="Java"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="35sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="88dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="10sp"
app:cardBackgroundColor="#36393F"
app:cardCornerRadius="10dp"
app:cardElevation="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10sp"
android:layout_marginTop="10sp"
android:fontFamily="#font/el_messiri_medium"
android:text="#string/variable"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:id="#+id/intro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_marginBottom="10sp"
android:autoLink="web"
android:fontFamily="#font/alata"
android:lineHeight="27dp"
android:justificationMode="inter_word"
tools:ignore="UnusedAttribute"
android:paddingHorizontal="10dp"
android:text="#string/variable_intro"
android:textColor="#color/white"
android:textColorLink="#9997CB"
android:textSize="16sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="10sp"
app:cardBackgroundColor="#36393F"
app:cardCornerRadius="10dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:fontFamily="#font/el_messiri_medium"
android:text="#string/local_var"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10sp"
android:autoLink="web"
android:fontFamily="#font/alata"
android:lineHeight="27dp"
android:justificationMode="inter_word"
tools:ignore="UnusedAttribute"
android:paddingHorizontal="10dp"
android:text="#string/localvar_explain"
android:textColor="#color/white"
android:textColorLink="#9997CB"
android:textSize="16sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="10sp"
app:cardBackgroundColor="#36393F"
app:cardCornerRadius="10dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:autoLink="web"
android:fontFamily="#font/alata"
android:lineHeight="27sp"
android:paddingHorizontal="10dp"
android:text="#string/java_sample"
android:textColor="#color/white"
android:textColorLink="#9997CB"
android:textSize="16sp"
tools:ignore="UnusedAttribute" />
<androidx.cardview.widget.CardView
android:layout_width="335sp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:layout_margin="15sp"
app:cardBackgroundColor="#FFF"
app:cardElevation="8dp">
<TextView
android:id="#+id/code1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginBottom="10sp"
android:textSize="13sp"
android:fontFamily="serif-monospace"
android:lineHeight="27sp"
android:paddingHorizontal="10dp"
android:text="#string/localvar_code"
android:textStyle="bold"
android:textColor="#color/black"
android:textColorLink="#9997CB"
android:textIsSelectable="true"
tools:ignore="UnusedAttribute" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Related

Button Click and Change Text with Sliding Root Navigation Drawer

I am trying to create a logout button on the Sliding Root Navigation which I found here
This is my main activity
class HomeScreenActivity : AppCompatActivity() {
private lateinit var binding: ActivityHomeScreenBinding
private lateinit var leftDrawerBinding: MenuLeftDrawerBinding
private lateinit var homeScreenViewModel: HomeScreenViewModel
private var slidingRootNav: SlidingRootNav? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomeScreenBinding.inflate(layoutInflater)
leftDrawerBinding = MenuLeftDrawerBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.slToolbar)
title = ""
slidingRootSetup(savedInstanceState)
setUpBottomNavBar()
homeScreenViewModel = ViewModelProvider(this)[HomeScreenViewModel::class.java]
replaceFragment(InGymFragment())
// leftDrawerBinding.logoutFab.setOnClickListener(listener)
}
// Side Panel
private fun slidingRootSetup(savedInstanceState: Bundle?) {
slidingRootNav = SlidingRootNavBuilder(this)
.withToolbarMenuToggle(binding.slToolbar)
.withMenuOpened(false)
.withGravity(SlideGravity.LEFT)
.withContentClickableWhenMenuOpened(true)
.withSavedState(savedInstanceState)
.withMenuLayout(R.layout.menu_left_drawer)
.inject()
}
private val listener = View.OnClickListener {
when (it.id) {
leftDrawerBinding.logoutFab.id -> logout()
}
}
private fun logout() {
Toast.makeText(this, "logout clicked!", Toast.LENGTH_LONG).show()
// homeScreenViewModel.logoutUser
// startActivity(Intent(this,AuthenticationScreenActivity::class.java))
}
This is the menu left drawer
Screenshot of menu left drawer
<?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/white"
android:backgroundTint="#color/colorPrimaryDark"
android:orientation="vertical">
<TextView
android:id="#+id/user_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="#string/name"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_name">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/logout_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#color/white"
android:focusableInTouchMode="true"
android:text="#string/logout"
tools:viewBindingType="com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton"
android:textColor="#color/black"
app:icon="#android:drawable/ic_lock_power_off"
app:iconTint="#color/black"
app:layout_constraintBottom_toTopOf="#id/app_version"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/app_version"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="#string/app_creator_with_version"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the main activity layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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=".view.ui.HomeScreenActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_blue">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_bottom_appbar"
app:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/sl_toolbar"
android:layout_width="match_parent"
android:layout_height="86dp"
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" />
<ProgressBar
android:id="#+id/id_progress_bar"
android:layout_width="65dp"
android:layout_height="65dp"
android:indeterminateDrawable="#drawable/progress_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/profile"
android:layout_marginStart="8.5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/welcomeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="#string/app_name"
android:textColor="#ECE8E8"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="#+id/profile_image"
app:layout_constraintTop_toTopOf="#+id/profile_image" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/today_s_date"
android:textColor="#FFFFFF"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="#+id/welcomeMessage"
app:layout_constraintTop_toBottomOf="#+id/welcomeMessage" />
<ImageView
android:id="#+id/notificationsImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/profile_image"
android:contentDescription="#string/notification" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/main_frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/bottom_nav_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/appBarLayout" />
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="#+id/bottom_nav_bar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="#drawable/rounded_top_bottomnav"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:cnb_menuResource="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
I don't know how to add an adapter to this. I tried searching for anyone already done that before but didn't find any clue.
Please See: Adding an android:onClick="logout" with fun logout(view: View) { Toast.makeText(this, "logout clicked via xml!", Toast.LENGTH_LONG).show() } works. But I wish to change the text too how am I suppose to get a hold to it?
You should include some relevant code of what you have tried so far, in order to better understand the question.
Anyway, if I get that correctly you need to add a Button in the menu.xml file, then in your fragment/activity.kt you can set a onClickListener on said button to implement the logic for logging out.

Kotlin based Android app crash when changing activity using a button

I am building my first app and i am trying to change activities using buttons.The first button works fine. But the second crashes the app giving this error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
I tried researching, but i didn't find a working solution. So please help me out here
login.kt
package com.example.login
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class login : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val loginbutton = findViewById<Button>(R.id.signinbtn)
loginbutton.setOnClickListener {
val intent = Intent(this,gamefeed::class.java)
startActivity(intent)
finish()
}
val signupbutton = findViewById<Button>(R.id.signupbtn)
signupbutton.setOnClickListener {
val intent = Intent(this,Registration::class.java)
startActivity(intent)
finish()
}
}
}
activity_login.xml
<?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:id="#+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/ic_spalsh"
tools:ignore="InvalidId">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="392dp"
android:layout_height="217dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="399dp"
android:layout_height="228dp"
android:layout_marginBottom="32dp"
android:contentDescription="#string/todo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.285"
app:srcCompat="#drawable/login1" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="#string/Join_with_us"
android:textColor="#color/black"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout"
tools:ignore="TextContrastCheck" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:background="#drawable/background_edittext"
android:elevation="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/username"
android:importantForAutofill="no"
android:inputType="textPersonName|textEmailAddress"
android:padding="24dp"
tools:ignore="TextContrastCheck"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF"
/>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editTextTextPassword"
android:layout_width="match_parent"
android:layout_height="71dp"
android:ems="10"
android:hint="#string/password"
android:importantForAutofill="no"
android:inputType="textPassword"
android:padding="24dp" />
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="#string/forgot_password"
android:textColor="#color/purple_700"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout"
tools:ignore="TextContrastCheck" />
<Button
android:id="#+id/signupbtn"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:backgroundTint="#color/white"
android:text="#string/Dont_have_account"
android:textColor="#color/black"
android:textSize="14sp"
android:visibility="visible"
app:cornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signinbtn"
tools:visibility="visible" />
<Button
android:id="#+id/signinbtn"
android:layout_width="176dp"
android:layout_height="68dp"
android:layout_marginTop="24dp"
android:background="#drawable/background_btn"
android:backgroundTint="#color/bottom_nav_background"
android:padding="15dp"
android:text="#string/login"
android:textColor="#color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:layout_constraintWidth_percent="0.6"
tools:ignore="DuplicateSpeakableTextCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
Registration.kt
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class Registration : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
val signinbutton2 = findViewById<Button>(R.id.loginbtn)
signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
}
activity_registration.xml
<?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:id="#+id/activity_registration"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Registration"
android:background="#drawable/ic_spalsh">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout3"
android:layout_width="393dp"
android:layout_height="213dp"
android:background="#drawable/registration1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.40"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/Register_Here"
android:textColor="#color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/constraintLayout3"
tools:ignore="TextContrastCheck" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="356dp"
android:layout_height="354dp"
android:layout_marginStart="30dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="30dp"
android:background="#drawable/background_edittext"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.263"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/email"
android:importantForAutofill="no"
android:inputType="textPersonName|textEmailAddress"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/username"
android:inputType="textPersonName"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:ems="10"
android:hint="#string/age"
android:inputType="textPersonName"
android:padding="10dp"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<TextView
android:id="#+id/gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#null"
android:text="#string/gender"
android:textColor="#AEA8BC"
android:textColorHighlight="#color/purple_700"
android:textSize="20sp"
tools:ignore="TextContrastCheck" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="44dp"
android:orientation="horizontal">
<RadioButton
android:id="#+id/malerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/male"
tools:ignore="TouchTargetSizeCheck" />
<RadioButton
android:id="#+id/femalerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:hint="#string/female"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />
<RadioButton
android:id="#+id/otherrb"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_weight="1"
android:hint="#string/other"
tools:ignore="TouchTargetSizeCheck" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/password1"
android:inputType="textPassword"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="#+id/confirmpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword"
android:padding="10dp"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<Button
android:id="#+id/regbtn"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:layout_marginStart="105dp"
android:background="#drawable/background_btn"
android:textColor="#color/white"
android:text="#string/register"
tools:ignore="DuplicateSpeakableTextCheck" />
</LinearLayout>
<Button
android:id="#+id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="123dp"
android:background="#00FFFFFF"
android:text="#string/Allready_have_account"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout2"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.Registration}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.login.Registration.onCreate(Registration.kt:15)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
You get an error because there is no loginbtn on the activity_registration.xml page
class Registration : AppCompatActivity() {
private lateinit var regbtn:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
regbtn=findViewById<Button>(R.id.regbtn)
signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
I recommend you to look at the binding structure
build.gradle(module)
buildFeatures {
viewBinding true
dataBinding true
}
Registration.kt
class Registration : AppCompatActivity() {
private lateinit var binding: RegistrationBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAdminBinding.inflate(layoutInflater)
val view=binding.root
setContentView(view)
binding.signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
}

How to calculate values on edittext when other edittext is focused?

I am trying to make some math operation when some edittext has focus. I have 3 edittexts and depending on which one has focus the math operation changes and so do the other two edittext values. I have something similar in Java and it works fine, but on Kotlin it does not enter any of the desired functions.
override fun onClick(v: View?) {
if (edtProductSuggestedPrice.hasFocus() ) {
calculateWitIVA(1)
}
if(edtProductSuggestedPriceWithIva.hasFocus()){
calculateWitIVA(2)
}
}
How can I make this work?
I believe you need to use MutableLiveData<String>
and it is recommended to use two-way data binding
for example
my ViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.yazan.talabatclonedriver.Repository
import com.yazan.talabatclonedriver.db.UserEntity
class UserViewModel(private val repository: Repository) : ViewModel() {
val userName: MutableLiveData<String> = MutableLiveData("N/A")
val phoneNum: MutableLiveData<String> = MutableLiveData("404")
val driverPass: MutableLiveData<String> = MutableLiveData("")
}
and my Layout
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="viewModel"
type="com.yazan.talabatclonedriver.viewModel.UserViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/orange"
tools:context=".ui.frags.SignInFrag">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="30dp"
android:text="Welcome to \nTalabat Driver"
android:textColor="#FFF"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:src="#drawable/scooter_delivery_pic"
app:layout_constraintBottom_toTopOf="#+id/cv_login_container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:ignore="ContentDescription" />
<androidx.cardview.widget.CardView
android:id="#+id/cv_login_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
app:cardElevation="20dp"
app:layout_constraintBottom_toTopOf="#+id/tv_navigate_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="15dp"
android:text="Log into your driver account"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<EditText
android:id="#+id/et_userName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:ems="5"
android:text="#={viewModel.userName}"
android:hint="Enter username"
android:textSize="20sp" />
<EditText
android:id="#+id/et_userPass"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:ems="5"
android:text="#={viewModel.driverPass}"
android:hint="Enter password"
android:textSize="20sp" />
<Button
android:id="#+id/bt_signIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/orange"
android:padding="10dp"
android:text="Continue"
android:textAllCaps="false"
android:textSize="18sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/tv_navigate_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{viewModel.driverPass}"
android:textColor="#FFF"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
when you change the et_userName edit text the tv_navigate_text should change at the same moment
don't forget to add
BuildFeatures{
dataBinding = true
}
to your level app Gradle

How do I pass entries from xml to kt?

I volunteer with an organization that provides no-cost labor to build wheelchair ramps, wrapiowa.org
One of our volunteers did an app for Apple phones to assist in this activity. I am attempting to duplicate his work for Android using Android Studio but need help. My expertise is in wood designing and construction, not app programming.
Thanks, Ron
Here is what I have, two user inputs for length in inches and 16th’s and height in inches and 16th’s.
Input variables: edittext_number_length, edittext_number16_length, edittext_number_height, edittext_number16_height
The output I want is an angle in degrees and a run length:
Output variables: textView_cut_angle, textView_run
Based on this math:
Math:
length = edittext_number_length + (edittext_number16_length/16)
height = edittext_number_height + (edittext_number16_height/16)
textView_cut_angle = (asin(height/lenght)*180/3.1416)
textView_run = (sqrt(pow(length, 2) - pow(height,2)))
angle.kt
package com.example.wrap_iowarampdesignaid
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
class angle : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_angle)
}
fun launchAngleActivity(view: View) {
startActivity(Intent(this#angle,illustration::class.java))
}
fun launchUpdateActivity(view: View) {
}
}
activity_angle.xml
<?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=".angle">
<ImageView
android:id="#+id/logo"
android:layout_width="218dp"
android:layout_height="100dp"
android:layout_marginTop="16dp"
android:contentDescription="#string/logo"
android:scaleType="centerInside"
android:src="#drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/string2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="#string/angle2"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo" />
<Button
android:id="#+id/angle_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="launchAngleActivity"
android:text="#string/s_illustration"
android:textAllCaps="false"
android:textColor="#android:color/white"
app:backgroundTint="#2196F3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/update_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:onClick="launchUpdateActivity"
android:text="#string/Update"
android:textAllCaps="false"
android:textColor="#android:color/white"
app:backgroundTint="#2196F3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView18" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/s_length"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/string2" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/s_heigth"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView7" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/sc_angle"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/update_button" />
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="#string/sc_length"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView19" />
<TextView
android:id="#+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="#string/degrees"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView19" />
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="#string/inches"
android:textColor="#android:color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView20" />
<EditText
android:id="#+id/edittext_number_length"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toStartOf="#+id/edittext_number16_length"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<EditText
android:id="#+id/edittext_number16_length"
android:layout_width="25dp"
android:layout_height="0dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toStartOf="#+id/textView_number_length"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<EditText
android:id="#+id/edittext_number_height"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView18"
app:layout_constraintEnd_toStartOf="#+id/edittext_number16_height"
app:layout_constraintTop_toTopOf="#+id/textView18" />
<EditText
android:id="#+id/edittext_number16_height"
android:layout_width="25dp"
android:layout_height="0dp"
android:background="#color/white"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="#+id/textView18"
app:layout_constraintEnd_toStartOf="#+id/textView_number16_length"
app:layout_constraintTop_toTopOf="#+id/textView18"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView_number_length"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:text="#string/_16"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView7" />
<TextView
android:id="#+id/textView_number16_length"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#color/white"
android:text="#string/_16"
app:layout_constraintBottom_toBottomOf="#+id/textView18"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView18" />
<TextView
android:id="#+id/textView_cut_angle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:background="#color/white"
android:text="0"
app:layout_constraintBottom_toBottomOf="#+id/textView19"
app:layout_constraintEnd_toStartOf="#+id/textView21"
app:layout_constraintTop_toTopOf="#+id/textView19" />
<TextView
android:id="#+id/textView_run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:background="#color/white"
android:text="0"
app:layout_constraintBottom_toBottomOf="#+id/textView20"
app:layout_constraintEnd_toStartOf="#+id/textView22"
app:layout_constraintTop_toTopOf="#+id/textView20" />
</androidx.constraintlayout.widget.ConstraintLayout>

Relative layout with wrap content

I want to customize AlertDialog to have a fixed header and a footer with scrollable RecyclerView. So I tried to do it with RelativeLayout but if there are many items in RecyclerView then the footer hides below RecyclerView. And I add fixed height to the RecyclerView then if there are fewer items then there is extra space between RecyclerView and footer.
With RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:gravity="center"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/main"
android:orientation="vertical">
<TextView
android:text="Add Additional Information"
android:id="#+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:gravity="center"
android:textColor="#color/white"
android:textStyle="bold"
android:background="#drawable/popupTitle"
android:padding="#dimen/_10sdp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/additionalDataList"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/drlBottomMenu"
android:layout_height="wrap_content"
android:background="#color/offWhite"
android:orientation="vertical"
android:layout_below="#+id/main">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Continue"
android:layout_gravity="center"
android:id="#+id/continueButton"
style="#style/buttonStyle"
android:backgroundTint="#color/colorPrimary"
android:layout_weight="0.5"
android:textAllCaps="false"
android:padding="12dp"
android:layout_marginTop="#dimen/_12sdp"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginRight="#dimen/_20sdp"
android:textColor="#color/white"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cancel"
android:padding="12dp"
android:layout_gravity="center"
android:id="#+id/cancelButton"
android:textAllCaps="false"
style="#style/buttonStyle"
android:backgroundTint="#color/gray"
android:layout_weight="0.5"
android:textColor="#color/white"
android:layout_marginBottom="#dimen/_12sdp"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginRight="#dimen/_20sdp"
/>
</LinearLayout>
</RelativeLayout>
With LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/roundedCorner">
<TextView
android:text="Add Additional Information"
android:id="#+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16dp"
android:gravity="center"
android:textColor="#color/white"
android:textStyle="bold"
android:background="#drawable/popupTitle"
android:padding="#dimen/_10sdp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/additionalDataList"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
<Button
android:text="Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/continueButton"
style="#style/buttonStyle"
android:backgroundTint="#color/colorPrimary"
android:layout_marginTop="#dimen/_12sdp"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginRight="#dimen/_20sdp"
/>
<Button
android:text="Cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cancelButton"
style="#style/buttonStyle"
android:backgroundTint="#color/gray"
android:layout_marginBottom="#dimen/_12sdp"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginRight="#dimen/_20sdp"
/>
</LinearLayout>
Can anyone please help me in designing this.
Desired output should be:
EDIT
After using constraintlayout I am getting following result:
Try this
Activity code
import android.app.Dialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView additionalDataList;
ArrayList<String> arrayList = new ArrayList<>();
DataAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.test);
Window window = dialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
additionalDataList = dialog.findViewById(R.id.additionalDataList);
additionalDataList.setLayoutManager(new LinearLayoutManager(this));
additionalDataList.setHasFixedSize(true);
addDataToList();
adapter = new DataAdapter(this, arrayList);
additionalDataList.setAdapter(adapter);
dialog.show();
}
private void addDataToList() {
for (int i = 0; i < 50; i++) {
arrayList.add("Item :" + i);
}
}
}
layout.test
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
android:gravity="center"
android:padding="10dp"
android:text="Add Additional Information"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/additionalDataList"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/additionalDataList"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="#+id/drlBottomMenu"
app:layout_constraintEnd_toEndOf="#id/drlBottomMenu"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/additionalInfoTitle" />
<LinearLayout
android:id="#+id/drlBottomMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/additionalDataList">
<Button
android:id="#+id/continueButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:layout_marginRight="20dp"
android:layout_weight="0.5"
android:padding="12dp"
android:text="Continue"
android:textAllCaps="false"
android:textColor="#android:color/white"
app:backgroundTint="#color/colorPrimary" />
<Button
android:id="#+id/cancelButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="12dp"
android:layout_marginRight="20dp"
android:layout_weight="0.5"
android:padding="12dp"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#android:color/white"
app:backgroundTint="#android:color/darker_gray" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
OUTPUT
output when list has few items
output when list has more items
Try using MaterialDialog or AlertDialog, and then create the layout with constrainlayout + guidelines. It's way more easy.
<android.support.v7.widget.RecyclerView
android:id="#+id/additionalDataList"
android:scrollbars="vertical"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginBottom="height of bottom header"<-------------add this line
/>
or you can use constraintlayout and set height to 0dp so recylerview
can take up availabe space
one more option is to use linear layout and set it to wrap content and
dynamically inflate views
Try using Constraint inside constraint like
<?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:layout_height="match_parent">
<android.support.constraint.ConstraintLayout
android:id="#+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:padding="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/additionalInfoTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
android:background="#android:color/darker_gray"
android:gravity="center"
android:padding="10dp"
android:text="Add Additional Information"
android:textColor="#android:color/white"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/additionalDataList"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_weight="8"
android:scrollbars="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/additionalInfoTitle" />
<Button
android:id="#+id/continueButton"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:backgroundTint="#color/colorPrimary"
android:text="Login"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/additionalDataList" />
<Button
android:id="#+id/cancelButton"
style="#style/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:backgroundTint="#color/gray"
android:text="Cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/continueButton" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

Categories

Resources