how to jump to an anchor tag from a button clickhandler - gwt

I have a clickhandler for a button. I want the screen scrolls to an anchor like
Anchor a = new Anchor();
a.setName("stopHere");
(...)
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// Do somethings
(...)
// Scroll to Anchor "stopHere"
?????
}
});
How can I do it?
This is similar when using an anchor click to go to stopHere point , but I wan do it next the button is clicked.
Thanks.

The method Element#scrollIntoView() should do what you want:
final Anchor a = new Anchor();
a.setName("stopHere");
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
a.getElement().scrollIntoView();
}
}
Or use Window#scrollTo(int, int)
final Anchor a = new Anchor();
a.setName("stopHere");
button.addClickHandler(new ClickHandler()
{
#Override
public void onClick(ClickEvent event)
{
Window.scrollTo(a.getAbsoluteLeft(), a.getAbsoluteTop());
}
}

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

Starting a new activity android

I am trying to learn the principles of starting a new activity in android. I am using "Android studio". I have created a MainActivity class which contains a text which tells the user to push the button, and the button. When the button is pushed the new activity is supposed to start(aktivitet2Klasse). This activity contains two textviews and a button. When the first button is pressed, the tetview should display "works fine!". When the second button is pushed it is supposed to display "Finished!". The point with the app is just to start one activity from another. However after pushing the button on the first screen (first activity) the app crashes. I have studies a lot of the same issues on these sides, but I cannot figure what is wrong.
Here is the code:
The main activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView instruction;
private Button startButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instruction = (TextView) findViewById(R.id.InstruksjonsView);
startButton = (Button) findViewById(R.id.btn1);
startButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(MainActivity.this,aktivitet2Klasse.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The next activity:
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
you forgot to set the content view in the second activity
public class aktivitet2Klasse extends AppCompatActivity {
private Button button2;
private TextView text;
private Button button3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//YOU NEED TO SET THE CONTENTVIEW
// like setContentView(R.layout.activity_secondary);
Intent intent = getIntent();
button2 = (Button) findViewById(R.id.btn2);
button3 = (Button) findViewById(R.id.btn3);
text = (TextView) findViewById(R.id.textView1);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Works fine!");
}
});//End button2Listener
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setText("Finished!");
}
});//End button3Listener
}

Single and Double Click on Button in Android?

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

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

GWT - Browser window resized handler

I am developing a GWT application that render a text on a canvas. I want to resize the canvas whenever browser window resized. The problem is if I used Window.addResizeHandler, the rendering process with each resize will be very slow. So I need a way to resize the canvas only when the user release the mouse button after finishing resize. Is there anyway to do that?
You could add a delay, so that the resize is only processed after the window hasn't been resized for some number of milliseconds:
Window.addResizeHandler(new ResizeHandler() {
Timer resizeTimer = new Timer() {
#Override
public void run() {
doComplexLayoutCalculations();
}
};
#Override
public void onResize(ResizeEvent event) {
resizeTimer.cancel();
resizeTimer.schedule(250);
}
});
Window.addResizeHandler(new ResizeHandler() {
#Override
public void onResize(ResizeEvent event) {
Scheduler.get().scheduleDeferred(
new Scheduler.ScheduledCommand() {
public void execute() {
// layout stuff
}
});
}
});