Long click on a list item in GWT - list

Is there a way in gwt for a long click on a list-item like in a native app for a mobile device.
That is the intuitive click on a smartphone beside the simple click.
I would like to offer a possibility for the user to delete the item.
Something like a longclicklistener...

This has worked for me:
final Timer longpress = new Timer(){
#Override
public void run() {
Window.alert("long press!");
}};
marker.addMouseDownListener(new Marker.MouseDownHandler() {
#Override
public void handle(MouseEvent event) {
longpress.schedule(TIME_LONG_PRESS);
}
});
marker.addMouseUpListener(new Marker.MouseUpHandler() {
#Override
public void handle(MouseEvent event) {
longpress.cancel();
}
});

There is no LongPressListener per se , but you can implement a MouseListener ( http://google-web-toolkit.googlecode.com/svn/javadoc/1.5/com/google/gwt/user/client/ui/MouseListener.html ) and onMouseDown , start a timer , cancel it in onMouseUp(). If the timer expires before it is cancelled , then you have a long click.
Timer t = new Timer() {
public void run() {
//fire long press event.
}
};
new MouseListener(){
public void onMouseDown(Widget sender,int x,int y){
timer.cancel();
timer.schedule(1000);
}
public void onMouseUp(Widget sender,int x,int y){
timer.cancel();
}
}

Take a look at m-gwt ( its a mobile UI Framework for GWT) and it has different GestureRecognizers.
see: http://www.m-gwt.com

Related

Qt, how to know the request from which page with CustomScheme

I's using the QWebEngineView to render a h5 page, the h5 page use our custom scheme protocol to communicate with native.
For example, when the native receive the 'scheme_name://close' message from h5 page, the native will close the h5 page.
How can i know the message from which page?
Firstly, i use QDesktopServices::setUrlHandler() to set a url handler, but it's a global handler, and the callback must accepts a single QUrl argument, i can not know the page which the request from.
class Dialog : public QDialog {
public:
void Close() {
web_view_->close();
close();
}
private:
QWebEngineView *web_view_;
}
void OnCustomScheme(const QUrl &url) {
if (url.scheme() != "scheme_name") {
return;
}
if (url.host() == "close") {
// call Dialog::Close();
// who->Close();
}
}
Secondly,i try to use QWebEngineProfile::installUrlSchemeHandler, the receive can use a member of my page, like:
class SchemeHandler : public QWebEngineUrlSchemeHandler {
public:
void requestStarted(QWebEngineUrlRequestJob *job) override {
emit SignalSchemeRequest(job->requestUrl());
}
signals:
void SignalSchemeRequest(const QUrl &url);
};
class Dialog : public QDialog {
public:
Dialog() {
handler_ = new SchemeHandler(this);
connect(handler_, &SchemeHandler::SignalSchemeRequest,
this, &Dialog::SlotSchemeRequest);
profile_ = new QWebEngineProfile(this);
profile_->installUrlSchemeHandler("scheme_name", handler_);
}
void LoadPage(const QUrl &url) {
QWebEnginePage *page = new QWebEnginePage(profile_, this);
web_view_->setPage(page);
page->load(url);
}
void Close() {
web_view_->close();
close();
}
private slots:
coid SlotSchemeRequest(const QUrl &url) {
if (url.host() == "close") {
Close();
}
}
private:
QWebEngineView *web_view_;
SchemeHandler *handler_;
QWebEngineProfile *profile_;
}
Does it has any better way to do?
Everytime to load a page, i have to new a QWebEnginePage.

ActionBar back button crash my app

I have problem with my school project when I want to back to my CurentCourseActivity using back button form action bar I have NE
Attempt to invoke virtual method 'java.util.List `com.example.pingu.mylanguages.Language.getList()' on a null object reference at
com.example.pingu.mylanguages.CurentCourseActivity.onCreate(CurentCourseActivity.java:37)`
While I use normal back button problem is not appear.
When we choose Lesson from GridView that makes a new activity. When we choose the back button from ActionBar then I have NE.
CourentCourseActivity:
public class CurentCourseActivity extends AppCompatActivity {
private Language language;
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("s","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_curent_course);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState!=null){
language = (Language) savedInstanceState.getSerializable("Lang");
Log.d("xx","Coś ma");
}
if (getIntent().getExtras() != null) {
language = (Language) getIntent().getSerializableExtra("Lang"); //Obtaining data
}
GridView grid = (GridView) findViewById(R.id.grid);
CurrentCourseAdapter adapter = new CurrentCourseAdapter(this, R.layout.grid_item_curent_course, language.getList());
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(CurentCourseActivity.this,LessonActivity.class);
intent.putExtra("Lesson",(Lesson)language.getList().get(i));
startActivity(intent);
}
});
getSupportActionBar().setTitle(language.getName());
}
}
LessonActivity:
public class LessonActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if(getIntent().getExtras()!=null){
Lesson lesson = (Lesson) getIntent().getSerializableExtra("Lesson");
getSupportActionBar().setTitle(lesson.getNamel());
}
}
}
The answer is simplest then I expect. So I need to overide this button and do it as normal back button because ActrionBar back button destroy parent activity.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}

CardBuilder show more text

is it possible to arrange Show more button using GDK now? I have pretty big portion of text which I would like to split for few cards for example.
Thank you for help.
The issue with Google Glass is that you can't really "button" in the way that you're thinking. You can't tap on a certain part of the screen. You only can swipe down, up, left, right, and tap.
What you can do, is listen for those possible gestures and then act accordingly - maybe create a TextBox that can scroll and scroll through it on the swipes. Or maybe go to the next card/update the text in the card when you tap. Here is how you detect these actions:
You need to create a GestureDetector. Here is how I do it in my projects:
public class EXAMPLE {
private GestureDetector gestureDetector;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gestureDetector = createGestureDetector(this);
}
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() {
#Override
public boolean onDown(MotionEvent motionEvent) {
return false;
}
#Override
public void onShowPress(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
#Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) {
return false;
}
#Override
public void onLongPress(MotionEvent motionEvent) {
}
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
return false;
}
});
return gestureDetectorTemp;
}
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (gestureDetector != null) {
return gestureDetector.onTouchEvent(event);
}
return false;
}
}
That last part is very important. On any generic motion event, if the gestureDetector isn't null, you'll send the event through the gestureDetector for processing.
KEEP IN MIND ALSO that you need to understand what the return false; and return true; things mean. If you return false, then that means that the event wasn't consumed. If you return true, then the event is consumed. In other words, if you return true, then nothing else will activate, because the event gets 'eaten up,' but if you return false, this sends the event on to other functions which may do something when an action is taken.
Now just take this, and change the onSingleTapUp() method's contents to do what you want...something like
card.setText(nextSetOfText);
or
textView.setText(nextSetOfText);
You could split your long text into an array of strings with the maximum length that'll fit on the string, then just cycle to the next string in your array when the person taps.

How to refresh item in a popup menu?

i have a popup menu (that comes out when the user uses right click on specified elements), wich items are readed from a list.
I want that when an item is selected, that item is disabled in the popupMenu (then if some action happen it will return enabled).
I have implemented the popupMenu, but i cannot implement this enable/disable JMenuItem element. Anyone can help me? Thanks
class PopupTriggerListener extends MouseAdapter {
public void mousePressed(MouseEvent ev) {
if (ev.isPopupTrigger()) {
menu.show(ev.getComponent(), ev.getX(), ev.getY());
x = ev.getX();
y = ev.getY();
}
}
public void mouseReleased(MouseEvent ev) {
if (ev.isPopupTrigger()) {
menu.show(ev.getComponent(), ev.getX(), ev.getY());
x = ev.getX();
y = ev.getY();
}
}
public void mouseClicked(MouseEvent ev) {
}
}
}
JLabel label = new MyLabel("right-click");
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuItem item = new JMenuItem("Test1");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu item Test1");
JLabel newLabel = new JLabel("test");
label.add(newLabel);
newLabel.setBounds(x, y, 40, 10);
}
});
menu.add(item);
item = new JMenuItem("Test2");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu item Test2");
}
});
menu.add(item);
getContentPane().add(label);
pack();
setSize(300, 100);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
The way this is mostly done is using Actions. Actions are extensions of the ActionListener interface. You can set the Action of, for example, a JMenuItem and in the Action you can set enabled to false. This will automatically disable the JMenuItem. Alternately you can enable it by setting enabled to true on the Action.
Here is the Action API #Oracle: Action API JAVA
And here is a discourse on how to use Actions: How to use Actions JAVA

how to refresh the linear layout view after deleting an element

I have a simple app, in one activity I take name and date of birth. I store it in the database. and in the main activity I have linearlayout which will show all the names.
When I click on any of the name in the main activity, it should delete that name from the database and also refresh the view.
I am able to delete the entry from database, but my linear layout view is not being updated. Can some one pls help.
public class child extends Activity {
private Intent intent;
private LinearLayout layout;
private LayoutInflater linflater;
private int i =0;
private Cursor cr;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child);
layout = (LinearLayout)findViewById(R.id.layout);
Button addBtn = (Button)findViewById(R.id.AddButton);
Button remBtn = (Button)findViewById(R.id.RemoveButton);
intent = new Intent(this,login.class);
layout = (LinearLayout) findViewById(R.id.mylayout1);
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Check the database if there are any entries available. If available, then
//list them on the main screen
final myDBAdapter mydb = new myDBAdapter(getApplicationContext());
mydb.open();
cr = mydb.GetMyData();
if(cr.getCount()>0)
{
cr.moveToFirst();
for (int i=0;i<cr.getCount();i++)
{
cr.moveToPosition(i);
buildList(cr.getString(1),cr.getString(2));
}
}
//Start the login activity which will return the newly added baby name
addBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivityForResult(intent, 1001);
}
});
//Remove all the entries from Database
remBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(cr.getCount()>0)
{
cr.moveToFirst();
for (int i=0;i<cr.getCount();i++)
{
Toast.makeText(getApplicationContext(), cr.getString(1),
Toast.LENGTH_LONG).show();
mydb.RemoveEntry(cr.getString(1));
cr.moveToPosition(i);
}
}
}
});
mydb.close();
}
private void buildList(final String bname,String bsex)
{
final View customView = linflater.inflate(R.layout.child_view,
null);
TextView tv = (TextView) customView.findViewById(R.id.TextView01);
//tv.setId(i);
tv.setText(bname);
tv.setTextColor(getResources().getColor(R.color.black));
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDBAdapter mydb = new myDBAdapter(getApplicationContext());
mydb.open();
if (mydb.RemoveEntry(bname)>0)
{
Toast.makeText(getApplicationContext(), "Row deleted",
Toast.LENGTH_LONG).show();
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
WHAT IS REQUIRED HERE TO UPDATE THE VIEW???
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
else
{
Toast.makeText(getApplicationContext(), "Row not deleted",
Toast.LENGTH_LONG).show();
}
}
});
layout.addView(customView);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1001)
{
if(resultCode == RESULT_OK)
{
Bundle extras = data.getExtras();
buildList(extras.getString("bname"),extras.getString("bsex"));
}
}
}
}
Hmm I'm also trying to get this to work, Have you tried looking at maybe refreshing the LinearLayout every say 5 seconds using a game loop? This may prove to be useful as it helped me with my problem http://aubykhan.wordpress.com/2010/04/25/android-game-programming-the-game-loop/
You may also be able to call onCreate(Bundle) function again while this is a terrible terrible way to do this it will work.