NAVIGATION_MODE_TABS and ViewPager not working properly in landscape orientation - android-actionbar

I have an activity which contains a viewpager to swipe across 4 fragments.
I'm also using ActionBar.NAVIGATION_MODE_TABS to display the tabs name and allow navigating by pressing on on the tab as well as swiping
public void onCreate(Bundle savedInstanceState) {
...
this.viewPager = (ViewPager) findViewById(R.id.viewPager);
this.viewPagerAdapter = new MyViewPagerAdapter(this, getSupportFragmentManager());
this.viewPager.setAdapter(this.viewPagerAdapter);
this.viewPager.setCurrentItem(0);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Create a tab listener that is called when the user changes tabs.
final TabListener tabListener = new TabListener() {
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction arg1) {
viewPager.setCurrentItem(tab.getPosition());
}
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
}
};
// add the tabs
this.viewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between pages, select the corresponding tab.
actionBar.setSelectedNavigationItem(position);
}
});
This works fine, but when the device is in landscape orientation, the tabs are displayed as an action spinner, then the spinner content doesn't refresh anymore when swiping in the viewpager even though onPageSelected() is called with the correct position
Any idea how to fix this ?
Thanks

For now I will use the following workaround, but I'm still interested in finding a real solution.
The workaround is to force the display of the tabs even in landscape by setting the navigation mode after the tabs have been added as seen here

Related

Text box hint and text invisible in authentication UI in Cognito

When I start up the sign in activity for my app, it comes up with what should appear, with the exception of the text boxes, which have no visible hints. Also, when the user types into the text boxes, the text is invisible (probably same color as the background of the text box]
This is what happens
Heres my onCreate():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Start the authentication UI
AuthUIConfiguration config = new AuthUIConfiguration.Builder()
.userPools(true)
.backgroundColor(Color.rgb(0x36, 0x89, 0x56))
.isBackgroundColorFullScreen(true)
.build();
SignInActivity.startSignInActivity(this, config);
LoginActivity.this.finish();
final IdentityManager identityManager = AWSProvider.getInstance().getIdentityManager();
// Set up the callbacks to handle the authentication response
identityManager.login(this, new DefaultSignInResultHandler() {
#Override
public void onSuccess(Activity activity, IdentityProvider identityProvider) {
Toast.makeText(LoginActivity.this,
String.format("Logged in as %s", identityManager.getCachedUserID()),
Toast.LENGTH_LONG).show();
// Go to the main activity
final Intent intent = new Intent(activity, RangingActivity.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);
activity.finish();
}
#Override
public boolean onCancel(Activity activity) {
return false;
}
});
}
And here are the resources I followed to make the activity:
https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-user-sign-in.html
https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-user-sign-in-customize.html
Thanks

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.

java.lang.NoClassDefFoundError: com.google.android.glass.app.Card

I am trying to create a sample glass application using below tools -
GDK Preview API 19
Java 6
Android 4.4.2
I have added an activity as below -
public class HelloWorldActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context con = this;
Card myCard = new Card(con);
myCard.setText("Hello, World!");
myCard.setFootnote("First Glassware for Glass");
View cardView = myCard.getView();
setContentView(cardView);
//setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
It gives an error for Card class not found, I have added the gdk.jar(21 KB) but still it throws an error. Please advise
Also I have checked the Card class code and its a stub class and does not contain implementation. Could you please provide correct gdk jar with correct implementation ?
If you are trying to make a simple HelloWorld app, you can follow THIS
I got it to work just today just by doing so. Please see my answer HERE

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