1

I am trying to write code , where after loading the Layout , i want to know which are the view elements are related to the layout like TextView, EditText , Checkbox etc.

MyCode :

MainActivity.java

package com.example.myview;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
    // How to get to know about the UI Elements related to this layout //

    }


}

demo_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 <LinearLayout 
           android:id="@+id/lnr"    
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#ddccee"
           android:orientation="vertical">

        <TextView 
            android:id="@+id/txt1"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:textSize="15dp"
            android:textColor="#ffffff"
            android:text="Test Layout"
            android:layout_gravity="center"
            android:gravity="center"/>

        <EditText 
            android:id="@+id/edit1"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:textSize="15dp"
            android:textColor="#ffffff"
            android:text="Test Layout"
            android:layout_gravity="center"
            android:gravity="center"/>


        <TextView 
            android:id="@+id/txt2"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:textSize="15dp"
            android:textColor="#ffffff"
            android:text="Test Layout"
            android:layout_gravity="center"
            android:gravity="center"/>


</LinearLayout>

This layout has TextView , EditText , so , i want to know how to get UI elements are related to this layout programmatically.

So , please suggest me some solution.

Manish
  • 1,215
  • 10
  • 29
MyCode
  • 289
  • 1
  • 5
  • 21

3 Answers3

1

You can use getChildCount REF to get the count of views added toViewGroup and then use getChildAt REF to get the View at given index.

You are using LinearLayout as base layout which already extends the ViewGroup Just change below line.

    LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
Manish
  • 1,215
  • 10
  • 29
  • Yes thanks but how can use ViewGroup... in the Activity – MyCode May 15 '15 at 08:47
  • yes this we can do that , so we need to have CustomView class – MyCode May 15 '15 at 08:52
  • yes i did view.getChildCount() , so i am getting value as 1 , but i think it should come 3 right... , please let me what's the problem – MyCode May 15 '15 at 08:56
  • Your primary LinearLayout has one child layout so value is 1 now if you get the child count at this layout that will e three – Manish May 15 '15 at 08:57
  • great i understood , but if i want consider the inner LinearLayout then , how can i get the child count... – MyCode May 15 '15 at 09:00
  • If you already know the View ids you should use view.findViewById() by passing the view id this is better approach. Above approach is when view ids are not known. – Manish May 15 '15 at 09:01
  • yes findViewById is good approach , i am only trying to it , when there will be many ui elements will be there in a layout. May be we can consider a big online registration form – MyCode May 15 '15 at 09:03
  • In that case keep getting the views by index and then check if its ViewGroup instance then get the child count for that and get views for that view group. This way you can get all views – Manish May 15 '15 at 09:04
  • So there will be an array of views – MyCode May 15 '15 at 09:06
  • No Array you keep using the approach as in answer just keep checking if view you got is viewgroup then get its child also – Manish May 15 '15 at 09:07
  • can we get to know the ViewGroup after loading a layout – MyCode May 15 '15 at 09:08
  • After getting the View you can always check instanceof ViewGroup for that View – Manish May 15 '15 at 09:09
  • so like this if(view instanceof LinearLayout){ } – MyCode May 15 '15 at 09:09
  • In Place of LinearLayout set ViewGroup – Manish May 15 '15 at 09:10
  • but the child count will always come 1 for the post layout , so can i get 3 childcount – MyCode May 15 '15 at 09:14
  • hey i found it check this out http://stackoverflow.com/questions/2597230/loop-through-all-subviews-of-an-android-view/18980154#18980154 – MyCode May 15 '15 at 09:20
0

After you've inflated your demo_layout just call findViewById on your view instance like this:

View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
TextView txt1 = (TextView) view.findViewById(R.id.txt1);
flyingAssistant
  • 872
  • 7
  • 19
  • oh !! man , this thing every Android developers knows , i want to know , how to get to know there is a Textview in the layout programatically – MyCode May 15 '15 at 08:52
  • yes no issue , may be you just misunderstood the question , but no worries , so can you please suggest me some solution – MyCode May 15 '15 at 08:56
0

Elements of view must be retrieved by view.findViewById(). Except in activity you use findViewById() directly to retrieve view elements of the contentView you set in setContentView().

CrazyOrr
  • 307
  • 2
  • 3
  • 13