So I am working on creating my first test app using Android Studio and Kotlin.
Also, should I just not use Kotlin when developing apps? I was told to use Kotlin..
Anyways back to my problem.
I wanted to know what the parameters were for AlertDialog.Builder(this)
I know it's supposed to be this or this#mainactivity but I don't know what the parameter is.
There is no intellisense and I couldnt find any documents.
How about a custom Alert Dialog that yes it is boiler plate code but oh the style!
You need this XML
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/imgDI"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/caution" />
<TextView
android:id="#+id/tvDAT"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="30dp"
android:text="Delete Note"
android:textColor="#color/color_Black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvDAC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="Do You Want to DELETE this Note"
android:textColor="#color/color_Black"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnYES"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="240dp"
android:layout_marginTop="110dp"
android:background="#color/color_Transparent"
android:text="DELETE"
android:textColor="#color/color_deepBlue"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnNO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="110dp"
android:background="#color/color_Transparent"
android:text="CANCEL"
android:textColor="#color/color_deepBlue"
android:textSize="18sp"
android:textStyle="bold" />
here is the button that calls the view
btnDelete.setOnClickListener{
if(etPerson.text.toString().equals("")){
message("No Match Found")
return#setOnClickListener
}
doCustom()
}
And now doCustom IT is a little funky to call the other function
fun doCustom() {
/* This method uses the custom_dialog.xml file created for greater control over
the styling of the Custom Alert Dialog for various screen sizes and to be
able to set the text size of the dialog message text
*/
val makeDialog = LayoutInflater.from(this).inflate(R.layout.custom_dialog,null)
val mBuilder = AlertDialog.Builder(this).setView(makeDialog)
val mAlertDialog = mBuilder.show()
val btnYES = makeDialog.findViewById<Button>(R.id.btnYES)
val btnNO = makeDialog.findViewById<Button>(R.id.btnNO)
mAlertDialog.setCancelable(false)
btnYES.setOnClickListener {
removePerson()
mAlertDialog.dismiss()
}
btnNO.setOnClickListener {
message("Record NOT Deleted")
etPerson.setText("")
Timer().schedule(800){
thisACTIVITY()
}
mAlertDialog.dismiss()
}
mAlertDialog.show()
}
private fun removePerson() {
val dbHandler = DBHelper(this)
val result = dbHandler.deletePerson(etPerson.text.toString())
if (result) {
etPerson.setText("")
message("Record Removed")
Timer().schedule(1000){
thisACTIVITY()
}
}else{
etPerson.setText("NO MATCH -> click View Person List")
btnViewList.visibility = View.VISIBLE
btnEdit.visibility = View.INVISIBLE
btnDelete.visibility =View.INVISIBLE
btnAdd.visibility = View.INVISIBLE
message("NO Match Found")
}
}
Related
I'm making a recipe search application using forkify API. I get such json (let's take pizza recipes for example). I've made a recycler and searchable, but the recipes themselves are given as a link to the site with that recipe (see source_url in the json). I've made a webview for this, but there's one problem. I need to get that source_url and have it match the recipe I clicked on. I tried to make an additional element in resycler, small invisible textview, and put source_url there, so when I clicked on it, it would take text and pass it to a variable and load it as url into webview. (Very silly solution, but I didn't think of another one.) It's not what I had in mind. Basically my code works, but the url is not passed from all textViews. I've been observing its behaviour and I can only assume that it loads every 4 in vertical mode and every 2 in horizontal mode. And this is not what I need. Please help me to solve this problem. Below is my code:
Adapter:
#JvmOverloads
fun RecyclerView.affectOnItemClicks(
onClick: ((position: Int, view: View) -> Unit)? = null,
onLongClick: ((position: Int, view: View) -> Unit)? = null
) {
this.addOnChildAttachStateChangeListener(
RecyclerItemClickListener(
this,
onClick,
onLongClick
)
)
}
class RecyclerAdapter(
private val dataset: List<Recipe>
)
: RecyclerView.Adapter<RecyclerAdapter.FoodHolder>() {
inner class FoodHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindRecipe(recipe: Recipe) {
itemView.textView.text = recipe.title
itemView.textView3.text = recipe.publisher
itemView.imageView.load(recipe.image_url) {
// placeholder image is the image used
// when our image url fails to load.
placeholder(R.drawable.ic_baseline_error_24)
}
itemView.helptv.text = recipe.source_url
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerAdapter.FoodHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.recyclerview_item_row, parent, false)
return FoodHolder(view)
}
override fun onBindViewHolder(holder: RecyclerAdapter.FoodHolder, position: Int) {
holder.bindRecipe(dataset.get(position))
}
override fun getItemCount(): Int = dataset.size
}
The code in the MainActivity where I put the text from that textview into an auxiliary variable and then switch to another activation:
ConstandVar.browser_url = helptv.text.toString()
val intent = Intent(this,BrowserActivity::class.java)
startActivity(intent)
layout recyclerview:
<?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="wrap_content"
android:padding="5dp">
<com.google.android.material.card.MaterialCardView
android:id="#+id/materialCardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:clickable="false"
app:cardElevation="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:state_dragged="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/item_constraint"
android:layout_width="match_parent"
android:layout_height="120dp"
android:padding="5dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="5dp"
android:text="Title"
android:textColor="#000000"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
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:text="publisher: The Pioner Women"
android:textColor="#E57373"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/helptv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="1sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Receive json method:
fun getRecipe(callback: (List) -> Unit) {
val apiService = AppModule.provideRetrofitInstance(ConstandVar.BASE_URL)
apiService.getRecipe(food).enqueue(object : Callback<requestdata> {
override fun onFailure(call: Call<requestdata>, t: Throwable) {
Log.d("tag", "getRecipe Error")
}
override fun onResponse(call: Call<requestdata>, response: Response<requestdata>) {
return response.body()!!.recipes?.let { callback(it) }!!
}
})
Any help would be much appreciated, thanks in advance!
Please make little bit change to make adapter like that way
read most of comment ,
RecyclerView itemClickListener in Kotlin
create callback listener and return url in main activity
i am showing an AlertDialog OnClickListener from Activity:
fav_btn.setOnClickListener {
if(token.equals("false")){
val builder = AlertDialog.Builder(this)
val optionDialog = AlertDialog.Builder(this).create()
builder.setView(R.layout.alert_reg)
builder.setPositiveButton(android.R.string.yes) { dialog, which ->
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
builder.setNeutralButton(android.R.string.no) { dialog, which ->
optionDialog.dismiss()
}
builder.show()
}
else{
if (isFavourite)deleteFavorite(eventId)
else postFavImage(eventId)
}
}
this is the layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
android:background="#drawable/round_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Assalamualaikum Qalbies"
android:textSize="19dp"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:layout_marginTop="#dimen/_20sdp"
android:textColor="#AC6AA6"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/_20sdp"
android:paddingTop="#dimen/_8sdp"
android:paddingRight="#dimen/_20sdp"
android:text="Text"
android:textSize="14dp"
android:fontFamily="#font/poppins"
android:gravity="center"
android:layout_marginBottom="#dimen/_5sdp"
android:textColor="#1AB7B8"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/_20sdp"
android:paddingTop="#dimen/_8sdp"
android:paddingRight="#dimen/_20sdp"
android:text="To continue, please"
android:textSize="14dp"
android:fontFamily="#font/poppins"
android:gravity="center"
android:layout_marginBottom="#dimen/_5sdp"
android:textColor="#4C4F4F"/>
<LinearLayout
android:background="#drawable/round_corner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/cancel_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/_20sdp"
android:paddingTop="#dimen/_8sdp"
android:paddingRight="#dimen/_20sdp"
android:text="Cancel"
android:textSize="14dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_edittext"
android:backgroundTint="#F3F3F3"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:layout_marginBottom="#dimen/_5sdp"
android:textColor="#7D7D7D"/>
<TextView
android:id="#+id/confirmation_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edittext"
android:backgroundTint="#1AB7B8"
android:layout_gravity="center_horizontal"
android:paddingLeft="#dimen/_20sdp"
android:paddingTop="#dimen/_8sdp"
android:clickable="true"
android:paddingRight="#dimen/_20sdp"
android:text="Sign-Up"
android:textSize="14dp"
android:fontFamily="#font/poppins_bold"
android:gravity="center"
android:layout_marginBottom="#dimen/_5sdp"
android:textColor="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
so far everything is working fine but I want to set the textviews "cancel_reg" and "confirmation_reg" rather than "setPositiveButton" and "setNeutralButton"
I tried to do this in the activity:
fav_btn.setOnClickListener {
if(token.equals("false")){
val builder = AlertDialog.Builder(this)
val optionDialog = AlertDialog.Builder(this).create()
builder.setView(R.layout.alert_reg)
val goreg = findViewById<TextView>(R.id.confirmation_reg)
val cancel = findViewById<TextView>(R.id.cancel_reg)
goreg?.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
startActivity(intent)
}
cancel?.setOnClickListener {
val intent = Intent(this, SignupActivity::class.java)
optionDialog.dismiss()
}
builder.show()
}
else {
if (isFavourite)deleteFavorite(eventId)
else postFavImage(eventId)
}
}
i tried to delete "setPositiveButton" and "setNeutralButton" and replace them with Textviews "confirmation_reg" and "cancel_reg" from the view but i can't click them
am i doing anything wrong?
You need to call findViewById on the AlertDialog's View
currently its being called on your Activity or Fragment. to fix this update your dialog creation code as following
val builder = AlertDialog.Builder(requireContext())
// Inflate the XML with layout inflator and keep a reference to find TextView's
val dialogView = layoutInflater.inflate(R.layout.alert_reg, null)
builder.setView(dialogView)
// Find TextView's using the dialogView
val goreg = dialogView.findViewById<TextView>(R.id.confirmation_reg)
val cancel = dialogView.findViewById<TextView>(R.id.cancel_reg)
goreg?.setOnClickListener {
// Do something
}
cancel?.setOnClickListener {
// Do something
}
builder.show()
Try setting clickable attribute to true in xml.
This question already has answers here:
How not to repeat math operations two times in calculator app?
(2 answers)
Closed 2 years ago.
Hello I have made a simple calculator in "Kotlin" using android studio
the problem I got and I don't have a way to fix it is how not to repeat the math operations after typing a number .. Perhaps because I am new to the world of Android application development and I do not know the way I should avoid this problem.
Example for what I mean, He entered addition/subtraction twice:
My codes :
Main.kt
package com.iosmostafa.calculator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.iosmostafa.calculator.R
import kotlinx.android.synthetic.main.activity_main.*
import net.objecthunter.exp4j.ExpressionBuilder
class Main : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Numbers
tvOne.setOnClickListener { appendOnExpresstion("1", true) }
tvTwo.setOnClickListener { appendOnExpresstion("2", true) }
tvThree.setOnClickListener { appendOnExpresstion("3", true) }
tvFour.setOnClickListener { appendOnExpresstion("4", true) }
tvFive.setOnClickListener { appendOnExpresstion("5", true) }
tvSix.setOnClickListener { appendOnExpresstion("6", true) }
tvSeven.setOnClickListener { appendOnExpresstion("7", true) }
tvEight.setOnClickListener { appendOnExpresstion("8", true) }
tvNine.setOnClickListener { appendOnExpresstion("9", true) }
tvZero.setOnClickListener { appendOnExpresstion("0", true) }
tvDot.setOnClickListener { appendOnExpresstion(".", true) }
//Operators
tvPlus.setOnClickListener { appendOnExpresstion("+", false) }
tvMinus.setOnClickListener { appendOnExpresstion("-", false) }
tvMul.setOnClickListener { appendOnExpresstion("*", false) }
tvDivide.setOnClickListener { appendOnExpresstion("/", false) }
tvOpen.setOnClickListener { appendOnExpresstion("(", false) }
tvClose.setOnClickListener { appendOnExpresstion(")", false) }
tvNew1.setOnClickListener{(appendOnExpresstion("%",false))}
tvClear.setOnClickListener {
tvExpression.text = ""
tvResult.text = ""
}
tvBack.setOnClickListener {
val string = tvExpression.text.toString()
if(string.isNotEmpty()){
tvExpression.text = string.substring(0,string.length-1)
}
tvResult.text = ""
}
tvEquals.setOnClickListener {
try {
val expression = ExpressionBuilder(tvExpression.text.toString()).build()
val result = expression.evaluate()
val longResult = result.toLong()
if(result == longResult.toDouble())
tvResult.text = longResult.toString()
else
tvResult.text = result.toString()
}catch (e:Exception){
Log.d("Exception"," message : " + e.message )
}
}
}
fun appendOnExpresstion(string: String, canClear: Boolean) {
if(tvResult.text.isNotEmpty()){
tvExpression.text = ""
}
if (canClear) {
tvResult.text = ""
tvExpression.append(string)
} else {
tvExpression.append(tvResult.text)
tvExpression.append(string)
tvResult.text = ""
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:orientation="vertical">
<TextView
android:id="#+id/tvExpression"
android:layout_width="match_parent"
android:layout_height="80sp"
android:ellipsize="start"
android:gravity="end"
android:singleLine="true"
android:textColor="#color/numberButton"
android:textSize="40sp" />
<TextView
android:id="#+id/tvResult"
android:layout_width="match_parent"
android:layout_height="100sp"
android:ellipsize="end"
android:gravity="end"
android:singleLine="true"
android:textColor="#color/numberButton"
android:textSize="30sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/tvClear"
style="#style/ActionButtonStyle"
android:text="حذف" />
<TextView
android:id="#+id/tvOpen"
style="#style/ActionButtonStyle"
android:text="(" />
<TextView
android:id="#+id/tvClose"
style="#style/ActionButtonStyle"
android:text=")" />
<TextView
android:id="#+id/tvNew1"
style="#style/ActionButtonStyle"
android:text="%" />
<TextView
android:id="#+id/tvDivide"
style="#style/ActionButtonStyle"
android:text="/" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/tvSeven"
style="#style/NumberButtonStyle"
android:text="7" />
<TextView
android:id="#+id/tvEight"
style="#style/NumberButtonStyle"
android:text="8" />
<TextView
android:id="#+id/tvNine"
style="#style/NumberButtonStyle"
android:text="9" />
<TextView
android:id="#+id/tvMul"
style="#style/ActionButtonStyle"
android:text="X" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/tvFour"
style="#style/NumberButtonStyle"
android:text="4" />
<TextView
android:id="#+id/tvFive"
style="#style/NumberButtonStyle"
android:text="5" />
<TextView
android:id="#+id/tvSix"
style="#style/NumberButtonStyle"
android:text="6" />
<TextView
android:id="#+id/tvMinus"
style="#style/ActionButtonStyle"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/tvOne"
style="#style/NumberButtonStyle"
android:text="1" />
<TextView
android:id="#+id/tvTwo"
style="#style/NumberButtonStyle"
android:text="2" />
<TextView
android:id="#+id/tvThree"
style="#style/NumberButtonStyle"
android:text="3" />
<TextView
android:id="#+id/tvPlus"
style="#style/ActionButtonStyle"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/tvDot"
style="#style/NumberButtonStyle"
android:text="." />
<TextView
android:id="#+id/tvZero"
style="#style/NumberButtonStyle"
android:text="0" />
<ImageView
android:id="#+id/tvBack"
style="#style/NumberButtonStyle"
android:scaleType="center"
android:src="#drawable/backspace" />
<TextView
android:id="#+id/tvEquals"
style="#style/ActionButtonStyle"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Can you replace your "+" operator to prohibit this pattern?
tvPlus.setOnClickListener { appendOnExpresstionNoDups("+", false) }
fun appendOnExpresstionNoDups(string: String, canClear: Boolean) {
val lastIndex = tvExpression.text.toString().length()-1
val string = tvExpression.text.toString().toCharArray()[lastIndex]
if(string != "+") {
appendOnExpresstion(string, canClear)
}
}
I omitted edge case conditionals for simplicity. I've also never used kotlin before, so you may need to fidget with the syntax a little.
This isn't really a Kotlin thing, you need to write some logic in your app that handles an operator button press, and decides whether it should be added or not, or if the last operator should be changed to the new one, that kind of thing. You need to be able to explain how it should work, before you start writing code that follows that behaviour.
#PaulNikonowicz 's suggestion is one way of doing it that fits with your current code - it's basically saying "if the user presses the + button, check if the display ends in a +, and only call the appendOnExpression method if it doesn't. You're running a check to see if it's valid before performing the operation, and stopping it from moving to an invalid state.
Calculators can be surprisingly tricky! There's a bunch of logic involved in making them act consistently, like a state machine. Having a separate method for operators that runs some validation is a good start (although you could just make it replace the last character if that's an operator, usually with a calculator if you press + and then change your mind and press -, it will use the last one you pressed)
Im a little stuck, how can i get a variable from main activity to be displayed on a second activity?
a code example would be great.
Also a second Problem:
How can i create a function in mainactivty when a button is pressed in a second activity?
This is what i have so far, but when i press the button in the second activity, the app crashes.
the button's function needs to be able to change a variable's value in MainActivity and Run a toast saying it was selected.
Main Activity
//SETTING THE DRINK SIZE BASED ON POPUP BUTTONS
public int DrinkSize;
public void SetDrinkSize_Small(View view) {
DrinkSize = 1;
Toast Small = Toast.makeText(getApplicationContext(),
"Drink Size Set To Small",
Toast.LENGTH_SHORT);
Small.show();
}
public void SetDrinkSize_Medium(View view) {
DrinkSize = 2;
Toast Medium = Toast.makeText(getApplicationContext(),
"Drink Size Set To Medium",
Toast.LENGTH_SHORT);
Medium.show();
}
public void SetDrinkSize_Large(View view) {
DrinkSize = 3;
Toast Large = Toast.makeText(getApplicationContext(),
"Drink Size Set To Large",
Toast.LENGTH_SHORT);
Large.show();
}
CustomPopUp.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Small">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Green"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Medium">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:background="#color/Orange"
android:gravity="center_horizontal"
android:orientation="vertical"
android:onClick="SetDrinkSize_Large">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large"
android:textColor="#color/White"
android:textSize="18dp"
android:textStyle="bold" />
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginTop="15dp"
android:src="#drawable/drop" />
</LinearLayout>
You can easily send and receive data using intents, as you can see in this Android tutorial: Intent tutorial.
And in case you want to send some data back to the first one, you can use this post: Sending info back.
I was using BottomSheetDialog for taking some input from the customer, my view contains TextInputLayout and button below the TextInputLayout when the user clicks on TextInputLayout the below button is getting hide by keyboard, so I need to that button above the keyboard, tried many possibilities but not able to fix it.
Attaching the pics regarding it.
Java file(activity) code:
BottomSheetDialog customTipAmountBottomSheetDialog;
private void showCustomTipAmountBottomSheet() {
customTipAmountBottomSheetDialog = new BottomSheetDialog(OrderActivity.this);
View customTipAmountBottomSheetView = getLayoutInflater().inflate(R.layout.custom_tip_amount_bottom_sheet, null);
customTipAmountBottomSheetDialog.setContentView(customTipAmountBottomSheetView);
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.showSoftInput(binding.invoiceDialog.submit, InputMethodManager.RESULT_HIDDEN);
TextInputLayout customTipAmountBSTIL = customTipAmountBottomSheetView.findViewById(R.id.customTipAmountBSTIL);
EditText customTipAmountBSET = customTipAmountBottomSheetView.findViewById(R.id.customTipAmountBSET);
ImageView submit_custom_tip_amount = customTipAmountBottomSheetView.findViewById(R.id.submit_custom_tip_amount);
submit_custom_tip_amount.setOnClickListener(view -> {
String amount = customTipAmountBSET.getText().toString();
if (amount.length() > 0 && Integer.parseInt(amount) > 0) {
int tipAmount = Integer.parseInt(amount);
if (tipAmount > 0 && tipAmount < 51) {
binding.invoiceDialog.customTipAmountTv.setText(tipAmount);
captainTipAmount = tipAmount;
customTipAmountBottomSheetDialog.dismiss();
} else {
customTipAmountBSTIL.setError("Amount should be less than 50");
}
} else {
customTipAmountBSTIL.setError("Requires a valid amount");
}
});
customTipAmountBottomSheetDialog.show();
}
layout file custom_tip_amount_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_height="match_parent">
<TextView
android:id="#+id/customTipAmountCaptainNameTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Tip amount for Rapido Captain"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.TextInputLayout
android:id="#+id/customTipAmountBSTIL"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:hint="Enter amount"
app:boxBackgroundColor="#color/grey_100"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/customTipAmountCaptainNameTv">
<EditText
android:id="#+id/customTipAmountBSET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890"
android:ems="2"
android:focusable="true"
android:imeOptions="actionDone"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="#+id/submit_custom_tip_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submitFeedbackConfirmationPopUp"
android:padding="#dimen/margin_8"
app:layout_constraintBottom_toBottomOf="#+id/customTipAmountBSTIL"
app:layout_constraintEnd_toEndOf="#+id/customTipAmountBSTIL"
app:layout_constraintTop_toTopOf="#+id/customTipAmountBSTIL"
app:srcCompat="#drawable/ic_arrow_forward_black_24dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit_custom_tip_amount" />
in Manifest file for activity file:
android:windowSoftInputMode="adjustPan"
Tried searching alot, but still not able to fix it. i got one answer related to my problem but it was with BottomSheetFragment()keyboard hides BottomSheetFragment, but i need with BottomSheetDialog. Please someone help me regarding this.
Let me know anything else info is required.
On onCreate() method in your activity java class use the below method.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Few days ago, i was faced same problem. Then using this portion of code into my onCreate() method, then the problem was solved.
Sample:
There are ways to achieve this:
1. Write below code into your Android Manifest file under <activity> tag:
android:windowSoftInputMode="stateHidden|adjustResize"
2. Write below code into your onCreate() method of activity:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
I hope its work for you.