How to show native alert in cocos2D android? - android

I am creating a game in cocos2D android. I need to have an alert at the end of my game.
Can I do this in cocos2D android?

I think you should do this with JNI using the showMessageBoxJNI(const char * pszMsg, const char * pszTitle) method in the MessageJni.cpp class (in /cocos2dx/platform/android/jni). Simply import MessageJni.cpp in the class where you want to add an alert:
#include "./cocos2dx/platform/android/jni/MessageJni.h" // Note: this is a relative path, take care to the beginnin of the path "./" or "././" or etc..
showMessageBoxJNI("My alert message", "My alert title"); //Add this where you want in your class
Hope this helps.

Use menu for that. I think it is better option. on doing this, you can put click even to game over.when your game s over write below code
CCMenuItemFont item6 = CCMenuItemFont.item("Game over", this, "gameover");
CCMenuItemFont.setFontSize(14);
item6.setColor( new ccColor3B(0,0,0));
CCMenu menu = CCMenu.menu(item6);
menu.alignItemsVertically();
addChild(menu);
and onclick of that menu this write below function. it will be called onclick.
public void gameover()
{
try {
CCScene scene = nextlevellayer.scene();
CCDirector.sharedDirector().pushScene(scene);
} catch (Exception e) {
e.printStackTrace();
}
}

Related

VrVideoView remove UI

I'm trying to display a 3D stereo video in a googlevr app in a clean fashion, without showing the UI. I know about usability guidelines, but the device running the app will be always kept inside a viewer in a sort of demo, so no touch interaction expected.
I'm using a VrVideoView.
So I already got rid of fullscreen button, info button, stereo mode button, google cardboard tutorial screen named "transition view" and touch tracking to move the view.
videoWidgetView.setFullscreenButtonEnabled(false);
videoWidgetView.setInfoButtonEnabled(false);
videoWidgetView.setStereoModeButtonEnabled(false);
videoWidgetView.setTransitionViewEnabled(false);
videoWidgetView.setTouchTrackingEnabled(false);
I also enabled fullscreen stereo by default.
videoWidgetView.setDisplayMode(VrWidgetView.DisplayMode.FULLSCREEN_STEREO);
But I can't remove the close button "x" and the option button.
I think that the "x" is fullscreenBackButton of VrWidgetView, parent of VrVideoView. Which hasn't methods to control its visibility.
Is there a way to remove those two buttons?
Maybe subclassing and rewriting part of the widget code?
Maybe just a little hack putting a black overlay above those corners?
I've also tried as suggested
findViewById(R.id.ui_back_button).setVisibility(GONE);
or even
findViewById(com.google.vr.widgets.common.R.id.ui_back_button).setVisibility(GONE);
without success, they give:
NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
Please check this post: Google VR Unity Divider, Settings and Back button hiding in v0.9.
VrVideoView extends VrWidgetView. There you will find a clue on how to disable the settings button in the updateButtonVisibility() method: vrUiLayer.setSettingsButtonEnabled(displayMode == 3).
Alternatively try tracing the resource ID of the buttons and do:
findViewById(R.id.ui_back_button).setVisibility(GONE);
findViewById(R.id.ui_settings_button).setVisibility(GONE);
You can also iterate on all resources and try disable one by one:
final R.drawable drawableResources = new R.drawable();
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();
for (int i = 0, max = fields.length; i < max; i++) {
final int resourceId;
try {
resourceId = fields[i].getInt(drawableResources);
} catch (Exception e) {
continue;
}
/* make use of resourceId for accessing Drawables here */
}
For me this solution works.
It uses field functionality to get vrUiLayer which is a private member of VrWidgetView which is the parent of VrVideoView.
Field f;
try {
f = this.videoWidgetView.getClass().getSuperclass().getDeclaredField("vrUiLayer");
f.setAccessible(true);
UiLayer vrLayer = (UiLayer) f.get(this.videoWidgetView);
// - here you can directly access to the UiLayer class - //
// to hide the up-right settings button
vrLayer.setSettingsButtonEnabled(false);
//setting listener to null hides the x button
vrLayer.setBackButtonListener(null);
// these visibility options are frequently updated when you play videos,
// so you have to call them often
// OR
vrLayer.setEnabled(false); // <- just hide the UI layer
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Don't forget to import the class
import com.google.vr.cardboard.UiLayer
Beware that using vrLayer.setEnabled(false) hides also the center line, leaving a complete clean vr experience.

C#, Android: Build own event listener

here is the problem. I am using a custom component from someone else called a "Fancy showcase view". It focuses on buttons in my activity on highlights them with a text as a tutorial through the app. I am starting the first message, and when the user dissmises this by clicking anywhere in the activity, the next button is supposed to be highlighted. Unfortunately, the component, which otherwise is perfect, doesnt have a listener implemented like "OnDismis" of the first tutorial view so the next could start. Just putting both into code one after the other skips the second one. It also tried working with lifecycle methods, such as OnFocuseChanged() but even after the tutorial gets dismissed, this method isnt called a second time. What would you guys say is the best way to handle this? Here is what is NOT working:
try
{
new FancyShowCaseView.Builder(this) // if this crashes, we need clean rebuild
.Title(title1)
.TitleStyle(0, (int)GravityFlags.Center | (int)GravityFlags.Center)
.Build()
.Show();
}
catch (Exception e)
{
Toast.MakeText(this, "There was an error ... " + e, ToastLength.Short).Show();
}
try
{
new FancyShowCaseView.Builder(this) // if this crashes, we need clean rebuild
.Title("TEST")
//.TitleStyle(0, (int)GravityFlags.Center | (int)GravityFlags.Center)
.FocusOn(txtL)
.Build()
.Show();
}
catch (Exception e)
{
Toast.MakeText(this, "There was an error ... " + e, ToastLength.Short).Show();
}
The second one doesnt show up. There are no event handlers and I cannot make use of lifecycle methods. Click counting wouldnt work either, since the user might click on the activity while it is loading so hard coded values arent a good option either. Any ideas?
Thanks:)
Use the FancyShowCaseQueue to control the sequence.
You add individual FancyShowCaseViews to it and when you "Show()" the queue, each FancyShowCaseView happens in the order in-which you added them to the queue.
Example:
var fancyView1 = new FancyShowCaseView.Builder(this)
.Title("StackOverflow 1")
.FocusOn(button1)
.Build();
var fancyView2 = new FancyShowCaseView.Builder(this)
.Title("StackOverflow 2")
.FocusOn(button1)
.Build();
var fancyQueue = new FancyShowCaseQueue()
.Add(fancyView1)
.Add(fancyView2);
fancyQueue.Show();
I am using a Xamarin.Android binding library of FancyShowCaseView, but you can review the Java-based examples are in the sample app in the repo, ie:AnimatedActivity.java
Also you can implement the ME.Toptas.Fancyshowcase.IDismissListener interface:
public void OnDismiss(string p0)
{
//
}
public void OnSkipped(string p0)
{
//
}
And use that implementation on each of your FancyShowCaseViews:
var fancyView2 = new FancyShowCaseView.Builder(this)
.Title("StackOverflow 2")
.FocusOn(button2)
.DismissListener(this)
.Build();

How to get the android keyboard to appear when using Qt For Android?

I've created a minimal working example of an Input box I'd like to develop using a QGraphicsItem. Here is the code (I'd figure the .h is not necessary):
TestEditor::TestEditor()
{
text = "";
boundingBox = QRectF(0,0,200,100);
}
QRectF TestEditor::boundingRect() const{
return boundingBox;
}
void TestEditor::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
painter->setBrush(QBrush(Qt::gray));
painter->drawRect(boundingBox);
painter->setBrush(QBrush(Qt::black));
painter->drawText(boundingBox,text);
}
void TestEditor::keyReleaseEvent(QKeyEvent *event){
qDebug() << "Aca toy";
text = text + event->text();
update();
}
My tester application is simply adding it to a graphics view to test it:
TestEditor *editor = new TestEditor();
editor->setText("Algo de texto como para empezar");
editor->setFlag(QGraphicsItem::ItemAcceptsInputMethod,true);
editor->setFlag(QGraphicsItem::ItemIsFocusable,true);
editor->setFlag(QGraphicsItem::ItemIsSelectable,true);
ui->gvScreen->scene()->addItem(editor);
When I test this on my PC it works fine. When I compile it for android, I get the problem that keyboard doesn't appear so I can't try it out. How can I force the keyboard to appear?
Well In case anyone is wondering I've found a way to force the android keyboard to show.
QInputMethod *keyboard = QGuiApplication::inputMethod();
keyboard->show();
I've lost the code where I used it so I don't rembember if QGuiApplication can be called from anywhere. But if it can't you can simply sotre the pointer to the keyboard from your main form/class and pass it as a parameter to any sort of required item or class

Displaying GIF image

I have a gif image (3 dots appearing and disappearing one by one) which I am trying to set on my splash screen. How to do that using just load-animation in xml? The below is the code for my splash screen.
public class MainActivity extends Activity {
private static String TAG = MainActivity.class.getName();
private static long SLEEP_TIME = 5; // Sleep for some time
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Start timer and launch main activity
IntentLauncher launcher = new IntentLauncher();
launcher.start();
}
private class IntentLauncher extends Thread {
#Override
/**
* Sleep for some time and than start new activity.
*/
public void run() {
try {
// Sleeping
Thread.sleep(SLEEP_TIME*1000);
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// Start main activity
Intent intent = new Intent(MainActivity.this, Option.class);
MainActivity.this.startActivity(intent);
MainActivity.this.finish();
}
}
}
Android doesn't natively support gifs in that way. To play a gif you can either load it in a WebView or use the Movie class.
There's a helpful Ruby script (original gist here) that will convert a gif to an animation-list xml file.
#!/usr/bin/ruby
require 'fileutils'
require 'RMagick'
require 'slop'
require 'builder'
opts = Slop.new do
banner "gif2animdraw [options]\n"
on :i, :input=, 'path to animated GIF (or directory of GIFs)', :required => true
on :o, :outdir=, 'path to root output directory', :required => true
on :d, :density=, 'density name to use for frames', :required=>true
on :oneshot, 'if set, animation does not repeat', :required => false, :default=>false
help
end
begin
opts.parse
rescue
puts opts.help
exit -1
end
if !['ldpi', 'mdpi', 'tvdpi', 'hdpi', 'xhdpi', 'xxhdpi', 'xxxhdpi'].include?(opts[:d])
puts "Invalid density #{opts[:d]}"
exit -1
end
glob=File.directory?(opts[:i]) ? File.join(opts[:i], '*.gif') : opts[:i]
Dir[glob].each do |gif|
input=Magick::ImageList.new(gif).coalesce
output=File.join(opts[:o], 'drawable')
output_density=File.join(opts[:o], 'drawable-'+opts[:d])
basename=File.basename(gif, '.gif').downcase.gsub(/\W/,'_')
FileUtils.mkdir_p output
FileUtils.mkdir_p output_density
Dir.glob(File.join(output_density, basename+'*.png')).each {|f| File.delete(f) }
input.write File.join(output_density, basename+'_%d.png')
builder = Builder::XmlMarkup.new(:indent=>2)
builder.tag!('animation-list',
'xmlns:android'=>'http://schemas.android.com/apk/res/android',
'android:oneshot'=>opts[:oneshot]) do
i=0
input.each do |frame|
builder.item('android:drawable'=>"#drawable/#{basename}_#{i}",
'android:duration'=>frame.delay*1000/input.ticks_per_second)
i+=1
end
end
open(File.join(output, basename+'.xml'), 'w') do |f|
f.puts builder.target!
end
end
I think using a GIF is a rather heavy solution to your problem. Have you thought about using a library such as WaitingDots.
The author uses a custom textview and text span to achieve the same result. This solution will provide you with must for room for customizing the animation.

Creating Gestures in android

Hi i am following this tutorial
http://www.vogella.de/articles/AndroidGestures/article.html
i want to create an application in which user can add his gesture inmy application and then use it for authentication.i know using this code i can check whether gesture entered by him is correct or not.
package de.vogella.android.gestures;
import java.util.ArrayList;
public class GestureTest extends Activity implements OnGesturePerformedListener {
private GestureLibrary gestureLib;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GestureOverlayView gestureOverlayView = new GestureOverlayView(this);
View inflate = getLayoutInflater().inflate(R.layout.main, null);
gestureOverlayView.addView(inflate);
gestureOverlayView.addOnGesturePerformedListener(this);
gestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!gestureLib.load()) {
finish();
}
setContentView(gestureOverlayView);
}
#Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = gestureLib.recognize(gesture);
for (Prediction prediction : predictions) {
if (prediction.score > 1.0) {
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT)
.show();
}
}
}
}
ok but please help me that how to add in gesture in R.raw.animate file.please suggest any way or link for adding a gesture in android app .
Extracted from here :
Android 1.6 and higher SDK platforms include a new application
pre-installed on the emulator, called Gestures Builder. You can use
this application to create a set of pre-defined gestures for your own
application...
...
As you can see, a gesture is always associated with a name. That name
is very important because it identifies each gesture within your
application. The names do not have to be unique. Actually it can be
very useful to have several gestures with the same name to increase
the precision of the recognition. Every time you add or edit a gesture
in the Gestures Builder, a file is generated on the emulator's SD
card, /sdcard/gestures. This file contains the description of all the
gestures, and you will need to package it inside your application
inside the resources directory, in /res/raw.
Here you have the source code of Gesture Builder
Gesture builder is installed in the emulator , but you can download it from here
And gesture source code examples here
Also, you may need to call gestureLib.load() before using it
Source code of gesture builder:
https://android.googlesource.com/platform/development/+/master/apps/GestureBuilder/

Categories

Resources