Error-popup in EditText is not showing? - java

I have an EditTextField with a DatepickerListener on it. If I press a submit button it should validate that field and if they are empty it should show an error message. To display this error message I use the EditText.setError() method but the popup doesnt show. I dont know how to fix it.
I tested it on 2 Devices. Once with a OnePlus 5 on Android 7.1.1 and once on a Samsung Galaxy 7 on Android 7.0.0. Same behaviour on both devices.
Thank you in Advance.
Here is my Code (Dummy Version):
private void initRequestButton(View view) {
Button button = view.findViewById(R.id.submitButton);
EditText dateTextField = view.findViewById(R.id.someField);
button.setOnClickListener(v -> {
String dateText = dateTextField.getText().toString();
if (dateText.isEmpty()) {
dateTextField.setError("A date is required");
} else {
//do Something
}
});
}
The Layout
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
app:errorEnabled="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_marginTop="10dip"
android:orientation="horizontal">
<EditText
android:id="#+id/someField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip"
android:layout_weight="0.5"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="none" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_marginTop="5dip"
android:orientation="horizontal">
<Button
android:id="#+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.3"
android:text="#string/someText" />
</LinearLayout>

Just remove the
android:focusable="false"
android:focusableInTouchMode="false"
properties from the edittext.

The code you are using to set error is alright, but the popup pops only when the edittext is focussed either by touching manually or by programmatically, just add,
yourEdtext.requestFocus()
after setting error to the field. This will show the popup after validation fails

Related

AndroidStudio EditText can't be edited (cursor disappear)

I'm having a weird problem i've never encountered.
I'm building a layout for an activity. My only issue is, i can't edit the edittexts in any way. I've tried both with the emulator and the real device. To be more specific, when i click on the edittext, the cursor appears for a very short moment, then disappears. The keyboard shows up but get stuck and doesn't work. In the java code only enable / disable the the editext :
#Override
public void onClick(View v) {
if(bottlename.isEnabled()){
//bottlenumber.setEnabled(false);
ArrayList<Bottle> bottle = BottleView.getBottlelist();
bottle.set(Integer.parseInt(bottlenumber.getText().toString())-1, new Bottle(bottlename.getText().toString(),Integer.parseInt(bottlenumber.getText().toString()), R.drawable.vodka));
bottlename.setEnabled(false);
}else{
bottlename.setEnabled(true);
//bottlenumber.setEnabled(true);
}
}
});
Any idea what could be the issue?
I've already tried many solutions from other posts, with no luck.
Attaching the xml code here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/bottleimage"
android:layout_width="150dp"
android:layout_height="160dp" />
<LinearLayout
android:layout_width="350dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/bottlename"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:text="Bottle"
android:textAlignment="center"
android:inputType="text"
android:textSize="36sp"
android:textStyle="bold" />
<EditText
android:id="#+id/bottlenumber"
android:layout_width="match_parent"
android:layout_height="75dp"
android:background="#android:color/transparent"
android:enabled="false"
android:focusable="true"
android:gravity="fill_vertical"
android:inputType="number"
android:text="X"
android:textAlignment="center"
android:textSize="24sp" />
</LinearLayout>
<ImageButton
android:id="#+id/editbottle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:scaleType="center"
android:src="#android:drawable/ic_menu_edit" />
</LinearLayout>
</RelativeLayout>
Is all your EditText fields disabled (android:enabled="false") in your layout intentionally?
the behavior it's a bit weird, I suggest to move the action of reading the value of the editText to a button, so you will have to change the text of the button like this :
When EditTexts enabled ==> "Send"
When EditTexts disabled ==> "Enable"

Button not clickable on scrollview Android

Here is my layout file code
Everything is working fine but Button in not clickable.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/maps"
android:gravity="center_horizontal"
android:orientation="vertical">
<include layout="#layout/custom_actionbar" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="30dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/pf_color"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="TRACK"
android:textSize="20dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Please help related this problem. I am thankful to you.
I am not able to sort out what is the actual problem. I tried android:clickable="true" but still same problem
Thanks in advance.
Try putting this line into your button in the XML:
android:onClick="myClickFunction"
So it would be:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/pf_color"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="TRACK"
android:textSize="20dp"
android:onClick="myClickFunction" />
And then put this in your activity:
public void myClickFunction(View v) {
// do stuff when clicked
}
dont forget to set onCLickListener
does ur main class implements onClickListener if so do:
public void onClick(View v) {
switch (v.getId()) {
case: R.id.YOURBUTTON:
//do ur stuff
break;
default:
//todo when the screen is touched
break;
}
}
I couldnt find your buttons ID btw thats why I called it YOURBUTTON
Do you use latest support library, com.android.support:support-v4:24.0.0? I think it's a bug because i have the same problem..

Creating nested Linear layout dynamically with weights to have multiple buttons on same line

I'm trying to create a toggle button that will display more buttons and some TextView like shown below.
With the toggle button ON it will display the TextViews and 3 extra buttons, once switched off it will close those, Rearranging the toggle buttons below it as shown.
The part I specifically need help with is creating the second linear layout and setting weightings for each of my buttons?
Public void toggleswtich(){
layout = (LinearLayout) findViewById(R.id.ReportsLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
Button enterJob = new Button(Reports.this);
layout.setLayoutParams();
// if (button.isActivated()) {
enterJob.setText("Open");
layout.setWeightSum(1);
layout.addView(enterJob);
enterJob.setOnClickListener(OnClickOpenComp(enterJob));
// }
// else {
enterJob.setText("Clsoe");
layout.addView(enterJob);
enterJob.setOnClickListener(OnClickOpenComp(enterJob));
//}
}
As requested this is my xml, however there isnt much to show as everything is created with code in run time once a Job is saved.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_width="match_parent">
<TextView
android:textSize="40dp"
android:layout_width="wrap_content"
android:text="Username"
android:layout_height="wrap_content" />
<EditText
android:textSize="40dp"
android:id="#+id/etUsername"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:nestedScrollingEnabled="false">
<LinearLayout
android:id="#+id/ReportsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:textSize="40dp"
android:id="#+id/bEmptyJN"
android:text="Enter Job number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<requestFocus
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here's what it looks like with jobs, picture the jobs as toggle buttons
Let me know if you'd like anymore information

Change Button color when pressed

I've found other answers to this but they where 2 and 6 years old so I didn't know if that was the best option.
I'm making an app that converts a simple integer in hex, oct and binary.
The user can choose in which of the three options the integer must be converted to by three buttons located at the top of the Fragment.
The effect that I'm searching for is that if the user presses one of the three Buttons, the one that has been clicked remains of a certain color.
This is my XML code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingRight="16dp"
android:paddingLeft="16dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dp">
<Button
android:id="#+id/hex_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="#string/hex_button"/>
<Button
android:id="#+id/binary_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="#id/hex_button"
android:text="#string/binary_button"/>
<Button
android:id="#+id/oct_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="#id/binary_button"
android:text="#string/oct_button"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/converted_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:inputType="number"
android:text="#string/convert_button"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="50dp">
<EditText
android:id="#+id/int_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/integer_edit_text_hint"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="50dp"
android:layout_alignParentBottom="true" >
<EditText
android:id="#+id/converted_number_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
Do I have to make changes in the Java code or just in the XML to achieve this?
You will need to change color of button widget for onClick() method
You can use two background drawables : one for active and other for inactive.
Add ClickListeners on all the buttons and toggle the backgrounds using setBackgroundResource(); method.
You will have to set an onClickListener for the buttons. In this listener, you check which button invoked the event that triggered the listener, and you change the color of this button there.
So you will have to do it in the java code. A listener basically looks like this:
Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Change Button Color here, and do other stuff
Button button = (Button) findViewById(v.getId());
button.setBackgroundColor(int color);
}
});
This use in xml activity like for button :-
<Button
android:id="#+id/button_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:layout_toRightOf="#id/binary_button"
android:text="#string/oct_button"
android:onClick="changeColor"
/>
Now in java class use like....
private boolean isChangeColor=true;
public void changeColor(View v)
{
if(isChangeColor)
v.setBackgroundColor(Color.BLACK);
else
v.setBackgroundColor(Color.WHITE);//use your original color here
isChangeColor=!isChangeColor;
}

Android/Java: EditText focus moves whole layout, and only ScrollView should be

This phase is supposed to take all basic information and add so-called "Generals", as many as the user wants, but when I click on en "Enter your phone number" EditText, or any of the generals' EditTexts, and the virtual keyboard appears, the whole layout gets pulled up so that the focused EditText can be visible.
Problem is that I need to have the top ribbon containing "Confirm" button available at all occasions. I'm aware of the fact the user can fix that by pressing the Back button, but that'd be extremely unpractical. I need to know how to keep the ribbon up there after everything else gets pulled up by the mentioned EditTexts.
Screenshot of the layout before mentioned EditTexts are tapped:
https://www.mediafire.com/convkey/2da9/4pa1balkda4y4d46g.jpg
Screenshot of the layout after mentioned EditTexts are tapped:
https://www.mediafire.com/convkey/3f9e/so8qq8vud6m3h996g.jpg
Here is layout's XML. "topRibbonLL" LinearLayout should always keep its place at the top, while everything else should be pulled up by tapping the mentioned EditTexts.
<?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" >
<LinearLayout
android:id="#+id/topRibbonLL"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/firstDataTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:text="Step 3: First data"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/confirmButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Confirm"
android:onClick="newFileStep3ConfirmPressed" />
</LinearLayout>
<CheckBox
android:id="#+id/shareableCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other people can share my file" />
<TextView
android:id="#+id/shareableDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:text="Description of the shareableCheckBox effect."
android:textAppearance="?android:attr/textAppearanceSmall" />
<ScrollView
android:id="#+id/dataScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/dataFileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true" >
</EditText>
<EditText
android:id="#+id/dataFullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:focusable="true"
android:focusableInTouchMode="true" />
<EditText
android:id="#+id/dataAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPostalAddress"
android:focusable="true"
android:focusableInTouchMode="true" />
<EditText
android:id="#+id/dataPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:focusable="true"
android:focusableInTouchMode="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Generals:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/generalsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/newGeneralButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add new general"
android:onClick="newGeneralButtonPressed" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Necessary EditTexts are re-set-up after the layout is opened, so that they'd have grey text etc.
private void setupNewCardEditText(final EditText editText, final String text) {
editText.setTextColor(R.integer.tempTextColor);
editText.setText(text);
editText.setGravity(Gravity.CENTER);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
if (editText.getTextColors().getDefaultColor() == R.integer.tempTextColor) {
editText.setTextColor(0xFF000000);
editText.setText("");
}
} else {
if (editText.getText().toString().equals("")) {
editText.setTextColor(R.integer.tempTextColor);
editText.setText(text);
}
}
}
});
}
I solved my problem. I used low-priority threading with a while loop to re-position the ribbon whenever it's not on top and visible. I also had to change the root layout to RelativeLayout to I could have control of the coordinates.

Categories