This is my xml file. There are 35 xml files like this in my app. Their textview and button numbers are different. Some of them are more than 10 , some of are less than 5. But considering the devices screen, i designed all xml files using scroll view. Also i needed tablelayout and used tablelayout, table row inside it and textviews inside tablerows. When i finished the app everything was ok. Now i added mobile ad banners in xml files and it started to crash.
If i add the banner just before ending of tablelayout , app doesnt crash and ad is displayed at teh end of table but the vision is too bad.Because if there are 3 rows in the xml then the banner takes place after the last textview and this is something like the middle of the page. No matter how many textviews the xml use, i want them to be scrollable and in the end i want the banner take place at the end of the page.
I tried some android:gravity="bottom" or
alignparentbottom="true" things but they didn't work. In my researches i realised that i may need to add some linear or relative layout codes but when i tried them thay dont fit with scrollview and crash. Here is one of the xml files. How should i need to modify this ?
In the images , First one is how it works , the banner takes place where the textview ends , i want the second image , no matter how long the tetviews the banner should be at the bottom of the page.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue">
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</TextView>
// some other buttons or textviews...
</TableRow>
</TableLayout>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</ScrollView>
If the banner needs to be always shown at the bottom,
you can set the weight of the ScrollView in a LinearLayout to 1.
Example
<?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"
android:background="#drawable/blue">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#drawable/blue">
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</TextView>
// some other buttons or textviews...
</TableRow>
</TableLayout>
</ScrollView>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
I can't test the code here because I do not have the Banner component but try wrapping both the TableLayout and the Banner in a LinearLayout.
modified layout file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableLayout
android:id="#+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
</TableRow>
</TableLayout>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
UPDATE
After testing the file locally I realized that crash is being caused by the Banner because a ScrollView can have only one child. I'm surprised that you were able to compile your project with the error in the layout file. So one solution is wrap the TableLayout and Banner in a LinearLayout. See example 1:
example 1
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</TableRow>
</TableLayout>
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
After thinking about it, it may be possible to eliminate the TableLayout completely. You can use a LinearLayout to stack items on top of each other. So that would simplify your layout file quite a bit. See example 2.
example 2
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="5dp"
android:text="TRY"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" />
<com.startapp.android.publish.banner.Banner
android:id="#+id/startAppBanner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
UPDATE 2
Then your issue must be with your Java file. I have tested the layout file locally and it does render properly. See screenshot below.
UPDATE 3
The solution required updates to both the layout file and the Activity class. The logic is fairly simple. If the number of rows in the table cause the table height to exceed the height of the scrollview, then scrolling is enabled. If scrolling is enabled, the banner ad is appended as child of the table. If scrolling is disabled, the banner ad is inserted as a child of the linearlayout. In order to determine the correct heights it is necessary to subscribe to the OnGlobalLayout event. The following code is a ** working sample ** of what you will need. In order to test both scenarios (scrolling enabled/disabled) just change the value of the ROW_COUNT variable. I used 3 for scrolling disabled and 30 for enabled. You will see the banner placed appropriately.
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="#id/scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#e0e0e0">
<TableLayout
android:id="#+id/table"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:text="TRY" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
TestActivity.java
public class TestActivity extends Activity {
private static String TAG = "TestActivity";
private Activity mActivity;
private LinearLayout mLinearLayout;
private TableLayout mTableLayout;
private RelativeLayout mRelativeLayout;
private static int ROW_COUNT = 30;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Log.d(TAG, "onCreate");
mActivity = this;
mRelativeLayout = (RelativeLayout)findViewById(R.id.relativelayout);
mLinearLayout = (LinearLayout)findViewById(R.id.linearlayout);
mTableLayout = (TableLayout)findViewById(R.id.table);
float density = getResources().getDisplayMetrics().density;
final int padding = (int)Math.floor((density * 20f));
for(int i=0;i<ROW_COUNT;i++){
TextView view = new TextView(mActivity);
view.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT));
view.setPadding(padding,padding,padding,padding);
view.setText("TableRow");
TableRow row = new TableRow(mActivity);
row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
row.addView(view);
mTableLayout.addView(row);
}
ViewTreeObserver observer = mRelativeLayout.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TextView view = (TextView) findViewById(R.id.banner);
if(view == null){
view = new TextView(mActivity);
view.setId(R.id.banner);
view.setBackgroundColor(Color.LTGRAY);
view.setText("BannerAd");
view.setGravity(Gravity.CENTER_HORIZONTAL);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
view.setPadding(0, padding, 0, padding);
if(mTableLayout.getHeight() > mLinearLayout.getHeight()) {
mTableLayout.addView(view);
}else{
mLinearLayout.addView(view);
}
}
}
});
}
}
I can post screenshots if needed, but test this code out and let me know if you have any questions.
Related
I've did my due diligence in searching the net but I can't seem to find a proper solution for the following.
I'm using a table layout and a table row and I am putting in 3 buttons with TEXT. Here is a sample of the layout file.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tlRemoteTable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="ABCD" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="ABCDEFGHI" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="ABCDEFGHIJKLMNOP" />
</TableRow>
</TableLayout>
The image outcome is here:
My aim is the make it even, all having 1/3 ratio. I understand that the text length is the one that is forcing it to expand. How can I wrap the text instead?
The ultimate outcome will be a dynamically generated file. So I'll be programming it, instead of XML.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tlRemoteTable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0f">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="ABCD" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="ABCDEFGHI" />
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="ABCDEFGHIJKLMNOP" />
</TableRow>
</TableLayout>
you can change button width to 0dp all set is's working
Thanks! That worked!
Here's the programatic codes if anyone needs it!
TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT, 1.0f);
TableRow.LayoutParams btnParams = new TableRow.LayoutParams(0, TableRow.LayoutParams.MATCH_PARENT);
This question seems to have been asked a dozen times already bu I haven't been able to deduce the solution from them.
I have a layout called tile.xml that looks like this:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="May Fong Robinson"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
android:textAppearance="?android:attr/textAppearanceMedium" />
I need to add this one of my views called feed.xml which I've declared as a ScrollView because I want to have multiple objects in it so I can vertically scroll though them. Here's my feed.xml file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:background="#F1F1F1"
android:layout_height="fill_parent" >
</ScrollView>
I'd like to dynamically add my "tile" from tile.xml to my "feed" feed.xml view while changing the text of the TextViews on the fly.
How can I do this? Thanks
The new feed.xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >
<LinearLayout
android:id="#+id/tiles"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
...and the new tile.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:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="May Fong Robinson"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:text="Beautiful star-shaped spillway, Kechut Reservoir, Jermuk, Armenia"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
First of all, add the 2 TextViews inside a layout.
then, in your code, where you want to dynamically add the TextViews you do the following :
ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.tile, null);
scroll.addView(view);
To access the TextViews you have to do the following :
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText("");
Also make sure to give your TextViews different IDs in the XML file. Currently they are both textView1
ScrollView can only have 1 child, meaning the current structure of your "tile" layout can't be added to the ScrollView.
What you need is to add another Layout, let's say LinearLayout to your ScrollView so that it would be the ScrollView's only direct child. To this LinearLayout you can add as many childs as you want.
This can be done by using the LayoutInflater which you've probably encountered in numerous examples.
**TRY THIS**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F1F1F1" >
<LinearLayout
android:id="#+id/tiles"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="#layout/tile" />
</LinearLayout>
I have a Layout with a HorizontalScrollView containing a LinearLayout for a Menu where the contents are inflated with the contents of the DB. This works fine however when there are not enough elements to make the HSV scroll this does not fill the width of the screen which ideally should be centered. i.e.
Currently:
| Element 1 Element 2 | <- edge of screen
Instead of:
| Element 1 Element 2 | <- edge of screen
whilst still being able to:
| Element 1 Element 2 Element 3 Element 4 Elem| <- edge of screen now scrolling
The layout XML is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLinearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="25dp" >
</TextView>
<ScrollView
android:id="#+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="30dp">
<LinearLayout
android:id="#+id/footerLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
With the following XML being inflated inside footerLayout:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footer_content"
android:textSize="18sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="FOOTER"
android:singleLine="true" />
I just solved this issue. I ran into it a few hours ago. You need to center the HorizontalScrollView in its parent and set its width/height to wrap_content. The layout you put in the the HSV must have its width/height set to wrap content as well. The important part here is to not set any gravity/layout_gravity on this layout or you may experience (very annoying) clipping issues after inflating your views. Example below is contained in a RelativeLayout.
<HorizontalScrollView android:id="#+id/svExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#id/rlExample">
<LinearLayout
android:id="#+id/llExample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView >
I had the same problem and finally got it to work. Here is a minimal example:
The main activity layout with the HorizontalScrollView:
<?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">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
The layout of the elements that will be inflated and put inside the scroll view:
<?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="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your text" />
</LinearLayout>
And this is an example of the code to inflate and add the elements:
LinearLayout container = (LinearLayout) findViewById(R.id.container);
LayoutParams elementLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 1f);
int elementCount = 3; // ...or however many you like
for (int i = 0; i < elementCount; i++) {
LinearLayout element = (LinearLayout) getLayoutInflater().inflate(R.layout.element, null);
container.addView(element, elementLayoutParams);
}
It's mainly based on a technique for stretching the content of ScrollViews explained in this article by Romain Guy.
However in this case where you are adding contents dynamically, another key point is setting the positive value for the weight (1f in this example) using LayoutParams when adding the elements to the container. If you have a static set of elements that you can include directly in the container and don't need to inflate you can specify the weight of the elements' outer LinearLayout in the XML layout like so:
android:layout_weight="1"
But if you do it dynamically that won't work because the weight will reset to 0 and the elements collapse. Hence you need to set it via the LayoutParams.
Below is the simplest way worked for me.
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<LinearLayout
android:id="#+id/layout_others"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</HorizontalScrollView>
managed to get round center-horizontal clipping the left side:
HorizontalScrollView part of layout has been changed to:
<LinearLayout
android:id="#+id/footerWrapperLayoutToGetAroundCenteringIssue"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center_horizontal">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_height="fill_parent"
android:layout_width="wrap_content">
<LinearLayout
android:id="#+id/footerLayout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Its just a pity when it centers them they are not evenly distributed.
If anyone has any ideas please let me know.
assign wieght 1 to the textview in inflated view and make width wrapcontent
If the parent layout is a constraint layout:
<HorizontalScrollView
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="0dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
.
.
</LinearLayout>
</HorizontalScrollView>
I am facing problem to customize Linear Layout. My xml is as follows:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<LinearLayout android:id="#+id/LinearLayout_LeftPanel"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#0000ff"
>
<TextView android:id="#+id/LeftPanel_Title"
android:text="Frequently asked questions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
/>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout_MainContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_toRightOf="#+id/LinearLayout_LeftPanel"
>
<TextView android:id="#+id/MainContent_Title"
android:text="Alabama Rules of Civil Procedure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:textSize="19px"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
I want to set the width of #+id/LinearLayout_LeftPanel layout one third of the screen width pro-grammatically on the onCreate().
so how can do this ?
The answer from Mina Samy is almost correct, but you're using a RelativeLayout as your topmost container. The android:layout_weight attribute only works when the surrounding container is a LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:id="#+id/LeftPanel"
android:layout_weight="1">
...
</LinearLayout>
<LinearLayout android:id="#+id/MainContent"
android:layout_weight="3">
...
</LinearLayout>
</LinearLayout>
EDIT:
I just saw that you said you wanted to set it "programmatically in the onCreate method". If you really want to do this instead of setting it in the XML like I wrote above, you have to do do something like this:
protected void onCreate(Bundle b) {
LinearLayout leftPanel = (LinearLayout)findViewById(R.id.LeftPanel);
LinearLayout.LayoutParams leftPanelParams = (LinearLayout.LayoutParams)leftPanel.getLayoutParams();
leftPanelParams.weight = 1;
// do the same thing for MainContent but set the Weight to 3;
}
you can use set the weight property of both Linearlayouts
set the first android:layout_weight="1"
and the left panel android:layout_weight="3"
thanks
Okay, so I've got this TableLayout, and its full of data - with all of the rows added programmatically. I've got the TableLayout inside of a HorizontalScrollView, which in turn is inside a ScrollView - this gives me scrolling both horizontally and vertically. What I'm trying to do now is add a header row to it that will not scroll. I've tried moving things around so that both of my scroll views were actually inside of the TableLayout and adding the TableRows to the HorizontalScrollView; my hope was to be able to then add the header row outside of the scroll views.
The only other thing I can think of is having a second table layout just for the header row, but getting the columns to line up seems like it would be difficult. Any ideas?
One approach is by embedding a TableLayout within another TableLayout's row and putting the header in the preceding row as seen below. Aligning the data and the header requires the layout_width property of the header View objects and the data's View objects to be set to the same dip values. Also, the layout_weight property of the inner TableLayout's View objects must match its corresponding header.
Now, in the XML below, I have placed 3 TextViews in the inner TableLayout in a single row to match with the column headers. This is just to show how the alignment can be done. You can populate that data programmatically by inflating a layout and adding it at runtime.
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Name"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1"/>
<TextView android:text="Score"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="Level"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
<ScrollView android:layout_height="120dp">
<TableLayout android:id="#+id/score_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Al"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1">
</TextView>
<TextView android:text="1000"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="2"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</TableLayout>
I actually came up with another decent way of doing this.
Simply build the table normally with the header row as the first row, inside of a vertical orientation LinearLayout. Next, programmatically remove the first row then add it as the first child to the LinearLayout. This worked like a charm.
Edit: This also works without having to specify static column widths.
I know that the question is old, but it was the first one, that Google gave me as I've had the same problem. And since I think I've found a better solution, I would like to share it.
Idea: put the TableLayout (inside the ScrollView) into RelativeLayout and create an overlay, that would draw the first (header) row over everything else.
Here is layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/table_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="UselessParent"
>
<TableLayout
android:id="#+id/table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</ScrollView>
</RelativeLayout>
And here is the code:
TableLayout table = (TableLayout)view.findViewById(R.id.table);
final TableRow headerRow = new TableRow(context);
table.addView(headerRow);
table.addView(new TableRow(context));
table.addView(new TableRow(context));
table.addView(new TableRow(context));
RelativeLayout tableWrapper = (RelativeLayout)view.findViewById(R.id.table_wrapper);
View fakeHeaderView = new View(context) {
#Override
public void draw(Canvas canvas) {
headerRow.draw(canvas);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = headerRow.getMeasuredWidth();
int height = headerRow.getMeasuredHeight();
widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
};
tableWrapper.addView(fakeHeaderView);
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Name"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1"/>
<TextView android:text="Score"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="Level"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
<ScrollView android:layout_height="120dp">
<TableLayout android:id="#+id/score_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:text="Al"
android:layout_width="100dp"
android:layout_column="0"
android:layout_weight="1">
</TextView>
<TextView android:text="1000"
android:layout_width="30dp"
android:layout_column="1"
android:layout_weight="1">
</TextView>
<TextView android:text="2"
android:layout_width="30dp"
android:layout_column="2"
android:layout_weight="1">
</TextView>
</TableRow>
</TableLayout>
</ScrollView>
</TableLayout>
For those not happy with having to pre-define your sizes, I found a bit of a hack that's working for me.
Basically, make a separate table for the title and put it over your main table, but with the same Top alignment, then create two copies of the title row and after adding one to the main table, add the other to the title table and set the child view's layoutParams to the row form the main table.
Here's my basic example.
in your layout:
<HorizontalScrollView
android:id="#+id/table_horizontal_scroll_view"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ScrollView
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:id="#+id/table_vertical_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/grid_table_layout"
/>
</ScrollView>
<TableLayout
android:layout_alignLeft="#+id/table_vertical_scroll_view"
android:layout_alignRight="#+id/table_vertical_scroll_view"
android:layout_alignStart="#+id/table_vertical_scroll_view"
android:layout_alignEnd="#+id/table_vertical_scroll_view"
android:layout_alignParentTop="true"
android:background="#color/grid_view_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/grid_floating_row_layout"
/>
</RelativeLayout>
</HorizontalScrollView>
Then when you add your rows:
//clear out any views
tableLayout.removeAllViews();
floatingRowLayout.removeAllViews();
TableRow[] rows = getTableContentRows() // content of your table
TableRow[] titleRows = {getTitleRow(), getTitleRow()}; //two copies of your title row
tableLayout.addView(titleRows[0]); // first add the first title to the main table
addRows(rows) // add any other rows
floatingRowLayout.addView(titleRows[1]); // floatingRowLayout is connected to id/grid_floating_row_layout
titleRows[0].setVisibility(View.INVISIBLE); // make the title row added to the main table invisible
// Set the layoutParams of the two title rows equal to each other.
// Since this is done after the first is added to the main table, they should be the correct sizes.
for(int i = 0; i < titleRows[0].getChildCount(); i++) {
titleRows[1].getChildAt(i).setLayoutParams(titleRows[0].getChildAt(i).getLayoutParams());
}
Use Two tableLayout One in ScrollView like give android:stretchColumns="*"
<RelativeLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:paddingTop="5dp"
android:layout_height="match_parent"
android:layout_below="#+id/llSpinner">
<TableLayout
android:layout_width="match_parent"
android:id="#+id/tableHead"
android:stretchColumns="*"
android:layout_height="wrap_content">
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/tableTotal"
android:layout_below="#+id/tableHead"
android:id="#+id/scrolltable">
</ScrollView>
<TableLayout
android:layout_width="match_parent"
android:id="#+id/tableTotal"
android:stretchColumns="*"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
</TableLayout>
</RelativeLayout>
then create a common view for ROWs and most important mention android:layout_width="0dp"
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:focusable="true"
android:focusableInTouchMode="false"
android:clickable="true"
android:background="#android:drawable/list_selector_background">
<TextView
android:text="S.No"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/tbsNo"
android:layout_weight="1"
android:gravity="center"
android:layout_column="1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Date"
android:layout_weight="1"
android:gravity="center"
android:id="#+id/tbDate"
android:layout_column="2" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Count"
android:layout_weight="1"
android:gravity="center"
android:id="#+id/tbMrCount"
android:layout_column="3" />
</TableRow>
Now in Activity
Tablelayout tableHeading =(TableLayout)findViewById(R.id.tableHead);
Tablelayout table =(TableLayout) findViewById(R.id.table_repots);
trHeading = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null);
trHeading.setBackgroundColor(Color.parseColor("#6688AC"));
trHeading.setPadding(0,10,0,10);
TextView tv;
tv = (TextView) trHeading.findViewById(R.id.tbsNo);
tv.setText("S.No");
tv = (TextView) trHeading.findViewById(R.id.tbDate);
tv.setText("Date");
table.addView(tv);