Need help working out the logic in the code: Kotlin - android

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.

Related

Is it possible to put label textbox button with tabbed activity? Being specific i have added hand drawn design photo

Design Layout
it shows the layout of app i am building Electricity Bill view app, in which there is tabbed activity on top of it there is textbox button. In textbox user will enter Consumer No when user clicks button then he/she is forwarded to another activity(another page) of app. which shows last billed and last payment details and have one plus symbol like button. when user clicks on it he/she have two other option one to update mobile number and other to get E-bill.
I made this layout with help of TabLayout from Containers upon this there is one button to GetDetails of Consumer Bill provided that we will get this by Consumer number from TextBox entered by End User. Constraint Layout is used in whole 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=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="409dp"
android:layout_height="51dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="280dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_GetDetails">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/history" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bookmark" />
</com.google.android.material.tabs.TabLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginBottom="5dp"
android:text="#string/welcome_to_ugvcl_vij_bill"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:text="#string/find_details_by_consumer_number"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textInputEditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<Button
android:id="#+id/btn_GetDetails"
android:layout_width="320sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="64dp"
android:text="#string/get_details"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/tabLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputEditText" />
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textInputEditText"
android:layout_width="320sp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="25dp"
android:bufferType="normal"
android:drawableStart="#drawable/ic_baseline_person_24"
android:drawableLeft="#drawable/ic_baseline_person_24"
android:drawablePadding="10sp"
android:gravity="center_vertical"
android:hint="#string/consumer_number"
android:inputType="number"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/btn_GetDetails"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now what i want to do is when user clicks GetDetails Button then in Another Activity or Another Page of Application itself it should open/show some of Consumer Details like his/her name followed by his/her corresponding Consumer number. It should not fire intent to open this in any installed web browser.
I coded for opening this in web browser is...
package com.example.tabbedlayout;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_GetDetails);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent IntViewBill = new Intent(Intent.ACTION_VIEW);
IntViewBill.setData(Uri.parse("http://ugvcl.info/UGBILL/"));
startActivity(IntViewBill);
}
});
}
}
and works fine....
But as i mentioned earlier i want it to open in app itself.
One more thing,
there is a scenario like web page "http://ugvcl.info/UGBILL/" uses Captcha Code(Image of Written TextNumber together) to be Entered first by End User to get details of Consumer Bill.
captcha code looks like this Captcha code changes everytime
How can we handle that.
I made this layout with help of TabLayout from Containers upon this there is one button to GetDetails of Consumer Bill provided that we will get this by Consumer number from TextBox entered by End User. Constraint Layout is used in whole 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=".MainActivity">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="409dp"
android:layout_height="51dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="280dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/btn_GetDetails">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/history" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bookmark" />
</com.google.android.material.tabs.TabLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:layout_marginBottom="5dp"
android:text="#string/welcome_to_ugvcl_vij_bill"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:text="#string/find_details_by_consumer_number"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/textInputEditText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<Button
android:id="#+id/btn_GetDetails"
android:layout_width="320sp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="64dp"
android:text="#string/get_details"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/tabLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputEditText" />
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textInputEditText"
android:layout_width="320sp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="25dp"
android:bufferType="normal"
android:drawableStart="#drawable/ic_baseline_person_24"
android:drawableLeft="#drawable/ic_baseline_person_24"
android:drawablePadding="10sp"
android:gravity="center_vertical"
android:hint="#string/consumer_number"
android:inputType="number"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/btn_GetDetails"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
In short you can use TabLayout or some margin properties to achieve this type of Layout.

Remove Text from the edit text when edit text is focused

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("")
}
}

Fragment Not Showing in the Activity

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.

How do I create multiple edit text with the activity instead of a database? Kotlin

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:

Android Studio Kotlin activity doesn't show elements at runtime

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>

Categories

Resources