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
You can use inflation for that
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.
So the problem isn’t due to styles.