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

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.

Related

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);

android creating text view dynamically

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.

Change the text of textview based on its ID

I am dynamically adding a textView into a layout and giving it ID's. Based on some conditions I want to change the text of the TextView based on its ID assigned.
Something like this: personOne.setText("abcd"); where personOne.id = buttonID;
personOne = new TextView(this);
int buttonID = 2000 + personCount;
personOne.setTextAppearance(this, R.style.button_text);
personOne.setTextColor(getResources().getColor(R.color.red));
personOne.setTextSize(15);
personOne.setText(personAdded);
personOne.setId(buttonID);
Help / Suggestions much appreciated.
Thank you
Use something like this:
TextView tv = (TextView) findViewById(yourId);
tv.setText("abcd");
Assuming the TextView is a child of the Activity's content view.
You can use the buttonID to find and manipulate TextView dynamically as follows:
TextView dynamicTextView = (TextView) findViewById(buttonID);
dynamicTextView.setText("The text you want to set");

Android -Dynamic TextView over previous TextView

I am making an example of dynamic textview, that are created by pressing a button. the format in which they are created is similar to:
1- Textview (fist created)
2- TextView
3- TextView (last created)
I wonder if we can create them in an ascending order, one above the previous TextView.
something like this:
3- TextView (last created)
2- TextView
1- TextView (first created)
this is my example code:
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView("TextView"));
count = count +1;
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(count+ "- " + text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
return textView;
}}
You can specify the index where TextView has to be added in ViewGroup...
ViewGroup.addView(TextView, 0);
each time you are creating a TextView and adding to mLayout, add with the index 0 which will add on top of previous TextView
mLayout.addView(createNewTextView("TextView"),0);
You could create a LinearLayout and add one below the other, using layout.addView(TextView). I've a psedo code below which explains how it is done.
for(Option option : options){
view = View.inflate(mContext,R.layout.textView,null);
((TextView)view.findViewById(R.id.txt_key)).setText(" SOME TEXT : ");
((TextView)view.findViewById(R.id.txt_value)).setText("SOME VALUE ");
mLlCtr.addView(view);
}

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