Android UI alignment issues when supporting kitkat - android-layout

I have been struggling to find a reliable solution to solve an issue from the past two weeks.
In our android app, we make use of a back ground image. There are some borders in this image in which we have to place different views.
The application needs to be supported from 4.0 to current latest version.
The issues I am facing are listed below.
The app when installed on devices with navigation bar looks perfect, but when run on devices without navigation bar the alignment is disturbed.
What did I try?
I tried checking if the OS version is 18 (4.3) and according to that created two layouts for each activity and installed.
The code is here
if (DEVICE_API_VERSION > 17) {
setContentView(R.layout.layoutwithnavigationbar);
} else {
setContentView(R.layout.layoutnonavigationbar);
}
But there are devices like samsung galaxy note II, which are upgradable to 4.4.2 and have no navigation bar.
Then I tried out the following proceedure.
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
boolean hasMenuKey = ViewConfiguration.get(mContext).hasPermanentMenuKey();
System.out.println("has back key . . . . "+hasBackKey);
System.out.println("has home key . . . . "+hasHomeKey);
System.out.println("has menu key . . . . " + hasMenuKey);
if(hasHomeKey || hasBackKey) {
setContentView(R.layout.layoutnonavigationbar);
} else {
setContentView(R.layout.layoutwithnavigationbar);
}
But again I had no luck.
In sony xperia sl which is 4.1.2 version of android, xhdpi, no navigation bar picks the layout perfectly, but in sony xperia m2, returns hasBackKey as true even if it is having a navigation bar and has no hardware menu buttons, and picks up layout with no navigation bar and layout looks ugly. Even tried few more things like resource id of navigation bar but in htc one s which has hardware buttons and no navigation bar, has a resource id for navigation bar. so it didn't work. I got the resource id as shown below.
int resourceId = resources.getIdentifier("navigation_bar_height",
"dimen", "android");
Did anyone have this problem?
Am I doing any thing silly or missing any thing?
What I am looking for?
1) My app should work from 4.0 to latest version.
2) If any device got navigation bar it can either be made completely transparent and use screen below that too, so that UI is not affected or any other better solution is also fine.
3) Want to know if there is any standard way to know if the device has a navigation bar or uses hardware buttons?
4) How can I make the navigation bar transparent in devices prior to Kitkat.
Note : App only is in landscape mode. Even tried transparent Navigation bar themes but they are only supported in kitkat. I am using values-hdpi,values-xhdpi, ect to get the UI aligned in different devices.
Please help me find a solution.

Finally after a lot of research I could not find a reliable solution. I don't know if it is the standard way of doing but it served my purpose. I used the answer in the stackoverflow question. get real size of screen
Got the display size of the screen as well using the following
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
if real width and display width are same it means device does not have a navigation bar, else device has a navigation bar. Accordingly I am setting appropriate layouts. I am concerned only with width as my app only runs in landscape mode.

Related

Not able to set status bar background in Xamarin.iOS

I need to change status bar UI, where background is black and text colour is white, in Xamarin.iOS (not forms).
For that I wanted to customise the status bar view, in have searched all I could on the internet and found below code working for majority of the people.
var statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
//tried with key statusBarWindow.statusBar as well.
statusBar.BackgroundColor = ...;
Above code crashes in Xamarin.iOS (tested on iPhone Xs Simulator).
Here is the log of the crash.
at (wrapper managed-to-native) ObjCRuntime.Messaging.xamarin_IntPtr_objc_msgSend_IntPtr(intptr,intptr,intptr)
at Foundation.NSObject.ValueForKey (Foundation.NSString key) [0x0001c] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.2.1.12/src/Xamarin.iOS/Foundation/NSObject.g.cs:599
Could anyone help me with any hint as to why this code crashes in Xamarin.
I tried finding the cause on this GitHub thread https://github.com/mono/Embeddinator-4000/issues/597
But here also I am not able to get any clue of it.
Also, if there is any other way to customise the status bar I would like to drop this idea and go ahead with proposed one.
From what I know this works but usually when I use this I always make a check if the selector is available or not using RespondsToSelector:
UIView statusBar = UIApplication.SharedApplication.ValueForKey(new NSString("statusBar")) as UIView;
if (statusBar.RespondsToSelector(new ObjCRuntime.Selector("setBackgroundColor:")))
{
statusBar.BackgroundColor = UIColor.White;
}
Also, do not forget to add the style to info.plist
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleBlackOpaque</string>
Update
Use the above code in AppDelegate.cs in the FinishedLaunching method.

Nokia S40 custom header bar

I'm building a custom header bar for Nokia S40, in LWUIT.
I've found 2 issues.
My Form has a BorderLayout and I've added the header bar (Container) in the NORTH. This container doesn't fill the width of the Form, as the CategoryBar does. I try with BoxLayout but I get the same result. I want that my custom header bar looks like the native CategoryBar, full width and visible over the rest of the Components.
Second issue.
I set my app to full screen using Display.getInstance().setForceFullScreen(true); and I've lost all the back Commandsof my app. Any idea?
You cannot change "headers" color if you are using Nokia S40. The color will be follow the mobile phone color theme.
The only way is using full screen, and draw ourself the Headers.
In my application it's work:
protected void createTitle(Form f) {
Container titleContainer = f.getTitleArea();
titleContainer.removeAll();
titleContainer.setLayout(new BorderLayout());
titleContainer.addComponent(BorderLayout.CENTER, titleBarContainer);
titleContainer.addComponent(BorderLayout.EAST, labelLogo);
}

UISplitViewController problems with IOS 5.1

I have a problem,
I am programming with Monotouch 5.2.8 for IOS 5.1.
But since the IOS 5.1 update my iPad configs the UISplitViewController so it is docked on the
left side instead of presented as a popover.
It works with IOS 5.0 but in 5.1 i got this problem.
Here is the source code for my UISplitViewController:
splitViewController = new UISplitViewController ();
splitViewController.WeakDelegate = detailViewController;
splitViewController.ViewControllers = new UIViewController[] {
navigationController,
detailViewController
};
From Apple's iOS 5.1 SDK release notes:
In 5.1 the UISplitViewController class adopts the sliding presentation
style when presenting the left view (previously only seen in Mail).
This style is used when presentation is initiated either by the
existing bar button item provided by the delegate methods or by a
swipe gesture within the right view. No additional API adoption is
required to obtain this behavior, and all existing API, including that
of the UIPopoverController instance provided by the delegate, will
continue to work as before. If the gesture cannot be supported in your
app, set the presentsWithGesture property of your split view
controller to NO to disable the gesture. However, disabling the
gesture is discouraged because its use preserves a consistent user
experience across all applications.
Here (login required).
UPDATE:
From what I understand on the above, we can kiss the automatic popover appearance of the master controller goodbye in iOS 5.1.
The only way I see is possible to keep the "old" appearance, is by implementing our own UIPopoverController and taking advantage of the ShouldHideViewController delegate method. Thankfully with MonoTouch, we have that method available as a property in the UISplitViewController class, making things a bit simpler.
I do get a strange behavior though. With iOS SDK 5.1 on my Mac and iOS 5.1 on my iPad; on the device, I get the "sliding" appearance, while on the simulator I get the "old", popover appearance. This is with MonoTouch 5.2.4, which is the latest stable version. Also, it does not contain a PresentsWithGesture property. I tried setting its value to false through MonoTouch.ObjCRuntime messaging, but no luck. The selector keeps returning true. So I cannot deactivate the swipe gesture.
Even tried creating my own UIPopoverController and assigning it as the master in the split controller to see what happens. Doesn't work because UIPopoverController is not a UIViewController...
Some useful info in this question, for ObjC.
Turns out you can disable the presentsWithGesture in the application delegate, but once the view controllers have been presented, there is no changing it.
I needed to disable the appearance of the left view controller during a login process, but turns out I can't enable it later.

Simple UIWebView

I'm making a tab bar app and one of the tabs is a youtube channel. It works fine and is perfect but the one problem is when i click on the video it plays but only in the standard view and not in Landscape. Any ideas on how to fix. Im sure its really simple but i've been having no luck.
This question probably answers what you are trying to do:
Webview resizes automatically to portrait and landscape view in iphone
The key thing to take away from it is that you need your view controller to tell the app that it supports the other orientations, not just portait.
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation
{
return YES;
}

Customising the title bar in Java ME

Is it possible to customise the title bar in Java ME?
You might like to check out J2ME Polish, which provides a massive amount of UI customisation for MIDlets, including the title bar: link text
The API doesn't provide functionality for customising the default title bar, but we can attempt to write our own bar. This is in itself a minor breach of UI conventions. Some phones allow us to use setTitle(null) to remove the title. The phones in the Java mobile toolkit behave this way, but the Series 40 and 60 emulators don't seem to support this and instead generates a default title. On the other hand, the Sony Ericssons and Motorolas I've tested seem to support this.
However, we can detect whether the ability to remove the titlebar is present. We do not use the sizeChanged callback as the calling of this function may be delayed when the canvas is not visible. Instead we call getHeight both before and after removing the bar. According to the spec, getHeight should always return the correct and up to date value, even when the canvas is not being displayed. Here is code for implementing the detection:
public static boolean HIDE_TITLE_ENABLED;//Whether the implementation allows us to hide the title bar
static{
//See if we can remove the title by ensuring it is non-nil, then attempting
//to remove it. If we can't, then reset it.
Canvas c=new Canvas(){
protected void paint(Graphics g){
}
};
c.setTitle("test");
int preHeight=c.getHeight();
c.setTitle(null);
int afterHeight=c.getHeight();
HIDE_TITLE_ENABLED=preHeight!=afterHeight;
}
It is also possible to hide the title bar using full screen mode, but this hides other elements as well. This method is popular in games.

Resources