Alternating editText text color onButtonClick - java

I am currently trying to create a highlighter function for my app where on a button being clicked by the user the program checks to see if the editText TextColor is black, default, or Yellow, highlighted, then switches between the two.
I am using an onClickListener if Statement:
hB0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ed1.getTextColors().equals(Color.BLACK)){
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}
}
});
I have set the textColor of the EditText to Color.Black in the XML file and I have looked for similar problems with little to show for it! Thank you for your time.
Edit: The problem is when I press the button the text stays the same colour and doesn't switch between the two.
Edit2: To take it further, when I comment out the if statement and make the button simply change the text colour to Color.YELLOW it works perfectly. I just want to be able to switch the colour BACK to black.
Edit 3: The XML Code for the highlighter button and the textView I want to change the text color in.
<Button
android:id="#+id/highLighter0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:minWidth="4dp"
android:minHeight="10dp"
android:text="H"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="#+id/slot0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="#+id/slot0"
app:layout_constraintTop_toTopOf="#+id/slot0" />
<EditText
android:id="#+id/slot0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="center"
android:hint="6 AM"
android:inputType="textCapSentences|textNoSuggestions|textMultiLine"
android:lines="4"
android:maxLines="4"
android:minLines="1"
android:rotationX="0"
android:textColor="#color/black"
app:layout_constraintEnd_toStartOf="#+id/highLighter0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/shoppingListButton"
tools:layout_conversion_absoluteHeight="45dp"
tools:layout_conversion_absoluteWidth="370dp" />
EditText #id=slot0 is ed1 in the java code.

You need to change your code with below code:
if (ed1.getCurrentTextColor() == Color.BLACK) {
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}

Related

How to create a clickable Edittext?

I know a there is a lot of similar questions out there, but none of them work for me. I want to make a EditText that acts like a Button. I want it to be not editable (and no keyboard should popup). However, when I set an onClickListener(), the EditText doesn't react to my onClick() event. How should I do this correctly?
In my xml I disabled the focusableInTouchMode and I set the clickable to be true. My resulting EditText is not editable, but it doesn't response to my onClickListener().
My xml of the EditText:
<EditText
android:id="#+id/taskviewer_date"
android:focusableInTouchMode="false"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textColorHint="#color/colorShadow"
android:inputType="none"
android:hint="No Due Date"/>
My Code:
EditText text = (EditText) findViewById(R.id.taskviewer_date);
text.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("taskviewer", "OnClickListener called !");
}
}
);
Use onTouch events or put a clickable FrameLayout over your EditText to catch click events. Make FrameLayout's visibility Gone when you want your edittext to be editable
Use of EditText's setOnClickListener will work if you set setTextIsSelectable to true.
In the XML:
<android.support.design.widget.TextInputLayout
android:id="#+id/textInpuLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/hint_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/textInpuEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:inputType="date"/>
</android.support.design.widget.TextInputLayout>
In the appropriate fragment or the activity:
mTextInpuEditText.setEnabled(true);
mTextInpuEditText.setTextIsSelectable(true);
mTextInpuEditText.setFocusable(false);
mTextInpuEditText.setFocusableInTouchMode(false);
mTextInpuEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Edit Text is clicked");
}
});

Edit Text - Focus on Touch

I have created a note app.
In my app when i create a new note. By default, the cursor is at title edit text but I want to have a feature that if I touch anywhere on the screen the cursor should go to the description edit text. I have kept the description edit text in a separate linear layout which has a weight of 1.
Kindly find the images to get an idea.
In the given picture the area covered with the blue line I want to touch anywhere in that area and my focus should be directed to edit text where I have written the hint description
XML
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/scview"
android:layout_weight="1">
<EditText
android:id="#+id/body_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="18sp"
android:inputType="textMultiLine"
android:hint="Description"
android:backgroundTint="#android:color/transparent"/>.
</LinearLayout>
Use onClickListener() on parent layout of your xml like this
RelativeLayout parentRelativelayout=(RelativeLayout)findViewByRid(R.id.your_parent_layout_in_xml);
EditText edtDescription=(Edittext)findViewByRid(R.id.body_content);
parentRelativelayoutsetOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
edtDescription.requestFocus();
}
});
You need to override dispatchTouchEvent method and changes focus , as user touch screen , dispatchTouchEvent method will be fired.
#override Boolean dispatchTouchEvent(MotionEvent ev ) {
Toast.makeText(context ,"dispatchTouchEvent", Toast.LENGTH_SHORT).show()
return super.dispatchTouchEvent(ev)
}

When EditText with pre-filled text becomes editable, all text moves to one single line

I have an EditText inside of a ScrollView which is pre-filled with a dynamic amount of text. It could be one line or 50 or more. At this point, the EditText is uneditable and the text is shown just fine. However, when I press a button to make the edit text editable, the entire contents of the EditText are thrown into one very long, horizontally scrolling line.
You can see exactly what I'm talking about here:
https://zippy.gfycat.com/GenerousBigHerring.webm
How can I prevent this from happening? I've been screwing around with this for a while now. I've tried setting lines and maxLines, etc, but I can't figure it out.
Edit:
This is my current ScrollView/EditText:
<ScrollView
android:id="#+id/result_text_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/result_title_text"
android:layout_above="#+id/result_bottom_sheet"
android:layout_marginTop="20dp"
android:padding="5dp">
<EditText
android:id="#+id/extracted_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:padding="15dp"
tools:text="Hello, World"
android:textColor="#color/black"
android:textStyle="bold"
android:textSize="#dimen/extracted_text_size"
android:textIsSelectable="true"
android:inputType="none"
/>
</ScrollView>
From code, here's what I'm doing to to enable/disable editing:
#Override
public void enableTextEditing() {
mExtractedText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
mExtractedText.setOnKeyListener(this);
}
#Override
public void disableTextEditing() {
mExtractedText.setInputType(InputType.TYPE_NULL);
mExtractedText.setOnKeyListener(null);
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
//All this does is call the View methods to disable text editing
//and close the keyboard.
mPresenter.doneEditingButtonPressed();
return true;
}
return false;
}
Add android:lines , android:minLines and android:maxLines to your EditText view
Also change android: inputType = "textMultiline"
You can add android:scrollbars="vertical" to your EditText as well if you wish

Android, Button don't working at all

This code represent my real issue...
I'm working on an U.I. more complexe.
I have a custom view that need to have a button out of his leftSide. Why? Because i have four of this custom view aside . And those button's view to create some intercalary view !
I have a simple layout that contains a button.
I had to make him out of his layout's parent, with the property clipChildren="false"
But the button don't response to the onClickListener.
I certainly miss something, but what?
The animation click isn't played at all...
Even the Android's click's song isn't played...
The java code have no effect there..
The button's id button2 work.
The button's id button don't work..
Here is my xml code.
<LinearLayout
android:orientation="vertical"
android:background="#0000FF"
android:clipChildren="false"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#FF0"
android:layout_marginLeft="-20dp"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_marginLeft="-80dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:id="#+id/button2"
android:layout_width="80dp"
android:layout_height="80dp" />
</LinearLayout>
and the onCreate Methode:
Button buton1 = (Button) rootView.findViewById(R.id.button);
buton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.wtf("-----", "Click");
Toast.makeText(getActivity(), "button1", Toast.LENGTH_LONG).show();
}
});
Button buton2 = (Button) rootView.findViewById(R.id.button2);
buton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "button2", Toast.LENGTH_LONG).show();
}
});
Try putting the button back inside its parent.
If it works there, have a very long hard think about why you want this button to not live inside its parent. This is a bad idea almost all of the time as it breaks things (both practically and conceptually).
I would suspect the issue is that something else which has a better claim to the space the button is occupying is consuming the touch event.

When user clicks a button weird text appears in inputbox

Here is my xml code for a sample button.
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_marginTop="25dp"
android:layout_weight="0.166"
android:text="1"
android:onClick="ProcessInput" />
Now in mainActivity.java i wrote this code
public void ProcessInput(View v)
{
Button btn = (Button) v;
inputText.append(btn.toString());
}
The output is shown in the screenshot
You simply convert to string not getting value of button so change from
inputText.append(btn.toString());
to
inputText.append(btn.getText().toString());
You need to use .getText() to get button text. btn.toString() will return id of btn.
Try this
inputText.append(btn.getText().toString());

Categories