CardBuilder show more text - google-glass

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.

Related

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

Receiving touch events on CardScrollView

I have a CardScrollView that has multiple items in it and I would like to be able to pull up a menu on an item, similar to the built in Timeline.
I know Card cannot have a specific menu attached to it so I have the menu prepared at the Activity level.
However, something seems to be swallowing all onKeyDown events.
public class HostsView extends CardScrollView {
private String TAG = "HostsView";
private HostsCardScrollAdapter cards;
private Activity parent;
public HostsView(Activity parent, HostDatabase hostDb) {
super(parent);
cards = new HostsCardScrollAdapter(parent);
//populates the cards and what not
this.setAdapter(cards);
this.activate();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//I never see this log
Log.d(TAG, "Key event " + event.toString());
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
parent.openOptionsMenu();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
If you only need to handle a simple tap on a card in a CardScrollView, you can call setOnItemClickListener to attach an AdapterView.OnItemClickListener, just as you would with a standard Android ListView. This is typically much simpler than working with GestureDetector for this basic use case.
yesterday I came across the same problem. I solved it with a GestureDetector, as the GDK documentation recommends. Here is the code I used:
private GestureDetector mGestureDetector;
#Override
public void onCreate(Bundle savedInstanceState) {
mGestureDetector = createGestureDetector(this);
}
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
#Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.LONG_PRESS || gesture == Gesture.TAP) {
Log.d(MainActivity.TAG, "Tap"); //When I tap the touch panel, I only get LONG_PRESS
openOptionsMenu();
return true;
} else if (gesture == Gesture.TWO_TAP) {
return true;
} else if (gesture == Gesture.SWIPE_RIGHT) {
return true;
} else if (gesture == Gesture.SWIPE_LEFT) {
return true;
}
return false;
}
});
return gestureDetector;
}
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}
Your menu should open now!

Google Glass GDK Tap Gesture

With GDK and sample code provide by Google the TAP gesture is not being recognized as a TAP. Is returned as LONG_PRESS everytime. Below is the code:
import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;
public class MainActivity extends Activity {
Logger log = Logger.getLogger("MainActivity");
private GestureDetector mGestureDetector;
// ...
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
mGestureDetector = createGestureDetector(this);
}
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
//Create a base listener for generic gestures
gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
#Override
public boolean onGesture(Gesture gesture) {
log.info(gesture.name());
if (gesture == Gesture.TAP) {
// do something on tap
return true;
} else if (gesture == Gesture.TWO_TAP) {
// do something on two finger tap
return true;
} else if (gesture == Gesture.SWIPE_RIGHT) {
// do something on right (forward) swipe
return true;
} else if (gesture == Gesture.SWIPE_LEFT) {
// do something on left (backwards) swipe
return true;
}
return false;
}
});
gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
#Override
public void onFingerCountChanged(int previousCount, int currentCount) {
// do something on finger count changes
}
});
gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
#Override
public boolean onScroll(float displacement, float delta, float velocity) {
// do something on scrolling
}
});
return gestureDetector;
}
/*
* Send generic motion events to the gesture detector
*/
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}
}
Am I missing something here or is this a bug?
If you're only looking to capture tap events for a UI (without using GestureDetector and everything), in Glass touchpad taps are registered as center clicking a d-pad, so you can simply intercept the KEYCODE_DPAD_CENTER key presses.
Try this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
// The touchpad was tapped
return true;
}
return false;
}
I had the same problem as you and my answer could be strange but I avoided it using a switch instead of the if else structure. Moreover with this new code you will be able to capture the rest of the gestures.
I hope it will help you as well.
private GestureDetector createGestureDetector(Context context){
GestureDetector gestureDetector = new GestureDetector(context);
//Create a base listener for generic gestures
gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
#Override
public boolean onGesture(Gesture gesture) {
Log.e(TAG,"gesture = " + gesture);
switch (gesture) {
case TAP:
Log.e(TAG,"TAP called.");
handleGestureTap();
break;
case LONG_PRESS:
Log.e(TAG,"LONG_PRESS called.");
return true;
case SWIPE_DOWN:
Log.e(TAG,"SWIPE_DOWN called.");
return true;
case SWIPE_LEFT:
Log.e(TAG,"SWIPE_LEFT called.");
return true;
case SWIPE_RIGHT:
Log.e(TAG,"SWIPE_RIGHT called.");
return true;
case SWIPE_UP:
Log.e(TAG,"SWIPE_UP called.");
return true;
case THREE_LONG_PRESS:
Log.e(TAG,"THREE_LONG_PRESS called.");
return true;
case THREE_TAP:
Log.e(TAG,"THREE_TAP called.");
return true;
case TWO_LONG_PRESS:
Log.e(TAG,"TWO_LONG_PRESS called.");
return true;
case TWO_SWIPE_DOWN:
Log.e(TAG,"TWO_SWIPE_DOWN called.");
return true;
case TWO_SWIPE_LEFT:
Log.e(TAG,"TWO_SWIPE_LEFT called.");
return true;
case TWO_SWIPE_RIGHT:
Log.e(TAG,"TWO_SWIPE_RIGHT called.");
return true;
case TWO_SWIPE_UP:
Log.e(TAG,"TWO_SWIPE_UP called.");
return true;
case TWO_TAP:
Log.e(TAG,"TWO_TAP called.");
return true;
}
return false;
}
});
gestureDetector.setFingerListener(new com.google.android.glass.touchpad.GestureDetector.FingerListener() {
#Override
public void onFingerCountChanged(int previousCount, int currentCount) {
// do something on finger count changes
Log.e(TAG,"onFingerCountChanged()");
}
});
gestureDetector.setScrollListener(new com.google.android.glass.touchpad.GestureDetector.ScrollListener() {
#Override
public boolean onScroll(float displacement, float delta, float velocity) {
// do something on scrolling
Log.e(TAG,"onScroll()");
return false;
}
});
return gestureDetector;
}
Copying and pasting the GestureDetector code from the GDK and modifying it is all you should need to do. If it's working for double tap then I'd suspect you may have some hardware issue with Glass.
Have you tried doing a Toast for Gesture.TAP? Perhaps TAP and LONG PRESS are the same?
The code below will call generateCard() when you tap Glass.
private GestureDetector createGestureDetector(Context context) {
GestureDetector gestureDetector = new GestureDetector(context);
//Create a base listener for generic gestures
gestureDetector.setBaseListener( new GestureDetector.BaseListener() {
#Override
public boolean onGesture(Gesture gesture) {
if (gesture == Gesture.TAP) { // On Tap, generate a new number
generateCard();
return true;
} else if (gesture == Gesture.TWO_TAP) {
// do something on two finger tap
return true;
} else if (gesture == Gesture.SWIPE_RIGHT) {
// do something on right (forward) swipe
return true;
} else if (gesture == Gesture.SWIPE_LEFT) {
// do something on left (backwards) swipe
return true;
}
return false;
}
});
gestureDetector.setFingerListener(new GestureDetector.FingerListener() {
#Override
public void onFingerCountChanged(int previousCount, int currentCount) {
// do something on finger count changes
}
});
gestureDetector.setScrollListener(new GestureDetector.ScrollListener() {
#Override
public boolean onScroll(float displacement, float delta, float velocity) {
// do something on scrolling
return false;
}
});
return gestureDetector;
}
/*
* Send generic motion events to the gesture detector
*/
#Override
public boolean onGenericMotionEvent(MotionEvent event) {
if (mGestureDetector != null) {
return mGestureDetector.onMotionEvent(event);
}
return false;
}

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.