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();
}
});
Related
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
}
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" />
I am new in android programming. I have made an app which is fill in the blanks type app. Until the answer-confirm button is clicked, the next and the previous Button should be disabled. If it is clicked and answer is checked, then next and previous Button to get enabled. please help!!!!!!!!
If you want to disable a button in xml use this code
<Button
android:text="Next"
android:id="#+id/my_button_del"
android:layout_width="72dp"
android:layout_height="40dp"
android:visibility="invisible"/>
for enabling the button when we click on previous then in onClick function(previous) add this code
next.setVisibility(View.VISIBLE);
next is the button
something like that (maybe its not the best way, you can play with it and make it better)
protected void onCreate(Bundle savedInstanceState) {
...
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
enableNextButton();
}
});
...
}
private void enableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_active);
nextButton.setClickable(true);
nextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goNext();
}
});
}
private void disableNextButton(){
nextButton.setBackgroundResource(R.drawable.button_inactive);
nextButton.setClickable(false);
}
and in your xml the buttons should be something like
<Button
android:id="#+id/first_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_active"
android:layout_margin="#dimen/generic_margin"
android:text="#string/first_button_text"
android:textColor="#color/white"/>
<Button
android:id="#+id/next_button"
android:layout_width="match_parent"
android:layout_height="#dimen/generic_button_heigth"
android:background="#drawable/button_inactive"
android:clickable="false"
android:layout_margin="#dimen/generic_margin"
android:text="#string/next_button_text"
android:textColor="#color/white"/>
this way you start with an active button and an inactive button, when the first is pressed you can "activate" the next button
Please use punctuations and uppercases but anyway.
You can check here for more infos about buttons : http://developer.android.com/reference/android/widget/Button.html
Otherwise, the method setVisible() allow you to make visibile or not a button of your layout. Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
or in xml:
<Button ... android:visibility="gone"/>
EDIT
Oh sorry ! So you can use setEnabled().
My XML contains Five EditText box and One Button. My cursor is now pointed to the First EditText box. How can I click a button to place the cursor automaticly to Third EditText box.
Thank you!
on your button's onClick() put..
thirdEditText.requestFocus();
Something like,
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
thirdEditText.requestFocus();
}
});
editText3.requestFocus();
add in onClick method of button.
Use requestFocus() method to gain focus.
Or put < requestFocus/> in your XML layout.
this is the code:
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
}
});
try
EditText editText = (EditText) findViewById(R.id.textId);
editText.requestFocus();
just add <requestFocus/> tag in the EditText like:
<EditText
android:id="#+id/editText"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_below="#id/label"
android:inputType="numberDecimal"
android:textSize="25dp" >
<requestFocus />
</EditText>