How to implement ViewSwitcher - viewswitcher

I have two XML files currently. I have my activity_main.xml and a second file called horizontal.xml I want to switch between the two such that when the phone is held vertically it displays activity_main and when the phone is held horizontally it displays horizontal.xml Thus far, I have been unsuccessful implementing the viewswitcher. Any suggestions?
package com.example.flash;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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;
}
}

You don't want to do that, as Android has an implemented way to this: Providing Resources/Alternative Resources.
In short: You put the landscape layout in layout-land, and it is switched automatically by Android for you. No need to code anything :)

Related

how to select the one suggestion randomly among the five suggestions from google search box

By using this code i can click one suggestion from all suggestions. I cannot click the suggestions randomly. I need to click one suggestion randomly and verify only the clicked the suggestion is displayed or not.
package google;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Google {
public WebDriver driver;
#Test(priority=1)
public void Firefoxaccess() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:/Users/naveenkumar.d/Downloads/geckodriver-v0.17.0-win64/geckodriver.exe");
driver=new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("n");
Thread.sleep(3000);
driver.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div[2]/div[1]/div[2]/div[2]/div[1]/div/ul/li[1]/div/div[2]")).click();
}
}
You could compose a selector based on a rand method.
For example:
r = generate random number here
and the selector should be something like:
//ul[#role='listbox']/li[r]
As for how to check that the option was selected you need to check that the search input has your text.
Text should be what you typed concatenated with the text from your random option, meaning the text from //ul[#role='listbox']/li[r]//b
And the selector for the check: //input[#name='q'][contains(#value, 'your_text')]

CodeName One error: cannot find symbol

I have created a simple sample Application with CodeNameOne without any changes.
Certificate and Provisioning file is available and configured as project properties.
IDE: Eclipse
Desktop OS: Windows
Executing 'Send IOS Debug Build' starts the server build and shows the
following error:
/var/folders/p7/d3z112yd0156kxkm2p21p8ym0000gn/T/build5327647990993852705xxx/stub/TestBuildDeployStub.java
/var/folders/p7/d3z112yd0156kxkm2p21p8ym0000gn/T/build5327647990993852705xxx/stub/TestBuildDeployStub.java:14:
error: cannot find symbol private TestBuildDeploy i;
^ symbol: class TestBuildDeploy location: class TestBuildDeployStub
/var/folders/p7/d3z112yd0156kxkm2p21p8ym0000gn/T/build5327647990993852705xxx/stub/TestBuildDeployStub.java:23:
error: cannot find symbol i = new TestBuildDeploy();
^ symbol: class TestBuildDeploy location: class TestBuildDeployStub Note:
/var/folders/p7/d3z112yd0156kxkm2p21p8ym0000gn/T/build5327647990993852705xxx/stub/TestBuildDeployStub.java
uses or overrides a deprecated API. Note: Recompile with
-Xlint:deprecation for details. 2 errors
This is the java class:
package com.canda.mario.myapp;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.ui.Toolbar;
import java.io.IOException;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class TestBuildDeploy {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World");
hi.addComponent(new Label("Hi World"));
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
}
If you change the package/class of a Codename One application after it's created you need to change it everywhere both in the project and in the codenameone_settings.properties that is why we recommend never changing it.
We don't make this process easy since you are married to life when creating a package name. This is used to uniquely identify you in the stores and can't be changed ever once an app is submitted so you need to understand that this isn't something you should do... Give package name deep consideration before creating the app!

Codename One: Background Location Listener not firing on Android Lollipop

I'm building an app that should periodically capture the user's location (I'm looking for every 60 minutes), tracking the cities they've visited.
I first started off using the foreground location listener and it was perfect, it seems to fire every few minutes but I've put checks in place so that it only actually tracks a location if enough time has passed. When I switch to other apps it looks like the foreground listener will continue to fire for a period of time, and then just stop firing, which to me makes sense since I'm thinking the OS is backgrounding the app. At which point, I would expect the background listener to have been registered and wake the app when that listener is fired.
On to my question... I'm having trouble with the background location listener. I understand that it won't fire on simulator, but it's also not firing when I build debug (using built-in certificate) to my device. For the sake of this question I've distilled what my app is doing down to barebones, based off the example listed here: https://gist.github.com/shannah/86c739edac34216d3c4d
Just to be sure I tried switching the background listener to the standard foreground one (.setLocationListener(new BackgroundListener())), and running on the simulator, I can verify that my label gets updated with appropriate data.
I also had done some testing on my actual app where I would pop a dialog in the no-arg constructor to say the listener was initialized, and another dialog when locationUpdated was called. I was able to see the popup on init, but there was no dialog on locationUpdated, which led me to believe my device was never firing it.
The device I'm testing on is a Samsung S4 with Android 5.0.1 (Lollipop).
Here is the test application I wrote which closely mimics what my actual application is doing.
bglocation.java
package com.bglocation;
import java.util.List;
import com.codename1.io.Storage;
import com.codename1.location.LocationManager;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
/**
* This file was generated by Codename One for the purpose
* of building native mobile applications using Java.
*/
public class bglocation {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World");
LocationManager.getLocationManager().setBackgroundLocationListener(BackgroundLocationListener.class);
String lastCheckin = (String)Storage.getInstance().readObject("LOCATION");
String label = "No checkins.";
if (lastCheckin != null) {
label = lastCheckin;
}
Label hiLabel = new Label("Last checkin: " + label);
hi.addComponent(hiLabel);
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
}
BackgroundLocationListener.java
package com.bglocation;
import java.util.Date;
import com.codename1.io.Storage;
import com.codename1.location.Location;
import com.codename1.location.LocationListener;
public class BackgroundLocationListener implements LocationListener {
#Override
public void locationUpdated(Location location) {
Storage.getInstance().writeObject("LOCATION", new Date().toString());
}
#Override
public void providerStateChanged(int newState) { }
}
The background listener is invoked once there is a significant location change, it is also running on a completely different process so you don't really have a UI or access to your application instance.
What you need to do to communicate with your app is firing a local notification or launching an intent or storing the location into a file or a database and once your app is launched get the data from there.

JavaFX - Playing loop video

How should I loop a video in JavaFX?
I'm trying to just play a video one time after another, so I was looking for some sample code in many places and I could'nt make it work!
This is what doesn't work for me:
public MyMediaPlayer (){
media = new Media(getVideo());
mediaPlayer = new MediaPlayer(media);
mediaView = new MediaView(mediaPlayer);
startMediaPlayer();
}
private String getVideo() {
return getClass().getResource("videos/limbo.mp4").toString();
}
public final void startMediaPlayer() {
mediaPlayer.setMute(true);
mediaPlayer.setCycleCount(javafx.scene.media.MediaPlayer.INDEFINITE); //this is the line that should do the magic, but it doesn't...
mediaPlayer.play();
}
The following works for me (video loops forever). I can't replicate your issue.
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.media.*;
import javafx.stage.Stage;
public class VideoPlayerExample extends Application {
public static void main(String[] args) throws Exception { launch(args); }
#Override public void start(final Stage stage) throws Exception {
final MediaPlayer oracleVid = new MediaPlayer(
new Media("http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv")
);
stage.setScene(new Scene(new Group(new MediaView(oracleVid)), 540, 208));
stage.show();
oracleVid.setMute(true);
oracleVid.setRate(20);
oracleVid.setCycleCount(MediaPlayer.INDEFINITE);
oracleVid.play();
}
}
I'm under Java 7, doesn't work there . . . the problem seems to be MP4 format.
If you can't play MP4 files, either:
The MP4 is not encoded in a format JavaFX understands (the JavaFX 2.2 Media javadoc details the allowed formats).
OR
You don't have appropriate codecs installed on your machine to allow the MP4 file to be decoded. See the JavaFX 2.2 Media system requirements for information on what you need to install on your machine to allow MP4 files to be displayed.

How to make a java interface that can open an image file and save it to database and resize it?

Say I have a code like this:
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.*;
public class InsertImg2Dbase extends JFrame implements ActionListener{
JButton open = new JButton("Open image to save...");
private Connection con;
public void getConnection(){
try{
if(con==null){
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
con=DriverManager.getConnection("jdbc:db2://localhost:50001/sample","username","password");
}
}catch (SQLException e){
JOptionPane.showMessageDialog(null, e.getMessage());
}catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if(source==open){
// this is where the event when opening the file to be saved will be coded
}
}
public InsertImg2Dbase(){
ActionListener al = new InsertImg2Dbase();
open.setBounds(20, 20, 175, 25);
open.addActionListener(al);
add(open);
}
public void properties(){
setLayout(null);
setTitle("Open Image To Save");
setResizable(false);
setSize(220,90);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args){
InsertImg2Dbase ins = new InsertImg2Dbase();
ins.properties();
}
}
What should I add in the code so the JButton will open an explorer to open an image file(such as jpeg, png, gif, bmp) which will be saved to my database (say my tablename is: "images") as a blob file. And can I add a function which will resize my image let's say for a dimension of 300x650 before being saved to my database?
I would welcome any kind help, I'm still in the learning process and if you could just guide me, I am more than grateful. Any help would be welcomed! Thank you
You could incorporate an external tool inside DB2 in order to extend the basic functionality and add support to specific things, like in your case images. For this, you need ImageMagik, then you create some external stored procedures and that's is.
A very good tutorial about this is here in DeveloperWorks: http://www.ibm.com/developerworks/data/library/techarticle/dm-0504stolze/
Once you have done that, you can call db2 procedures that manipulates the stored image before retrieve to the application.

Resources