Im new to kotlin and Im trying to make a navigation bar with a time selector based on NumberPicker, with 2 other empty fragments (for now atleast), but my problem is that if I start an intent the Navigation bar doesnt show, when I dont put it in code (intent) the navigation bar shows up working correctly, what Im doing wrong, am I missing something??
MainActivity.kt
package com.example.helloworld
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.example.helloworld.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.bottomNavigationView.setOnItemSelectedListener {
when (it.itemId) {
R.id.first -> replaceFragment(Home())
R.id.second -> replaceFragment(Voices())
R.id.third -> replaceFragment(Settings())
else -> {
}
}
true
}
replaceFragment(Home())
Intent(this, SecondActivity::class.java).also {
startActivity(it)
}
}
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.frameLayout, fragment)
fragmentTransaction.commit()
}
}
SecondActivity.kt
package com.example.helloworld
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_second.*
class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)
numPickerMin2.minValue = 0
numPickerMin2.maxValue = 23
numPickerSec2.minValue = 0
numPickerSec2.maxValue = 59
var min = 0
var sec = 0
var amOrPm = ""
val str = arrayOf<String>("AM", "PM")
numPickerAM2.minValue = 0
numPickerAM2.maxValue = (str.size - 1)
numPickerAM2.displayedValues = str
numPickerMin2.setOnValueChangedListener { numberPicker, i, i2 ->
min = numberPicker.value
}
numPickerSec2.setOnValueChangedListener { numberPicker, i, i2 ->
sec = numberPicker.value
}
numPickerAM2.setOnValueChangedListener { numberPicker, i, i2 ->
val i = numberPicker.value
amOrPm = str[i]
}
btGetValue2.setOnClickListener {
reminderTime2.text = "$min : $sec : $amOrPm"
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.helloworld"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="0.2">
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="27" />
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
tools:targetApi="31"
android:theme="#style/Theme.HelloWorld">
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="sensorPortrait"
android:label="#string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity">
</activity>
</application>
</manifest>
ActivityMain.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"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/bottom_nav"/>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_second.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:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
android:background="#color/BackgroundFirst"
tools:context=".Home">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout4"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginTop="55dp"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="#+id/guideline3">
<AutoCompleteTextView
android:id="#+id/dropdown_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="none"
android:text="Choose Alert"/>
</com.google.android.material.textfield.TextInputLayout>
<com.shawnlin.numberpicker.NumberPicker
android:id="#+id/numPickerMin2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginEnd="23dp"
android:gravity="center"
android:theme="#style/AppTheme.Picker"
app:layout_constraintBottom_toBottomOf="#+id/numPickerSec2"
app:layout_constraintEnd_toStartOf="#+id/numPickerSec2"
app:layout_constraintTop_toTopOf="#+id/numPickerSec2"
app:np_dividerColor="#color/black"
app:np_dividerDistance="55dp"
app:np_dividerLength="100dp"
app:np_dividerThickness="3dp"
app:np_formatter=""
app:np_height="180dp"
app:np_max="12"
app:np_min="0"
app:np_selectedTextColor="#color/selected"
app:np_selectedTextSize="60sp"
app:np_textAlign="textAlignCenter"
app:np_textColor="#color/black"
app:np_textSize="40sp"
app:np_typeface="#string/roboto_light"
app:np_value="1"
app:np_wheelItemCount="10"
app:np_width="74dp"
app:np_wrapSelectorWheel="true" />
<com.shawnlin.numberpicker.NumberPicker
android:id="#+id/numPickerSec2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginTop="37dp"
android:layout_marginEnd="9dp"
android:layout_marginBottom="27dp"
android:gravity="center"
android:theme="#style/AppTheme.Picker"
app:layout_constraintBottom_toTopOf="#+id/textInputLayout4"
app:layout_constraintEnd_toStartOf="#+id/numPickerAM2"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:np_dividerColor="#color/black"
app:np_dividerDistance="55dp"
app:np_dividerLength="100dp"
app:np_dividerThickness="3dp"
app:np_formatter=""
app:np_height="180dp"
app:np_max="59"
app:np_min="0"
app:np_selectedTextColor="#color/selected"
app:np_selectedTextSize="60sp"
app:np_textAlign="textAlignCenter"
app:np_textColor="#color/black"
app:np_textSize="40sp"
app:np_typeface="#string/roboto_light"
app:np_value="1"
app:np_wheelItemCount="10"
app:np_width="74dp"
app:np_wrapSelectorWheel="true" />
<com.shawnlin.numberpicker.NumberPicker
android:id="#+id/numPickerAM2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_marginStart="56dp"
android:layout_marginEnd="55dp"
android:gravity="center"
android:theme="#style/AppTheme.Picker"
app:layout_constraintBottom_toBottomOf="#+id/numPickerSec2"
app:layout_constraintEnd_toEndOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="#+id/numPickerSec2"
app:np_dividerColor="#color/black"
app:np_dividerDistance="55dp"
app:np_dividerLength="100dp"
app:np_dividerThickness="3dp"
app:np_formatter=""
app:np_height="180dp"
app:np_max="1"
app:np_min="0"
app:np_selectedTextColor="#color/selected"
app:np_selectedTextSize="60sp"
app:np_textAlign="textAlignCenter"
app:np_textColor="#color/black"
app:np_textSize="40sp"
app:np_typeface="#string/roboto_light"
app:np_value="1"
app:np_wheelItemCount="10"
app:np_width="74dp"
app:np_wrapSelectorWheel="true" />
<Button
android:id="#+id/btGetValue2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="78dp"
android:backgroundTint="#color/selected"
android:baselineAligned="false"
android:text="Get Time"
android:textSize="15sp"
app:layout_constraintBottom_toTopOf="#+id/reminderTime2"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline2" />
<TextView
android:id="#+id/reminderTime2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="63dp"
android:text="Time"
android:textSize="40sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline2" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="351dp"
android:layout_height="103dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:scaleX="1.5"
android:scaleY="1.5"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/helloday_logo" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"
/>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
Instead of creating a second activity, I suggest that you create another fragment. Use it in the same way as the other fragments which you currently have in your app.
Related
So all I want to do is click the 'Sign up' button on my app and it takes me to the next page. I have made the second page and added a 'setOnclickListener' to the page I want but when I click on the button it takes me to another page that's white. I'm still new to Kotlin. There are no errors popping up that would effect it. Any ideas?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center"
android:orientation="vertical"
android:background="#drawable/gradient_background"
android:padding="20dp"
tools:context=".MainActivity">
<!--logo-->
<ImageView
android:id="#+id/logo_image_view"
android:layout_width="117dp"
android:layout_height="116dp"
android:layout_gravity="center"
android:layout_marginTop="80dp"
android:contentDescription="#string/todo"
android:scaleType="centerInside"
android:src="#drawable/logo"
tools:ignore="ImageContrastCheck" />
<!--non interactive log in text-->
<!--EmailAddress enter text-->
<TextView
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:hint="#string/email"
android:textColorHint="#880E4F"
tools:ignore="VisualLintTextFieldSize,TextContrastCheck" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:hint="#string/password"
android:textColorHint="#4E342E"
tools:ignore="VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<ProgressBar
android:id="#+id/progressbar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--Login-->
<Button
android:layout_marginTop="40dp"
android:id="#+id/btn_login"
android:text="#string/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--register button-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:id="#+id/button"
android:text="#string/sign_up"
android:onClick="mainActivity"
tools:ignore="UsingOnClickInXml" />
</LinearLayout>
MainActivity.kt
package com.example.gymapp
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var signUpButton: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
signUpButton = findViewById(R.id.button)
signUpButton.setOnClickListener {
val intent = Intent(this,RegistrationActivity::class.java)
startActivity(intent)
}
}
fun mainActivity(view: View) {}
}
RegistrationActivity.java
package com.example.gymbuddies;
import android.app.Activity;
public class RegistrationActivity extends Activity {
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Gymapp"
tools:targetApi="31">
<activity android:name=".register" />
<meta-data
android:name="android.app.lib_name"
android:value="" />
<activity
android:name=".Login"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".SignInActivity"
android:exported="false">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity android:name=".RegistrationActivity"
android:label="Registration">
</activity>
<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>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center"
android:orientation="vertical"
android:background="#drawable/gradient_background"
android:padding="20dp"
tools:context=".register">
<!--logo-->
<ImageView
android:id="#+id/logo_image_view"
android:layout_width="117dp"
android:layout_height="116dp"
android:src="#drawable/logo"
android:scaleType="centerInside"
android:layout_marginTop="80dp"
android:layout_gravity="center"
/>
<!--non interactive log in text-->
<!--EmailAddress enter text-->
<TextView
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"/>
<!--email-->
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:hint="#string/email"
android:textColorHint="#4527A0"
tools:ignore="VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<!--name-->
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:gravity="center"
android:id="#+id/name"
android:hint="#string/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
tools:ignore="VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<!--dob-->
<!--password-->
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:gravity="center"
android:id="#+id/password"
android:hint="#string/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
tools:ignore="VisualLintTextFieldSize" />
</com.google.android.material.textfield.TextInputLayout>
<ProgressBar
android:id="#+id/progressbar"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_marginTop="50dp"
android:id="#+id/btn_register"
android:text="#string/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Register.kt
package com.example.gymapp
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.textfield.TextInputEditText
import com.google.firebase.auth.FirebaseAuth
class Register : AppCompatActivity() {
private lateinit var editTextEmail: TextInputEditText
private lateinit var editTextPassword: TextInputEditText
private lateinit var editName: TextInputEditText
private lateinit var buttonReg: Button
private lateinit var mAuth: FirebaseAuth
private lateinit var progressbar: ProgressBar
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
mAuth = FirebaseAuth.getInstance()
progressbar = findViewById(R.id.progressbar)
editTextEmail = findViewById(R.id.email)
editTextPassword = findViewById(R.id.password)
editName = findViewById(R.id.name)
buttonReg = findViewById(R.id.btn_register)
buttonReg.setOnClickListener {
progressbar.visibility = View.VISIBLE
val email = editTextEmail.text.toString()
val password = editTextPassword.text.toString()
editName.text.toString()
// handle the button click event
if (email.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Wrong email or password", Toast.LENGTH_SHORT).show()
progressbar.visibility = View.GONE
return#setOnClickListener
}
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this) { task ->
progressbar.visibility = View.GONE
if (task.isSuccessful) {
Toast.makeText(baseContext, "Welcome!.",
Toast.LENGTH_SHORT).show()
} else {
// If sign in fails, display a message to the user.
Toast.makeText(baseContext, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
}
}
}
}
strings.xml
<resources>
<string name="app_name">GymBuddies</string>
<string name="password">Password</string>
<string name="email_address">Email Address</string>
<string name="type_your_name">Type your name</string>
<string name="login">Login</string>
<string name="email">Email</string>
<string name="register">register</string>
<string name="name">Name</string>
<string name="sign_up">Sign up</string>
<string name="todo">TODO</string>
</resources>
I've read through the code numerous times and I can't see anything conflicting.
I am following this tutorial to create an android app with space animation stuff. In the step titled "Transition Manager" you're instructed to add the following code to your MainActivity.kt file:
`
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
`
When this code is added the variable "constraintLayout" throws up an unresolved reference error
I think constraintLayout is supposed to be pointing to something in the xml and something is failing to make that happen, I just don't know what.
The kotlin code after adding the above block is:
`
package com.raywenderlich.android.razegalactic
import android.os.Bundle
import android.support.constraint.*
import android.support.v7.app.AppCompatActivity
import android.transition.TransitionManager
import kotlinx.android.synthetic.main.keyframe1.*
/**
* Main Screen
*/
class MainActivity : AppCompatActivity() {
private val constraintSet1 = ConstraintSet()
private val constraintSet2 = ConstraintSet()
private var isOffscreen = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.keyframe1)
switch1.setOnCheckedChangeListener { _, isChecked ->
switch1.setText(if (isChecked) R.string.round_trip else R.string.one_way)
constraintSet1.clone(constraintLayout) //1
constraintSet2.clone(this, R.layout.activity_main) //2
departButton.setOnClickListener { //3
//apply the transition
TransitionManager.beginDelayedTransition(constraintLayout) //4
val constraint = if (!isOffscreen) constraintSet1 else constraintSet2
isOffscreen = !isOffscreen
constraint.applyTo(constraintLayout) //5
}
}
}
}
`
The main xml is as follows:
`
<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:id="#+id/ConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/flightsIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rocket_icon"
app:layout_constraintBottom_toBottomOf="#+id/spaceStationIcon"
app:layout_constraintEnd_toStartOf="#+id/roverIcon"
app:layout_constraintStart_toEndOf="#+id/spaceStationIcon"
app:layout_constraintTop_toTopOf="#+id/spaceStationIcon" />
<ImageView
android:id="#+id/spaceStationIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/space_station_icon"
app:layout_constraintEnd_toStartOf="#+id/flightsIcon"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/roverIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/rover_icon"
app:layout_constraintBottom_toBottomOf="#+id/flightsIcon"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/flightsIcon"
app:layout_constraintTop_toTopOf="#+id/flightsIcon" />
<TextView
android:id="#+id/spaceStationLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/space_stations"
app:layout_constraintEnd_toEndOf="#+id/spaceStationIcon"
app:layout_constraintStart_toStartOf="#+id/spaceStationIcon"
app:layout_constraintTop_toBottomOf="#+id/spaceStationIcon" />
<TextView
android:id="#+id/textView1"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginEnd="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingEnd="20dp"
android:text="#string/dca"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintEnd_toEndOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/roverLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/rovers"
app:layout_constraintEnd_toEndOf="#+id/roverIcon"
app:layout_constraintStart_toStartOf="#+id/roverIcon"
app:layout_constraintTop_toBottomOf="#+id/roverIcon" />
<TextView
android:id="#+id/textView2"
android:layout_width="124dp"
android:layout_height="98dp"
android:layout_marginStart="40dp"
android:background="#color/colorPrimary"
android:gravity="center"
android:paddingStart="20dp"
android:text="#string/mars"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#+id/doubleArrowsIcon"
app:layout_constraintStart_toStartOf="#+id/doubleArrowsIcon"
app:layout_constraintTop_toTopOf="#+id/doubleArrowsIcon" />
<TextView
android:id="#+id/flightsLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="#string/flights"
app:layout_constraintEnd_toEndOf="#+id/flightsIcon"
app:layout_constraintStart_toStartOf="#+id/flightsIcon"
app:layout_constraintTop_toBottomOf="#+id/flightsIcon" />
<ImageView
android:id="#+id/doubleArrowsIcon"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginBottom="40dp"
android:src="#drawable/double_arrows"
app:layout_constraintBottom_toTopOf="#+id/guideline1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/switch1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="200dp"
android:background="#color/colorAccent"
android:checked="false"
android:padding="8dp"
android:switchPadding="24dp"
android:text="#string/one_way"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:padding="8dp"
android:text="#string/traveller"
android:textColor="#android:color/white"
app:layout_constraintStart_toStartOf="#+id/guideline2"
app:layout_constraintTop_toBottomOf="#+id/switch1" />
<ImageView
android:id="#+id/rocketIcon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="8dp"
android:src="#drawable/rocket_icon"
app:layout_constraintCircle="#id/galaxyIcon"
app:layout_constraintCircleAngle="270"
app:layout_constraintCircleRadius="100dp" />
<ImageView
android:id="#+id/galaxyIcon"
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/galaxy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/guideline1"
app:layout_constraintVertical_bias="0.495" />
<Button
android:id="#+id/departButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/depart"
android:textColor="#android:color/white"
app:backgroundTint="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="158dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="0dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintGuide_percent="1" />
</android.support.constraint.ConstraintLayout>
`
can anyone tell me what I might be missing or how this can be fixed?
Thanks in advance.
You're suppose to reference a view from the xml before you can use it.
Do this, also if the name of the layout the MainActivity is suppose to use is main.xml, it should be the one your setContentView use not some other layout.
lateinit var cosntraintLayout: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main) // <- main.xml
cosntraintLayout = findViewById<ConstraintLayout>(R.id.ConstraintLayout) // <- add this line
...
...
}
switch1 and departButton will also throw the same error, but I'll leave it for you to figure it out based on the code I provided.
I modified my code by referring to the "https://www.tutorialspoint.com/add-and-remove-views-in-android-dynamically-in-kotlin". There was no error in my code, but the view added when running in the app was not visible. I have no idea what the problem is. Please help me!!
Activity to which the view is added
package com.akj.callback
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import com.akj.callback.databinding.ActivitySearchSubject2Binding
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_search_subject_2.*
import kotlinx.android.synthetic.main.activity_search_subject_2.to_go_check
import kotlinx.android.synthetic.main.choiced_subject_list.*
import java.util.*
class search_subject_2 : AppCompatActivity() {
lateinit var binding3:ActivitySearchSubject2Binding
private var parentLinearLayout: LinearLayout? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search_subject_2)
parentLinearLayout = findViewById(R.id.parent_linear_layout)
binding3= ActivitySearchSubject2Binding.inflate(layoutInflater)
setContentView(binding3.root)
userList3.onItemClickListener= AdapterView.OnItemClickListener{
parent, view, position, id ->
//parent?.getItemAtPosition(position).toString()
onAddField()
}
val user = arrayOf("abc", "bcd", "cde", "def", "efg")
val userAdapter: ArrayAdapter<String> = ArrayAdapter(
this,android.R.layout.simple_list_item_1,user)
to_go_check.setOnClickListener {
val intent6=Intent(this#search_subject_2,checkthesetting::class.java)
startActivity(intent6)
}
binding3.searchView3.setOnQueryTextListener(object :androidx.appcompat.widget.SearchView.OnQueryTextListener{
override fun onQueryTextSubmit(query: String?): Boolean {
binding3.searchView3.clearFocus()
if(user.contains(query)){
userAdapter.filter.filter(query)
}
return false
}
override fun onQueryTextChange(newText: String?): Boolean {
if(newText==""){
}
else{
binding3.userList3.adapter=userAdapter
userAdapter.filter.filter(newText)
}
return false
}
})
}
fun onDelete(view: View) {
parentLinearLayout!!.removeView(view.parent as View)
}
fun onAddField() {
val inflater =
getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rowView: View = inflater.inflate(R.layout.choiced_subject_list, null)
parentLinearLayout!!.addView(rowView, parentLinearLayout!!.childCount - 1)
Toast.makeText(applicationContext,"fdsvkgjh", Toast.LENGTH_SHORT).show()
}
}
choiced_subject_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="#+id/number_edit_text"
android:text="title_fdskhdsffsd"
android:textColor="#color/black"
android:textSize="30dp"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="phone"
/>
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"
android:onClick="onDelete" />
</LinearLayout>
activity_search_subject_2.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"
tools:context=".search_subject_2">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.04" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.94" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.03" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.97" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.06" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.11" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.87" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/coner_search_bar"
android:theme="#style/AppSearchView"
app:layout_constraintBottom_toTopOf="#id/guideline6"
app:layout_constraintEnd_toEndOf="#id/guideline4"
app:layout_constraintStart_toStartOf="#id/guideline3"
app:layout_constraintTop_toBottomOf="#id/guideline5"
app:queryHint="강좌 검색"
app:iconifiedByDefault="false"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/to_go_check"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="#id/guideline3"
app:layout_constraintEnd_toStartOf="#id/guideline4"
app:layout_constraintTop_toBottomOf="#id/guideline1"
app:layout_constraintBottom_toTopOf="#id/guideline2"
app:layout_constraintVertical_bias="0.98"
android:text="확인"
android:textColor="#FFF"
android:textSize="20dp"
android:background="#drawable/coner_search_bar"
android:stateListAnimator="#null"
/>
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:scrollbars="none"
app:layout_constraintBottom_toTopOf="#+id/to_go_check"
app:layout_constraintEnd_toStartOf="#id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#id/guideline3"
>
<LinearLayout
android:id="#+id/linear_button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/njklsfd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="전공검색" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="교양검색" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="교수검색" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="정렬" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="학점:3" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:stateListAnimator="#null"
android:text="학점:2" />
<androidx.appcompat.widget.AppCompatButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#drawable/search_subject_bar"
android:text="학점:1"
/>
</LinearLayout>
</HorizontalScrollView>
<ListView
android:id="#+id/userList3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline8"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#id/guideline3"
app:layout_constraintTop_toTopOf="#+id/guideline6"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:orientation="vertical"
android:id="#+id/parent_linear_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
app:layout_constraintBottom_toTopOf="#id/scrollView"
app:layout_constraintEnd_toStartOf="#+id/guideline4"
app:layout_constraintStart_toEndOf="#id/guideline3"
app:layout_constraintTop_toBottomOf="#id/guideline8" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try this:
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val rowView: View = inflater.inflate(R.layout.row_child_view, null, false)
Mainview.removeView(rowView)
I don't know how to hide footer button when show soft key board.
please, help me.
[AndroidManifest.xml]
<activity
android:name=".presentation.signup.SignUpActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
[activity_sign_up.xml]
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layout_sign_up"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue_4c_color">
<!-- toolbar -->
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar_sign_up"
style="#style/toolbarCommonStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- toolbar title -->
<TextView
android:id="#+id/tv_toolbar_title_sign_up"
style="#style/toolbarTitleCommonStyle"
android:text="#string/sign_up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- signup fragment -->
<FrameLayout
android:id="#+id/fragment_container_sign_up"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/view_footer_sign_up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar_sign_up" />
<!-- footer button -->
<View
android:id="#+id/view_footer_sign_up"
style="#style/footerBgViewStyle"
android:background="#color/footer_view_sign_up_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/btn_next_sign_up"
style="#style/footerBtnStyle"
android:background="#drawable/shape_square_radius_dark_blue"
android:text="#string/next"
app:layout_constraintBottom_toBottomOf="#+id/view_footer_sign_up"
app:layout_constraintEnd_toEndOf="#+id/view_footer_sign_up"
app:layout_constraintStart_toStartOf="#+id/view_footer_sign_up"
app:layout_constraintTop_toTopOf="#+id/view_footer_sign_up" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
[fragment_sign_up_email_password.xml]
<?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">
<ScrollView
android:id="#+id/scrollview_sign_up_email_password"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/layout_sign_up_email_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/blue_4c_color"
android:focusable="true"
android:focusableInTouchMode="true">
<!-- id -->
<TextView
android:id="#+id/tv_title_id_sign_up"
style="#style/excludedPaddingStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="19dp"
android:layout_marginEnd="16dp"
android:text="#string/id"
android:textAppearance="#style/Font16RegularStyle"
android:textColor="#color/white_color"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/et_id_sign_up"
style="#style/editTextCommonStyle"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="9dp"
android:hint="#string/hint_input_email"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toStartOf="#id/btn_double_check_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_id_sign_up"
app:layout_constraintTop_toBottomOf="#+id/tv_title_id_sign_up" />
<Button
android:id="#+id/btn_double_check_sign_up"
style="#style/elevationCommonStyle"
android:layout_width="84dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:background="#drawable/shape_btn_sign_up"
android:text="#string/double_check"
android:textAppearance="#style/Font16RegularStyle"
android:textColor="#color/white_color"
app:layout_constraintBottom_toBottomOf="#id/et_id_sign_up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/et_id_sign_up"
app:layout_constraintTop_toTopOf="#id/et_id_sign_up" />
<TextView
android:id="#+id/tv_notify_id_sign_up"
style="#style/excludedPaddingStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:text="#string/notify_sign_up_email"
android:textAppearance="#style/Font14RegularStyle"
android:textColor="#color/white_80_alpha_color"
app:layout_constraintEnd_toEndOf="#id/tv_title_id_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_id_sign_up"
app:layout_constraintTop_toBottomOf="#id/et_id_sign_up" />
<!-- password -->
<TextView
android:id="#+id/tv_title_password_sign_up"
style="#style/excludedPaddingStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="#string/password"
android:textAppearance="#style/Font16RegularStyle"
android:textColor="#color/white_color"
app:layout_constraintEnd_toEndOf="#id/tv_title_id_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_id_sign_up"
app:layout_constraintTop_toBottomOf="#id/tv_notify_id_sign_up" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/et_password_sign_up"
style="#style/editTextCommonStyle"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="9dp"
android:hint="#string/hint_input_password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="#id/btn_double_check_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_password_sign_up"
app:layout_constraintTop_toBottomOf="#+id/tv_title_password_sign_up" />
<TextView
android:id="#+id/tv_notify_password_sign_up"
style="#style/excludedPaddingStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:text="#string/notify_sign_up_password"
android:textAppearance="#style/Font14RegularStyle"
android:textColor="#color/white_80_alpha_color"
app:layout_constraintEnd_toEndOf="#id/tv_title_id_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_id_sign_up"
app:layout_constraintTop_toBottomOf="#id/et_password_sign_up" />
<ImageView
android:id="#+id/iv_check_password_sign_up"
style="#style/elevationCommonStyle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="17dp"
android:background="#drawable/selector_sign_up_check"
app:layout_constraintBottom_toBottomOf="#id/et_password_sign_up"
app:layout_constraintEnd_toEndOf="#id/et_password_sign_up"
app:layout_constraintTop_toTopOf="#id/et_password_sign_up" />
<!-- confirm password again -->
<TextView
android:id="#+id/tv_title_password_again_sign_up"
style="#style/excludedPaddingStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="#string/password_again"
android:textAppearance="#style/Font16RegularStyle"
android:textColor="#color/white_color"
app:layout_constraintEnd_toEndOf="#id/tv_title_id_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_id_sign_up"
app:layout_constraintTop_toBottomOf="#id/tv_notify_password_sign_up" />
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/et_password_again_sign_up"
style="#style/editTextCommonStyle"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginTop="9dp"
android:hint="#string/hint_input_password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="#id/btn_double_check_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_password_sign_up"
app:layout_constraintTop_toBottomOf="#+id/tv_title_password_again_sign_up" />
<TextView
android:id="#+id/tv_notify_password_again_sign_up"
style="#style/excludedPaddingStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:text="#string/notify_sign_up_password_again"
android:textAppearance="#style/Font14RegularStyle"
android:textColor="#color/white_80_alpha_color"
app:layout_constraintEnd_toEndOf="#id/tv_title_id_sign_up"
app:layout_constraintStart_toStartOf="#id/tv_title_id_sign_up"
app:layout_constraintTop_toBottomOf="#id/et_password_again_sign_up" />
<ImageView
android:id="#+id/iv_check_password_again_sign_up"
style="#style/elevationCommonStyle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="17dp"
android:background="#drawable/selector_sign_up_check"
app:layout_constraintBottom_toBottomOf="#id/et_password_again_sign_up"
app:layout_constraintEnd_toEndOf="#id/et_password_again_sign_up"
app:layout_constraintTop_toTopOf="#id/et_password_again_sign_up" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</layout>
I want to hide footer button(activity_sign_up.xml) when
editText(fragment_sign_up_email_password) have focus.
I want to make the bottom button disappear at the same time when the soft keyboard is shown.
You can use the onGlobalLayoutListener to know when the keyboard is shown.
view?.viewTreeObserver?.addOnGlobalLayoutListener {
val rect = Rect()
view?.getWindowVisibleDisplayFrame(rect)
val screenHeight = view?.rootView?.height
val keypadHeight = screenHeight?.minus(rect.bottom) ?: return
if (keypadHeight > screenHeight * KEYBOARD_MINIMUM_HEIGHT_PERCENTAGE) {
//keyboard is shown do what ever you like
} else {
//keyboard is hidden
}
}
Thank you, #Gal Machluf I've tried your method, but After show soft keyboard, the bottom button disappears. What should I do?
[KeyboardVisibilityUtils.kt]
private val onGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
window.decorView.getWindowVisibleDisplayFrame(windowVisibleDisplayFrame)
val visibleDecorViewHeight = windowVisibleDisplayFrame.height()
Log.d("debugTest", "visibleDecorViewHeight : $visibleDecorViewHeight")
if (lastVisibleDecorViewHeight != 0) {
if (lastVisibleDecorViewHeight > visibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX) {
val currentKeyboardHeight = window.decorView.height - windowVisibleDisplayFrame.bottom
Log.d("debugTest","decorView height : ${window.decorView.height}, 현재보여지고 있는 크기의 가장 아래 : ${windowVisibleDisplayFrame.bottom}")
onShowKeyboard?.invoke(currentKeyboardHeight)
} else if (lastVisibleDecorViewHeight + MIN_KEYBOARD_HEIGHT_PX < visibleDecorViewHeight) {
onHideKeyboard?.invoke()
}
}
lastVisibleDecorViewHeight = visibleDecorViewHeight
}
[SignUpFragment.kt]
//키보드 가시성 유틸 초기화
keyboardVisibilityUtils =
KeyboardVisibilityUtils(activity!!.window,
onShowKeyboard = {
keyboardListener?.onShowKeyboard()
},
onHideKeyboard = {
keyboardListener?.onHideKeyboard()
}
)
[SignUpActivity.kt]
override fun onShowKeyboard() {
Log.d("debugTest", "하단버튼을 없앤다.")
viewDataBinding.run {
btnNextSignUp.visibility = View.GONE
viewFooterSignUp.visibility = View.GONE
}
}
override fun onHideKeyboard() {
Log.d("debugTest", "하단버튼을 생성한다.")
viewDataBinding.run {
btnNextSignUp.postDelayed({
btnNextSignUp.visibility = View.VISIBLE
viewFooterSignUp.visibility = View.VISIBLE
}, 50)
}
}
This question already has answers here:
Can not find a View with findViewById()
(4 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 5 years ago.
I'm trying to write a messaging application, but when I start it, it crashes, and I do not get an error message, so I have no clue why.
Launch Activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.*;
public class ConnectActivity extends AppCompatActivity {
private Client client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final EditText txtServerAddress = (EditText) findViewById(R.id.edit_text_server_address);
final EditText txtPortNumber = (EditText) findViewById(R.id.edit_text_port_number);
final EditText txtUsername = (EditText) findViewById(R.id.edit_text_username);
final Button btnLogIn = (Button) findViewById(R.id.button_log_in);
btnLogIn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String username = txtUsername.getText().toString();
if(username.length() == 0)
return;
String portNumber = txtPortNumber.getText().toString();
if(portNumber.length() == 0)
return;
String serverAddress = txtServerAddress.getText().toString();
if(serverAddress.length() == 0)
return;
int port = 0;
try
{
port = Integer.parseInt(portNumber);
}
catch(Exception en)
{
return;
}
ClientActivity clientActivity = new ClientActivity(client);
client = new Client(serverAddress, port, username, clientActivity);
if(!client.start())
return;
Intent intent = new Intent(ConnectActivity.this, ClientActivity.class);
startActivity(intent);
}
});
setContentView(R.layout.activity_connect);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
Launch Content 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:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.liftedstarfish.lifte.gpschat0_2.ConnectActivity"
tools:showIn="#layout/activity_connect">
<TextView
android:id="#+id/text_view_server_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/server_address"
android:textColor="#android:color/black"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/text_view_port_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/port_number"
android:textColor="#android:color/black"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edit_text_server_address" />
<TextView
android:id="#+id/text_view_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/username"
android:textColor="#android:color/black"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/edit_text_port_number" />
<EditText
android:id="#+id/edit_text_server_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName|numberDecimal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view_server_address" />
<EditText
android:id="#+id/edit_text_port_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="number"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view_port_number" />
<EditText
android:id="#+id/edit_text_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_view_username" />
<Button
android:id="#+id/button_log_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:text="#string/log_in"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
Launch Activity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
tools:context="com.liftedstarfish.lifte.gpschat0_2.ConnectActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_connect" />
</android.support.design.widget.CoordinatorLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liftedstarfish.lifte.gpschat0_2">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".ClientActivity"
android:label="#string/title_activity_client"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".ConnectActivity"
android:label="#string/title_activity_connect"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
As far as I can tell, I've done everything correctly, but the application is still crashes. I've tried cleaning the application and rebuilding it, but that did not help.