Question

here is my code i got error params is null

     viewDynamic = new TextView(getApplicationContext());
     RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
     RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)viewDynamic.getLayoutParams();
     params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
     params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
     params.addRule(RelativeLayout.ABOVE,userEditTxt.getId());


     viewDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
     viewDynamic.setText(userEditTxt.getText());
     viewDynamic.setTextColor(Color.RED);
     viewDynamic.setTextScaleX(2);
     viewDynamic.setTextSize(20);
     viewDynamic.setPadding(10, 10, 10, 10);
   //viewDynamic.setVisibility(View.GONE);

     relativeLayout.addView(viewDynamic,params);
Was it helpful?

Solution

If you haven't added the view to any parent container, or havent called setLayoutParams manually, then getLayoutParams() will return null.

Make soure you call viewDynamic.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); before you call viewDynamic.getLayoutParams();, or add the view to a container, so LayoutParams will be generated for the view.

I suggest you add your View like this:

 viewDynamic = new TextView(getApplicationContext());
 RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
 RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
 params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
 params.addRule(RelativeLayout.ABOVE,userEditTxt.getId());

 viewDynamic.setText(userEditTxt.getText());
 viewDynamic.setTextColor(Color.RED);
 viewDynamic.setTextScaleX(2);
 viewDynamic.setTextSize(20);
 viewDynamic.setPadding(10, 10, 10, 10);

 relativeLayout.addView(viewDynamic,params);//you attach params to your View here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top