androidandroid-layoutandroid-include

Android - how to access buttons inside include layout


I created header.xml and include thus in Main.xml and Menu.xml. In header.xml i have a button.

Header.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"

>
    <ImageView
        android:layout_width="150dp"
        android:layout_height="100dp"
        android:src="@drawable/logo"
        android:id="@+id/header"
        android:paddingTop="10px"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/insideButtons">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Main Menu"
            android:gravity="right"
            android:layout_marginRight="50px"
            android:id="@+id/mainMenu"
            android:textColor="#604811"
            android:textSize="20dp"

            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="About University"
            android:gravity="right"
            android:layout_marginRight="50px"
            android:id="@+id/about"
            android:textColor="#604811"
            />

    </LinearLayout>

I want to know that how i access this button from main activity and menu activity. I am using following code but its not working.

TextView btn;
public class Header extends AppCompatActivity  {

TextView home;



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


    home = (TextView) findViewById(R.id.mainMenu);

    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

             Intent i = new Intent(getApplicationContext(),Menu.class);
            startActivity(i);
        }
    });

}}

Solution

  • Solution 1

    Add this to your Main.xml

    <include
        layout="@layout/header.xml"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    Use the following code in Main.java to access it using the same way and see if it works.

    TextView home;
    
    home= (TextView) findViewById(R.id.mainMenu);
    
    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            Intent i = new Intent(getApplicationContext(),Menu.class);
            startActivity(i);
        }
    });
    

    What I did above will allow you to access all the elements of header.xml from Main.java by directly referring to their ids. All you have to do is include the xml properly and then you can directly access its components by their respective ids.

    Solution 2

    Use this for the button or textview you want to set the onClick for

    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="changeActivity"
            android:text="button1" />
    

    Then create a respective function for what happens when you click on it.

    public void changeActivity(View view)
        {
            Intent intent = new Intent(FromActivity.this, ToActivity.class);
            startActivity(intent);
        }