Single and Double Click on Button in Android? - android-activity

I want to create Single and Double click on Button in Android...
Thanks for help in Advance.
I have already tried using button.setOnClickListener() for single click on button but i couldn't find double click on button

Try this code : (btn is the button you want to check for single and double click)
int i = 0;
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
i++;
Handler handler = new Handler();
Runnable r = new Runnable() {
#Override
public void run() {
i = 0;
}
};
if (i == 1) {
//Single click
handler.postDelayed(r, 250);
} else if (i == 2) {
//Double click
i = 0;
ShowDailog();
}
}
});

Related

How to define gravity of a Dialog Fragment?

I want my dialog to stick to the start but it always comes in the center.
i will give you example==>
Following is the code -->Onclick image -->Dialog Appears==>
Dialog dialog; //your dialog declaration
//
//
//
oncreate(){
imageView = (ImageView) findViewById(R.id.customProfileGridImg);
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.activity);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.START |Gravity.TOP; //change direction as per your requirement
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.show();
}
});
}

Hide the Title bar while scrolling and Show the TItle bar after Scrolling?

I want to hide the title bar when i scrolling the items in the ListView and i want to show the title bar after scrolling. Suggest any ideas to solve this issue.
First add the Xml View into ActionBar like this:
LayoutInflater inflater = (LayoutInflater) getActionBar()
.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View customActionBarView = inflater.inflate(R.layout.main, null);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(R.layout.main);
Then do the changes in onScrollStateChanged() method:
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
case SCROLL_STATE_IDLE:
actionBar.show();
break;
case SCROLL_STATE_TOUCH_SCROLL:
actionBar.hide();
break;
}
}
//declare this two globally
public static int ch = 0, cht = 1;
int myLastVisiblePos;
//Then add onScrollListener to your ListView
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int currentFirstVisPos = view.getFirstVisiblePosition();
if (currentFirstVisPos > myLastVisiblePos) {
if (ch == 1) {
ch++;
cht = 1;
getActionBar().hide();
} else if (ch == 0) {
getActionBar().show();
ch++;
}
}
if (currentFirstVisPos < myLastVisiblePos)
if (cht == 1)
getActionBar().show();
myLastVisiblePos = currentFirstVisPos;
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
This solution worked for me very good:
// mLastFirstVisibleItem defined globally
quranTextList.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
/**
* Hide actionbar when scroll down
*/
if (mLastFirstVisibleItem < firstVisibleItem)
if (getSupportActionBar().isShowing())
getSupportActionBar().hide();
if (mLastFirstVisibleItem > firstVisibleItem)
if (!getSupportActionBar().isShowing())
getSupportActionBar().show();
mLastFirstVisibleItem = firstVisibleItem;
}
});
Source: Android ActionBar hide/show when scrolling list view

Fire Button onAction on key released and touch released JavaFX

The docs say the button should fire for clicks, keypress, and touch. It doesn't seem to work.
Some code to play with.
public class FireButtonTest extends Application {
#Override
public void start(Stage primaryStage) {
TextArea ta = new TextArea();
Button btn1 = new Button();
btn1.setText("btn1");
btn1.setOnAction((ActionEvent event) -> {
ta.setText(ta.getText()+"btn1 fired \n");
});
Button btn2 = new Button();
btn2.setText("btn2");
btn2.setOnAction((ActionEvent event) -> {
ta.setText(ta.getText()+"btn2 fired \n");
});
btn2.setOnKeyReleased((KeyEvent ke) -> {
if (ke.getCode() == KeyCode.ENTER) {
ta.setText(ta.getText()+"btn2 key event -> ");
btn2.fire();
}
});
//?? can't test this
btn2.setOnTouchReleased((TouchEvent te) -> {
if (te.getTouchCount() == 1){
ta.setText(ta.getText()+"btn2 touch event -> ");
btn2.fire();
}
});
VBox root = new VBox(5);
root.getChildren().addAll(btn1, btn2, ta);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Button Test");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Why can't I just press enter when the button is focused?
Doh! I guess you have to use the space bar. Here's the source.
<!-- language: java -->
protected static final List<KeyBinding> BUTTON_BINDINGS = new ArrayList<KeyBinding>();
static {
BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_PRESSED, PRESS_ACTION));
BUTTON_BINDINGS.add(new KeyBinding(SPACE, KEY_RELEASED, RELEASE_ACTION));
}

popup panel in gwt

How the popup panel work when on the mouse enter it will show and on leaving it will hidden and on showing of popup panel when the cursor move inside the popup it will stay over.
I am doing this but it is having problem that the popup is not staying....any idea?
Event.addNativePreviewHandler(new NativePreviewHandler() {
public void onPreviewNativeEvent(final NativePreviewEvent event) {
final int eventType = event.getTypeInt();
switch (eventType) {
case Event.ONMOUSEOUT:
{
System.out.println("hello");
popup.setVisible(false);
break;
}
case Event.ONMOUSEOVER:
{
l1.addMouseOverHandler(new MouseOverHandler()
{
public void onMouseOver(MouseOverEvent event)
{
popup.show();
popup.setPopupPositionAndShow(new PopupPanel.PositionCallback()
{
public void setPosition(int offsetWidth, int offsetHeight)
{
int left = (Window.getClientWidth() - offsetWidth) / 24;
int top = (Window.getClientHeight() - offsetHeight) / 5;
popup.setPopupPosition(left, top);
}
});
MouseOutHandler handler1=new MouseOutHandler()
{
public void onMouseOut(MouseOutEvent event)
{
System.out.println("welcome2");
popup.setVisible(false);
}
};
popup.addHandler(handler1,MouseOutEvent.getType());
popup.sinkEvents(Event.ONMOUSEOUT);
event.preventDefault();
}
});
break;
}
default:
}
}
});

Softkeyboard is shown only on calling method from button click

I am programatically showing the soft keyboard when a layout is loaded on a button click.
I am showing the softkeyboard only when the textfield has focus. It works fine. But when I call the same method at another place in the code(not a button click) then the soft keyboard does not show up. Below is my code. Please point out where I went wrong.
public void showNewView() {
setContentView(R.layout.activity_main);
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
isRegisterScreen = true;
final EditText text1 = (EditText) findViewById(R.id.label1);
final EditText text2 = (EditText) findViewById(R.id.label2);
final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
text1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(labelText, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(text1.getWindowToken(), 0);
}
}
});
text2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if(hasFocus){
inputManager.showSoftInput(text2, InputMethodManager.SHOW_IMPLICIT);
}else{
inputManager.hideSoftInputFromWindow(phoneText.getWindowToken(), 0);
}
}
});
text1.requestFocus();
}