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 8997105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:50:37+00:00 2026-06-15T23:50:37+00:00

I need some help with an android app I am making, let me explain:

  • 0

I need some help with an android app I am making, let me explain:

Current solution: working but not optimal

In my XML i have a TableLayout defined.
I programmatically add 4 Views to a row (RelativeLayout), and then add this row to the TableLayout.
There can be up to 25 rows of these 4 views.
This is working fine. But I’m having a problem styling these for multiple layouts.
Setting margins, padding, weights programmatically all works, but there are so many different devices which makes it very hard to maintain.
(And as far as I know a TableRow does not allow aligning to the right)
Specifically i want to align the last view to the right side of the screen.

What i would like to have:

I would create an XML file containing the layout of these 4 views (probably in a RelativeLayout). I would then like to programmatically add these 4 views using the XML layout I defined.

Can i do some sort of (pseudo-code coming up):

TextView1 = new TextView
TextView2 = new TextView
TextView3 = new TextView
TextView4 = new TextView
//Do stuff with the textviews
X = getXMLLayout(definedXMLlayout)
X.view(1) = TextView1
X.view(2) = TextView2
X.view(3) = TextView3
X.view(4) = TextView4
TableLayout.addRow(X)

This way i can style those RelativeLayout rows in a .xml file and place them under the -large -land -xlarge etc… maps.

So my question is : How do I do this?

Or, is there a better way to style multiple programmatically created views?

Edit: i did read up on the other SO articles about this, but most don’t exactly describe what i need. I know i can just add all of the views in code, but then i also have to style them in code, which is something i don’t want. (And i know about the styles file, but this is also not a good option for me)

Grts

Edit2:

Hi Shreva,

Basicly this “works”, i did have to change the following code

final LayoutInflater lyInflater= (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

into

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and instead of a LinearLayout i’m working with a RelativeLayout.
But for some reason the layout is not correct, this is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/rl_playerCard"
    android:layout_height="wrap_content"
    android:background="@drawable/playercard" >
<TextView
        android:id="@+id/tv_min"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        style="@style/txtMin"
        android:text="@string/str_min" />

    <TextView
        android:id="@+id/tv_plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tv_min"
        style="@style/txtPlus"
        android:text="@string/str_plus" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/tv_plus"
        style="@style/txtPlayercardGrey"
        android:text="name" />

    <TextView
        android:id="@+id/tv_score"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        style="@style/txtPlayercardGreen"
        android:text="score" />
</RelativeLayout>

padding/margin is done in my styles.xml files, and also some text colour & text size.
3 views are now on the left side on the edge of the screen, on top of eachother.
the tv_score is on the right side.
so this means the margins/padding are not being taken into account, neither is the

android:layout_toRightOf

Any idea why that is happening?

grts

edit3

these are the 4 styles i have defined, i have others which are working fine. And when looking at the xml layout in graphical layout everything seems fine as well.

<!-- playercard name -->
    <style name="txtPlayercardGrey" parent="@android:style/Widget.TextView">
        <item name="android:textSize">17sp</item>
        <item name="android:textColor">#666666</item>
        <item name="android:layout_marginLeft">15dp</item>
    </style>

    <!-- playercard score -->
    <style name="txtPlayercardGreen" parent="@android:style/Widget.TextView">
        <item name="android:textSize">17sp</item>
        <item name="android:textColor">#7DC500</item>
        <item name="android:layout_marginRight">15dp</item>
    </style> 

    <style name="txtMin" parent="@android:style/Widget.TextView">
        <item name="android:textSize">25sp</item>
        <item name="android:textColor">#ff0000</item>
        <item name="android:paddingLeft">15dp</item>
        <item name="android:paddingRight">15dp</item>
        <item name="android:layout_marginLeft">15dp</item>
        <item name="android:layout_marginRight">15dp</item>

    </style>

    <style name="txtPlus" parent="@android:style/Widget.TextView">
        <item name="android:textSize">25sp</item>
        <item name="android:textColor">#00a62b</item>
        <item name="android:paddingLeft">15dp</item>
        <item name="android:paddingRight">15dp</item>
        <item name="android:layout_marginLeft">15dp</item>
        <item name="android:layout_marginRight">15dp</item>
    </style>

here’s a picture of how it looks:
https://i.stack.imgur.com/lNN05.png

and here how it should look like:
https://i.stack.imgur.com/lNBYS.png

  • 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-06-15T23:50:38+00:00Added an answer on June 15, 2026 at 11:50 pm

    You can use inflation for that

    final LayoutInflater lyInflater= (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout lLayoutRow = (LinearLayout) lyInflater.inflate(
                    R.layout.table_row_layout, null);
    
    TextView tv1 = (TextView) lLayoutRow.findViewById(R.id.rowTv1);
    TextView tv2 = (TextView) lLayoutRow.findViewById(R.id.rowTv2);
    TextView tv3 = (TextView) lLayoutRow.findViewById(R.id.rowTv3);
    TextView tv4 = (TextView) lLayoutRow.findViewById(R.id.rowTv4);
    

    Edit:

    I tried your code with your styles.xml. I have inflated it in my main layout. And it gave me following output in 3.2″ QVGA emulator.

    Screenshot

    So the problem isn’t due to styles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating an Android app utilising OpenGL ES, and need some help with the
I need some help. In my android app I have a ViewFlipper that contains
I am trying to create my first Android app and need some help. Basically
I am writing an Android app and need some help. I have a string
Hey everyone, I am in need of some help with my first android app,
I'm new in Android development and I need some help coming with a solution
i need some help in addition to android-layouts. For eyample: Actually I use a
I need some help dealing with three Threads in Android One thread is the
Hii everybody , I am noob at android and need some help... I am
I am working on an android app in which I am using some Native

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.