Loading local html-page into webview? - G1 Q&A, Help & Troubleshooting

I have a large set of html-pages (css-formatted, + some javascript) that I want saved inside the project so the user dont have to go online to browse for them from the app.
1) My first question is how do I open a a local html-page (named, for instance "myWebPage.html") in a WebView?
I know I can make a WebView load a html-string with the following code:
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<WebView android:id="@+id/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
WebKitTest.java
Code:
package com.androidspanishcourse.WebKitTest;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebKitTest extends Activity {
// Declare webview
WebView browser;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ?
browser=(WebView)findViewById(R.id.webkit);
// Loads html-string into webview
browser.loadData("<html><body>Hi dude<br><br>Hi again</body></html>", "text/html", "UTF-8");
}
}
But I dont know how to load a specific local html-page.
2) My second question is: where do I put the html-pages? I understand that resources are generally put in the /res/-folder of the project, but do I put them in a specific sub-folder of the res-folder?

http://developer.android.com/reference/android/webkit/WebView.html
1.Webview.loadUrl(String url)
2.The prefix "file:///android_asset/" will cause WebView to load content from the current application's 'assets' folder. For example, a myimage.gif file in the /assets folder can be used in the html image tag as:
<img src="file:///android_asset/myimage.gif">. Same applies to html file.

Thanks a lot, waacow
Also, apparently you need to add "browser.getSettings().setJavaScriptEnabled(true);" for the webview to be able to handle javascript.

Display HTML file from assets folder
Hello,
Just save your HTML file in your assets folder and then load it from assets to webview.
MainActivity.java
package com.deepshikha.htmlfromassets;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webview;
ProgressDialog progressDialog;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init(){
webview = (WebView)findViewById(R.id.webview);
webview.loadUrl("file:///android_asset/download.html");
webview.requestFocus();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading");
progressDialog.setCancelable(false);
progressDialog.show();
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
try {
progressDialog.dismiss();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Thanks!

Related

[Q] return a modified copy of original method

Hey guys,
I've just started experimenting with some coding, picked up a few tutorials and made some stuff work.
Now i'm stuck at a point where i want to return a modded version of a method which will replace the original method.
This is what i have so far.
Code:
package mjxl.xposed.cm11;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
public class CM11notifications implements IXposedHookLoadPackage {
@Override
public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
if (lpparam.packageName.equals("com.android.systemui")) {
ClassLoader classLoader = lpparam.classLoader;
XC_MethodReplacement methodreplacer = new XC_MethodReplacement() {
protected Object replaceHookedMethod(
XC_MethodHook.MethodHookParam paramAnonymousMethodHookParam)
throws Throwable {
return *WHAT DO I PUT HERE?? *;
}
};
XposedHelpers.findAndHookMethod("com.android.systemui.MmsConfig", classLoader, "applyLegacyRowBackground", methodreplacer);
}
}
}
I want to return a copy of "applyLegacyRowBackground" with some edits, so it will take my layouts.
Now i know i have to return it where "Integer.valueOf(255); is, but how to ?
Can anyone guide me ?
Here's the method which i want to place there.
Code:
protected void applyLegacyRowBackground(StatusBarNotification sbn, View content) {
if (sbn.getNotification().contentView.getLayoutId() !=
R.layout.notification_template_base_mjxl) {
int version = 0;
try {
ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(sbn.getPackageName(), 0);
version = info.targetSdkVersion;
} catch (NameNotFoundException ex) {
Log.e(TAG, "Failed looking up ApplicationInfo for " + sbn.getPackageName(), ex);
}
if (version > 0 && version < Build.VERSION_CODES.GINGERBREAD) {
content.setBackgroundResource(R.drawable.notification_row_legacy_bg_mjxl);
} else {
content.setBackgroundResource(R.drawable.notification_bg_mjxl);
}
}
}
Since the method you're replacing is void (it returns nothing): `return None;`.
PS. You're trying to hook `applyLegacyRowBackground()` instead of `applyLegacyRowBackground(StatusBarNotification, View)`. You should pass the arguments' classes to findAndHookMethod(…), for example:
Code:
XposedHelpers.findAndHookMethod("com.android.systemui.MmsConfig", classLoader, "applyLegacyRowBackground", StatusBarNotification.class, View.class, methodreplacer);
If you can't access the class directly (because it's not in the SDK, for example), pass its full class name as a string instead (e.g. "android.view.View" instead of `View.class`).

Help with Android Studio App Development MAC

Hi everybody,
Im in need of help, Ive been attempting to create an qoute application and ive been coming up with an error when i try to run it.
Here is the error report i get.
Information:Gradle: Executing tasks: [assemble]
Information:24/03/2016, 18:33 - Compilation completed with 2 errors and 0 warnings in 8s 478ms
Error:Gradle: Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
/Users/David/AndroidStudioProjects/David'sMotivation2/app/src/main/java/com/example/david/davidsmotivation/Splash.java
Error: (16, 9) Gradle: error: reached end of file while parsing.
here is the code where it shows that there is an bug/error.
package com.example.david.davidsmotivation;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Splash extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Papyrus.ttc");
TextView myTextview = (TextView) findViewById(R.id.textView1);
myTextview.setTypeface(myTypeface);
{
Im sorry if this isn't clear, please say if i should include more information.:good:
are u sure its is Papyrus.ttc ? usualy typefaces ends with .ttf extension
Sent from my Nexus 4 using Tapatalk
Did you post the complete Splash.java file. Because the code you posted is incomplete
Code:
package com.example.david.davidsmotivation;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Splash extends AppCompatActivity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "Papyrus.ttc"); [COLOR="RoyalBlue"]// Is this correct ? Fonts are generally '.ttf' files[/COLOR]
TextView myTextview = (TextView) findViewById(R.id.textView1);
myTextview.setTypeface(myTypeface);
[COLOR="Red"][B]}
}[/B][/COLOR]

I have been following a book and there is one thing I can't figure out...

I have been following a book. It seems legit but who knows... There is an activity and I'm supposed to copy whatever was in the book into the program (Android Studio) So I've finished copying the code and now there is an error, "Cannot Resolve Symbol 'menu'
I just don't get it. Here is the code. I will bold the line with the problem.
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("lifecycle", "onCreate");
setContentView(R.layout.activity_main);
}
@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 void onRestart() {
super.onRestart();
Log.d("Lifecycle","onRestart");
}
@override
public void onResume() {
super.onResume();
Log.d("Lifecycle","onResume");
}
@override
public void onPause() {
super.onPause();
Log.d("Lifecycle", "onPause");
}
@override
public void onStop() {
super.onStop();
Log.d("Lifecycle", "onStop");
}
@override
public void onDestroy() {
super.onDestroy();
Log.d("Lifecycle", "onDestroy");
}
}
) of course can't resolve symbol menu, because you need to create a menu directory in the layout directory, and there create a menu_main xml file.
Trimis de pe al meu Sony Z2 D6503
How do I do that?
pascaleandrei99 said:
) of course can't resolve symbol menu, because you need to create a menu directory in the layout directory, and there create a menu_main xml file.
Trimis de pe al meu Sony Z2 D6503
Click to expand...
Click to collapse
I've made a menu directory and I have also made a menu_main.xml file. What do I do now?
yaaaaaaaa
IanJohnTom said:
I've made a menu directory and I have also made a menu_main.xml file. What do I do now?
Click to expand...
Click to collapse
i figured it out!

Widget Show Other App's Icon

I'm trying to create a widget that will show the icon's of some app's installed on the device.
I manage to get a list of packages installed in the device, but i don't know how to get the icon from these.
I have this in my activity:
Code:
final static List<String> pacote_app = new ArrayList<String>();
final List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for (ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
// installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
//Discard this one
//in this case, it should be a user-installed app
} else {
String pacote = (String) app.packageName; //NOME DOS PACOTES
pacote_app.add(pacote);
}
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
WidgetViewsFactory
Code:
import static com.example.magcr23.mywidget.LoremActivity.pacote_app;
public class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory {
Context context;
private Context ctxt=null;
private int appWidgetId;
public WidgetViewsFactory(Context ctxt, Intent intent) {
this.ctxt=ctxt;
appWidgetId=intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
@Override
public void onCreate() {}
@Override
public void onDestroy() {
// no-op
}
@Override
public int getCount() {
return(pacote_app.size());
}
@Override
public RemoteViews getViewAt(int position) {
RemoteViews row=new RemoteViews(ctxt.getPackageName(),
R.layout.list_item);
//FILL LIST
row.setTextViewText(android.R.id.text1, pacote_app.get(position));
//SEND WORD TO ACTIVITY
Intent i=new Intent();
Bundle extras=new Bundle();
extras.putString(WidgetProvider.EXTRA_WORD, pacote_app.get(position));
i.putExtras(extras);
row.setOnClickFillInIntent(android.R.id.text1, i);
//--------------
return(row);
}
@Override
public RemoteViews getLoadingView() {
return(null);
}
@Override
public int getViewTypeCount() {
return(1);
}
@Override
public long getItemId(int position) {
return(position);
}
@Override
public boolean hasStableIds() {
return(true);
}
@Override
public void onDataSetChanged() {
// no-op
}
}
WidgetProvider
Code:
public class WidgetProvider extends AppWidgetProvider{
public static String EXTRA_WORD=
"com.commonsware.android.appwidget.lorem.WORD";
@Override
public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int i=0; i<appWidgetIds.length; i++) {
Intent svcIntent=new Intent(ctxt, WidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget=new RemoteViews(ctxt.getPackageName(),
R.layout.novo_widget);
widget.setRemoteAdapter(appWidgetIds[i], R.id.listagem,
svcIntent);
Intent clickIntent=new Intent(ctxt, LoremActivity.class);
PendingIntent clickPI=PendingIntent
.getActivity(ctxt, 0,
clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.listagem, clickPI);
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}
super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
}
}
WidgetService
Code:
public class WidgetService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
return(new WidgetViewsFactory(this.getApplicationContext(),
intent));
}
Layout
Code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin"
android:orientation="horizontal">
<ListView
android:id="@+id/listagem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
ListView Layout
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textColor="#000"
/>
</LinearLayout>
(Whoops, topic a bit old, haven't see the date)
Hi,
You should give a look to PackageItemInfo methods, especially loadIcon (https://developer.android.com/refer...ml#loadIcon(android.content.pm.PackageManager)
In your code:
Code:
final static List<String> pacote_app = new ArrayList<String>();
final List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for (ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
// installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
//Discard this one
//in this case, it should be a user-installed app
} else {
String pacote = (String) app.packageName; //NOME DOS PACOTES
pacote_app.add(pacote);
// I guess you want to get your icon here
Drawable packageIcon = app.loadIcon(pm);
}
}
To show it, the easiest might be to add an ImageView and call setImageDrawable
Good luck!

Gridview Display from SQLLITE DATABASE

Hi,
I would like to read data from my sqllite database. below my code but i am getting error . "Cannot resolve method maketext java.lang.exception,int)
Code:
package in.whomeninja.android_barcode_scanner;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class ShowProduct extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showproduct);
GridView gvproduct=(GridView)findViewById(R.id.gvproduct);
List<String> li=new ArrayList<>();
ArrayAdapter<String> dataAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_item,li);
dataAdapter.setDropDownViewResource(R.layout.showproduct);
try {
SQLiteDatabase db=openOrCreateDatabase("coke_db",MODE_PRIVATE,null);
Cursor cr=db.rawQuery("SELECT * FROM PRODUCT_TBL",null);
if(cr!=null){
if(cr.moveToFirst()){
do{
String no=cr.getString(cr.getColumnIndex("barcodeResult"));
String name=cr.getString(cr.getColumnIndex("latitude_textview"));
String type=cr.getString(cr.getColumnIndex("longitude_textview"));
String qty=cr.getString(cr.getColumnIndex("textView1"));
String remark=cr.getString(cr.getColumnIndex("textView6"));
//li.add(no);
li.add(no);
li.add(name);
li.add(type);
li.add(qty);
li.add(remark);
gvproduct.setAdapter(dataAdapter);
}while (cr.moveToNext());
}else{
Toast.makeText(getApplicationContext(), "No Data to show", Toast.LENGTH_LONG).show();
}
}
}catch (Exception e){
Toast.makeText(ShowProduct.this, e, Toast.LENGTH_SHORT).show();
}
}
}

Categories

Resources