Question

In my first attempt to create a layout programmatically, I created the below posted first-xml file and I want to append to it programmatically the below posted second-xml file. I also referred to this tutorial but it seems that creating linearlayout and relativelayout programmatically are different. Also in my attempt to do create the relativelayout eclipse underscores my layoutparams with red squiggle, plus, i do not know whether the parameter was set first is the width or the height. Please advice and provide recommendations.

my attempts:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_layout_programmatically_00);

    RelativeLayout mRelLayout00 = new RelativeLayout(this);
    android.view.ViewGroup.LayoutParams mRelLayoutParams = new  
            LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT
            , android.view.ViewGroup.LayoutParams.WRAP_CONTENT);

First-xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" 
android:layout_width="match_parent">

      <LinearLayout 
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
      </LinearLayout>

second-xml"whta i want to achieve programmatically":

<RelativeLayout 
              android:id="@+id/RelativeLayout01"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="top|center_horizontal">
              <TextView
                  android:id="@+id/TextView01"
                  android:layout_height="wrap_content"
                  android:text="@string/menu"
                  android:textSize="@dimen/titlesTextSize"
                  android:layout_marginLeft="18dp"
                  android:shadowDx="0"
                  android:shadowDy="0"
                  android:shadowRadius="10"
                  android:layout_width="wrap_content"
                  android:shadowColor="@android:color/white"
                  android:textColor="@color/titleColor">
              </TextView>    
          </RelativeLayout>
Was it helpful?

Solution

Try the following. I have not tested it. Let me know if you have any problems.

Define the styles like this

<style name="MyStyleText">
    <item name="android:textColor">#ff0000</item>
    <item name="android:textSize">12sp</item>
    <item name="android:shadowColor">#ffffff</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">0</item>
    <item name="android:shadowRadius">10</item>
  </style>

And in java file

RelativeLayout.LayoutParams lp ;
    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);


RelativeLayout.LayoutParams textLP ;
    textLP = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

RelativeLayout rl = new RelativeLayout(this);


TextView tv = new TextView(this);
tv.setText(getString(R.string.hello_world));
tv.setTextAppearance(getApplicationContext(), R.style.MyStyleText);

rl.addView(tv, textLP);
this.addContentView(rl, lp);

OTHER TIPS

// try this way,hope this will help you...

**activity_main.xml**

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <LinearLayout
        android:id="@+id/lnrMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>
</FrameLayout>

**MainActivity.java**

public class MainActivity extends Activity{

    private LinearLayout lnrMain;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lnrMain = (LinearLayout) findViewById(R.id.lnrMain);

        RelativeLayout mRelLayout00 = new RelativeLayout(this);
        RelativeLayout.LayoutParams mRelLayoutParams = new
                RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        mRelLayout00.setLayoutParams(mRelLayoutParams);
        TextView textView = new TextView(this);
        RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        textParams.setMargins(18,0,0,0);
        textView.setLayoutParams(textParams);
        textView.setTextSize(16);
        textView.setShadowLayer(10,0,0,android.R.color.white);
        textView.setText("menu");
        mRelLayout00.addView(textView);
        lnrMain.addView(mRelLayout00);
    }
}
LinearLayout ll = (LinearLayout)findViewById(R.id.my_linearlayout_in_first_xml);

View view = View.inflate(this, R.layout.second_xml, null);

ll.addView(view);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top