android - How to know the View Elements after loading a layout? -
i trying write code , after loading layout , want know view elements related layout 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 know ui elements related 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 , , want know how ui elements related layout programmatically.
so , please suggest me solution.
you can use getchildcount
ref count of views added toviewgroup
, use getchildat
ref view
@ given index.
you using linearlayout
base layout extends viewgroup
change below line.
linearlayout view = (linearlayout)layoutinflater.from(this).inflate(r.layout.demo_layout, null);
Comments
Post a Comment