How to create a clickable Edittext? - java

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

Related

I am getting error when trying to setOnClickListener

Getting error when I use button setOnClickListener, also getting same error when use ViewBinding.
My Mainactivity.java is looks like this
setContentView(R.layout.activity_select_player);
button=(Button) findViewById(R.id.play);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
}
});
findViewById is showing error when I call to set in on button, and thats why I am also cant use setOnClickListener to button.
my activity_main.xml is looks like this
<Button
android:id="#+id/play"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="#drawable/button_bg"
android:text="#string/play"
android:textColor="#color/white"
android:textSize="34sp"
app:cornerRadius="30dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:layout_constraintVertical_bias="0.672" />
This is the error,
void android.view.View.setOnClickListener(android.view.View$OnClickListener)
The view you are setting as activity's view doesn't match with the one that contains the button you are trying to interact with.
Just replace:
setContentView(R.layout.activity_select_player);
With:
setContentView(R.layout.activity_main);

EditText focus is not working in android studio, i want to hide a textview when focus change in edittext, how to do this?

EditText as ChangeContact
EditText focus is not working in android studio, i want to hide a textview when focus change in edittext, how to do this?
ChangeContact.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Toast.makeText(mContext, "focus loosed", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(mContext, "focused", Toast.LENGTH_LONG).show();
bookLinear.setVisibility(View.GONE);
}
}
});
My edit text in XMl file
<EditText
android:id="#+id/etChangeAddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:layout_marginBottom="8dp"
android:background="#drawable/shape_transparent"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="Address"
android:inputType="textPersonName"
android:padding="5dp"
app:layout_constraintBottom_toTopOf="#+id/etChangeNumber"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
But i cant see the Toast
Please help ....
Have you tried with this for ChangeContact?
android:focusable="true"
android:focusableInTouchMode="true"
also
ChangeContact.requestFocus() ?
In my case I can't get on focus change event because I forgot to delete setContentView() function and used it with binding initialization like this:
setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater, null, false)
also eventually I understood that binding initialization also was incorrect

How do I create collapsable edittext areas similar to the attachment in android

Thanks for reading this, I would like to create a collapsable edittext content area in my android activity and I don't know how to do it. see the screenshot below
when the user clicks a particular view containing the edittext, an edittext appears and is only hidden when the user clicks the view again.
KOTLIN
text.setOnClickListener(View.OnClickListener {
if(text_layout.visibility == View.VISIBLE)
text_layout.visibility = View.GONE
else
text_layout.visibility = View.VISIBLE
})
use Visiblity to handle that
JAVA
text = findViewById(R.id.text);
text_layout = findViewById(R.id.text_layout);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(text_layout.getVisibility() == View.VISIBLE)
text_layout.setVisibility(View.GONE);
else
text_layout.setVisibility(View.VISIBLE);
}
});
XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello asdasdasd!"
android:id="#+id/text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="200dp"
android:id="#+id/text_layout"
android:visibility="gone"
android:background="#000"/>

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.

How can I change the text with a button click?

When I click a button, I want the the string #string/dummy_content in fullscreen_content to change. How can I do that?
xml:
<TextView android:id="#+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:textColor="#2f4b66"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:text="#string/dummy_content">
button xml:
<Button android:id="#+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/dummy_button"
android:clickable="true"
android:onClick="printStarter"
/>
The Java function I would like to activate:
public void printStarter(View view) {
}
Try this..
You can't change string resource at the run time
public void printStarter(View view) {
((TextView)findViewById(R.id.fullscreen_content)).setText("Your Text");
}
You need to get the reference of the TextView whose text you need to set. Here is the code:
public void printStarter(View view){
TextView text=(TextView)findViewById(R.id.fullscreen_content);
text.setText("Some text....");
}
You can't change string resource dynamically since it's a compiled resource.

Categories