I have tried everything but i am not able to find the bug in the code what was it, i want to show the fragment in the activity but the fragment is not showing, What i have tried is
followed instruction as per https://developer.android.com/guide/fragments/create#kotlin, result = not worked.
Watched and followed 6 to 10 youtube tutorials, result = not worked.
tried to find the solution on the google, result = tried and checked everything as per every single article but not worked.
Tried different layouts in the XML layout file like as Fragment, FrameLayout, <androidx.fragment.app.FragmentContainerView>
i was learning the java for android dvelopment but when i know that the primary language for android development is kotlin then i just move to the kotlin.
So i am very new to kotlin please tell me what is the problem why the fragment is not working.
MainActivity
package com.ak_applications.kottry.screens
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.ak_applications.kottry.R
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnGo: Button = findViewById(R.id.btn_go)
btnGo.setOnClickListener {
startActivity(Intent(this, login_signup::class.java))
}
}
}
MainActiviy.Layout
<?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=".screens.MainActivity"
android:background="#drawable/blue_and_purple">
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:id="#+id/btn_go"
android:layout_width="220dp"
android:layout_height="60dp"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
android:text="GO"
android:textColor="#color/white"
android:textSize="24sp"
app:elevation="34dp"
app:rippleColor="#color/white"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Class in which i want to show the fragment.
Layout
<?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="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
</LinearLayout>
Class
package com.ak_applications.kottry.screens
import android.os.Bundle
import android.os.PersistableBundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import com.ak_applications.kottry.R
import com.ak_applications.kottry.screens.fragments.login_fragment
class login_signup: AppCompatActivity(R.layout.login_signup_layout) {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
if (savedInstanceState == null) {
supportFragmentManager.commit {
setReorderingAllowed(true)
add(R.id.frag_container, login_fragment())
}
}
}
}
The Fragment that i want to show
Fragment Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".screens.fragments.login_fragment"
android:background="#color/black">
<TextView
android:id="#+id/txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:fontFamily="#font/raleway_semi_bold"
android:textColor="#color/white"
android:textSize="32sp"
android:letterSpacing="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/txt_login_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Credintials to get access"
android:textColor="#9CFFFFFF"
android:fontFamily="#font/raleway_medium"
android:textSize="18sp"
app:layout_constraintTop_toBottomOf="#id/txt_login"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/user_id_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="100dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:boxCornerRadiusTopStart="8dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:startIconDrawable="#drawable/user_active_state"
app:layout_constraintTop_toBottomOf="#id/txt_login_des"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="#id/password_feild"
app:layout_constraintVertical_bias="0">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Email or User name or Phone Number"
android:textColorHint="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:boxCornerRadiusTopStart="8dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:startIconDrawable="#drawable/lock_unlock_anime"
app:layout_constraintTop_toBottomOf="#id/user_id_feild"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="#id/btn_signIn"
app:layout_constraintVertical_bias="1">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="•••••••••••••"
android:textColorHint="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_signIn"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:text="Sign In"
android:backgroundTint="#color/white"
android:textAllCaps="false"
android:textColor="#color/black"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#id/password_feild"
app:layout_constraintBottom_toTopOf="#id/fbtn_google"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
app:rippleColor="#color/black"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_register"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:text="Register"
android:textAllCaps="false"
android:textColor="#color/white"
app:layout_constraintBottom_toTopOf="#id/fbtn_google"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="0"
android:layout_marginTop="10dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_signIn"
app:rippleColor="#color/black"
app:strokeColor="#color/white"
app:strokeWidth="1dp" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fbtn_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#6F000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="#drawable/google_cute_icon"
android:tintMode="add"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fbtn_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/black"
android:background="#drawable/icons8_facebook"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toLeftOf="#id/fbtn_google"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1"
android:layout_marginEnd="15dp"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="#drawable/icons8_facebook"
android:tintMode="add"
app:maxImageSize="30dp"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fbtn_twitter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#6F000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="#id/fbtn_google"
app:layout_constraintHorizontal_bias="0"
android:layout_marginStart="15dp"
app:elevation="35dp"
app:borderWidth="0dp"
android:layout_marginBottom="50dp"
android:src="#drawable/google_cute_icon"
android:tintMode="add"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Class
package com.ak_applications.kottry.screens.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import com.ak_applications.kottry.R
class login_fragment : Fragment(R.layout.fragment_login_fragment) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
}
Plase help me out i don't understand what is the issue with my code.
In login_signup activity, change the onCreate() from
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?)
to
override fun onCreate(savedInstanceState: Bundle?)
Not really sure what the problem might be, but I'll give a couple of possible solutions.
1st:
Try using a FragmentContainerView to contain your fragment. Something like this:
<androidx.fragment.app.FragmentContainerView
android:id="#+id/frag_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="PATH TO YOUR FRAGMENT">
In the name tag you can out the path to the fragment that you want to inflate first.
The path should something like com.ak_applications.kottry.login_fragment.
2nd:
You can try a different way to use the supportFragmentManager. I use it like this:
supportFragmentManager.beginTransaction().replace(R.id.frag_container, login_fragment()).commit()
Just give it a go on any of the two.
Related
When you enter some value in the "product price" field and select one of the packages (after clicking on the calculate button), the amount indicated in the packages themselves should be added, and the result of this should be displayed in the TextView, when you click on the "round" button, the resulting value should be rounded ... I'm lost and don't know how to implement this, I tried and searched for information on all kinds of sites, but there is very little information, maybe knowledgeable people will :(
program
Kotlin code:
package com.example.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.RadioButton
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private lateinit var radioButton: RadioButton
private lateinit var radioButton2: RadioButton
private lateinit var radioButton3: RadioButton
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
radioButton = findViewById(R.id.radioButton)
radioButton2 = findViewById(R.id.radioButton2)
radioButton3 = findViewById(R.id.radioButton3)
}
fun SetOnClick(view: View){
}
}
Xml code :
<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">
<Button
android:id="#+id/button"
android:layout_width="204dp"
android:layout_height="61dp"
android:layout_marginStart="149dp"
android:layout_marginEnd="168dp"
android:layout_marginBottom="228dp"
android:text="Рассчитать"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="298dp"
android:layout_marginEnd="25dp"
android:layout_marginBottom="315dp"
android:minHeight="48dp"
android:text="Округлить"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:layout_marginTop="63dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Цена продукта" />
</com.google.android.material.textfield.TextInputLayout>
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="83dp"
android:layout_marginEnd="188dp"
android:text="Начальный пакет (+5%)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="165dp"
android:text="Продвинутый пакет (+10%)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioButton" />
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="35dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="196dp"
android:text="Элитный пакет (+25%)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioButton2" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="157dp"
android:layout_marginTop="68dp"
android:layout_marginEnd="196dp"
android:layout_marginBottom="141dp"
android:text="TextView"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.395"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button"
app:layout_constraintVertical_bias="0.475" />
</androidx.constraintlayout.widget.ConstraintLayout>
You are using RadioButton without any RadioGroup which is the reason why you are able to select multiple RadioButton at a time. Check this documentation and this tutorial on how to work with radio buttons.
Regarding your Business Logic on how rounding will happen you can use the MathsUtil class to perform your rounding off operation.
My app has two fragments and the main activity. I'm using a menu bar icon to go from the main activity/first fragment to the second fragment.
The issue is when the icon is clicked a fatal exception occurs and I get the following error:
java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{ccc58e7 V.E...... ........ 0,0-1080,1584} does not have a NavController set
The error calls out a line in the override fun onOptionsItemSelected(item: MenuItem): Boolean function. The specific line is:
view?.let { Navigation.findNavController(it).navigate(MainFragmentDirections.actionMainFragmentToNotesFragment3()) }
This code was taken from a different app that is working so I don't understand why the error is happening here.
I would appreciate some help.
Here is all of the code:
MainFragment.kt
package com.genauapps.cis2818_proj4_runningapp
import android.os.Bundle
import android.view.*
import androidx.fragment.app.Fragment
import androidx.navigation.Navigation
import android.view.Menu
import android.view.MenuItem
import android.app.Activity
import android.util.Log
class MainFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
setHasOptionsMenu(true)
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.menu, menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_add_workout -> {
Log.i("test", "Button Clicked")
view?.let { Navigation.findNavController(it).navigate(MainFragmentDirections.actionMainFragmentToNotesFragment3()) }
}
}
return true
}
}
fragment_main.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:padding="10dp"
tools:context=".MainFragment"
android:name="androidx.navigation.fragment.MainFragment"
>
<TextView
android:id="#+id/main_activity_tv_main_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="monospace"
android:text="#string/text_view_main_title"
android:textColor="#color/old_mauve"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/divider"
android:layout_width="409dp"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="?android:attr/listDivider"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_activity_tv_main_title" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/divider" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_notes.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:padding="10dp"
tools:context=".NotesFragment"
android:name="androidx.navigation.fragment.MainFragment"
>
<TextView
android:id="#+id/notes_fragment_tv_myworkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="#string/text_view_myworkout"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/notes_fragment_et_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:fontFamily="monospace"
android:hint="#string/edit_text_time"
android:importantForAutofill="no"
android:textAlignment="center"
android:textColor="#color/black"
android:textColorHint="#color/black"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notes_fragment_tv_myworkout"
tools:ignore="TextContrastCheck" />
<EditText
android:id="#+id/notes_fragment_et_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:fontFamily="monospace"
android:hint="#string/edit_text_date"
android:importantForAutofill="no"
android:inputType="date"
android:minHeight="48dp"
android:textColorHint="#color/black"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notes_fragment_et_time" />
<EditText
android:id="#+id/notes_fragment_et_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:fontFamily="monospace"
android:hint="#string/edit_text_distance"
android:inputType="numberDecimal"
android:textColorHint="#color/black"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notes_fragment_et_date"
android:importantForAutofill="no" />
<EditText
android:id="#+id/notes_fragment_et_notes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:fontFamily="monospace"
android:hint="#string/edit_text_notes"
android:importantForAutofill="no"
android:inputType="textCapSentences|textAutoCorrect"
android:minHeight="48dp"
android:textColor="#color/black"
android:textColorHint="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notes_fragment_et_distance" />
<Button
android:id="#+id/notes_fragment_bt_start_stop"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:backgroundTint="#color/y_in_mn_blue"
android:fontFamily="monospace"
android:text="#string/button_text"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notes_fragment_et_notes" />
<Button
android:id="#+id/notes_fragment_bt_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="#color/y_in_mn_blue"
android:fontFamily="monospace"
android:text="#string/button_text_save"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/notes_fragment_bt_start_stop" />
</androidx.constraintlayout.widget.ConstraintLayout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/navigation"
app:startDestination="#id/mainFragment">
<fragment
android:id="#+id/mainFragment"
android:name="com.genauapps.cis2818_proj4_runningapp.MainFragment"
android:label="fragment_main"
tools:layout="#layout/fragment_main" >
<action
android:id="#+id/action_mainFragment_to_notesFragment3"
app:destination="#id/notesFragment3" />
</fragment>
<fragment
android:id="#+id/notesFragment3"
android:name="com.genauapps.cis2818_proj4_runningapp.NotesFragment"
android:label="fragment_notes"
tools:layout="#layout/fragment_notes" >
<action
android:id="#+id/action_notesFragment3_to_mainFragment"
app:destination="#id/mainFragment" />
</fragment>
</navigation>
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_add_workout"
android:icon="#drawable/ic_baseline_run_circle_24"
android:title="#string/menu_add_workout"
app:showAsAction="ifRoom" />
</menu>
Logcat
2021-12-11 17:24:18.121 27715-27715/com.genauapps.cis2818_proj4_runningapp I/test: Button Clicked
2021-12-11 17:24:18.125 27715-27715/com.genauapps.cis2818_proj4_runningapp D/AndroidRuntime: Shutting down VM
2021-12-11 17:24:18.126 27715-27715/com.genauapps.cis2818_proj4_runningapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.genauapps.cis2818_proj4_runningapp, PID: 27715
java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{ccc58e7 V.E...... ........ 0,0-1080,1584} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.java:84)
at androidx.navigation.fragment.NavHostFragment.findNavController(NavHostFragment.java:120)
at androidx.navigation.fragment.FragmentKt.findNavController(Fragment.kt:29)
at com.genauapps.cis2818_proj4_runningapp.MainFragment.onOptionsItemSelected(MainFragment.kt:56)
at androidx.fragment.app.Fragment.performOptionsItemSelected(Fragment.java:3154)
at androidx.fragment.app.FragmentManager.dispatchOptionsItemSelected(FragmentManager.java:2937)
at androidx.fragment.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:427)
at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:334)
at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:264)
at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:109)
at androidx.appcompat.app.AppCompatDelegateImpl.onMenuItemSelected(AppCompatDelegateImpl.java:1185)
at androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:834)
at androidx.appcompat.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:158)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:985)
at androidx.appcompat.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:975)
at androidx.appcompat.widget.ActionMenuView.invokeItem(ActionMenuView.java:625)
at androidx.appcompat.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:151)
at android.view.View.performClick(View.java:7448)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
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)
As per #ianhanniballake's comment:
In activity_main.xml, I changed the FragmentContainerView to a NavHostFragment container and it took care of the problem.
I want to remove the text when the edit text is focused programmatically, when i try to do that the fragment class throws null pointer exception.
The design which i am using is Material Text Input Layout, when i set the hint in the text input layout, the hint goes upward in the box which is around the TextInputLayout and i don't want that. i just want the box,
So i set the text as an hint in the EditTextField in the XML file and i want to remove the text when user click in the EditText or when the EditText is focused.
Here is the Screen Recorded for better understanding of the issue
https://www.youtube.com/watch?v=6EgBymObb4A
Here is the layout XML File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".screens.fragments.login_fragment"
android:background="#color/black"
android:focusable="true"
android:focusableInTouchMode="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<TextView
android:id="#+id/txt_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="false"
android:fontFamily="#font/raleway_semi_bold"
android:textColor="#color/white"
android:textSize="32sp"
android:letterSpacing="0.1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="#id/txt_login_des"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/txt_login_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Your Credintials to get access"
android:textColor="#9CFFFFFF"
android:fontFamily="#font/raleway_medium"
android:textSize="12sp"
app:layout_constraintTop_toBottomOf="#id/txt_login"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="15dp"
android:layout_marginStart="20dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/user_id_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginTop="30dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
android:layout_marginBottom="20dp"
app:layout_constraintTop_toBottomOf="#id/txt_login_des"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#id/password_feild">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/LoginUserIDField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusedByDefault="false"
android:text="User ID"
android:textColor="#74FFFFFF"
android:drawableStart="#drawable/custom_user_edit_txt_input"
android:drawablePadding="10dp"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password_feild"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="40dp"
app:boxCornerRadiusBottomEnd="8dp"
app:boxCornerRadiusBottomStart="8dp"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:boxStrokeColor="#color/white"
app:boxStrokeErrorColor="#FF0000"
app:boxStrokeWidth="1dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#id/btn_signIn">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/UserLoginPassword"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="•••••••••••••"
android:drawableStart="#drawable/custom_pass_txt_input"
android:drawablePadding="10dp"
android:textColor="#74FFFFFF"
android:padding="10dp"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_signIn"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="10dp"
android:backgroundTint="#color/white"
android:text="Sign In"
android:textAllCaps="false"
android:textColor="#color/black"
app:rippleColor="#color/black"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="#id/btn_register"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_register"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_marginBottom="50dp"
android:text="Register"
android:textAllCaps="false"
android:textColor="#color/white"
app:rippleColor="#color/black"
app:strokeColor="#color/white"
app:strokeWidth="1dp"
app:layout_constraintBottom_toTopOf="#id/bottom_fab_btns_lyt"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<LinearLayout
android:id="#+id/bottom_fab_btns_lyt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent">
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_facebook"
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="#color/white"
app:icon="#drawable/icons8_facebook"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="#drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="#color/white"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_google"
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="#color/white"
app:icon="#drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="#drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="#color/white"/>
<com.google.android.material.button.MaterialButton
android:id="#+id/btn_twitter"
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeWidth="1dp"
app:strokeColor="#color/white"
app:icon="#drawable/google_cute_icon"
app:iconTintMode="add"
app:iconSize="24dp"
app:iconTint="#000000"
app:iconPadding="0dp"
android:background="#drawable/fab_background"
android:backgroundTintMode="add"
app:backgroundTintMode="add"
app:rippleColor="#color/white"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment Class
package com.ak_applications.kottry.screens.fragments
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.AnimatedVectorDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import androidx.fragment.app.Fragment
import android.view.View
import android.view.ViewGroup
import com.ak_applications.kottry.R
import com.ak_applications.kottry.databinding.FragmentLoginFragmentBinding
class login_fragment : Fragment(R.layout.fragment_login_fragment) {
private var _binding: FragmentLoginFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
_binding = FragmentLoginFragmentBinding.inflate(inflater, container, false)
val view = binding.root
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
}
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
I tried
if(binding.LoginUserFeild.isFocused)
{
binding.LoginUserFeild.setText("")
binding.LoginUserFeild.setTextColor(Color.parseColor("#ffffff"))
}
also
if(binding.LoginUserFeild.isFocused)
{
binding.userLoginfeild.setHint("") // which is TextInputLayout.
}
also I have used the scroll layout as parent layout for the fragment but as you can see in the screen recording when user click on the EditText Feild the layout goes to top but user scroll down while the EditText selected the layout won't go all the way to up, layout missing somthing i don't know.
I am new to android please help me here.
binding.LoginUserFeild.setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) {
binding.LoginUserFeild.setText("")
}
}
How can I create an edit text row, I want them to appear in the layout, so it appears in the xml of the activity as it would? It doesn't matter if it is out of order, but I would like to be able to read all the EditText as well, taking into account that n_var of EditText will be created:
My activity .kt:
class RestrictionsActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_restrictions)
var intent = getIntent()
var min_max = intent.getBooleanExtra("min_max",true)
var n_var = intent.getIntExtra("var",0) //n number of EditText
}
}
My layout .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=".WriteProblem.RestrictionsActivity"
android:background="#drawable/bg_night_walk">
<TextView
android:id="#+id/TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Funcion Objetivo"
android:textColor="#color/colorWhite"
android:textSize="25dp"
android:textStyle="bold"
android:layout_marginTop="30dp"
android:layout_marginLeft="15dp"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/id_fo_input"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#drawable/min_or_max_input"
android:padding="2dp"
android:text="012312"
android:textAlignment="center"
android:textColor="#color/colorWhite"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/min_max_fo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/min_max_fo"
app:layout_constraintTop_toTopOf="#+id/min_max_fo"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
I want it to be like that, no matter the design, I'll be doing it:
I'm building my first android app through android studio using kotlin, and I'm having issues with the elements of an activity showing up when I run the app.
I created a button in the main activity that navigates to the new activity, and inside the new activity is a prompt for a text input.
However, the new activity is just blank when ran.
<?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=".CalcActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="39dp"
android:layout_marginBottom="651dp"
android:text="Enter Polynomial Fraction"
android:textAppearance="#style/TextAppearance.AppCompat.Display3"
android:textColor="#435768"
android:textSize="30sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<EditText
android:id="#+id/poly_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="31dp"
android:layout_marginTop="107dp"
android:layout_marginEnd="167dp"
android:layout_marginBottom="579dp"
android:ems="10"
android:inputType="textLongMessage"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
tools:visibility="visible" />
</androidx.constraintlayout.widget.ConstraintLayout>
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn_click_me = findViewById<Button>(R.id.CalcButton)
// set on-click listener
btn_click_me.setOnClickListener {
// your code to perform when the user clicks on the button
val intent = Intent(this, CalcActivity::class.java)
startActivity(intent)
}
}
}
package com.example.prac
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.EditText
class CalcActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_calc)
}
}
Why hard-coding margin with big values this will not work on all screen sizes also you need to know how to use contraintlayout this is a link to codelab also this is a working sample for your code
<?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">
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Enter Polynomial Fraction"
android:textAppearance="#style/TextAppearance.AppCompat.Display3"
android:textColor="#435768"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:id="#+id/poly_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textLongMessage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView2"
/>
</androidx.constraintlayout.widget.ConstraintLayout>