I have set the onClick property of an EditText which is located in a fragment:
<EditText android:id="#+id/edittext1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:inputType="none" android:maxLines="1"
android:singleLine="true" android:layout_marginTop="16dp"
android:focusable="false"
android:longClickable="false"
android:clickable="true"
android:onClick="doSomething"
android:cursorVisible="false"
android:editable="false">
Then in the fragment class I have:
public void doSomething(View view) {
//show dialogfragment...
}
But the method doSomething is grayed out and I get the warning 'Method doSomething is never used'.
Note: This code was originally in an activity and was working fine.
Is there another way to handle onClick in fragments?
first initialize an EditText instance in the top of your fragment
EditText et;
then
in your onCreateView()
add:
etEmployee=(EditText)rootView.findViewById(R.id.edittext1);
then implement your onClickListener()
et.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//what ever you need to do goes here
}
});
Note: in fragments you have to refer to the inflatedView to be able to access your editText in this case rootView.
also the code you are using doesn't work becouse here you are using fragments and using onClick attribute in your xml would only make you able to use it in the MainActivity that contain your fragment.
hope this will help.
If you add android:onClick="doSomething" to your fragment then method will be invoked in your activity but not in Activity. If you want the callback in your fragment add through pragmatically.
edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
You can always try to add a OnClickListener to the EditText if it does not matter to you in which way the function gets called.
edittext1 = (EditText) view.findViewById(R.id.edittext1);
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
In your layout XML try specifying a tools:context="com.mypackage.path.MyFragment". Don't know if it'll fix it for 'onClick', but it has fixed similar issues for me in the past.
This goes in the top most 'ViewGroup' in your layout file, example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mypackage.path.MyFragment" />
Related
In my activity , an EditText is invisible by default and once a button is clicked it becomes visible.
I managed to do that but once the EditText is visible , I can't input text to it.In other words, the soft keyboard never appears ,the writing cursor never appears , and the hint inside it never goes and it's like "frozen".
I tried the following but didn't solve my problem
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editsearch.setVisibility(View.VISIBLE);
editsearch.setEnabled(true);
editsearch.setFocusable(true);
}
});
and this is my xml
<EditText
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/colorAccent"
android:drawableEnd="#drawable/ic_search"
android:drawablePadding="3dp"
android:layout_alignParentTop="true"
android:drawableRight="#drawable/ic_search"
android:visibility="invisible"/>
Try adding editsearch.requestFocus() method to your code after the line editsearch.setFocusable(true).
I found the solution.
I use relative layout and below my Edittext , there is a listview. I found that the Edittext was behind the listview so I brought it to front.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editsearch.setVisibility(View.VISIBLE);
editsearch.requestFocus();
editsearch.bringToFront();
editsearch.invalidate();
}
});
I need to create this type of UI.
But it should work similar to Radio Button that is when one button got selected other two should be deselected.
There are various approaches like using three Images for each button and also images for pressed and unpressed state.
But how to achieve this using Button Tag.
Perhaps here may be useful to library work
https://github.com/pucamafra/android-segmentedtab
You can create 3 buttons and control other buttons with OnClickListener. For example:
layout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
class:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(true);
btn2.setSelected(false);
btn3.setSelected(false);
//Do something
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(false);
btn2.setSelected(true);
btn3.setSelected(false);
//Do something
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn1.setSelected(false);
btn2.setSelected(false);
btn3.setSelected(true);
//Do something
}
});
Sure, here .. keep it in selected state in OnClickListener when event x is fired. when event y is fired, keep button2 in selected state *& don't forget to remove button1 from selected state, Just try to improve this, it will work
I just made this , you are welcome to use & commit changes too!
Hi im trying to add on click listener to editText so i can disable the softkeyboard when user clicks on edittext using this code below, how to do that?
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
First it needs to be focusable...
<EditText
...
android:inputType="none"
android:focusable="false"
... />
You have to implement it in your code and than just add this to get an click listener...
myEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide the keyboard
// show own keyboard or buttons
}
});
try it and set OnClickListener
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/edt"
android:inputType="none"
android:focusable="false"
android:editable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The easiest and straight forward way you can set it is by editing the xml file as follows:
android:onClick="onClickMyEditText"
and define the same method in Activity class:
public void onClickMyEditText(View view) {
//your code here
}
Im using this library for range bar:
https://github.com/oli107/material-range-bar
the OnClickListener() does not work. This is my xml:
<com.appyvet.rangebar.RangeBar
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/rangebar"
android:layout_width="match_parent"
android:clickable="true"
android:layout_height="60dp"
android:onClick="Click"
/>
with android:onClick="Click" and with this function:
public void Click(View v) {
Log.e(TAG,"come");
userScroll=false;
}
it doesn't come inside function even with this function:
rangeBar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
I want to use this function and not a setOnRangeBarChangeListener().
Please try using one either use Click or setOnClickListener.
Dont use Both as they contradict.
Hope it helps.
I have a RelativeLayout with a few TextView as children
<RelativeLayout
android:id="#+id/shift_parent_name"
android:layout_width="fill_parent"
android:layout_weight="0.25"
>
<TextView
android:id="#+id/shift_parent_nametitle"
android:text="#string/shift_parent_nametitle"
style="#style/header_text"
/>
<TextView
android:id="#+id/shift_parent_namefield"
android:layout_alignParentRight="true"
android:layout_below="#id/shift_parent_nametitle"
style="#style/wrap"
/>
How do I go about using the RelativeLayout as a button to react to a click event if any part of the area is pressed?
Just add a OnClickListener to your RelativeLayout
I have a RelativeLayout called "RelativeMain1". This is how i make it start Activity
RelativeLayout relativeclic1 =(RelativeLayout)findViewById(R.id.RelativeMain1);
relativeclic1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
startActivityForResult(new Intent(A_My_Galaxy.this,C_Student_Book_Planet.class), 0);
}
});
After you add the onClickListener to your layout, it should work.
Add a "OnClickListener" to your RelativeLayout.
Note: Don't forget to add android:clickable="true" to your RelativeLayout.
Give your RelativeLayout an ID, like you typed shift_parent_name
Set your RelativeLayout XML to android:clickable="true"
Your final xml will be look like this:
<RelativeLayout
android:id="#+id/shift_parent_name"
android:layout_width="fill_parent"
android:layout_weight="0.25"
android:clickable="true">
then add the code in your onCreate method :
RelativeLayout relative1 = (RelativeLayout) findViewById(R.id.shift_parent_name);
relative1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
startActivity(new Intent(Main.this, About.class) );
}
});
Make sure to change your activity names, Main.this and About.class.
The main activity called Main.java and the second is About.java
RelativeLayout rl=(RelativeLayout)findViewById(R.id.RelativeMain1);
relativeclic1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
}
});
You can simply just add this android:onClick="onClick"