ScrollView can host only one direct child exception - android

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 );

Related

How to add a button over an ImageView in ScrollView dynamically? Android. Xamarin

I have an axml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mylinearlayout"
android:orientation="vertical"
android:configChanges="orientation|keyboardHidden"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_height="1dip"
android:layout_width="1dip" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView1">
<LinearLayout
android:id="#+id/filesScrollerLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
<VideoView
android:layout_width="1dip"
android:layout_height="1dip"
android:id="#+id/videoView1" />
My application takes a photo and video and puts it in the LinearLayout.
When I add video, I create an ImageView dynamically, put the firstframe of the video in this ImageView and add it in LinearLayout.
private void addBitmapToLayout(Bitmap bitmap)
{
ImageView iv = new ImageView(this);
iv.SetImageBitmap(bitmap);
linearlayout.AddView(iv);
}
How can I put a button over this ImageView in LinearLayuot dynamically to start the video?
Like this
Thank you.
Instead of adding an ImageView to your LinearLayout, add a custom layout where you'll have your ImageView as well as a Button on top
Define the layout for each item in your list :
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout>
Your function now becomes :
private void addBitmapToLayout(Bitmap bitmap) {
LayoutInflater inflater = LayoutInflater.from(context);
RelativeLayout layout = inflater.inflate(R.layout.list_item, null, false);
ImageView iv = (ImageView) layout.findViewById(R.id.imageView);
iv.SetImageBitmap(bitmap);
//Add click listener to your Button etc...
Button button = (Button) layout.findViewById(R.id.button);
linearlayout.AddView(layout);
}

addView() doesn't show a view

private void showText(){
textView.setText(Html.fromHtml(text));
Log.d("MyLog","tags.size="+tags.size());
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
ll.addView(button, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tagButtons.add(button);
}
}
This should be simple but this buttons doesn't show. I checked with logs- tags.size=5 so the problem is not in this. ll is a LinearLayout
Here's layout file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_margin="5dp"
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
So, it's a very simple code and I don't understand why are theese views are not being added to a layout with id "ll"
You should call invalidate() on the parent layout which forces children to be drawn on the screen
Remove the linearlayout reference which you have not posted in the question and use like as i have done, Try this
Call showText() in OnCreate- method If you are using Activities
private void showText(){
textView.setText(Html.fromHtml(text));
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
LinearLayout ll=(LinearLayout) findViewById(R.id.ll);
ll.addView(button);
}
}

Relative layout inside a scrollview is not working perfectly.

I am using a relativelayout inside a scrollview,and I am adding views to that relativelayout dynamically.Eventhough the views are more than the screensize its not scrolling.
Please help in solving this task.
My ScrollView xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ScrollView
android:id="#+id/mainScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg"
android:fillViewport="true"
>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
</RelativeLayout>
Code used for dynamically adding images:
if (isOddViewNo)
{
isOddViewNo=false;
final View view= getLayoutInflater().inflate(R.layout.sample, null);
iv= (ImageView)view.findViewById(R.id.iv_small);
ImageView iv1= (ImageView)view.findViewById(R.id.iv_big);
iv.setVisibility(View.VISIBLE);
iv1.setVisibility(View.GONE);
iv.setImageResource(R.drawable.ic_launcher);
iv.setX(x-45);
iv.setY(ypos+17);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
view.setLayoutParams(lp);
rl.addView(view);
x+=100;
}
else
{
isOddViewNo=true;
final View view= getLayoutInflater().inflate(R.layout.sample, null);
iv= (ImageView)view.findViewById(R.id.iv_big);
ImageView iv1= (ImageView)view.findViewById(R.id.iv_small);
iv.setVisibility(View.VISIBLE);
iv1.setVisibility(View.GONE);
iv.setImageResource(R.drawable.ic_launcher);
// iv.setLayoutParams(new LayoutParams(200,200));
iv.setX(x-45);
iv.setY(ypos);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
view.setLayoutParams(lp);
rl.addView(view);
x+=178;
}
My inflated sample.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" >
<ImageView
android:layout_height="50dp"
android:layout_width="50dp"
android:id="#+id/iv_small"
android:visibility="gone"
/>
<ImageView
android:layout_height="80dp"
android:layout_width="80dp"
android:id="#+id/iv_big"
android:visibility="gone"
/>
</LinearLayout>
My screenshot:
You have to set ScrollView height to wrap_content. Also never use fill_parent (deprecated) , use match_parent instead.

Add view to a vertical LinearLayout at bottom (programmatically)

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);

How can i write this layout code in my Android Activity?

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.

Categories

Resources