I want to add two views one after the other, I used this way but I am getting an error.
This is my XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rel1"
android:layout_alignParentTop="true"
></RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/rel2"
android:layout_below="#+id/rel1"
></RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
In the two Relative layouts rel1,and rel2 I will add my custom views which are going to be drawn dynamically.
my code:
setContentView(R.layout.main);
RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
RelativeLayout.LayoutParams para1 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
RelativeLayout.LayoutParams para2 = new RelativeLayout.LayoutParams(
viewWidth, viewHeight);
rlstat1.setLayoutParams(para1);
rlstat1.addView(mView);
para2.addRule(RelativeLayout.BELOW, R.id.rel1);
rlstat2.addView(mView2);
Here mView and mView2 are two view type which I want to set in the two relative layouts.
ViewWidth and ViewHeight are the width and height of the screen it is running.
The problem:
If only one view is added i.e. mView or mView2 it's showing but if both the views are added(like in above) then only one relative layout is shown.
I want that both of my views get set one below other.
Hope I am clear in my question.Can you please tell me the appropriate way to do this.
you are setting layout params to views dynamically in activity and these are new layout params objects, so your rule to add rel2 to below of rel1 gets cleared, instead try:
setContentView(R.layout.main);
RelativeLayout rlstat1=(RelativeLayout)findViewById(R.id.rel1);
RelativeLayout rlstat2=(RelativeLayout)findViewById(R.id.rel2);
RelativeLayout.LayoutParams para1 = rlStat1.getLayoutParams();
para1.width=LayoutParams.FILL_PARENT;
para1.height=LayoutParams.FILL_PARENT;
RelativeLayout.LayoutParams para2 = rlStat2.getLayoutParams();
para2.width=LayoutParams.FILL_PARENT;
para2.height=LayoutParams.FILL_PARENT;
rlstat1.setLayoutParams(para1);
rlstat1.addView(mView);
para2.addRule(RelativeLayout.BELOW, R.id.rel1);
rlstat2.addView(mView2);
Related
I want to set the layout inside another layout programatically in android.
Here, is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/Layout_second_overs"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="6"
android:orientation="vertical">
<!--Second textview overs-->
</LinearLayout>
<LinearLayout
android:id="#+id/Layout_second_balls"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<!--Second textview balls--> //Here i want to put one linear layout.
</LinearLayout>
</LinearLayout>
</LinearLayout>
Please, help me to solve out this problem.
LinearLayout parent = (LinearLayout) findViewById(R.id.parentlayout);
LinearLayout child = new LinearLayout(getApplicationcontext());
child .LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
parent.addView(child);
You can add Layout inside root Layout using below way.
// This is your parent `LinearLayout`
LinearLayout parent = (LinearLayout) findViewById(R.id.parentlayout);
// Create New LinearLayout inside parent layout using below way.
LinearLayout child = new LinearLayout(context);
parent.addView(child);
Additional you can set width and height dynamically.
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
child.setLayoutParams(layoutParams);
I want to have 2 layouts inside ScrollView. First layout should be on the full screen and second layout below first one. When activity starts it is not possible see second layout. Second layout is possible see after scroll screen. Please take a look on my image for better understand.
Layout code: `
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<!-- this layout can be any height you want -->
</LinearLayout>
</LinearLayout>
</ScrollView>`
Get the device screen height:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenHeight = size.y;
Find the LinearLayout:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
Set the height of the layout based on the screen height you got earlier:
LayoutParams params = layout.getLayoutParams();
params.height = screenHeight - getStatusBarHeight();
Done. Be sure to call all of those methods in your onCreate method.
Hope this helps!
I want to programmatically add a View to mainHolder that is declared as below:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<LinearLayout
android:id="#+id/mainHolder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal" />
I want the new view to fill the screen height. How is that possible?
Try this :
LinearLayout mainHolder = (LinearLayout)findViewById(R.id.mainHolder);
//untested adding of view I got it from http://stackoverflow.com/questions/5731487/how-to-add-views-to-linear-layout
mainHolder.addView(View, mainHolder);
//this is tested however
View.setMinimumHeight(mainHolder.getHeight());
set Linear Layout property
android:layout_width="match_content"
android:layout_height="match_content"
Then add following code:
LinearLayout mainHolder=(LinearLayout)findViewById(R.id.mainHolder);
RelativeLayout.LayoutParams layer=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layer.addRule(RelativeLayout.ALIGN_PARENT_TOP);
mainHolder.addView(layer);
Same as above code, you can add your views programatically
I've been searching around in Google for a bit but I can't seem to find what I want to do. I want to be able to programatically add an icon as an overlay in an activity at a specified position without using any xml.
An example of what I mean: http://cdn9.staztic.com/app/a/2326/2326236/pollfish-demo-2-1-s-307x512.jpg
Any ideas?
It depends at layout you are using. If you are using RelativeLayout, you can do it this way:
<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"
android:id="#+id/main" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:src="#android:drawable/ic_"/>
</RelativeLayout>
Which is equal with this Java code (except root RelativeLayout):
RelativeLayout layout = (RelativeLayout) findViewById(R.id.main);
ImageView child = new ImageView(this);
child.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
child.setLayoutParams(params);
layout.addView(child);
I have a fragment with two RelativeLayout, one inside the other. This fragment is inside a tab. I want that the internal layout(layoutInternal) is scrollable, but with the following code does not work. I create the view object into the layoutInternal dinamically. Where is my mistake?
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutExsternal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="#dimen/fragmentHall_10dp_margin"
android:layout_marginTop="#dimen/fragmentHall_10dp_margin">
<RelativeLayout
android:id="#+id/layoutInternal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
The following code is where i insert the view object (table) inside the intenalLayout.
while (iteratorTables.hasNext()) {
[...] //Here i calculate the dimension and coordinates
b_table = new ButtonTable(SelzApplication.getAppContext(),tableMap.getValue(), widthTable, heightTable);
b_table.setX(Math.round((tableMap.getValue().getX())* Xratio));
b_table.setY(Math.round((tableMap.getValue().getY())* Yratio));
//add Touch Listeners to the button
b_table.setOnLongClickListener(new OnLongTuchDragDrop());
b_table.setOnClickListener(new TableOnClick());
layoutInternal.addView(b_table, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
This is the screenshot of my app.
The RelativeLayout layoutInternal has the height initially set as android:layout_height="wrap_content" but its content is initially empty because the content is created dynamically; for this reason is not scrollable.
To solve this, when you finish to draw the tables into RelativeLayout layoutInternal, you have to set its height dynamically as below:
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) layoutInternal.getLayoutParams();
params.height = max_y_coordinate_used;
layoutInternal.setLayoutParams(params);