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());
Related
I'm writing an Android lab and somehow my bottom button and text enter part are not showing. I'm using Linear Layout. it shows in the XML file design mode but doesn't show when running the program. Can someone help me find out what the problem is?
I tried to add a pic of what my design page and program run pic but it's not letting me, when I run the program I can see the two strings that I added to an array, but just not see the button and plain text at the bottom
here is my code, I have my java file code list below the xml code:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="684dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="#+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send" />
<EditText
android:id="#+id/typehere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Type here"
android:textColor="#9C9B9B" />
<Button
android:id="#+id/receive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="receive" />
</LinearLayout>
</LinearLayout>
</ListView>
my activity
public class ChatRoomActivity extends AppCompatActivity {
ListView listView;
EditText input;
Button receive;
Button send;
ArrayList<String> items;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);
listView = findViewById(R.id.listView);
input = findViewById(R.id.typehere);
receive = findViewById(R.id.receive);
send = findViewById(R.id.send);
items = new ArrayList<>();
items.add("I'm sending");
items.add("I'm receiving");
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter); // see the list of result of adaptor
receive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = input.getText().toString();
addMessage(text);
input.setText("");
}
});
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String text = input.getText().toString();
addMessage(text);
input.setText("");
}
});
}
public void addMessage(String message){
items.add(message);
listView.setAdapter(adapter);
}
}
after straggling in the display, I found the height for ListView is longer than my screenview, so I edit the xml to
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginTop="-50dp"
android:orientation="horizontal"
tools:visibility="visible">
and the rest of the code didn't change
I guess for others that has same issue try to shorter the listview's height
You can use RelativeLayout and can add android:layout_above attribute to your ListView
Example code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/llBottomLayout"/>
<LinearLayout
android:id="#+id/llBottomLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"
tools:visibility="visible">
</LinearLayout>
</RelativeLayout>
EDITED
The examples in google searches and the android documentation are basic, and my question is more complex.
What I do not know, is how to put multiple components together and turn it into a single component. CardView + LinearLayout + TextView + ImageView = Custom View.
If I have a basic example, of cardview with at least one button and a space for external content. It would be enough for me to figure out how to do the rest. This within the cardview extension class, because I also do not know how the extension will understand where to create the components
EDITED 2
Example
QUESTION
I was trying to create a method of using Cardview with an expandable button, and I was able to do this in XML. But I wanted to be able to create a library so I could reuse it in other cases.
I'll show you the source code of what I did.
Layout XML
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/cwlltitulobtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="horizontal">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#### TITLE OF BUTTON ####"
android:textSize="18sp"
android:textStyle="bold"/>
<ImageView
android:id="#+id/imgarrowdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:scaleType="fitEnd"
android:visibility="visible"
app:srcCompat="#drawable/ic_keyboard_arrow_down_black_24dp" />
<ImageView
android:id="#+id/imgarrowup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:scaleType="fitEnd"
android:visibility="gone"
app:srcCompat="#drawable/ic_keyboard_arrow_up_black_24dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/cwllconteudo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:visibility="gone">
<!-- ########################################
######## CARDVIEW CONTENT HERE #########
########################################-->
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Java code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.cwlltitulobtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CVOpenCloseBTN();
}
});
}
public void CVOpenCloseBTN(){
ImageView arrowdown = (ImageView) findViewById(R.id.cvimgarrowdown);
ImageView arrowup = (ImageView) findViewById(R.id.cvimgarrowup);
LinearLayout pagecontent = (LinearLayout) findViewById(R.id.cwllconteudo);
if (conteudopage .getVisibility() == View.GONE) {
// it's collapsed - expand it
pagecontent.setVisibility(View.VISIBLE);
arrowdown.setVisibility(View.GONE);
arrowup.setVisibility(View.VISIBLE);
} else {
// it's expanded - collapse it
pagecontent.setVisibility(View.GONE);
arrowdown.setVisibility(View.VISIBLE);
arrowup.setVisibility(View.GONE);
}
}
}
My idea is that I can transform all this part of the xml and java code to be used in this way and faster, than to have to always repeat again:
<br.com.samantabiblioteca.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:titulo="#### TITLE OF BUTTON ####">
<!-- ########################################
######### CARDVIEW CONTENT HERE ########
########################################-->
</br.com.samantabiblioteca.CardView>
Is it possible to do this?
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.
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();
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.