1

I need to be able to add a textview to my app with code, not using the XML or Graphic interface... I have an imageview, but cannot figure out how to modify it to get a Textview (Im new to Android)...

Here is my imageview code:

//Add view using Java Code
ImageView imageView = new ImageView(AndroidAddViewActivity.this);
imageView.setImageResource(R.drawable.icon);
LayoutParams imageViewLayoutParams 
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(imageViewLayoutParams);

mainLayout.addView(imageView);

3 Answers 3

4
TextView textView = new TextView(this);
textView.setText("the text");
LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textViewLayoutParams);
mainLayout.addView(textView);
Sign up to request clarification or add additional context in comments.

1 Comment

what might be the data type for the mainLayout variable (View) ?
0

You can do so with the following:

TextView textView = new TextView(this);
textView.setText("the text");

Comments

0

You can add like

TextView nameHere = new TextView(this);
nameHere.setText("your text here");

This is just simple line of code you can modify it to do much more :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.