The setContentView()
call is supposed to be used to set the layout of the full screen. What you're doing currently in your Activity code is setting just a TextView as the full view of the screen, so the Activity has no reference to the XML layout that you created. This is why your 3 lines of code at the end fail, because the TextView
is trying to setup its LayoutParams
for how its parent should place and measure it, however it has no parent in this context. What I would recommend doing is giving an id
attribute to the RelativeLayout
in the XML to get a reference to it in Activity
code like so:
<?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="home_screen_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"/>
Then in your Activity
code, adjust it so that you call with the resource id of your XML file. If we assume it's called act_main.xml
in the layout folder of your resources directory (i.e. in src/main/resources/layout/act_main.xml
), you would call setContentView(R.layout.act_main)
as the first line in onCreate()
after the super()
call so that the framework has an opportunity to parse your XML and inflate it (i.e. instantiate, make calculations on the size and
determine placement of its components among other things). After that, use findViewById(R.id.home_screen_layout)
to get a reference to that RelativeLayout
so that you may create a new TextView
and add it to your already inflated layout.
package com.example.android.testerapp1;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
// make your view components private members as findViewById calls are expensive for the framework
private RelativeLayout homeScreenLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Have the activity inflate the XML file with your RelativeLayout
setContentView(R.layout.act_main);
// Now that it is inflated, get a reference to that parent
homeScreenLayout = (RelativeLayout) findViewById(R.id.home_screen_layout);
// Dynamically create a TextView associated with this Activity's context
TextView homeScreen = new TextView(this);
homeScreen.setText("Welcome to Test App 001" + "\nThis TextView was created dynamically in Java!");
homeScreen.setTextSize(24);
homeScreen.setTextColor(Color.CYAN);
homeScreen.setCursorVisible(true);
homeScreen.setPadding(16,56,16,56);
homeScreen.setBackgroundColor(Color.BLACK);
homeScreen.setGravity(Gravity.CENTER);
//dynamically set width to dp (converted to pixels ~600) and height to 'wrap content'
// convert dp amount to pixels for size
final float scale = getResources().getDisplayMetrics().density;
int pixelWidth = (int) (2000 / scale + 0.5f);
// Adjust the placement in the parent
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(pixelWidth , RelativeLayout.LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE); // make sure to use the function which takes a boolean value for rules like CENTER_IN_PARENT
homeScreen.setLayoutParams(params); // Add these parameters to the textview
// Let the layout know about your newly created textview so that it can re-draw its canvas
homeScreenLayout.addView(homeScreen);
}
}
As a note, I will add that a of what you're doing can be done in the XML with relative ease, but since you asked about setting it programmatically specifically, I won't go into detail on that aspect. But if you're interested in some structured resources, I would recommend checking out the Android Developer Guide, specifically the section on XML layouts and how they interact with Activities
EDIT: Note the changes I made to the code for the Activity. The major pieces are first inflating the empty RelativeLayout xml with setContentView(int id)
, and then adding the other TextView
to the given layout. There was a minor error in the code I presented concerning the CENTER_IN_PARENT
line. According to the [docs](https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int, int)), you must use the addRule(int, int)
version of the function when adding rules that use a boolean value.