android creating text view dynamically - android

I'm new to android. just playing with the basic things. here's my code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String status = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(status);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff99ccff"
android:orientation="vertical" >
</LinearLayout>
when i run this on my phone it says, "unfortunately, the app has stopped"

Change the order like
setContentView(R.layout.activity_display_message);
linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
You must setContentView(...) first and then initialize your Views.

You can add text view dynamically in other way also :
Create method inside your activity :
private TextView getCustomTextView(Context context, String tvValue, String tvHint) {
TextView textView = new TextView(context);
textView.setText("" + tvValue);
textView.setTextColor(context.getResources().getColor(R.color.black));
textView.setTextSize(20);
// to set font family
Typeface face = Typeface.createFromAsset(getAssets(),
"fonts/epimodem.ttf");
textView.setTypeface(face);
textView.setHint(tvHint+""); // static
textView.setHint(context.getString(R.string.text_hint)); // from string file
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return textView;
}
Add text view in linearlayout.
linearLayout.addView(getCustomTextView(this, "text value in string", "hint value in string"));
You can more then one text view using same code.

Related

How can I reference a text view that is created through code?

public TextView descriptionTextView(Context context, String text) {
final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new EditText(context);
textView.setLayoutParams(lparams);
textView.setTextSize(10);
textView.setTextColor(Color.rgb(0,0,0));
textView.setText(" " + text + "");
textView.setMaxEms(8);
textView.setKeyListener(null);
textView.setFocusableInTouchMode(false);
textView.setEnabled(false);
return textView;
}
Here is the code that I have written for the TextView.
I would like to reference it from another classes, or within the same class, but I cannot find a way to pin it down; as in I would like to change the value based on an input.
Give TextView a id and then access the textView via id like below -
give id like textView.setId(1);
public TextView descriptionTextView(Context context, String text) {
final ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new EditText(context);
textView.setLayoutParams(lparams);
textView.setTextSize(10);
textView.setId(1);
textView.setTextColor(Color.rgb(0,0,0));
textView.setText(" " + text + "");
textView.setMaxEms(8);
textView.setKeyListener(null);
textView.setFocusableInTouchMode(false);
textView.setEnabled(false);
return textView;
}
let say your layout for textView is LinearLayout.
you can get textView by id by using below code -
TextView tv = (TextView)view.findViewById(1); // view is LinearLayout object
Hope this helps!
This is the code to create TextView Dynamically
LinearLayout layout = findViewById(R.id.linearLayout);
TextView view = descriptionTextView(this, "Sample text");
layout.addView(view);
Below method for calling it from same/different class:
public TextView descriptionTextView(Context context, String text){
ViewGroup.LayoutParams lparams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView (context); //Change this to TextView
textView.setLayoutParams(lparams);
textView.setTextSize(10);
textView.setTextColor(Color.rgb(0,0,0));
textView.setText(" " + text + "");
textView.setMaxEms(8);
textView.setKeyListener(null);
textView.setFocusableInTouchMode(false);
textView.setEnabled(false);
return textView;
}
Creating Views other them XML file is not a preferred method in Android.
The best approach is to make this TextView in XML file and link it using its id.
like this.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
Futher use this anywhere in you classes and activity by creating an object like this.
TextView textView = findViewById(R.id.textView);
//to change value use this
textView.setText("Value you want to set");
//to change its visibility
textView.setVisibility(textView.GONE);
//to get its string
String value = textView.getText().toString();
If this method is not helpful and you still want to make views in Java file then, make them public and out of function boundaries to make them accessible everywhere.

Is there a way to change background color behind a button (button.setBackgroundResource)

I'm a new Android dev student, and I'm trying to create a dynamic layout witch contains a TextView and a button inside the same row.
but I have a little problem.
I set my Button in my Drawables ressources by
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
and now, I cannot change the backgroundcolor behind it.
I have created a newLinearlayout inside my main LinearLayout and have created a new textView and a new Button.
I have put them inside the LinearLayout's child and put it inside the main.
That's work but not the background color behind my button.
is there a way to do that?
my xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/grey"
android:id="#+id/historyLayout">
</LinearLayout>
my complete activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
mLinearLayout = findViewById(R.id.historyLayout);
mMoodSaved = new ArrayList(7); // Define the max size of my ArrayList
loadData();
for (int i = 1; i <= 7; i++) {
final TextView textView = new TextView(this);
mLinearLyt = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
mLinearLyt.setOrientation(LinearLayout.HORIZONTAL);
textView.setHeight(300);
textView.setWidth(400);
textView.setBackgroundColor(Color.RED);
textView.setTextColor(Color.BLACK);
textView.setTextSize(12);
textView.setText(String.valueOf(i));
mLinearLyt.setBackgroundColor(Color.YELLOW);
mLinearLyt.addView(textView);
mLinearLyt.setLayoutParams(params);
ImageButton button = new ImageButton(this);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
param2.setMargins(20,50,0,0);
param2.height = 100;
param2.width = 100;
button.setLayoutParams(param2);
button.setBackgroundResource(R.drawable.ic_comment_black_48px);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
soundConfirm();
Toast.makeText(getApplicationContext(), textView.getText(), Toast.LENGTH_LONG).show(); //Display Toast Message
}
});
mLinearLyt.addView(button);
mLinearLayout.addView(mLinearLyt);
}
}
Since this an ImageButton, set
button.setImageResource(R.drawable.ic_comment_black_48px)
instead of setBackgroundResource.

TextView auto create new line when end of the line and edittex not align with textview

I have 4 TextView: " what is your name?", "in some circumstances" ,you might in some circumstances,you might", "________" and " to help user understand why your app needs a permission.For example " and a Edittext.
Now I want create a layout and when I user layout add view in my class ( not file.xml ) it can show same :
I used custom layout same here : Custom RowLayout and add view in code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
int permissionCheck =
rowLayout = (RowLayout) findViewById(R.id.row);
TextView tv1 = new TextView(this);
tv1.setText("what is your name?");
rowLayout.addView(tv1);
EditText ed1 = new EditText(this);
ed1.setWidth(50);
rowLayout.addView(ed1);
TextView tv2 = new TextView(this);
tv2.setText(" in some circumstances ,you might in some circumstances,you might");
rowLayout.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText("_____");
rowLayout.addView(tv3);
TextView tv4 = new TextView(this);
tv4.setText("to help user understand why your app needs a permission.For example");
rowLayout.addView(tv4);
<convert.htv.ex.convertdata.RowLayout
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"></convert.htv.ex.convertdata.RowLayout>
and result
You can see two problems in here:
1. Edittext not in line with text view.
2. when string text length it create a new line. I want a part off textview will show in old line and when end of the line it will show the rest of textview in newline ( sample picture 1).
How I can do it? please help me.
1)Answer for first problem,
add this line of code
tv1.setGravity(Gravity.CENTER)
2) Change this
ed1.setWidth(50);
to
ed1.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);

MyFirstApp android tutorial error on DisplayMessageActivity.java

I been following it and i am stuck on 3 textview errors
tutorial:http://developer.android.com/training/basics/firstapp/starting-activity.html
I get 2 error saying textView cannot be resolved and one saying textView cannot be resolved as a variable. help!
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textview = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
setContentView(R.layout.activity_display_message);
// Show the Up button in the action bar.
setupActionBar();
`
You've declared it as textview (lowercase "v") but are referencing it as textView (uppercase "v"). Pick one!
TextView textView = new TextView(this); // Change the "v" to uppercase
textView.setTextSize(40);
textView.setText(message);
Variables in Java are case sensitive, so textView and textview are different. Change your code to this:
TextView textview = new TextView(this);
textview.setTextSize(40);
textview.setText(message);
// Set the text view as the activity layout
setContentView(textview);

Sending data to other activity and display some additional TextViews

I have an Android application in which i send data from one activity to another. I can do that part easily but the problem is i want to display some additional TextViews as well. So when i display the sent data using setContentView i can see only the sent data. I cannot see the additional TextViews. I want both of them to be displayed simultaneously.
I searched and tried hard to find a solution without any luck.I am attaching the code for reference.Thanks!
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
TextView textViewci= new TextView(this);
textViewci.setText(message);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
When i use this code,i am unable to see anything and the activity remains blank.Actually i am using a TableLayout here so that i can display data in form of tables
You have to add text view on your layout.
View linearLayout = findViewById(R.id.info);
//LinearLayout layout = (LinearLayout) findViewById(R.id.info);
TextView valueTV = new TextView(this);
valueTV.setText("hallo hallo");
valueTV.setId(5);
valueTV.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
((LinearLayout) linearLayout).addView(valueTV);
You have to add the text view in the parentview. For example, if you have used the text view in the linear layout then add the text view in the linear layout and then use the addview(linearlayout).
Below is the example
RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");
tv1.setId(23);
TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
layout.addView(tv1);
layout.addView(tv2, lp);
I have just make a test program for you. first class code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, SecondClass.class);
intent.putExtra("firstValue", "1");
intent.putExtra("secondValue", "2");
startActivity(intent);
}
});
Second class code :
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message1 = intent.getStringExtra("firstValue");
String message2 = intent.getStringExtra("secondValue");
RelativeLayout layout = new RelativeLayout(this);
TextView textView1= new TextView(this);
TextView textView2= new TextView(this);
TextView textViewci= new TextView(this);
textViewci.setText(message1+""+message2);
Log.e("intent value is", ""+message1+""+message2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, textView1.getId());
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
layout.addView(textView1);
layout.addView(textView2);
addContentView(layout, lp);
Try and let me know. YOu have to make related layouts for both classes.
Hope this will help
are you getting any error messages? if yes then please post them here. and the others are right.you need to add yours views to the parent layout.
I have found out the answer by myself.It's fairly easy and there is no need here to use views in the parent layout.I modified the textViews in my xml file and got their values using findViewById.I am attaching the updated code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop_information);
Intent intent = getIntent();
String message = intent.getStringExtra(CropsListActivity.EXTRA_MESSAGE);
textView=(TextView)findViewById(R.id.textViewci);
textView1=(TextView)findViewById(R.id.textViewmd);
textView2=(TextView)findViewById(R.id.textViewso);
textView.setText(message);
textView1.setText("Major Districts");
textView2.setText("Suitable Soil");
tf = Typeface.createFromAsset(getAssets(), "fonts/SHRUTIB.TTF");
textView.setTypeface(tf);
}

Categories

Resources