My app does not display accented characters correctly. I looked into this last year but was not able to find a solution. I've been digging around but still have not found a solution. I've supplied a screen shot that shows what happens when the user enters "London Château" in an EditText field which is loaded into a WebView when the button is clicked. The accented character "â" does not translate as expected. It translates to "Ãç", "London ChÃçteau". Would love to get a solution, Thanks in advance.
I have supplied code to demonstrate:
public class MainActivity extends Activity {
private WebView webView;
private EditText edittext_1;
private Button buttonUpdate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview_summary);
webView.getSettings().setJavaScriptEnabled(true);
edittext_1 = (EditText) findViewById(R.id.editText_1);
buttonUpdate = (Button) findViewById(R.id.button_update);
buttonUpdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String s = edittext_1.getText().toString();
webView.loadData(s, "text/html", "utf-8");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webview_summary"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight=".85" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight=".15" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/editText_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:hint="text"
android:minWidth="100dp"
android:text="" />
<Button
android:id="#+id/button_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/editText_1"
android:padding="10dp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
As soon as I posted I found a suggestion that worked. I changed the following line from:
webView.loadData(s, "text/html", "utf-8");
to:
webView.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);
try to get s from Html.fromHtml
String s = Html.fromHtml(edittext_1.getText().toString()).toString();
Related
I have created an Application where on Button click the Website should open within the same Activity.
Button 1 -> should load website = >"google"
Button 2 -> should load website = >"google play"
Button 3 -> should load website = >"you tube"
This is How i want it to look: https://i.stack.imgur.com/yzDF9.png
Sample code of activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout
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=".MainActivity">
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<ScrollView
android:id="#+id/Srcollview1"
android:layout_width="100dp"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 1"
></Button>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 2"
></Button>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="program 3"
></Button>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webviewing"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</WebView>
</LinearLayout>
</LinearLayout> </LinearLayout>
Sample code for MainActivity.java
Button b1,b2,b3;
WebView webView;
LinearLayout l1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.btn1);
b2 = (Button)findViewById(R.id.btn2);
b3 = (Button)findViewById(R.id.btn3);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.google.com");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://play.google.com/");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView = (WebView) findViewById(R.id.webviewing);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.youtube.com/");
}
});
}
Your code works fine, except you set the listener for b2 twice. Typo maybe?
If you are facing connection errors, just add the Internet permission to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET"/>
i create a imageview and textview in android with the help of html code so my question is how to align text and when i click Read more then it will open new pase so.. here is my full source code.can anyone help me please.thanks in advance
MainActivity
public class MainActivity extends Activity {
private final String htmlText = "<body><h1>Heading Text</h1><p>We understand that different client have different needs and requirements therefore special effort might be required to fulfill these requirements. Our custom software development services are aimed to fulfill......Read More </p>" +
"<blockquote>Example from <a href=\"www.javatechig.com\">" +
"Javatechig.com<a></blockquote></body>";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView htmlTextView = (TextView)findViewById(R.id.html_text);
htmlTextView.setText(Html.fromHtml(htmlText));
}
}
My Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/home" />
<TextView
android:id="#+id/html_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/imageView1"
/>
</RelativeLayout>
To align text below image remove following lines from text view in your layout
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
And add following line
android:layout_below="#+id/imageView1"
With Webview
Replace your layout with following code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<WebView
android:id="#+id/webview"
android:layout_below="#id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
And use following code in your activity
private final String htmlText = "<body><h1>Heading Text</h1><p>We understand that different client have different needs and requirements therefore special effort might be required to fulfill these requirements. Our custom software development services are aimed to fulfill......Read More </p>" +
"<blockquote>Example from <a href=\"http://www.javatechig.com\">" +
"Javatechig.com<a></blockquote></body>";
private WebView m_webview;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_webview = (WebView) findViewById(R.id.webview);
m_webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.equalsIgnoreCase("http://www.javatechig.com/")) {
Toast.makeText(MainActivity.this, url, Toast.LENGTH_SHORT).show();
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
m_webview.loadDataWithBaseURL("", htmlText, "text/html", HTTP.UTF_8, null);
}
Try adding android:gravity="center" in your xml for your TextView if you want to align your text at the center of it.
To open your page try : htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());
I want to justify the text inside the textview.Is there any way to justify the content inside the textview.I don't want to use webview...Is there a library for it??Plz help me...Thanks in advance..
Justify.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/back1"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/banner" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="20dp"
android:text="About Us" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.94"
android:text="Paragraph\n\nParagraph"
</ScrollView>
Activity
public class About_us extends Activity {
TextView txtName;
TextView txtName1;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_us);
getActionBar().setDisplayHomeAsUpEnabled(true);
txtName = (TextView) findViewById(R.id.textView);
txtName.setTextColor(Color.WHITE);
txtName1 = (TextView) findViewById(R.id.textView1);
txtName1.setTextColor(Color.WHITE);
txtName1.setTextSize(16);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.about_us, menu);
return true;
}
Android does not support full justify in TextView by default. You have to use WebView for that, but as you dont wanna use Webview. So, the Solution is ANDROID Text-Justify 2.0 Library.
You can find that Library on following github link
https://github.com/bluejamesbond/TextJustify-Android
Full setup information is provided on the same link.
XML Layout
<LinearLayout style="#style/behindMenuScrollContent"
android:paddingTop="25dp" >
<TextView
style="#style/behindMenuItemTitle"
android:text="People" />
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<jp.fureco.IconView
android:id="#+id/iconViewItem4"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textSize="20dp"/>
<TextView
android:layout_marginLeft="10dp"
style="#style/behindMenuItemLabel"
android:text="Visitor" />
</LinearLayout>
</LinearLayout>
Then I have myWebView just like this
WebView myWebView = (WebView)findViewById(R.id.webView1);
When a user clicks on iconViewLink4, I want it jump to the url "http://example.com/messsage"
How can I code that?
Yes you can. Just use android:onClick="methodName" in <LinearLayout> or add it programatically by using setOnClickListener like
LinearLayout linLay = (LinearLayout)findViewById(R.id.iconViewLink4);
linLay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// the method or code that will take control to your URL
}
}
Make it clickable and give it an OnClickListener (you can do this on any View)
<LinearLayout
android:id="#+id/iconViewLink4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true">
findViewById(R.id.iconViewLink4).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something here
}
});
i have created a webview for my android application , inicially i am tying to take application simpler as possible to understand things . i have created a webview to display the different page in it . i simply created a button which load url into webview which is working perfectly fine ,but the webview is loading the urls below the button . it is not overriding the main layout or not opening the url in newpage. i see the webpage in webview and the button above it . how can get rid of this button. thanks in advance. here is my java and xml code
activity
public class TestinglinkActivity extends Activity {
final Activity activity = this;
WebView webview;
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new MyWebViewClient());
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
Button btn_login = (Button) findViewById(R.id.btn_click_login);
btn_login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
webview.loadUrl("http://www.google.com.pk");
webview.goBack();
webview.goForward();
}
}
);
}
}
xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="vertical"
android:scrollbarSize="10dp"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/btn_click_login"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="2"
android:text="GO"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_marginRight="5dp"
android:layout_alignParentLeft="true"
android:gravity="center_vertical"
android:lines="4"
/>
<ImageView android:id="#+id/imageBtn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="5dp"
android:contentDescription="#string/hello"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
<TextView
android:id="#+id/date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center_vertical"
android:lines="4"
/>
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
</ScrollView>
It sounds to me like you want to start a new Activity when the button is pressed. The reason your WebView appears below the other layout is because that's exactly what your XML describes. If you want your WebView to appear full screen, you need to move the WebView to a separate Activity which is started when your button is clicked.
You probably will want to pass the URL in the Extra of the Intent used to start the new Activity.