I'm trying to make the Google Translate App using Google jar file. I have added external .jar files called google-api-translate-java-0.97 into my android project.
My code is as following
public class MainActivity extends Activity
{
String text;
//#SuppressWarnings("deprecation")
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
TranslateV2.setHttpReferrer("http://code.google.com/p/google-api-translate-java/");
text = Translate.DEFAULT.execute("hello", Language.ENGLISH, Language.MARATHI);
Toast.makeText(MainActivity.this, text, Toast.LENGTH_LONG).show();
} catch (GoogleAPIException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Im getting errors as below:
Could not find method com.google.api.translate.TranslateV2.setHttpReferrer, referenced from method com.example.translator.MainActivity.onCreate
java.lang.VerifyError: com/example/translator/MainActivity
Please help me to make successful translator App using another method if your have any.
Related
Am new to xamarin.form having a blockage...
This is what I want to do.
I have a button in welcome.xaml in xamarin.form and I want to perform a click event but I want a method from xamarin.driod to be implemented in the click.event.
Those this makes sense to anyone?
You need to write a custom control.
So you could handle the click event in the Android or IOS.
Read here about how to create a custom controls in xamarin.forms
You could also assign a static class that exist in the shared project from your driod project.
I typically prefer using IOC and Interfaces to implement platform specific logic in the Core project, but simple way to do it may just be to set a static Action on your pages code behind.
public partial class WelcomePage
{
public static Action DroidAction { get; set; }
private void Button_OnClicked(object sender, EventArgs e)
{
if (DroidAction == null)
{
Debug.WriteLine("Action was not set");
}
DroidAction?.Invoke();
}
Your Droids OnCreate could be an option of where to do this.
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
WelcomePage.DroidAction = new Action(() => Debug.WriteLine("I a platform specific action!"));
}
I'm developing an app and I need to know when the app gets Stopped, Closed, Exited, whatever interrupts it, in order to stop some services such as WebSocket. How can I get 'access' to those events?
Thanks!
I have tested the following in a small example. (Tested it on UWP and works, the OnSleep() is called, when i close the App). The OnSleep() Method which can be overridden in the App.xaml.cs is the Method you are looking for.
The Xamarin Application LifeCycle offers some methods for your needs.
OnStart - Called when the application starts.
OnSleep - Called each time the application goes to the background.
OnResume - Called when the application is resumed, after being sent to the background.
Note that there is no method for application termination. Under normal
circumstances (ie. not a crash) application termination will happen
from the OnSleep state, without any additional notifications to your
code.
Example:
using System;
using System.Diagnostics;
using Xamarin.Forms;
namespace App1
{
public partial class App : Application
{
public App()
{
InitializeComponent();
if (Device.RuntimePlatform == Device.iOS)
MainPage = new MainPage();
else
MainPage = new NavigationPage(new MainPage());
}
protected override void OnStart() {
Debug.WriteLine("OnStart");
}
protected override void OnSleep() {
Debug.WriteLine("OnSleep");
}
protected override void OnResume() {
Debug.WriteLine("OnResume");
}
}
}
Update
According to this you have to catch unhandled exceptions in the native code. That makes it a lil complicated to shutdown your services.
Example:
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity {
protected override void OnCreate(Bundle bundle) {
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
private void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) {
//crashed by exception
}
}
Further Reading on Unhandled Exceptions: here
I've created a test app to understand how to call the native gallery, select an image and display it on an imageView.
1st test
I had a simple setup. One activity and a fragment(residing within that activity) that has a button(calls gallery intent) and an imageView for the selected image. This worked perfectly.
2nd test
Changed the setup a bit by adding another activity and a tab host to the main activity. This new empty activity would be the launching activity and it's only purpose was to switch to the main activity that holds the fragment that calls the gallery intent. Everything works fine until I select the image from the gallery. Once the image is selected, it doesn't go back to the my test app. It's like a weird soft crash. Logcat doesn't display any errors so it's a bit hard to troubleshoot. I also placed a breakpoint on each line of the OnActivityResult override but they never hit.
I was hoping somebody would shed light on this matter.
SDK version is 23 and build tools is 23.0.3
Heres what I got so far:
Main2Activity(Launching activity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ActivityConfig.CreateActivity(MainActivity.class, this, true);
}
In case you were wondering what CreateActivity does:
public static void CreateActivity(Class activityClass, Context context,
boolean finishPreviousActivity)
{
Intent intent = new Intent(context, activityClass);
if (finishPreviousActivity)
{
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
}
context.startActivity(intent);
}
MainActivity(I only use the third position on the tabhost. That's where the fragment is)
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
tabHost.setup();
tabHost.addTab(Tabs.CreateTab("T1", R.id.tabFrag2, tabHost));
tabHost.addTab(Tabs.CreateTab("T2", R.id.tabFrag3, tabHost));
tabHost.addTab(Tabs.CreateTab("T3", R.id.tabFrag, tabHost));
tabHost.setCurrentTab(2);
}
Fragment
private ImageView imageView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_blank, container, false);
Button button = (Button)view.findViewById(R.id.Btn);
imageView = (ImageView)view.findViewById(R.id.Img);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, 100);
}
});
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100)
imageView.setImageURI(data.getData());
}
After much trail an error I noticed that after selecting an image it was going back to the launching activity, but not through onCreate or it's overridden onActivityResult. It was going through onResult. My conclusion was redesign the whole interface in a way that only one activity was required
I am having two problems when trying to use "updateMessage" in a JavaFX task.
Issue #1
seems to be a known behavior, but I am not yet sure how exactly I can workaround it.This one is not (yet) critical to me.
The problem is that not all the updates I am performing in a background Task are displayed in the UI (at least the UI does not hang/freezes anymore, which was my initial issue).
My Code of the UI part:
TextArea console = new TextArea();
Button button01 = new Button("Start");
button01.setOnAction(new EventHandler() {
#Override
public void handle(Event event) {
if (ms.getState() == State.READY) {
ms.messageProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable,
String oldValue, String newValue) {
console.appendText(newValue+"\n");
}
});
ms.start();
}
}
});
My Service:
public class MyService extends Service<Object> {
#Override
protected Task createTask() {
//here we use "MyTask" first to show problem #1
MyTask ct = new MyTask();
//here we use "MyTask2" first to show problem #2
// MyTask2 ct = new MyTask2();
try {
ct.call();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("MyService end");
return ct;
}
}
My Task (#1)
public class MyTask extends Task<Object> {
#Override
public EventHandler<WorkerStateEvent> call() {
System.out.println("call() is called");
if (Thread.currentThread().getName().equals("JavaFX Application Thread")){//yes, this might not be right, but if I do not do this, my stuff is executed twice because "call()" is called twice, but the textarea area is just updated in the second run (the non javafx application thread).
return null;
} else{
//actually here I want to do some 'heavy' stuff in the background
//and many things of this heavy stuff should be displayed / logged within the UI
//but very likely (hopefully) new messages (updateMessage) will not be send as fast as in the following loop
for (int i=0;i<10000000;i++){
updateMessage("This is update number'"+i+"' from the background thread");
}
Platform.runLater(new Runnable() {
#Override
public void run() {
try{
//here is the chance to get back to the view
}finally{
}
}
});
return null;
}
}
This basically works, but not every single loop is displayed in the UI.
How do I (correctly) make sure every loop is displayed?
Screenshot: Messages are displayed but not for every loop
Issue #2
Currently blocks my attempt to bring my little text-based game into a JavaFX application.
The main problem is that I am able to call "updateMessage" from the Task directly (see above), but not from a another (sub-)class which I would need to bring all message updates from my game (each message describes the progress of the game) to the UI.
The Task I use (Task #2):
public class MyTask2 extends Task<Object> {
#Override
public EventHandler<WorkerStateEvent> call() {
// ...
UITools myTools = new UITools();
myTools.logToUITest("Just one simple message");
// ...
Platform.runLater(new Runnable() {
#Override
public void run() {
try{
//here is the chance to get back to the view
}finally{
}
}
});
return null;
}
and the (sub-)class that I want to use to do the updateMessage (actually in my little game there would be even more classes that are called during the game and almost all of them trigger an update/message).
public class UITools {
public void logToUITest(String message){
updateMessage(message);
//how to allow 'updateMessage' from the Task to be executed from here?
}
This already results in "The method updateMessage(String) is undefined...".
How could I make it possible to call the updateMessage outside of the Task itself?
updateMessage() can only be called from within the call() method of a Task. It's a constraint imposed by the design of the Task class.
The missed message updates are due to the fact that there are too many updates and not all of them are forwarded to the event queue. Try to reduce the number of updates or sleep for a little while to separate them out in time
Im using this tutorial as my base code:
http://wptrafficanalyzer.in/blog/adding-navigation-tabs-containing-listview-to-action-bar-in-pre-honeycomb-versions-using-sherlock-library/
I had a project I built that targeted 2.1 , Then I had the brillant thought " Geee it sure would be swell to have one of those handy ActionBars ive been seeing around" Soon learned id have to switch my target build to 4.1 and figure out how to use ABS so that it could still be used on older versions of Android. Once I finally figured how to get the damn Actionbar from ABS to work I discovered my old Tabhost was now depreciated so id have to look into updating that also. Now ive found this tut which is simple enough to understand, but im wondering if there is a way to use Activitys for my tabs instead of fragments? Or am I just best doing more research and figuring out how to convert my existing activitys to fragments?
public class BuhzHyve extends SherlockActivity implements ActionBar.TabListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("onCreate Method Called","WIN WIN WIN");
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.i("onTabSelected Method Called","WIN WIN WIN");
TextView text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
Include the support library in your project and bam, you can now use fragments. Just make sure you are importing the Fragment from the support lib.
import android.support.v4.app.Fragment;
http://developer.android.com/tools/extras/support-library.html