I have made a simple app using Android Studio with bottom navigation. It all works well until I add code to the Fragments. Where is the place I need to add code in this example?
This part is written in the activity of a fragment. I have placed the variables in Public Class, and wanted to write the code in "public void onCreate", but the app crashed if I try to write anything there (even the most basic like declaring a variable).
My question is: in which part I should write the code? I am trying to put a simple Timer on this Fragment.
Hope my question is clear this time, first time posting here.
package com.example.testfitnessapp;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class TrainingFragment extends Fragment {
Button start, pause, reset;
View textView;
Handler handler;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.fragment_training, container, false);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// do your variables initialisations here except Views!!!
}
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
// initialise your views
}
}
Related
I'm trying to add an ad to my app.
Following this guide, I've ran into trouble with "Initialize the Mobile Ads SDK" for it wants me to import some Java code:
package ...
import ...
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
}
}
and I've got a Javascript project:
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import React from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createStackNavigator } from '#react-navigation/stack';
import Home from "./screens/home"
import heads from "./screens/heads"
import tails from "./screens/tails"
//problems start from here. This is the copied text from https://developers.google.com/admob/android/quick-start?hl=es#import_the_mobile_ads_sdk
package ...
import ...
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
}
}
//this is where they end
const Stack = createStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Home">
<Stack.Screen
name="Home"
component={Home}
options={{headerShown: false}} />
<Stack.Screen
name="tails"
component={tails}
options={{headerShown: false}} />
<Stack.Screen
name="heads"
component={heads}
options={{headerShown: false}} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
const Stack = createStackNavigator();
Errors
This is the output when trying to access the app:
App.js: Unexpected reserved word 'package' (10:0)
8 |
9 | //problems start from here. This is the copied text from https://developers.google.com/admob/android/quick-start?hl=es#import_the_mobile_ads_sdk
> 10 | package ...
| ^
11 | import ...
12 | import com.google.android.gms.ads.MobileAds;
13 | import com.google.android.gms.ads.initialization.InitializationStatus;
I'm not sure if I should be converting JAVA into JAVASCRIPT for this purpose in particular. I've found nothing on the internet around it, and there's no "translator" for programming languages as far as I know.
I'm clueless. I've already tried the same thing with KOTLIN but it failed as well. I don't want to add the KOTLIN code and the errors because it says that my post is mostly code (and it certainly is, but I don't know towards which direction to go from here)
Thank you!
Ok so it looks as though you're trying to put Java and JavaScript code in the same file, which won't work.
In your included javascript project snippet, this whole section:
//problems start from here. This is the copied text from https://developers.google.com/admob/android/quick-start?hl=es#import_the_mobile_ads_sdk
package ...
import ...
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
#Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
}
}
//this is where they end
Would need to be in a separate Java file.
Now, I see you're using react-native. I would need to know if you're using standard react-native or an expo managed project.
If you are using an expo managed project, there is already a package made for admob, just follow the instructions in the documentation here: https://docs.expo.io/versions/v39.0.0/sdk/admob/
If you are just using react native, it gets quite a bit more complicated. The docs that you provided are intended for Java / Kotlin native development opposed to native development through react, so it will be hard for you to get it working using the code from that document. This link should help you get it working for react-native: https://dev.to/srajesh636/how-to-show-ads-in-react-native-lcj
*note that in the included docs provided by google, where you see package ... and import ..., you're expected to fill in your actual package name, and any other imports you need for the class.
Hopefully this has provided some insight on your problem, let me know if there are still any issues.
package com.example.video_app;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity {
Button clk;
VideoView Videov;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clk=(Button)findViewById(R.id.button);
Videov=(VideoView)findViewById(R.id.videoView);
}
public void videoPlay(View v){
String videoPath="android.resource:// com.example.video_app/"+R.raw.sv;//video path
Uri uri= Uri.parse(videoPath);
Videov.setVideoURI(uri);
Videov.start();//start
}
}
I am new to android studio trying to build a simple app of video playing but the video is not playing cant get where the problem is.The video is not playing on emulator nor on my phone. i have tried all the codes mentioned in stackoverflow but could not find any solution... help to resolve this problem
Check out OnPreparedListener. Call start() from inside that event, since then the file will be ready to be played.
You haven't called videoPlay() method from any of the method,
Call videoPlay() method from onCreate() after findViewById() of video view.
and remove View v from videoPlay() method,i think it's useless.
I have an app with the following code on the MainActivity.java:
package com.MyApp;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
import android.view.Window;
import android.view.WindowManager;
import android.os.Bundle;
import com.MyApp.CustomLockscreenPackage;
public class MainActivity extends ReactActivity {
#Override
protected String getMainComponentName() {
return "MyApp";
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.setFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
}
#Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
#Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
}
The lines who make the app work on the lockscreen are:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window w = getWindow();
w.setFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
);
}
But, the entire app works on the lockscreen, and i need to make only certain screens works on the lockscreen (It's a call app).
I tried creating a native module but don't worked.
i know its possible remove the flags with:
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
So, if i can call these lines on my react native screen, it probably can work
[EDIT]: I made a module who does that for me: react-native-keep-awake
[EDIT]: How to use my module:
first of all, install him:
npm install --save https://github.com/willnaoosmit/react-native-keep-visible
(or with yarn, if you want to)
then:
import KeepAwake from 'react-native-keep-awake'; #import the module, of course
KeepAwake.activate(); #when you want to use the app on the lockscreen
KeepAwake.deactivate(); #when you dont want to use the app on the lockscreen anymore
I am using the free map service: 'http://www.opentouchmap.org/' to view various buildings in an establishment using a WebView object.
My question is, is there a GET-parameter or script I can use to create a border so that the user cannot E.g."Go to China and back"?
Here is my code so far:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MapActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
WebView webview = new WebView(this);
setContentView(webview);
webview.setWebViewClient(new WebViewClient());
//No need to worry about security
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.opentouchmap.org/? lat=latitude&lon=longitude&zoom=15");
//TODO: what to do here??
webview.evaluateJavascript("",null);
}
}
Hi i'm setting up my first webview app in android and i try to make my javascript code calls the android code according to this tutorial :
http://android.programmerguru.com/binding-javascript-and-android-code-example/
What i'm trying exactly to do is to play a sound on an onclick event like in my website but it's pretty slow and i read many post on it and i understood why i should use another way to do that.
As i found the tutorial i put in the previous link, i tried to make it work in this way but nothing happened. I create "raw" folder and put in it "click.mp3" file.
I'm probably forgetting something but i don't find it...
MyActivity.java files:
package com.example.mywebviewapp;
import android.media.MediaPlayer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
public class MyActivity extends Activity {
private WebView mWebView;
MediaPlayer mysound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//Inject WebAppInterface methods into Web page by having Interface name 'Android'
mWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
mWebView.loadUrl("http://mywebsite.com");
}
public class WebAppInterface{
public void soundAndroClick(){
mysound = MediaPlayer.create(MyActivity.this, R.raw.click);
mysound.start();
}
}
}
and i call the method soundAndroClick() in my main.js file like that:
function soundAndroid(){
Android.soundAndroClick();
}