I can't resolve my problem with scrollView and webView,
I know, that i can't put webview into scrollview but I have't idea how to jump it.
In my scrollView, I have image ( 1280 x 1300 > ) and this image must be scrolling, under image i must have webview with html content. have you any idea how should I do that?
is any another way to put html content with to some view?
this is my xml view, and I add image and webview in my activity class
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLinear"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scroll_journal_page"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ScrollView>
<Gallery
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#d5d5d3" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
i create view somethink like that:
protected void onPostExecute(Bitmap result) {
Animation fadeInAnimation = AnimationUtils.loadAnimation(Journal.this, R.anim.fade_in);
relativeLayout = new LinearLayout(getApplicationContext());
relativeLayout.setLayoutParams(new FrameLayout.LayoutParams(WIDTH, result.getHeight() + HEIGHT));
relativeLayout.setOrientation(1);
coverImage = new ImageView(getApplicationContext());
coverImage.setImageBitmap(result);
relativeLayout.addView(coverImage);
Integer pageNumber = Integer.valueOf(ArrayHelper.journalList.get(pageIdentity).get("pageId").toString());
File file = new File(Environment.getExternalStorageDirectory() + "/MCW/"+ pageNumber +"/content.html");
if(file.exists()) {
LinearLayout linearLayout = new LinearLayout(getApplicationContext());
linearLayout.setLayoutParams(new FrameLayout.LayoutParams(WIDTH, HEIGHT));
WebView wV = new WebView(getApplicationContext());
try {
InputStream fin = null;
int len;
fin = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fin.available()];
len = fin.read(buffer);
fin.close();
String rawText = EncodingUtils.getString(buffer, 0, len, "utf-8");
wV.loadDataWithBaseURL("", rawText, "text/html", "utf-8", null);
}catch (Exception e) {}
linearLayout.addView(wV);
relativeLayout.addView(linearLayout);
}
scrollview.addView(relativeLayout);
scrollview.setAnimation(fadeInAnimation);
isReadyToChange = true;
}
Just use a Scrollview with Height FillParent
add a linearlayout height= deviceHeight+imageHeight
use a linearlayout with height of device
add webview to this linearlayout
add image and linearlayout to this layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView android:layout_width="wrap_content"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView android:id="#+id/pho"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/dd1"/>
<WebView android:id="#+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
I am not sure whether this would meet your requirement or not, however
is any another way to put html content with to some view ?
You can use TextView to display HTML content
mTextView.setText(Html.fromHtml('html_content'));
Also go through Mark Murphy - HTML Tags Supported By TextView
As a side note, HTML may also be used as a string resource, as described Here
Related
I want to display a page numbering, like 3/10 at the bottom of my app, after swiping, the number will be 4/10.
My layout is webviews that the user ban swipe between them, i want to indicate the webview current index in a bottom bar that will be updated after each swipe.
basic layout:
<?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"
android:minWidth="25px"
android:minHeight="25px">
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="413.3dp"
android:id="#+id/tipsWebView"
android:layout_marginBottom="0.0dp" />
//bar position
</LinearLayout>
And to display different webviews i use :
adaptor.AddFragmentView((i, v, b) =>
{
if(m_swipeCounter < m_links.Length)
{
m_swipeCounter++;
}
else
{
m_swipeCounter= 0;
}
var view = i.Inflate(Resource.Layout.tab, v, false);
var client = new WebClient();
string htmlData = client.DownloadString(m_links[m_swipeCounter]);
htmlData +="<link rel=\"stylesheet\" type=\"text/css\" href=\"cssInject.css\">";
WebView tipsWebView = view.FindViewById<WebView>(Resource.Id.tipsWebView);
tipsWebView.SetWebViewClient(new WebViewClient());
tipsWebView.Settings.JavaScriptEnabled = true;
tipsWebView.LoadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "", null);
return view;
//create pager, pager adaptor to create the right webview link
});
So the index will be based on m_swipeCounter variable.
All the best.
it's possible with a LinearLayout, using weights and stuff, but it'll run faster using a RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.webkit.WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tipsWebView"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomText"
/>
<TextView
android:id="#+id/bottomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="0/0"
/>
</RelativeLayout>
I need to add views to a vertical Linearlayout at bottom programmatically. (Yes we know that, it's in the title).
Ok : i can add views to the linearlayout, it's not a problem. But it's for a chat, so i need to add views at the bottom for each message which is displayed. I don't find any attributes to the LinearLayout to do that.
Adding messages :
mHandler.post(new Runnable() {
#Override
public void run() {
TextView message = new TextView(ChatActivity.this);
String[] splitMessage = text.split(":");
message.setText(splitMessage[0]+"\n"+splitMessage[1]);
message.setGravity(Gravity.LEFT);
message.setMaxWidth(200);
message.setBackgroundColor(getResources().getColor(android.R.color.tertiary_text_light));
message.setTextColor(getResources().getColor(android.R.color.black));
message.setSingleLine(false);
mLayout.addView(message);
}
});
chat_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/live_obs_mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/empty"
android:orientation="vertical" >
<ScrollView
android:id="#+id/chat_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:isScrollContainer="tue"
>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal" >
<EditText
android:id="#+id/edit_chat"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="left|center_vertical"
android:layout_weight="5" />
<Button
android:id="#+id/chat_submit"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="right|center_vertical"
android:onClick="onSubmit"
android:text="OK" />
</LinearLayout>
</LinearLayout>
You can do it pragmatically like this, this is just an example. You may want to change your code based on that.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
TextView txt1 = new TextView(MyClass.this);
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
txt1.setLayoutParams(layoutParams);
linearLayout.addView(txt1);
I want to create an app with UI as follow..I have two webview add in screen..I want to when click to webview2,webview1 will hide and webview2 will fill fullscreen...If two webview load url is "http://www.google.com" ,it will display two webview separate but if I load an url different "http://www.google.com" as "http://www.youtube.com" it only display 1 webview youtube full screen..How I must do..Thank
Here is file 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="horizontal" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/layout1"
android:layout_weight="1"
>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview1"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/layout2"
android:layout_marginLeft="10dip"
>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview2"
/>
</LinearLayout>
and here is code in Activity:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
layout1=(LinearLayout)findViewById(R.id.layout1);
layout2=(LinearLayout)findViewById(R.id.layout2);
webview1=(WebView)findViewById(R.id.webview1);
webview1.loadUrl("http://www.google.com");
webview2=(WebView)findViewById(R.id.webview2);
webview2.loadUrl("http://www.google.com");
final LayoutParams lp=webview1.getLayoutParams();
final LayoutParams lp1=webview2.getLayoutParams();
lp.height=height;
lp1.height=height;
webview1.setLayoutParams(lp);
webview2.setLayoutParams(lp1);
webview2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
webview1.setVisibility(View.GONE);
lp.height=height;
lp.width=width;
webview2.setLayoutParams(lp);
}
});
I would recommend creating 2 custom WebViews.
WebView wv1 = findViewById(ID_WEBVIEW_1);
WebView wv2 = findViewById(ID_WEBVIEW_2);
if (wv1.getUrl() != "http://google.com" || wv2.getUrl() != "http://google.com") {
((ViewManager)entry.getParent()).removeView(entry);
}
http://developer.android.com/reference/android/view/ViewManager.html
I want to add images to scrollview, there is code i tried to use:
ScrollView sv = (ScrollView)findViewById( R.id.scrollView2);
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "PATH" ) );
iv.setScaleType( ScaleType.CENTER_INSIDE );
sv.addView( sv );
I get this exception:
java.lang.IllegalStateException: ScrollView can host only one direct child
So how i should add images to my scrollview ?
Added code:
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FF0000" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</ScrollView>
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/scrollView1"
android:background="#FFFF00" >
<LinearLayout
android:id="#+id/filesScrollerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</ScrollView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#FFFFFF"
android:layout_toRightOf="#+id/scrollView2" />
</RelativeLayout>
And on the end Activity onCreate calling this method:
public void addImage(String path){
LinearLayout sv = (LinearLayout)findViewById( R.id.filesScrollerLayout);
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( path ) );
iv.setScaleType( ScaleType.CENTER_INSIDE );
sv.addView( sv );
}
Thanks.
this may help.. it tells so because ScrollView cannot hold more than 1 child.. it needs single child which hosts all other view..
<ScrollView>
<LinearLayout
android:id="#+id/child">
<ImageView/>
...
...
</LinearLayout>
</ScrollView>
in your case
LinearLayout child = (LinearLayout)findViewById( R.id.child);
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "PATH" ) );
iv.setScaleType( ScaleType.CENTER_INSIDE );
child.addView( sv );
I wish to create a custom layout out for my sample application. Previously i used the xml layout for User interface, but now i wish to create the application using custom (without using xml layout). I used the below code in my xml, but i change this xml layout to custom code i can't implement (i don't have knowledge how to do this). How can i write this layout code in my Activity?. Any one could you please help me to solve this. Thanks in advance.
<?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" >
<ScrollView
android:id="#+id/webviewscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/webviewlinear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/webviewframe1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/webview1"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" >
</WebView>
<ImageView
android:id="#+id/webviewimage1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/one" >
</ImageView>
</RelativeLayout>
<RelativeLayout
android:id="#+id/webviewframe2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/webview2"
android:layout_width="fill_parent"
android:layout_height="350dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp" >
</WebView>
<ImageView
android:id="#+id/webviewimage2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/two" >
</ImageView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
As you want to create layout at run-time, you can do this way:
RelativeLayout layout1 = new RelativeLayout(this);
ImageView imgView1 = new imgView1(this);
layout1.addView(imgView1); // added ImageView into the RelativeLayout
do this kind of process for creating and adding widgets.
LinearLayout ll1,ll2;
ScrollView sv = new ScrollView(this);
RelativeLayout rl1,rl2;
WebView wv1,wv2;
ImageView iv1,iv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int LHeightWrap = LinearLayout.LayoutParams.WRAP_CONTENT;
int LWidthWrap = LinearLayout.LayoutParams.WRAP_CONTENT;
int LHeightFill = LinearLayout.LayoutParams.FILL_PARENT;
int LWidthFill = LinearLayout.LayoutParams.FILL_PARENT;
int RHeightWrap = RelativeLayout.LayoutParams.WRAP_CONTENT;
int RWidthWrap = RelativeLayout.LayoutParams.WRAP_CONTENT;
int RHeightFill = RelativeLayout.LayoutParams.FILL_PARENT;
int RWidthFill = RelativeLayout.LayoutParams.FILL_PARENT;
wv1 = new WebView(this);
iv1 = new ImageView(this);
rl1.addView(wv1,RWidthWrap,RHeightWrap);
rl1.addView(iv1,RWidthWrap,RHeightWrap);
wv2 = new WebView(this);
iv2 = new ImageView(this);
rl2.addView(wv2,RWidthWrap,RHeightWrap);
rl2.addView(iv2,RWidthWrap,RHeightWrap);
ll2.addView(rl1);
ll2.addView(rl2);
sv.addView(ll2);
ll1.addView(ll2);
You can do somthing like this. You can also set properties of any component using native code.