Ankata
Ankata

Reputation: 101

Can not display button when click on edit text

I have an edit text and button. By default, this button is invisible.

When i click on edit text, it displays soft-keyborad and this button is visible. I caught on click event of edit text, and set this button is visible. However, it is not visible until i click the second into edit text (which is in soft-keyborad displayed)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
        android:focusable="true" 
    android:focusableInTouchMode="true" 
android:orientation="vertical" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10" >

</EditText>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/editText1"
    android:text="Button" />

</RelativeLayout>

Code

public class anr extends Activity {
InputMethodManager imm; 
/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE); 
super.onCreate(savedInstanceState); 
setContentView(R.layout.testaaa); 

final Button btn = (Button)this.findViewById(R.id.button1);
btn.setVisibility(View.INVISIBLE);

final EditText txtSearch = (EditText)this.findViewById(R.id.editText1); 
txtSearch.setOnClickListener(new OnClickListener() { 

public void onClick(View v) { 
// TODO Auto-generated method stub 
txtSearch.setFocusable(true);//(false); 
txtSearch.setFocusableInTouchMode(true); 
txtSearch.requestFocus(); 
btn.setVisibility(View.VISIBLE);
} 
}); 

txtSearch.setOnKeyListener(new OnKeyListener() { 

public boolean onKey(View v, int keyCode, KeyEvent event) { 
// TODO Auto-generated method stub 
if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
(keyCode == KeyEvent.KEYCODE_ENTER)) 
{ 
    btn.setVisibility(View.INVISIBLE);
    hideSoftKeyboard(v); 
    txtSearch.clearFocus(); 
} 



return false; 
}}); 

} 
public void hideSoftKeyboard (View view) { 

imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
} 
}

Upvotes: 0

Views: 401

Answers (2)

kannappan
kannappan

Reputation: 2250

Try this code...

txtSearch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
btn.setVisibility(View.INVISIBLE);
hideSoftKeyboard(v); 
txtSearch.clearFocus(); 
} 

Upvotes: 0

Shachilies
Shachilies

Reputation: 1616

Please set on touch listener to your edit text .

and place your onclick methos work i.e.

txtSearch.setFocusable(true);
txtSearch.setFocusableInTouchMode(true); 
txtSearch.requestFocus(); 
btn.setVisibility(View.VISIBLE);

there . :)

Upvotes: 1

Related Questions