Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get references on <include> elements from code?

Tags:

android

How can I get reference to <include> elements(Can I get reference by id to an element in layout that I add through <include> tag)?

like image 664
user21321 Avatar asked Dec 07 '25 20:12

user21321


1 Answers

Get reference by using findViewById

See the following Example

RelativeLayoutTesting.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class RelativeLayoutTesting extends Activity {

    android.widget.RelativeLayout currentView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );

        // set the relative layout as our view 
        setContentView(R.layout.main1);
        EditText et = (EditText) findViewById(R.id.myEditText);
        et.setText("My Edit");
    }
}

main1.xml

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

    <ImageButton
        android:id="@+id/alpha"
        android:layout_width="fill_parent"
        android:layout_height="50dip"
        android:background="#ff0000" />
    <include
        layout="@layout/edittext_layoout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

edittext_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

</EditText>
like image 109
Sunil Kumar Sahoo Avatar answered Dec 12 '25 00:12

Sunil Kumar Sahoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!