I am totally new to android development basically I am a web developer, I done a Udemy course about converting a wordpress site to android webview app. Which I followed and made a app by just copying the code from files. Now when ever I open the app I see a white screen which sometimes give feeling that app is not working but the app is loading web page. Now I would like to add a loading image or loader any thing.
Here is my code of mainActivity.java
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of
//WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
another file is myAppWebViewClient.java
public class MyAppWebViewClient extends WebViewClient
{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
and xml file is as below AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bikanershop.bikanershop" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission
android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change
"com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" in the
lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission android:protectionLevel="signature"
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<uses-permission
android:name="com.bikanershop.bikanershop.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action
android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!--
IMPORTANT: Change "com.parse.tutorials.pushnotifications" to
match your app's package name.
-->
<category android:name="com.bikanershop.bikanershop" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
I am using android studio latest version that is 1.1.0
Use a ProgressDialog, as outlined Here.
For your case, since you need to close the dialog, you should just in-line your functionality from myAppWebViewClient.java.
So your new code would be something like this:
public class MainActivity extends ActionBarActivity {
private ProgressDialog dialog = new ProgressDialog(WebActivity.this);
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(Uri.parse(url).getHost().endsWith("bikanershop.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
});
dialog.setMessage("Loading..Please wait.");
dialog.setCanceledOnTouchOutside(false);
dialog.show();
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://bikanershop.com/");
// Force links and redirects to open in the WebView instead of in a browser
//mWebView.setWebViewClient(new WebViewClient()); //replaced with code above
// Stop local links and redirects from opening in browser instead of
//WebView
//mWebView.setWebViewClient(new MyAppWebViewClient());
Parse.initialize(this, "oI7lWjyQc0EhpDsn1cyfaoCtpUbKQp1rFbX6PPZN", "FIeGPOxqKe3jBuvXKjW4Ml69K12tjDRq6sLruqUQ");
ParseInstallation.getCurrentInstallation().saveInBackground();
}
Related
I am creating an application with webview (an application in which a website is displayed).
Currently, if I click the back button, the app closes the application
What I am looking for is not that a button with an arrow appears in the action bar and it takes me back, because I have eliminated that bar, what I want is to be able to use the buttons on the cell phone.
I hope you can help me, here the code of the MainActivity.java and the AndroidManifiest.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
private View decorView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = findViewById(R.id.mywebview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new myChrome());
webView.loadUrl("https://mywebview.com/");
decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if (visibility == 0)
decorView.setSystemUiVisibility(hideSystemBars());
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
decorView.setSystemUiVisibility(hideSystemBars());
}
}
AndroidManifiest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Dessert.decov">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.DESSERT">
<activity
android:name=".MainActivity"
android:exported="true"
android:configChanges="screenSize|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm programming an android app with Android Studio where Imagebutton opens a new screen so that the user can proceed to the next step. Imagebutton is working and new screen opens, but it isn't opening second activity but the same one.
I'm new to android and java programming, so please tell me if other codes are necessary for solving the problem and sorry if I shared unnecessary code.
I have created startActivity with intent.
Here are my current codes of MainActivity java-file and AndroidManifest xml-file:
MainActivity.java:
public class MainActivity extends AppCompatActivity {
TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
mTextMessage = findViewById(R.id.message);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
final ImageButton imgButton = findViewById(R.id.simpleImageViewHowdoglearnspackage);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondActivity.class));
}
});
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobidogi">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SecondActivity"
android:label="#string/title_activity_second" />
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I just created a Interstitial add in my test app. Went fairly easy, but now I want to add one to my main application and I cannot seem to load it.
This is what my android launcher looks like:
../
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new GameOfClaws(this), config);
//initialize ads...
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad));
//Load add
requestNewInterstitial();
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
requestNewInterstitial();
}
#Override
public void onAdLoaded()
{
Gdx.app.log(TAG, "Ad loaded!");
//interstitialAd.show();
}
});
}
private void requestNewInterstitial()
{
AdRequest adRequest = new AdRequest.Builder()
.build();
interstitialAd.loadAd(adRequest);
}
#Override
public void displayInterstitialAd() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
else
{
Gdx.app.log("MainActivity", "Add not loaded!");
}
}
});
}
/..
I simply call a interface method in the show method of my menuScreen to run the same method in the AndroidLauncher.
resolver.displayInterstitialAd();
It's basically the same as my working test app but there I load the interstitial by pressing a button.
This is my manifest:
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="#string/app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name="com.madmenyo.gameofclaws.android.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
The only things that are different is that my test app is in portrait mode and it has been uploaded a while ago but remains as a testing app and not published. My current app has just been uploaded for testing these ads and the play store. The play store works fine. I also changed the ad ID to the correct ID I got on the Admob website.
Add the following in manifest file
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent" >
</activity>
I have created a sample android application for receiving push messages from Parse. I had followed this video tutorial. The application is working fine and I am able to receive push notifications.
The classes required are as follows:
ParseApplication class:
public class ParseApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// Add your initialization code here
Parse.initialize(this, "ygdC4y3uzCe0TOEPOfB1U469Gg5ZJGxF2OGlNKCG", "Tk5ZlDiWWVJm2Xio5IiXk0KI5JM1jrvGPOSpFxzE");
PushService.setDefaultPushCallback(ParseApplication.this, SampleClass.class);
ParseUser.enableAutomaticUser();
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
}
}
ParseStarterProjectActivity class:
public class ParseStarterProjectActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ParseAnalytics.trackAppOpened(getIntent());
}
}
SampleClass class:
public class SampleClass extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_class);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sample_class, menu);
return true;
}
}
The manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parse.starter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<!--
IMPORTANT: Change "com.parse.starter.permission.C2D_MESSAGE" in the lines below
to match your app's package name + ".permission.C2D_MESSAGE".
-->
<permission
android:name="com.parse.starter.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.parse.starter.permission.C2D_MESSAGE" />
<application
android:name="com.parse.starter.ParseApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.parse.starter.ParseStarterProjectActivity"
android:label="Test Push" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.parse.starter.MainActivity"
android:label="Test Push" >
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver
android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<!-- IMPORTANT: Change "com.parse.starter" to match your app's package name. -->
<category android:name="com.parse.starter" />
</intent-filter>
</receiver>
<activity
android:name="com.parse.starter.SampleClass"
android:label="#string/title_activity_sample_class" >
</activity>
</application>
</manifest>
Now I want to get text from the push notifications when the user receives them. What should I do to get the text from the notifications?
This is the url to the push notification server.
I even went though this.
I had to change the SampleClass Activity like this:
public class SampleClass extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_class);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonData = extras.getString( "com.parse.Data" );
Log.i("Data Received:", jsonData);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sample_class, menu);
return true;
}
The rest is as it is. I referred this link.
I have the following code, but when I run it, it tells me that "http://www.google.com" is unavailable, which is total rubbish. If I run the code without the whole HelloWebViewClient class, it opens google properly, except in the pre-installed Android browser. I want to open it in-app. Thanks.
My Code:
public class MainActivity extends Activity {
Button btnGo;
Editable guiNumber;
WebView webView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGo = (Button) findViewById(R.id.btnGo);
webView = (WebView) findViewById(R.id.viewWeb);
webView.setVisibility(View.GONE);
webView.setWebViewClient(new HelloWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
btnGo.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
guiNumber = enterGUI.getText();
if (guiNumber.length() == 8) {
guiNumber.replace(4, 8, "");
gui = Integer.parseInt(enterGUI.getText().toString());
System.out.println(gui);
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
webView.setVisibility(View.VISIBLE);
webView.requestFocus();
webView.loadUrl("http://google.com");
}
else {
enterGUI.setError("Your GUI/ILGU number must be 8 digits long");
}
}
});
}
private class HelloWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
And the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.frostplant.clublink" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="6" android:targetSdkVersion="15"/>
<uses-permisssion android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Give your application the permission for accessing internet: android.permission.INTERNET This will allow your application to use network.
You have mistakenly typed 3 "s"s in your permission line:
<uses-permisssion android:name="android.permission.INTERNET"/>
This should be:
<uses-permission android:name="android.permission.INTERNET"/>