-2

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);
        }
    });

}}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Hani Mehdi
  • 187
  • 4
  • 9

3 Answers3

2

find included layout using findViewById and then find button on that view using findViewById.

View includeView=findViewById("Id of included layout"); Button btn=includeView.findViewById(R.id.mainBtn);

0

you need the id of its parent to get that:

assume the R.id.parent_view is the parent of R.id.nested_button :

View parent= findViewById(R.id.parent_view);
Button nestedButton= (Button) parent.findViewById(R.id.nested_button);
nestedButton.setText("Whatever");
Amir Ziarati
  • 14,248
  • 11
  • 47
  • 52
0

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);
    }
JBJ
  • 288
  • 2
  • 13
  • its not possible to use the above code in header.java ? because i have many layouts and i dont want to use this individually on all layouts – Hani Mehdi Apr 10 '17 at 09:15
  • Correct me if I'm wrong but to my understanding you want to access the components of header.xml in Main and Menu activities right? If I'm right then what I wrote above is the solution to that problem. Let me know if that's not the case. Explain your problem properly and I will give you a solution. – JBJ Apr 10 '17 at 09:17
  • my english is not good but im trying to explain you.actually i created the header.xml and include this file on main activity and menu acitivty. in header activity i used on click listner on textview. i want to access onclick listner from main acitivity and menu activity because header is included in main and menu activity.My requirement is when i am on main activity and i click textview which is included in header.xml the onclick listner should work. – Hani Mehdi Apr 10 '17 at 09:25
  • Alright, now I understand your problem. Solution #1(Better way imo): You can do what I did above, just create a header.xml and include it in your main and menu activities then create onClickListeners indiviually in main.java and menu.java like I did above. Solution #2: This is what you are asking for, basically you want to include the onClick functionality in Menu and Main without actually setting indiviual onClickListeners as stated in solution1. In that case set android:onClick="changeActivity" in button and then access it through a function. I'm gonna edit it in answer above take a look. – JBJ Apr 10 '17 at 09:36
  • I think you need to understand that "include" only includes the XML. What you are trying to do is access the java implementation of xml just by including the xml. Either create seperate onClickListeners (Solution 1) or create a generic method and access that from other classes(Solution 2). – JBJ Apr 10 '17 at 09:41