codename one open app with a button - button

I am confused on how use the buttons. I open the theme designer and create the button, then on the commands i choose the action execute and go to the Source (saving before)
On the code I have this method, on the simulator do nothing and in the device neither (on the simulator start an Open file window, i am using w7)
#Override
protected boolean onMainCommand105() {
try {
Display.getInstance().execute("fb://profile/14846274855890");
} catch (Exception e) {
Dialog.show("Error!", "Failed to start. installed?", "OK", null);
e.printStackTrace();
}
return true;
}
is this the correct way?

I saw my mistake thanks to this CN1 tutorial
http://www.codenameone.com/how-do-i---handle-eventsnavigation-in-the-gui-builder--populate-the-form-from-code.html
I should use Action Event, not execute
#Override
protected void onMain_Button1Action(Component c, ActionEvent event) {
try {
Display.getInstance().execute("https://www.facebook.com/CatedralAltaPatagonia/?fref=ts");
} catch (Exception e) {
Dialog.show("Error!", "Failed to start. installed?", "OK", null);
e.printStackTrace();
}
}

Or easier :
button.addActionListener(l -> {
System.out.println("test");
});

Related

J-Unit JavaFX Tookit not Initialized

I wrote a few J-unit tests for a class I use in my JavaFX project. I am however, running into a "Tookit not initialized" again and again. I'm new to JavaFX and J-Unit testing, so I'm not sure where to look.
I edited the VM options from -ea to "--module-path C:\Users\myName\Desktop\javafx-sdk-14.0.1\lib --add-modules javafx.controls,javafx.fxml" in order to fix a No suitable pipeline found error.
This is my test class code:
package HHS_PROJ1.models;
import javafx.scene.control.Button;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.*;
class AnimalTest {
private Animal animal;
#BeforeEach
void setUp() {
animal = new Animal(5, "Goat", 32, 10, 0, new Button("bid"));
}
#Test
void setHighestOffer() {
try {
animal.setHighestOffer(8);
fail("Entering a lower offer than current highestOffer should throw an exception");
} catch (IllegalArgumentException e) {
System.out.println("Caught lower offer than current highest in setHighestOffer(): " + e.getMessage());
} catch (Exception e) {
fail("Wrong exception thrown for entering a lower offer than the current highestOffer");
}
}
#Test
void setAmount() {
try {
animal.setAmount(-2);
fail("Entering a negative number should throw an exception");
} catch (IllegalArgumentException e) {
System.out.println("Caught negative number in setAmount(): " + e.getMessage());
} catch (Exception e) {
fail("Wrong exception thrown for entering a negative number");
}
}
}
I would like to be able to run my tests, to see if my code works as intended.
I fixed the issue by creating a private JFXPanel, then adding the following bits to VM options "javafx.web,javafx.media,javafx.swing"

DialogFragment not found in fragmentTransaction

I am displaying DialogFragment from a manager. DialogFragment display multiple times.
I want to know is there a way to check from transaction whether this fragment already displaying. So don't display it.
#Override
public void show(FragmentManager manager, String tag) {
try {
FragmentTransaction ft = manager.beginTransaction();
Fragment prev = manager.findFragmentByTag(tag);
if (prev == null) {
ft.add(this, tag);
///ft.addToBackStack(tag);
ft.commitAllowingStateLoss();
}
} catch (IllegalStateException e) {
Log.d("ABSDIALOGFRAG", "Exception", e);
}
}
I am calling my Fragment like
CustomerFeedbackDialog feedbackDialog = CustomerFeedbackDialog.newInstance(genaric.getData(), type);
feedbackDialog.show(getSupportFragmentManager(), "feedbackDialog");
I have call findFragmentByTag but it is always null. I don't want to show already displayed Fragment. otherwise it duplicate . Mulitple dialogFragment opens
I know I can do it using a flag in sharedprefs
EDIT Solution found
Thanks for your help. Problem solved and I posted answer below
If your problem is the same DialogFragment over another you can try adding a variable to your Manager:
#Nullable
private DialogFragment mCurrentDialogFrag;
And then whenever you add a new DialogFragment you set mCurrentDialogFrag to the new DialogFragment and then check before adding if current DialogFragment is the same as the new one.
I finally able to handle it by overriding show method
and addToBackStack(null) and executePendingTransactions
Firstly to put tag in findFragmentByTag addToBackStack is must otherwise tag is not added in fragmentTransaction.
#Override
public void show(FragmentManager manager, String tag) {
try {
FragmentTransaction ft = manager.beginTransaction();
Fragment prev = manager.findFragmentByTag(tag);
if (prev == null) {
ft.add(this, tag);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
manager.executePendingTransactions();
}
} catch (IllegalStateException e) {
Log.d("ABSDIALOGFRAG", "Exception", e);
}
}
Now if fragment is already in transaction. It will not display again..
If I look into DialogFragment's public void show(FragmentManager manager, String tag) method implementation, I can see this:
public void show(FragmentManager manager, String tag) {
this.mDismissed = false;
this.mShownByMe = true;
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commit();
}
In order to know my dialog is already part of FragmentManager transactions, I'm simply doing this:
if (getFragmentManager().findFragmentByTag("...the tag") == null) {
fragment.show(getFragmentManager(), "...the tag");
}
I have to mention the above is strictly appcompat experience, and it works. Obviously you can put this check code inside override of show as well.

How to force to click X(close, back button) in Displaymode (3) of VrVideoView?

I am making a VR Android application using VrVideoView. I want to force "close(X, back button)" to be clicked after the video is played. That is, I want to return to MainActivity after the video finishes. Below is the codes I've tried. However, I did not go back to MainActivity after the video was finished. If you know how, please let me know.
1.
videoWidgetView.pauseVideo();
videoWidgetView.seekTo(0);
videoWidgetView.shutdown();
2.
videoWidgetView.pauseVideo();
videoWidgetView.seekTo(0);
finishActivity(0);
3.
videoWidgetView.pauseVideo();
videoWidgetView.seekTo(0);
findViewById(R.id.ui_back_button).performClick();
4.
videoWidgetView.pauseVideo();
videoWidgetView.seekTo(0);
Field f;
try {
f =videoWidgetView.getClass().getSuperclass().getDeclaredField("vrUiLayer");
f.setAccessible(true);
UiLayer vrLayer = (UiLayer) f.get(videoWidgetView);
vrLayer.getView().findViewById(R.id.ui_back_button).performClick();
}
catch (NoSuchFieldException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
Just override OnDestroy method and add these lines into it:
#Override
protected void onDestroy() {
videoWidgetView.shutdown();
super.onDestroy();
}
Then call onDestroy() method in onCompletion method:
#Override
public void onCompletion() {
videoWidgetView.seekTo(0);
videoWidgetView.pauseVideo();
onDestroy();
}
Hope it works :)

How to add a set as wallpaper button

Hello everyone I am creating an online wallpaper app.Where users can access wallpaper online.i want add two button first one is set as wallpaper and second one is download button .So if anybody help me about this I will be thankful to them thanks.
public class GalleryDetailActivity extends ActionBarActivity {
public static final String EXTRA_IMAGE = "extra_image";
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mImageView = (ImageView) findViewById(R.id.image);
if (getIntent() != null && getIntent().getExtras() != null) {
if (getIntent().getExtras().containsKey(EXTRA_IMAGE)) {
Picasso.with(this).load(getIntent().getExtras().getString(EXTRA_IMAGE)).into(mImageView);
}
}
First of all, you should make sure you have permission to make such action. Add in your manifest:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
To download the image hosted in some site you could use a 'doInBackground Thread' as suggested below:
How to download and save an image in Android
The button to set the wallpaper is created by code below:
Button setWallpaper = (Button)findViewById(R.id.YOUR_BUTTON);
ImageView imagePreview = (ImageView)findViewById(R.id.YOUR_PREVIEW);
imagePreview.setImageResource(YOUR_IMAGE_RESOURCE);
setWallpaper.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(YOUR_IMAGE_RESOURCE);
} catch (IOException e) {
e.printStackTrace();
}
}});
YOUR_IMAGE could be a local resource, like:
R.drawable.myImageFile
The link in the answer there are several ways of how to download your online image. Please, check it and try set up your wallpaper with local images first.
File f = new File(Environment.getExternalStorageDirectory(), "yourfile.jpg");
String path = f.getAbsolutePath();
File jpg = new File(path);
if(jpg.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(path);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
WallpaperManager m=WallpaperManager.getInstance(this);
try {
m.setBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
}
}

JavaFX Converting text to hyperlinks in TableView after loading data from xml with JAXB

Please advise is there a way to update tableview and set simple text url to hyperlink in a table column?
Thanks a lot.
Thanks for the tip I tried and it worked! after that I also tried to add XML adapter in the JAXB and it also worked.
public class XMLHyperLinkAdapter extends XmlAdapter<String, Hyperlink> {
#Override
public Hyperlink unmarshal(String v) {
try{
Hyperlink link = new Hyperlink();
link.setText(v);
link.setOnAction((ActionEvent e) -> {
link.setVisited(false);
System.out.println("This link is clicked: " + link.getText());
});
return link;
}catch (Exception e)
{
e.printStackTrace();
return null;
}
}
#Override
public String marshal(Hyperlink v) throws Exception {
return v.getText();
}
}

Resources