Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6192563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:57:51+00:00 2026-05-24T02:57:51+00:00

This is my main.xml:` <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=match_parent android:addStatesFromChildren=false android:layout_height=wrap_content> <TableLayout android:id=@+id/header android:layout_marginRight=2dp android:layout_marginLeft=2dp

  • 0

This is my main.xml:`

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:addStatesFromChildren="false"
    android:layout_height="wrap_content">
    <TableLayout
        android:id="@+id/header"
        android:layout_marginRight="2dp"
        android:layout_marginLeft="2dp"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:layout_width="fill_parent"
        android:layout_alignParentTop="true">
        <TableRow>
            <TextView
                android:id="@+id/leftHeader"
                android:textColor="@android:color/black"
                android:layout_margin="1dp"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:background="#ffcccccc"
                android:text="textview1"
                android:layout_width="30dip" />
            <TextView
                android:id="@+id/rightHeader"
                android:textColor="@android:color/black"
                android:gravity="center_horizontal"
                android:layout_weight="1"
                android:layout_margin="1dp"
                android:background="@android:color/white"
                android:text="textview2"
                android:layout_width="70dip" />
        </TableRow>
    </TableLayout>
<ScrollView
    android:id="@+id/table_scroll"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="2dp"
    android:layout_marginLeft="2dp"
    android:layout_below="@+id/header"
    android:layout_above="@+id/buttonTable">
    <TableLayout
        android:id="@+id/maintable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black">
        <TableRow
            android:id="@+id/maintableRow">
            <TextView
                android:id="@+id/maintableLeft"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginRight="1dp"
                android:layout_marginLeft="1dp"
                android:text="textview1"
                android:layout_width="30dip" />
            <TextView
                android:id="@+id/maintableRight"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"
                android:text="textview2"
                android:layout_width="70dip" />
        </TableRow>
    </TableLayout>
</ScrollView>
<TableLayout
    android:id="@+id/buttonTable"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:layout_alignParentBottom="true">
    <TableRow>
        <Button
            android:id="@+id/button_ResetData"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Reset Data"
            android:layout_gravity="center"
            android:layout_weight="1" />
        <Button
            android:id="@+id/button_Refresh"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:text="Refresh"
            android:gravity="center"
            android:layout_weight="1" />
    </TableRow>
</TableLayout>

`

After I press one of the buttons its text alignment goes to the left, how can I make it stay in center?
something similar happens with the first table (header) (textview1, textview2) but here it also reduses the textsize the amount of the text in the view…

EDIT: I managed to find the code that causes the change but I don’t know how to fix this.
Maybe some one can explain me why does this happen and how to fix it?

    private void fillTableView(List<BatteryInfo> list) {

    TextView leftHeader = (TextView) findViewById(R.id.leftHeader);
    TextView rightHeader = (TextView) findViewById(R.id.rightHeader);

    LayoutParams leftHeaderLayoutParams = leftHeader.getLayoutParams();
    LayoutParams rightHeaderLayoutParams = rightHeader.getLayoutParams();

    TableLayout contentTable = (TableLayout) findViewById(R.id.maintable);      

    contentTable.removeAllViews();

    for (int i = list.size() - 1; i >= 0; i--) {

        TextView tv1 = new TextView(this);
        tv1.setTextColor(Color.BLACK);
        tv1.setGravity(Gravity.CENTER);
        tv1.setBackgroundColor(Color.LTGRAY);
        tv1.setLayoutParams(leftHeaderLayoutParams);
        tv1.setText(String.valueOf(list.get(i).getLevel()));

        TextView tv2 = new TextView(this);
        tv2.setTextColor(Color.BLACK);
        tv2.setGravity(Gravity.CENTER);
        tv2.setBackgroundColor(Color.WHITE);
        tv2.setLayoutParams(rightHeaderLayoutParams);
        tv2.setText(list.get(i).getDurationTimeInString());

        TableRow tblRow = new TableRow(this);
        tblRow.addView(tv1);
        tblRow.addView(tv2);

        contentTable.addView(tblRow);                   
    }       
}

After I use removeAllViews or any addView function the layout of the top most table changes as I explained before. Why does this happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:57:52+00:00Added an answer on May 24, 2026 at 2:57 am

    You can fix this by changing the android:layout_width="30dip" and android:layout_width="70dip" in leftHeader and rightHeader to android:layout_width="fill_parent". I have to admit that I’m not entirely certain why that is, though.

    Instead of clearing and re-filling a TableLayout every time, though, you might want to consider using a ListAdapter. It’s much more suited to what you’re doing (and will probably perform better, too).

    UPDATE NUMBER 2:

    Notice the change in the main.xml and the change in the onCreate method. This will make your header static while letting your listview scroll, and changing from TableLayout to LinearLayout for your buttons will fix the alignment issue.

    Here’s how to do this with a custom adapter instead of TableLayout (which isn’t really meant for dynamic data like this):

    Change you main.xml to this:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:addStatesFromChildren="false" android:layout_height="wrap_content">
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_alignParentTop="true" android:id="@+id/header">
    <TextView android:id="@+id/leftHeader" android:textColor="@android:color/black"
        android:layout_margin="1dp" android:gravity="center_horizontal"
        android:layout_weight="1" android:background="#ffcccccc" android:text="textview1"
        android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    <TextView android:id="@+id/rightHeader" android:textColor="@android:color/black"
        android:gravity="center_horizontal" android:layout_weight="1"
        android:layout_margin="1dp" android:background="@android:color/white"
        android:text="textview2" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
    </LinearLayout>
    
    <LinearLayout android:id="@+id/buttonTable" android:orientation="horizontal"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:layout_alignParentBottom="true" >
            <Button android:id="@+id/button_ResetData"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:text="Reset Data" android:layout_gravity="center"
                android:layout_weight="1" />
            <Button android:id="@+id/button_Refresh"
                android:layout_height="wrap_content" android:layout_width="fill_parent"
                android:text="Refresh" android:gravity="center"
                android:layout_weight="1" />
    </LinearLayout>
    
    <ListView android:id="@+id/listView" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_weight="1"
        android:fadingEdge="none"
        android:layout_below="@id/header" android:layout_above="@id/buttonTable">
    </ListView>
    

    Add XML layout files for your rows (row.xml):

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
        <TextView android:id="@+id/maintableLeft"
            android:layout_height="wrap_content" android:layout_weight="1"
            android:layout_marginRight="1dp" android:layout_marginLeft="1dp"
            android:gravity="center_horizontal" android:background="#ffcccccc"
            android:textColor="@android:color/black" android:layout_width="fill_parent" />
        <TextView android:id="@+id/maintableRight"
            android:layout_height="wrap_content" android:background="#ffcccccc"
            android:textColor="@android:color/black" android:layout_weight="1"
            android:layout_marginLeft="1dp" android:layout_marginRight="1dp"
            android:gravity="center_horizontal" android:layout_width="fill_parent" />
    </LinearLayout>
    

    Now everything is in place to create a custom class from BaseAdapter to display your information (and make your layout work better):

    class CustomAdapter extends BaseAdapter {
        private List<BatteryInfo> info;
        private LayoutInflater inflater;
    
        public CustomAdapter(Context context, List<BatteryInfo> x) {
            this.info = x;
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        public int getCount() {
            return info.size();
        }
    
        public Object getItem(int position) {
            return info.get(position);
        }
    
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View v;
            v = inflater.inflate(R.layout.row, parent, false);
    
            BatteryInfo b = (BatteryInfo) getItem(position);
    
            ((TextView) v.findViewById(R.id.maintableLeft)).setText(b
                    .getDurationTimeInString());
            ((TextView) v.findViewById(R.id.maintableRight)).setText(b
                    .getLevel());
    
            return v;
        }
    }
    

    Modify your onCreate method to inflate the header, add it, and then bind your data to your ListView using the new CustomAdapter (I’ve created a fake BatteryInfo class for this example, you’ll need to modify things slightly to get this working):

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        final ListView lv = (ListView) this.findViewById(R.id.listView);
        final Context context = this;
    
        Button refresh = (Button) this.findViewById(R.id.button_Refresh);
        refresh.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                List<BatteryInfo> x = new ArrayList<BatteryInfo>();
                x.add(new BatteryInfo());
                x.add(new BatteryInfo());
                x.add(new BatteryInfo());
                x.add(new BatteryInfo());
    
    
                lv.setAdapter(new CustomAdapter(context, x));
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom listView with the row as follows: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent
I have <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=fill_parent android:layout_height=fill_parent android:id=@+id/mainRelativeLayout> and I'm trying to setup the background
I've got this code that i'm working on : <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android
This my main.xml <RelativeLayout android:id=@+id/gloabal_permission android:layout_height=fill_parent android:layout_width=wrap_content android:background=@drawable/settings_selector_down android:paddingTop=3dp android:paddingBottom=3dp android:paddingRight=12dp android:paddingLeft=15dp> <TextView android:id=@+id/TextView06
Its a dumb question. Prepare yourself. My main.xml is this: <?xml version=1.0 encoding=utf-8?> <RelativeLayout
Code first: '''this is main structure of my program''' from twisted.web import http from
I have a crash (Application Stopped Unexpectedly) problem with this main.xml is a HelloWorld
I have a custom listView. The main layout xml is something like this: <?xml
I have this main.xml layout which has a fixed header and footer. The body
The situation is like this : Main project A. and a class library B.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.