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);
}
Related
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.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt1 = (TextView) findViewById(R.id.txt1);
TextView txt2 = (TextView) findViewById(R.id.txt2);
TextView txt3 = (TextView) findViewById(R.id.txt3);
TextView txt = (TextView) findViewById(R.id.txt4);
String myStringValue = "Android programming is fun.";
txt1.setText(myStringValue);
txt2.setText(myStringValue.charAt(0) + "");
}
i want to choose more than one character in charAt().
charAt() always return one char. if you want more you must use other method or create loop for you purpose.
For example
String myStringValue = "Android programming is fun.";
StringBuilder textViewText = new StringBuilder();
for(int i = 0 ; i< myStringValue.length();i++){
if (i>=24 && i<=26){
textViewText.append(myStringValue.charAt(i));
}
}
Log.i("test",textViewText.toString());//print fun
Or use substring method
txt2.setText(myStringValue.substring(24,26) + "");//print fun
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("");
}
}
Hi I have Listview in that when I select an item I display another page where I am setting the text views with appropriate informations but when i select an item and go back and select another item the textviews are not getting updated ?BTW there are 10 text views can somebody provide me the solution for this? Thanks.
super.onCreate(savedInstanceState);
setContentView(R.layout.lastl);
Intent i = getIntent();
ACK = i.getStringExtra("ack-no");
NAME = i.getExtras().getString("appl name");
TRADE = i.getExtras().getString("trade");
PIN = i.getExtras().getString("pin");
MOBILE = i.getExtras().getString("mobile");
EMAIL = i.getExtras().getString("email");
LVO1 = i.getExtras().getString("LVO1");
LVO2 = i.getExtras().getString("LVO2");
tv1 = (TextView) findViewById(R.id.set_ack);
tv1.setText(ACK);
tv2 = (TextView) findViewById(R.id.set_apl);
tv2.setText(NAME);
tv3 = (TextView) findViewById(R.id.set_trd);
tv3.setText(TRADE);
tv4 = (TextView) findViewById(R.id.set_dr);
tv4.setText(ArrayAdapter.ar.get(0));
tv5 = (TextView) findViewById(R.id.set_flr);
tv5.setText(ArrayAdapter.ar.get(1));
tv6 = (TextView) findViewById(R.id.set_bdg);
tv6.setText(ArrayAdapter.ar.get(2));
tv7 = (TextView) findViewById(R.id.set_str);
tv7.setText(ArrayAdapter.ar.get(3));
tv8 = (TextView) findViewById(R.id.set_area);
tv8.setText(ArrayAdapter.ar.get(4));
tv9 = (TextView) findViewById(R.id.set_dis);
//tv9.setText(ArrayAdapter.ar.get(5));
tv10 = (TextView) findViewById(R.id.set_pin);
tv10.setText(PIN);
tv11 = (TextView) findViewById(R.id.set_mb);
tv11.setText(MOBILE);
tv12 = (TextView) findViewById(R.id.set_email);
tv12.setText(EMAIL);
tv13 = (TextView) findViewById(R.id.set_pn);
tv14 = (TextView) findViewById(R.id.set_bdgg);
tv15 = (TextView) findViewById(R.id.set_dt);
tv16 = (TextView) findViewById(R.id.set_lvo);
tv16.setText(LVO1);
String lvo2 = LVO2.substring(0, LVO2.length() - 1);
Log.d("sssssss", lvo2);
tv17 = (TextView) findViewById(R.id.set_lvo2);
tv17.setText(lvo2);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
break;
case R.id.button2:
break;
}
}
this is my listView code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listmain);
pv = getIntent().getStringArrayListExtra("Array_list");
System.out.println("ARRAYLIST---->" + pv);
final ListView list = (ListView) findViewById(R.id.listView1);
mylistData = new ArrayList<HashMap<String, String>>();
String[] columnTags = new String[]{"col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8"};
int[] columnIds = new int[]{R.id.col1, R.id.col2, R.id.col3, R.id.col4, R.id.col5, R.id.col6, R.id.col7, R.id.col8};
int k = 0;
for (int i = 0; i <= 9; i++) {
HashMap<String, String> map = new HashMap<String, String>();
//initialize row data
for (int j = 0; j <= 7/*pv.size()-1*/; j++) {
map.put(columnTags[j], pv.get(k));
put(columnTags[j], "row" + i + "col" + j);
k++;
}
mylistData.add(map);
}
SimpleAdapter arrayAdapter = new SimpleAdapter(this, mylistData, R.layout.rowmain, columnTags, columnIds);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parnet, android.view.View view, int position, long id) {
new LongOperation().execute();
String text = list.getItemAtPosition(position).toString();
String g = text.replace("{col5=", "");
String g1 = g.replace(" col4=", "");
String g2 = g1.replace(" col7=", "");
String g3 = g2.replace(" col6=", "");
String g4 = g3.replace(" col1=", "");
String g5 = g4.replace(" col3=", "");
String g6 = g5.replace(" col2=", "");
String g7 = g6.replace(" col8=", "");
String val[] = g7.split(",");
for (int m = 0; m < val.length; m++) {
Log.d("VALUE", val[m]);
ar1.add(val[m]);
}
System.out.println(ar1);
Log.d("VALUES-------->", text);
mob = ar1.get(0);
Log.d("VALUE1", mob);
pin = ar1.get(1);
Log.d("VALUE2", pin);
lvo = ar1.get(2);
Log.d("VALUE3", lvo);
email = ar1.get(3);
Log.d("VALUE4", email);
ack = ar1.get(4);
Log.d("VALUE5", ack);
trade = ar1.get(5);
Log.d("VALUE6", trade);
name = ar1.get(6);
Log.d("VALUE7", name);
lvo2 = ar1.get(7);
Log.d("VALUE8", lvo2);
}
});
}
From your code: ar1.add(val[m]);
I guess that ar1 is a List<String> variable of your listview activity?
If that's true, maybe add a call to clear() may work:
ar1.clear();
for(int m =0 ; m< val.length ; m++)
{
Log.d("VALUE",val[m]);
ar1.add(val[m]);
}
This question already has answers here:
How do I get the resource id of an image if I know its name? [duplicate]
(5 answers)
Closed 8 years ago.
I've got this in my activity.java file.
text[0] = (EditText) findViewById(R.id.editText2);
text[1] = (EditText) findViewById(R.id.editText3);
text[2] = (EditText) findViewById(R.id.editText4);
text[3] = (EditText) findViewById(R.id.editText5);
text[4] = (EditText) findViewById(R.id.editText6);
text[5] = (EditText) findViewById(R.id.editText7);
text[6] = (EditText) findViewById(R.id.editText8);
text[7] = (EditText) findViewById(R.id.editText9);
How can I write it with a for loop.
for(int i=o;i<8;i++)
text[i] = (EditText) findViewById(R.id.<what here>);
How to reference edittext with index here?
You can achieve this by using getIdentifier()
for(int i=0; i<8; i++) {
for(int j=0; j<8; j++) {
String editTextID = "btn" + i + "-" + j;
int resID = getResources().getIdentifier(editTextID
,"id", "com.example.yourpackagename");
buttons[i][j] = ((EditTextID) findViewById(resID));
}
}
You can use a constant array for this.
// in your class
private static final int[] TEXT_IDS = { R.id.editText2, R.id.editText3, ... , R.id.editText9};
//initializing
for(int i = o; i < TEXT_IDS.length; i++)
text[i] = (EditText) findViewById(TEXT_IDS[i]);
You could try something like the answer below, which is taken verbatim from here.
import java.lang.reflect.Field;
/* ... */
for (int i = 1; i < 16; i++) {
int id = R.id.class.getField("s" + i).getInt(0);
tv[i] = (TextView)findViewById(id);
tv[i].setTypeface(face);
tv[i].setClickable(true);
tv[i].setOnClickListener(clickListener);
}