Get count of child views? - android

I have a LinearLayout which includes x amount of LinearLayouts (children) and a bunch of other views such as TextViews, Relative views etc..
How does one return the amount of children LinearLayout views only?

Normally you would use ViewGroup.getChildCount(), but since you want only the count of LinearLayouts you would need to create an algorithm to do so:
public int linearLayoutChildCount(ViewGroup parent) {
int count = 0;
for (int x = 0; x < parent.getChildCount(); x++) {
if (parent.getChildAt(x) instanceof LinearLayout) {
count++;
}
}
return count;
}
Of course, doing something like this is not ideal since it uses reflection which can be slow on Android.

Related

Android: How to programmatically select a group of Views and modify it?

My requirement is to apply '6' different colors to '6' ImageViews on programatically. In my Layout i can have more than six ImageViews and these colors has to be applied only to those specific ImageViews.
for (int i = 0; i < view.getChildCount(); i++) { }
But how do i identify those specific 6 ImageViews among all the views?
You can mark specific ImageViews with setTag() function (eg. imageView.setTag("Specific")) on their initialization and after that do next
for(i = 0; i < view.getChildCount(); i++){
//check for view is ImageView
if(view.getChildAt(i) instanceof ImageView){
//check for tag
if(((String) view.getChildAt(i).getTag()) == "Specific"){
//code for set color
}
}
}
Hope it helps

How to get each value from each EditText from a GridView or GridLayout?

I'm still a novice at Android Studio and I have one more task to do on my first Linear Algebra-related project before I am comfortable with coding the rest of it.
The task is to get each value of EditText in a matrix of EditTexts and store them into an int[][] matrix.
Now I'm really stuck on this for hours and the main reason is because I don't know how to probably use GridView and GridLayout. I also don't know which one is better to use for this.
Below is a representation of what I'm trying to do:
Let's say that the user wants to calculate with 2x3 matrices
The app will then display a 2x3 matrix of empty EditTexts:
And the user entered whatever numbers they want in the empty EditTexts
When they tap on the "Next Matrix" button, I want to get the numbers from each EditText and put them into an int[][] matrix so that can transfer it to another Activity.
If I can do that, I'll be able to print out the values of each EditText as a TextView.
And again, which Grid is better to use in order to place the matrix of EditTexts? GridView or GridLayout? How do I get the values of each EditTexts from them?
I think your problem on displaying matrix grid was solved. You might need to get the Matrix contents now. Try this
// SETUP YOUR GRIDVIEW
GridView mGridView = (GridView)findViewById(R.id.grid);
// COUNT TOTAL CHILDS IN IT
int gridchilds = mGridView.getChildCount();
// ITERATE THROUGH EACH CHILDS
for(int i = 0; i < gridchilds; i++)
{
// SELECT 'i' th CHILD IN A VIEWGROUP
ViewGroup gridChild = (ViewGroup) mGridView.getChildAt(i);
// GET THE CHILDREN SIZE OF THAT 'i' th CHILD
int childSize = gridChild.getChildCount();
// ITERATE THOUGH EACH
for(int k = 0; k < childSize; k++)
{
// CHECK IF IT IS AN 'EditText'
if( gridChild.getChildAt(k) instanceof EditText)
{
// GET THE MATRIX VALUE AT THAT CELL
String matrix_value=gridChild.getChildAt(k)).getText().toString();
}
}
}
Hope it helps
You can achieve it by using RecyclerView with GridLayoutManager.
Try the following - however I'm not sure the last method's logic (in getValues()) is correct. You may need to tweak up there.
(Looks like you're familiar with mathematic more than I do, so I leave it there for you ;))
Hope it'll work
RecyclerView rv = (RecyclerView) findViewById(your_recycler_view_id);
rv.setLayoutManager(new GridLayoutManager(context, COLUMN_COUNT, GridLayoutManager.VERTICAL, false));
rv.setAdapter(new YourCustomAdapter());
class YourCustomAdapter() {
EditText[][] editTexts = new EditText[COLUMN_COUNT][ROW_COUNT];
public YourCustomAdapter() {
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(your_edit_text_layout, parent, false);
return new YourViewHolder(view, listener);
}
#Override
public void onBindViewHolder(final RecyclerView.ViewHolder h, int position) {
YourViewHolder holder = (YourViewHolder) h;
editTexts[position / COLUMN_COUNT][position % COLUMN_COUNT] = holder.editText;
}
#Override
public int getItemCount() {
return COLUMN_COUNT * ROW_COUNT;
}
private class YourViewHolder extends RecyclerView.ViewHolder {
EditText editText;
private YourViewHolder() {
super(itemView);
this.editText = itemView.findViewById(your_edit_text_id);
}
}
public int[][] getValues() {
int [][] values = new int[ROW_COUNT][CULLOMN_COUNT];
for(int i = 0; i < ROW_COUNT; i ++) {
for(int j = 0; j < CULLOMN_COUNT; j ++) {
values[i][j] =
Integer.parseInt(editTexts[i][j].getText().toString());
}
}
}
}

ForEach element in layout or screen

I have been trying to hack a foreach to all elements of type label in screen or layout, with no luck!
My goal is to translate all screen1.labels.text, the translation are in list which has lists of pair (label.text, translation).
Is this possible in App-inventor?
Have you thought of getting an array list of sub view and iterating thought them. the below will work for direct children of the screen. if you need sub view as well you can use recursion
RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_screen_to_search);
for (int i = 0; i < layout.getChildCount(); i++)
{
View child = layout.getChildAt(i);
if(child instanceOf TextView)
{
txtView = (TextView)child;
if(txtView.getText().length!=0)
{
yourTranslateFunction(txtView.getText());
}
}
}

Restrict the scroll up of list view after a particular row

The title is little confusing let me explain my problem ,i have some data in list view and its is scrollable and there is no issue in that sort of problem.my requirement is while i scroll and for example the list views first visible portion is 2 then it should stop the scroll up.But we can scroll down,its like restring the scroll up of list view after a particular row is the first visible item in that list view.i tried some of the code but it has a problem it disable the entire scroll i cant scroll down the list view i use this method to disable the scroll of listview
public void enableDisableView(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int idx = 0; idx < group.getChildCount(); idx++) {
enableDisableView(group.getChildAt(idx), true);
}
for (int idx = 0; idx < group.getChildCount(); idx++) {
enableDisableView(group.getChildAt(idx), true);
}
}
}
i also used
mListView.setSelectionFromTop(1, 0);,smooth scroll etc
but it does not works.is there any idea or solution to achieve this aim.
Thank you

getting checkBox element count

Working with apis, I am uncertain of the number of checkBoxes that will appear in the application. How can get the count of the number of checkBoxes produced in the application ?
Also, these checkBoxes are declared inside a listView.
You can loop though all the child views in you layout and find out how many of them are checkboxes. Looping and checking is done in following way:
ll is LinearLayout here
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
if (view instanceof CheckBox) {
count++;
}
}

Categories

Resources