Android instanceof detecting all widgets? - java

I'm trying to set a font across all TextView's by iterating through the LinearLayout's views and using instanceof.
The form currently consists of 4 TextView's and one Button.
The below code is detecting all Views, even the Button as a TextView?
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
If I log each view's class name it returns the TextView and the Button so I know the correct controls are being picked up.
The problem is the Button and TextView's fonts are being set, and I only want the TextView's.
I have found a work around and that is to do the following but am intrigued as to why the above code does not work as expected.
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v.getClass().getName().contains("TextView")) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
Is it because both Button and TextView are of type View? If so, what would be the correct way about doing this?
Any help appreciated, thanks. Ricky.

Actually, Button is a subclass of TextView! That's why you see it as a TextView (it is also a TextView).
http://developer.android.com/reference/android/widget/Button.html
public class Button extends TextView
You could make a second if instanceof to exclude Buttons or use
if (v.getClass() == TextView.class)
But this won't match any other subclasses of TextView (if you use them).

It is simple
Button Class extends TextView Class
Check the documentaion

Related

Edit text input into an array

Context: I am trying to make an application that allows me to have multiple edit texts (in a to do list format). I am using a linear layout with a scroll view inside of it so that I can allow users to have as many notes as they need.
Question: How can I put each editText inside an array, furthermore, how can I put the string contents of each of the editText in an array.
Any help would be much appreciated!
You need to get text from your EditText like this
EditText et = FindViewById<EditText>(Resource.Id.et_nnnn);
string a = et.Text;
And then add to your array
You can get all child views of parent layout in an array. use something as below. pass parent layout view to follow function
SparseArray<Edittext> array = new SparseArray<Edittext>();
.
.
.
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof Edittext) {
Edittext edittext = (Edittext) view;
array.put(edittext.getId(), edittext);
}
}
}
To get all edittexts text you can loop over that array and store in different array or list using gettext on each child.

Change Layout position from code

I am creating an app where I will be loading some images and data from a database, it should look like this:
Image______Name of user
Image______Name of user
Image______Name of user
etc..
I tried to create it just with a dummy image and some text to figure out how it works.
I create a LinearLayout, ImageView and a TextView, I add those two to the LinearLayout, and than I add that LinearLayout to a RelativeLayout.
The problem is, that all the images and text are placed in the same place, on top of each other. How can I change it so it is in the format I need?
relativeLayout = (RelativeLayout) findViewById(R.id.rel);
for(int i = 0; i< 30; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView hello = new TextView(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
String hi = "Hey";
if(i == 0){hi = "Hello0";}
if(i == 2){hi = "Hello2";}
if(i == 3){hi = "Hello3";}
if(i == 4){hi = "Hello4";}
hello.setText(hi);
layout.addView(imageView);
layout.addView(hello);
relativeLayout.addView(layout);
}
I am using a for to loop it a few times just for test.
Instead of RecyclerView, add the items in a LinearLayout. You can also set position where to add the new item in the LinearLayout.
I would suggest you do instead is:
create a model object for the user details(name and picture)
Use ListView or RecyclerView with a simple adapter add items to an
ArrayList of model object and notify the adapter when data is
changed.
This way you'll be reusing the views, and that'll improve the performance much better.
for examples, you can take a look at these sample projects.
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/ListView
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/RecyclerView

How Can I setText in the EditText without knowing id?

I'd like use the setText() function for an EditText without knowing the id,
so if there are three EditText in an application, the command setText() should change the text on all three and I won't know the ids.
I was planning to use this command:
EditText ed = (EditText) findView (something);
I don't think it is that simple. Is there a way to accomplish this?
You could put the TextViews alone inside a ViewGroup (i.e. LinearLayout) and iterate along their children like this.-
for (int i = 0; i < containerView.getChildCount(); i ++) {
View view = containerView.getChildAt(i);
if (view instanceof TextView) {
TextView textView = (TextView) view;
textView.setText(yourText);
}
}

Issue with dynamically added android radiobutton

I have an android app which asks a question followed by x number of options. Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
This I am doing using the following code
LayoutInflater inflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayoutThis = (LinearLayout)findViewById(R.id.lladrList);
for (int i =0 ;i< options.length; i++){
LinearLayout lladroption=(LinearLayout)inflater.inflate(R.layout.adroption, null);
//get view id and set values
ImageView iv = (ImageView) lladroption.findViewById(R.id.ivadroption);
iv.setId(OPTIONIMAGE+i);
iv.setImageBitmap(downloadFile(options[i].getOptionData().getDataURL()));
((ViewGroup) iv.getParent()).removeView(iv);
iv.setOnClickListener(this);
linearLayoutThis.addView(iv);
RadioButton rb = (RadioButton) lladroption.findViewById(R.id.rbadroption);
rb.setOnClickListener(this);
rb.setId(OPTIONRADIOBUTTON+i);
rb.setText(options[i].getOptionString());
((ViewGroup) rb.getParent()).removeView(rb);
linearLayoutThis.addView(rb);
// rg.addView(rb);
}
I want the radiobuttons to belong to a particular radiogroup. But if I do a
rg.addView(rb) instead of linearLayoutThis.addView(rb); in the above code where rg is the radiogroup view ... all the radiobuttons are display in one go. Instead of displaying text,image,radiobutton,text,image,radiobutton,text,image,radiobutton I get text,image,text,image,text,image,radiobutton,radiobutton,radiobutton.
Does anyone know how to resolve this?
You dont have to add those dynamically. create a view group or layout with the textview image and the radio button you require. use listview to get the x number of options. If this is your requirment you can reply so that i can help you with it.

Clone textview to append it to a ViewGroup

I have a ViewGroup defined in XML with a view inside, at onCreate time I'd like to have a variable of those.
I don't want to go through the hassle of using a listview+adapter cause its clearly overkill as I know the list won't change since onCreate()
This is more or less the code I'd like to have.
TextView mytextview = myViewGroup.findViewById(R.id.mytext);
for(String test : strings){
mytextview = mytextview.clone();
mytextview.setText(test);
myViewGroup.addView(mytextview);
}
But it is not working.
Maybe use an inflater, and put the textview in an external layout file:
View v = LayoutInflater.from(this).inflate(R.layout.textview_include, null);
viewGroup.addView(v);
Using code of Mathias Lin and using the hint from javahead76:
LinearLayout x = (LinearLayout) findViewById(R.id.container);
for (int i = 0; i < 5; i++) {
View c = LayoutInflater.from(this).inflate(R.layout.singlerow, x);
TextView t = ((TextView)findViewById(R.id.textView1));
t.setId(i+10000);
t.setText("text"+i);
}
TextView b = (TextView) findViewById(10003);
b.setText("10003");
If you do this, you will most likely get the exact same id for every view created this way. This means doing things like ((TextView)v).setText("some text"); will be called on every TextView previously inflated from the same layout. You can still do it this way, but you should call setId() and have some reasonable method for ensuring you do not get the same id twice in a row - incrementation or universal time, etc.
Also, I think Android reserves a certain range of id's for dynamically created id's. You might avoid ID's in this range; but honestly, I don't know the id system works so I could be wrong on this point.

Categories

Resources