Dynamically adding data to more TextView - android

Is it possible to dynamically add text to several textview that are defined in xalm? For example, in a loop:
var textView1 = FindViewById<TextView>(Resource.Id.textView1);
var textView2 = FindViewById<TextView>(Resource.Id.textView2);
var textView3 = FindViewById<TextView>(Resource.Id.textView3);
var content = "Add me to more at one textView"
for (i = 1; i = 3; i++)
{
// add me;
}
I can easily do it as it adds textview programmatically by calling GetChildAt () in layout. Is it possible to add dynamic if textView is defined in xalm?

Say your texts ids are text1, text2, tex3, etc. Use:
for (int i=1; i<numberOfTextsViews; i++) {
int id = this.getResources().getIdentifier("text" + i, "id", getPackageName());
TextView temp = findViewById(id);
temp.setText("text you want to add to all your texts");
}

I write like this:
public TextView FindId(string resourceId)
{
var type = typeof(Resource.Id);
var field = type.GetField(resourceId);
int res = (int)field.GetRawConstantValue();
TextView tx = FindViewById<TextView>(res);
return tx;
}
and:
for (int i = 0; i <= 34; i++)
{
TextView tx = FindId($"textView_day{i}");
tx.Text = day[i];
tx.Tag = tx.Text;
tx.Click += Day_Click;
}
THX a lot Mike087 for getting me on the right track.

Related

Dynamically adding a ViewGroup to an active Layout

I'm looking to insert items into lists in the View below when any of the add buttons are clicked. What I'd like is to display all the information that is being collected, but I'm not really sure how to add a ViewGroup programatically and insert it into the ConstraintLayout in the correct locations while bumping all the corresponding items below.
I've updated the button code and now I'm running into a mess of other problems, currently when the button is clicked a TextView that reads "Name: " appears and is properly inserted into the Layout, but it seems that something isn't going right when I set the id for textView name and it either isn't being recorded or I'm not using the methods correctly.
Button Code
protected void addWeap(View view)
{
int id = 0;
int weapons = 0;
String weaponAdder = "";
int weaponPointer = 0;
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.conLayout);
EditText weapN = (EditText) findViewById(R.id.weapName);
EditText weapAB = (EditText) findViewById(R.id.weapattbns);
EditText weapDMG = (EditText) findViewById(R.id.weapDmg);
EditText weapInfo = (EditText) findViewById(R.id.weapInfo);
TextView proItem = (TextView) findViewById(R.id.proItem);
String weapName = "";
String weapAttBns = "";
String weapdmg = "";
String weapinfo = "";
weapName += weapN.getText();
weapAttBns += weapAB.getText();
weapdmg += weapDMG.getText();
weapinfo += weapInfo.getText();
weapons++;
weaponAdder += weapName + "|";
weaponAdder += weapAttBns + "|";
weaponAdder += weapdmg + "|";
weaponAdder += weapinfo + "|";
weapN.getText().clear();
weapAB.getText().clear();
weapDMG.getText().clear();
weapInfo.getText().clear();
TextView name = new TextView(this);
params.topToBottom = weaponPointer;
params.leftToLeft = R.id.parent;
name.setText(R.string.namePrint);
name.setTextColor(R.color.black);
name.setLayoutParams(params);
name.setPadding(8,8,0,0);
name.setId(id);
id++;
weaponPointer = id;
constraintLayout.addView(name);
params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
TextView weaponName = new TextView(this);
params.baselineToBaseline = id;
params.leftToRight = id;
params.rightToRight = R.id.parent;
weaponName.setText(weapName);
weaponName.setTextColor(R.color.black);
weaponName.setLayoutParams(params);
weaponName.setPadding(8,8,8,0);
constraintLayout.addView(weaponName);
params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
params.leftToLeft = R.id.parent;
params.rightToRight = R.id.parent;
params.topToBottom = id;
proItem.setPadding(8,8,8,0);
proItem.setLayoutParams(params);
}
Pre-Button Push
Post-Button Push

Android - Loop to collect all editText values

I have around 50 EditText fields and I want to get their values using a loop. Getting the values individually as below works fine:
SharedPreferences settings = getSharedPreferences(filename, 0);
SharedPreferences.Editor editor = settings.edit();
editor.clear(); //not sure if this is required
final EditText t1 = (EditText) findViewById(R.id.text1Value);
String T1 = t1.getText().toString();
editor.putstring("text1",T1);
final EditText t2 = (EditText) findViewById(R.id.text2Value);
String T2 = t2.getText().toString();
editor.putstring("text2", T2);
................................ and so on till EditText t50.
I have tried achieving this through the loop below but couldn't get it to work.
for(int x=1; x<50; x++)
{
EditText et[x] = (EditText) findViewById(R.id.text[x]Value);
String t[x] = et[x].getText().toString();
String Ref = "text" + x;
editor.putString(Ref, t[x]);
}
You can try this by creating an array of the ids of the textview and looping through the array.
Try this:
int[] ids = new int[]{R.id.text1Value,R.id.text2Value,R.id.text3Value};//and so on
int i =1;
for(int id : ids){
EditText t = (EditText) findViewById(id);
String Ref = "text" + i;
editor.putString(Ref, t.getText().toString());
i++;
}
If I assume you have all of these EditTexts inside a Layout called ll (should work for structures other than Layout too, such as a ViewGroup etc.):
ArrayList<String> values = new ArrayList<String>();
for (int i = 0; i < ll.getChildCount(); i++) {
if (ll.getChildAt(i) instanceof EditText) {
EditText et = (EditText)ll.getChildAt(i);
values.add(et.getText().toString());
}
}
My Android is a little rusty so apologies if there is something wrong with that, but should give you the general idea anyway
int[] ids = new int[]{R.id.text1Value,R.id.text2Value,R.id.text3Value};//and so on
int i =1;
for(int id : ids){
EditText t = (EditText) findViewById(id);
String Ref = "text" + i;
editor.putString(Ref, t.getText().toString());
i++;
}
private void clearScreen() {
int[] _ids = null;
_ids = new int[]{R.id.etIdTAtividade, R.id.etNomeTAtividade, R.id.etNmAtividade};
for (int i = 0; i < (_ids.length); i++) {
EditText t = (EditText) findViewById(_ids[i]);
t.setText("");
}
}

Initializing Widgets in android

I have 50 textviews.can i avoid initializing all the textviews like this.Is there is any code to initialize all these widgets automatically
t1 = (TextView) myFragmentView.findViewById(R.id.tvone);
t2 = (TextView) myFragmentView.findViewById(R.id.tvtwo);
t3 = (TextView) myFragmentView.findViewById(R.id.tvthree);
t4 = (TextView) myFragmentView.findViewById(R.id.tvfour);
t5 = (TextView) myFragmentView.findViewById(R.id.tvfive);
t6 = (TextView) myFragmentView.findViewById(R.id.tvsix);
t50 = (TextView) myFragmentView.findViewById(R.id.tvfifty);
you can get all EditTexts in your View by this method :
Method to get all EditTexts in a View
also you can create a map that contains "ID ,Textview"
HashMap<Integer,EditText> myEditTextList = new HashMap<Integer,EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.put( ((EditText) myLayout.getChildAt( i )).getId() ,
((EditText) myLayout.getChildAt( i )));
You can initialize all TextViews by renaming them to "tv1 ... tv50", and use this code:
int ressourceId;
int tv_number = 50;
TextView[] tv = new TextView[tv_number];
for(int i = 0; i < tv_number; i++) {
ressourceId = getResources().getIdentifier("tv" + (i + 1), "id", context.getPackageName());
tv[i] = (TextView) myFragmentView.findViewById(ressourceId);
}

Looping through TextView and setting the text

I have exactly 20 TextView and their id is in sequence, i.e. :
R.id.textView1, R.id.textView2, R.id.textView3 ...
I have a for loop:
for (int i = 1; i < 21; i++) {
TextView textView = (TextView) findViewById(R.id.textView ...);//
textView.setText("...");
is there a way to get TextView using this for loop and set their text?
if you gave to your TextView  as id R.id.textView1.. R.id.textView21, you ca use getIdentifier to retrieve the TextViews id from its name
for (int i = 1; i < 21; i++) {
String name = "textView"+i
int id = getResources().getIdentifier(name, "id", getPackageName());
if (id != 0) {
TextView textView = (TextView) findViewById(id);
}
}
The more efficient way would be creating an array of integers and iterate through it:
int[] textViewIDs = new int[] {R.id.textView1, R.id.textView2, R.id.textView3, ... };
for(int i=0; i < textViewIDs.length; i++) {
TextView tv = (TextView ) findViewById(textViewIDs[i]);
tv.setText("...");
}
This thread is very hold, but I faced the same sort of need : iterate through my layout structure and do something on each TextView. After having googlized it in many way, I finally decided to write my own implementation. You can find it here:
/* Iterates through the given Layout, looking for TextView
---------------------------------
Author : Philippe Bartolini (PhB-fr # GitHub)
Yes another iterator ;) I think it is very adaptable
*/
public void MyIterator(View thisView){
ViewGroup thisViewGroup = null;
boolean isTextView = false;
int childrenCount = 0;
try {
thisViewGroup = (ViewGroup) thisView;
childrenCount = thisViewGroup.getChildCount();
}
catch (Exception e){
}
if(childrenCount == 0){
try {
isTextView = ((TextView) thisView).getText() != null; // You can adapt it to your own neeeds.
}
catch (Exception e){
}
if(isTextView){
// do something
}
}
else {
for(int i = 0; i < childrenCount; i++){
MyIterator(thisViewGroup.getChildAt(i));
}
}
}

How to sort numbers in TextViews and display them?

Hi can some one suggest me a sample example of how i can sort the textviews based on the numbers in textviews. I am able to get the text from the TextViews need to sort and place the lowest number first.
Thank you.
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
for (int i = 0; i < intValues.length; i++) {
Integer intValue = intValues[i];
//here I want to assign sorted numberes to the TextViews
}
}
So I have followed Jeffrey's advice. Here is the code which still doesn't work properly. What could be wrong?
Created an array of TextViews:
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
tvs[2] = textView43;
tvs[3] = textView53;
tvs[4] = textView63;
tvs[5] = textView73;
tvs[6] = textView83;
Sorted the array and assinged new values to the TextViews:
Arrays.sort(tvs, new TVTextComparator());
textView23.setText(tvs[0].getText().toString());
textView33.setText(tvs[1].getText().toString());
textView43.setText(tvs[2].getText().toString());
textView53.setText(tvs[3].getText().toString());
textView63.setText(tvs[4].getText().toString());
textView73.setText(tvs[5].getText().toString());
textView83.setText(tvs[6].getText().toString());
And here is the Comporator class:
public class TVTextComparator implements Comparator<TextView> {
public int compare(TextView lhs, TextView rhs) {
Integer oneInt = Integer.parseInt(lhs.getText().toString());
Integer twoInt = Integer.parseInt(rhs.getText().toString());
return oneInt.compareTo(twoInt);
}
}
to sort your textViews, first put them in an array,
TextView[] tvs = new TextView[7];
tvs[0] = textView23;
tvs[1] = textView33;
// and so on
note that if you have handle to the parent container, you could easily build the array by using ViewGroup.getChildCount() and getChildAt().
now write a comparator for a text view,
class TVTextComparator implements Comparator<TextView> {
#Override
public int compare(TextView lhs, TextView rhs) {
return lhs.getText().toString().compareTo(rhs.getText().toString());
// should check for nulls here, this is NOT a robust impl of compare()
}
}
now use the comparator to sort the array,
Arrays.sort(tvs, 0, tvs.length, new TVTextComparator());
public void sortNumbers(View v) {
String[] numbers = new String[7];
numbers[0] = textView23.getText().toString();
numbers[1] = textView33.getText().toString();
numbers[2] = textView43.getText().toString();
numbers[3] = textView53.getText().toString();
numbers[4] = textView63.getText().toString();
numbers[5] = textView73.getText().toString();
numbers[6] = textView83.getText().toString();
Integer[] intValues = new Integer[numbers.length];
for (int i = 0; i < numbers.length; i++) {
intValues[i] = Integer.parseInt(numbers[i].trim());
}
Collections.sort(Arrays.asList(intValues));
textView23.setText(intValues[0]);
textView33.setText(intValues[1]);
textView43.setText(intValues[2]);
textView53.setText(intValues[3]);
textView63.setText(intValues[4]);
textView73.setText(intValues[5]);
textView83.setText(intValues[6]);
}

Categories

Resources