Opening a local HTML file - android

I have a textview in a layout (called - t_c) with the code:-
<TextView
android:id="#+id/GoToTCContacting"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="2"
android:background="#drawable/border2"
android:clickable="true"
android:onClick="GoToTCContacting"
android:padding="10dp"
android:text="Contacting"
android:textColor="#FFF"
android:textSize="18sp" />
I want the textview to open another layout (called - t_c_contacting) on click which has a webview in and that webview to open a html file I have in my layout folder called con.html.
This is how the t_c_contacting layout is coded:-
<WebView
android:id="#+id/contactingView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
This is how my class is set up to open t_c_contacting which works fine but I can't work out how to populate the webview with my file.
public void GoToTCContacting(View view)
{
setContentView(R.layout.t_c_contacting);
}

If you already have the HTML stored localy, simply call:
webView.loadDataWithBaseURL(mUrl, mHtmlData, MIME_TYPE, CHARSET, "");
Where:
mUrl can be a dummy Url if you want, or the actual URL of the page, so the WebView can figure out how to place some UI elements such as Images.
mHtmlData is a String that contains the actual HTML content.
MIME_TYPE is a String that represents the mimeType of the data, say: "text/html"
CHARSET is a String that represents the encoding of the data, say: "utf-8"
I hope that helps.

Related

how to display either video or image from response?

I am getting a response from JSON API where a "Video" tag contains a video or image name with its extension as shown below. I want to show video if that tag contains video (I am loading video in Web view) then hide Image view, or show image if it contains image then hide Web view.
Problem coming is that webview visibility is gone if the response postion has .mp4 and image view is visible but response image is not showing in Picasso.
Response:
[
{
"Video": "8100931.mp4"
},
{
"Video": "218519.jpeg",
}]
Layout:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/llVideoPost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<WebView
android:id="#+id/wvPostVideo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:focusable="false"
android:screenReaderFocusable="false"
android:visibility="visible"
app:layout_constraintDimensionRatio="1:1"
tools:ignore="MissingConstraints" />
<ImageView
android:id="#+id/imgPostImage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:contentDescription="#string/todo"
android:scaleType="fitXY"
android:visibility="gone"
app:layout_constraintDimensionRatio="1:1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
I tried this code
Code for calling response:
String videoTag= businessList.get(position).getVideo();
if (businessList.get(position).getVideo().endsWith(".mp4")) {
holder.wvPostVideo.setVisibility(View.VISIBLE);
holder.imgPostImage.setVisibility(View.GONE);
holder.wvPostVideo.loadUrl("https://smakerspace.s3.ap-south-1.amazonaws.com/Video/" + videoTag);
} else if (businessList.get(position).getVideo().endsWith(".jpeg")) {
holder.wvPostVideo.setVisibility(View.GONE);
holder.imgPostImage.setVisibility(View.VISIBLE);
Picasso.get()
.load("https://smakerspace.s3.ap-south-1.amazonaws.com/upload/"+videoTag)
.rotate(90)
.into(holder.imgPostImage);
}
replace https://smakerspace.s3.ap-south-1.amazonaws.com/upload/ with https://smakerspace.s3.ap-south-1.amazonaws.com/Video/
Becuase you have stored these image and video file into one db folder
And please use Videoview for video player its make eassy to play a video into android
Instead of passing url to picasso, can you please first try to download the image programmatically and then try to set to any ImageView.
Or you can use any rest clients to validate if APIs are working fine and returning valid image.
Once above is validated and found to be working fine, next step is to check if we got right instance of imgPostImage

How to add images,links in between string.xml

I added scrolling activity(textscoll) to my Project.So I wanted to add huge text to that activity and several images in between that huge text.So I added string file in string.xml and called it in scrolling activity(content_textscoll.xml)...
My string file
<string name="string_text_file">This is huge text...</string>
My content_textscoll.xml
<TextView
android:layout_width="wrap_content"
android:textColor="#000000"
android:textSize="20dp"
android:lineSpacingMultiplier="1.3"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"
android:text="#string/string_text_file" />
My problem is,
*I want to add several images in between above text.How to resolve that problem....?
I can add images to drawble folder.How to call them?
*How to add html links or other activity links
make web view :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="abhiandroid.com.htmlexample.MainActivity">
<WebView
android:id="#+id/simpleWebView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp" />
</RelativeLayout>
make assest folder if not exist and create new html file there with any name you want, then put your text and images there like this :
<p>in this tag (p tag) put your text </p>
<img>in this tag (img tag) put your images </img>
in your activity make something like this :
WebView webView;
public String fileName = "yourAssestFileName.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// init webView
webView = (WebView) findViewById(R.id.simpleWebView);
// displaying content in WebView from html file that stored in assets folder
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/" + fileName);
}

How do I use MvxImageView.DefaultImagePath?

I have successfully used MvxImageView with ImageUrl, but how can I set a drawable icon as a default for those users which don't have a profilepicture (that is, where ProfileThumbnailImageUrl is empty)?
<MvxImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
local:MvxBind="ImageUrl ProfileThumbnailImageUrl"
DefaultImagePath="#drawable/ic_action_user"/>
If this view is just being used once (e.g. not reused in a list) then you can just use android:src for your default image.
If you need to use DefaultImagePath then you can do this in a binding:
local:MvxBind="ImageUrl ProfileThumbnailImageUrl; DefaultImagePath DefaultImageUrl"
where DefaultImageUrl is a drawable accessed as res:name - e.g. res:ic_action_user, a file path (saved using the File plugin), or an http url
You should also be able to do this as a string literal:
local:MvxBind="ImageUrl ProfileThumbnailImageUrl; DefaultImagePath 'res:ic_action_user'"

Open text link in web view

I have the text in textview that is "Open the link http://www.google.com to find anything." The text i have set in my class file. SO, when i click on this link. The link is open in browser. I want open this link in my web view. How can i do this?
My xml file is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="444dp" />
Do something like this,
in your oncreate method,
WebView mywebView = (WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new MyWebViewClient());
And then create a class like,
class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(url.contains("somePartOfYourUniqueUrl")){
return super.shouldOverrideUrlLoading(view, url);
} else {
view.loadUrl(url); // load url in webview
return true;
}
}
Use java.net.URL for this
Here is a link for your help, using this you may parse url from given string
How to detect the presence of URL in a string
You can do that, with WebView, you need to declare a WebViewClient object and override the public boolean shouldOverrideUrlLoading (WebView view, String url) method, there you can filter urls or give some customized functionality.
In your case, to stay on the WebView, you would need to return false on that method.
Check out this tutorial.
To handle the click event on the TextView's url. As it's suggested on this question you can filter the ACTION_VIEW intent on your WebView containing Activity. If you need more guidance about intent-filters, check this out.
For a TextView its a little bit more work, You'll have to make your own copy of the Linkyfy class and use a TransformFilter to make the links behave however you want them to. Check out for an example Android Linkify class both web and #mentions all in the same TextView

Android : display html data

I want to display the content of an html page and an image to the right of that in my view.
So i have defined my layout as
<ScrollView android:id="#+id/scrllvwNo1"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="fill_parent" android:background="#drawable/home_bg">
<ImageView android:id="#+id/aboutcmkimage"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentRight="true" android:src="#drawable/about"
android:padding="5dip" />
<WebView android:id="#+id/aboutcmk" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:textColor="#000000"
android:layout_toLeftOf="#+id/aboutcmkimage"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true" />
</RelativeLayout>
</ScrollView>
and
I try to load the html page as below
WebView web = (WebView)findViewById(R.id.aboutcmk);
web.loadData(getString(R.layout.about),"texl/html","utf-8");
in this case i am getting the error
"this page contains error at line1 ... "
if I try
web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null);
no html output and no error
In both case the image is coming
Can anyone help me in debugging
My html has bullets , so i cannot use textview instead of webview
here is the definition of my htmlstring in xml file
<string name="About"><html><body><b>What is CMK?</b> ......</body></html>
Thanks a lot for the help
are you put this line What is CMK? ...... in strings.xml if yes then the correction is in this line
web.loadDataWithBaseURL(null,getString(R.layout.about),"texl/html","utf-8",null);
Correct one:
web.loadDataWithBaseURL(null, getString(R.string.About), "text/html", "UTF-8", null);
First of all, if you have saved your HTML specification as a string resource, your should access it as R.string.about and not R.layout.about. Change that, if it still doesn't work, try escaping the less than characters in your string, like this :
<string name="about"><html><body><b>What is CMK?</b> ......</body></html> </string>
I think you will have to escape the less than characters. Before loading the text, Log it. You'll see the problem.
Instead of storing the HTML in strings.xml, you can store it in its own file in the res/raw/ directory. For example, let's say you save the file in res/raw/mypage.html. I haven't tested this, but you should be able to open up raw resources and load them up in a WebView like:
try {
Resources resources = getResources();
InputStream inputStream = resources.openRawResource(R.raw.mypage);
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String htmlStr = new String(bytes);
webView.loadData(htmlStr, "text/html", "UTF-8");
} catch(Exception e) {
//blah
}

Categories

Resources