I created an alertDialog which basically contains two editTexts to enter the data(component Id, component name).When the user clicks on yes button in the alert dialog new component is added to the datbase(as of now I haven't added any on click listeners for yes and no buttons in the dialog) but for some reason I can't get the data from the edit text. I'm new to android app development please help me out guys.....Thanks in Advance :)
Note : 1. I'm using viewModel.flag so that the alertDialog stays persistent with rotations.
onCancel() method simply cancels the alertDialog(I intend to use this for updating certain parameters).
This is kotlin code which creates the alert dialogs.
fun onAddComponent()
{
val builder = AlertDialog.Builder(requireActivity())
builder?.setTitle("New Component Info")
val inflater = requireActivity().layoutInflater
builder?.setView(inflater.inflate(R.layout.new_comp, null))
builder?.setPositiveButton(R.string.Okay_button){ dialogInterface, which->
viewModel.flag = false
val editText : EditText? = compAlert?.findViewById(R.id.new_Component)
println(editText.toString())
}
builder?.setNegativeButton(R.string.NO){ dialogInterface, which->
viewModel.flag = false
}
compAlert= builder?.create()
compAlert?.setOnCancelListener{OnCancel()}
viewModel.flag = true
compAlert?.show()
}
This is the xml layout which I inflated in the onAddComponent()
<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">
<EditText
android:id="#+id/new_Component"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="comp Id"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/new_component_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="comp name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="#+id/new_Component"
app:layout_constraintStart_toStartOf="#+id/new_Component"
app:layout_constraintTop_toBottomOf="#+id/new_Component" />
</androidx.constraintlayout.widget.ConstraintLayout>
To get the text inside the EditText you need to do editText.getText().toString() and NOT editText.toString() which you have in your code.
In your code, editText.toString() will return the string representation of the editText object. See the documentation of String's toString() method here.
Related
I have a table made of buttons and each button has a text on it. However, I want that text to be invisible unless the button is clicked.
The buttons look like this:
<Button
android:background="#drawable/roundstyle"
android:backgroundTint="#color/c1"
android:id="#+id/btnOne"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight="1"
android:text="#string/i"
tools:ignore="UsingOnClickInXml"
android:onClick="displayText" />
In MainActivity, I tried to implement a function to display the text:
fun displayText(view: View) {
val b = view as Button
val buttonText = b.text.toString()
I think the easiest thing to do would be to use "if", so that if btnOne is not clicked, then the text should be invisible, else, the text should be visible. But I am not sure where to write this code in the Main Activity and how exactly, so that once the button has been clicked, the text remains on the screen.
Could someone help me with this, please?
Thank you.
can try this one by default set the button like this(the most easy solution)
<Button
android:background="#drawable/roundstyle"
android:backgroundTint="#color/c1"
android:id="#+id/btnOne"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:layout_weight="1"
android:tag = "some text"
tools:ignore="UsingOnClickInXml"
android:onClick="displayText" />
and click handler
fun displayText(view: View) {
val b = view as Button
val btnTag= b.tag.toString()
if (b.text.isEmpty()) {
b.text = btnTag
}
}
You can do this simply by changing the font size of the button text. you can initially set the font size to 0 and then set the right value after user clicked on it.
<Button
android:onClick="displayText"
android:text="Sample Text"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:textSize="0sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
To visible the text once
fun displayText(view: View) {
view.setTextSize(TypedValue.COMPLEX_UNIT_SP,12f)
}
This function will toggle the visibility of the text
fun displayText(view: View) {
if(view.textSize == 0f){
view.setTextSize(TypedValue.COMPLEX_UNIT_SP,12f)
}else{
view.setTextSize(TypedValue.COMPLEX_UNIT_SP,0f)
}
}
I am working on a small app where the user can start an activity.
By doing that, a text input shall is shown where todays date is set by default.
When clicking into the text field, the MaterialDatePicker shall be shown as dialog.
I have successfully implemented that the MaterialDatePicker shows up when clicking on a button, but I cannot find a sololution to show the dialog instead of the Android standard android keyboard.
Any advises on that?
Activity XML:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/edtStartWearLensDate"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="#string/txtLabelWearSelectStartDate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtLabelStartWearLensID">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Activity:
On Create:
edtStartWearLensDate = findViewById(R.id.edtStartWearLensDate);
edtStartWearLensDate.setOnClickListener(view -> onEdtStartWearLensDateClick());
On Click into the text field
private void onEdtStartWearLensDateClick() {
showDatePickerDialog();
}
Method to show dialog (works when calling from a Button on click listener)
private void showDatePickerDialog(){
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText("Select start date");
builder.setSelection(MaterialDatePicker.todayInUtcMilliseconds());
final MaterialDatePicker materialDatePicker = builder.build();
materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
#Override
public void onPositiveButtonClick(Object selection) {
edtStartWearLensDate.getEditText().setText(materialDatePicker.getHeaderText());
}
});
}
Try to set the clickable and focusable properties to true in the TextInputEditText:
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/text_input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="true"/>
And call the setOnClickListener method on it:
findViewById<TextInputEditText>(R.id.text_input_edit_text).setOnClickListener(view -> onEdtStartWearLensDateClick())
I wanted to create a custom Alertdialog Layout with a dropdown list and a few other things. I'm using Kotlin and I'm pretty new to it
Currently I'm stuck at the dropdown list as it doesn't show anything
Here is the 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">
<Button
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="Zurück"
app:icon="#drawable/ic_baseline_arrow_back_24"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/pizzaSelection"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="100dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
And here is how I call up the dialog in my activity:
private fun showPizzaDialog(){
val pizzaDialogBuilder = AlertDialog.Builder(this)
pizzaDialogBuilder.setView(R.layout.pizza_alertdialog)
val pizzaDropdown = findViewById<Spinner>(R.id.pizzaSelection)
val pizzaTypes = resources.getStringArray(R.array.pizzaTypes)
if (pizzaDropdown != null) {
val adapter = ArrayAdapter(this,
android.R.layout.simple_spinner_item, pizzaTypes)
pizzaDropdown.adapter = adapter
}
pizzaDialogBuilder.show()
}
The Items are currently hardcoded in the strings.xml resource file as a string-array.
The AlertDialog shows up and i can see and click on the arrow for the dropdown menu, but when I click on it nothing happens.
You're calling findViewById on your current Activity, which doesn't contain R.id.pizza_selection. Therefore I suspect you'll see
val pizzaDropdown = findViewById<Spinner>(R.id.pizzaSelection)
return null.
Try something like this:
// inflate your layout
val dialogView = LayoutInflater.from(this).inflate(R.layout.pizza_alert_dialog, null, false)
// and set it as dialog view
pizzaDialogBuilder.setView(dialogView)
// then call findViewById on this ViewGroup to get the Spinner
val pizzaDropdown = dialogView.findViewById<Spinner>(R.id.pizzaSelection)
my Kotlin syntax might not be correct, sorry. Important is that we're calling findViewById on dialogView, instead of the implicit this.findViewById()
I have question to answer my curiosity. I have implemented dropdown with material design like snippet code xml at the below
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/provinsi_register_form"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/address_register_form">
<AutoCompleteTextView
android:id="#+id/filled_dropdown_provinsi"
style="#style/greySubText"
android:layout_width="match_parent"
android:layout_height="55dp"
android:hint="#string/provinsi_form"
android:inputType="none" />
</com.google.android.material.textfield.TextInputLayout>
You can see the result implementation on this link
picture result dropdown
for the list, I got from ViewModel like on this snipet code below:
konfirmasiViewModel.provinsi.observe(this, { provinsi ->
for (i in provinsi.indices) {
provinsiNama.add(provinsi[i].provinsi!!)
}
val adapter =
ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, provinsiNama)
filled_dropdown_provinsi.setAdapter(adapter)
filled_dropdown_provinsi.setOnItemClickListener { adapterView, view, i, l ->
lifecycleScope.launch {
siscaPreference.saveId(provinsi[i].id!!)
}
prov = provinsi[i].id.toString()
}
})
How to make condition if user doesn't choose anything either click by button or not?
Thanks for much attention
I am having an issue with a custom view in a dialog on android API 10.
I use AlertDialog.Builder to construct my dialog. I include a custom view panel using the setView command on the builder.
This works on most of the API's I've tested with. The style changes somewhat from device to device, but that is what I want, for the style to match the device default.
My problem is that on API 10, any text that is in my custom view shows up as black on a black background.
Any text I insert using AlertDialog.Builder.setMessage() appears correctly.
What magical attribute/style is the dialog builder using to determine text appearance?
My app's theme is Theme.AppCompat.Light.
Here is my onCreateDialog method:
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.fragment_status_dialog, null);
mStatusTextView = (TextView) view.findViewById(R.id.text_status);
mConnectedDeviceTextView = (TextView) view.findViewById(R.id.text_connected_device);
MainService.ServiceState state = null;
if (getArguments().containsKey(SERVICE_STATUS_ARG_KEY)) {
state = (MainService.ServiceState) getArguments().getSerializable(SERVICE_STATUS_ARG_KEY);
}
setState(state);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(view);
builder.setMessage("This will show up just fine.");
builder.setTitle(getString(R.string.status_title));
builder.setNegativeButton(R.string.dialog_back_button_text, null);
builder.setNeutralButton(R.string.dialog_connect_to_text, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
mListener.onDialogConnectTo();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
Here's my fragment_status_dialog layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="18dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/status_connected_to_unknown"
android:paddingStart="4dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:id="#+id/text_connected_device"
android:layout_toRightOf="#+id/text_status"
android:layout_toEndOf="#+id/text_status"/>
</RelativeLayout>
Note, I've tried https://stackoverflow.com/a/24505312/2350083 but it didn't fix it.
Try calling AlertDialog#setInverseBackgroundForced(true).
What about simply setting the color of the text? Ex:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white"
android:text="#string/status_starting"
android:id="#+id/text_status"/>
The TextView is using the default text color of the device (or the app). If you set the color specifically to the TextView it will be overriden on devices irrespective of the API.