senaa
senaa

Reputation: 63

Display button from another activity

I have two activities/two XMLs.

I made a Facebook button inside signactivty.java (sign.xml) and I want to display the Facebook button in homeactivity.java (home.xml).

How can I use a button that's created in another activity?

Please give me an example.

Upvotes: 0

Views: 68

Answers (2)

Farrokh
Farrokh

Reputation: 1167

create facebook_button.xml like this

<Button xmlns:android="http://schemas.android.com/apk/res/android"
id="@+id/fb_button"
android:layout_width="64dp"
android:layout_height="64dp" />

create sign.xml layout file and use

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <include layout="@layout/facebook_button"></include>
    ....
</LinearLayout>

and so, in your home activity too...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <include layout="@layout/facebook_button"></include>
    ....
</LinearLayout>

Upvotes: 2

Kevin Kurien
Kevin Kurien

Reputation: 842

first create a third xml

button.xml 

and create a layout inside it with only the button view inside it
then include that xml file in whichever xml file you want
In your case include it in sign.xml and home.xml

Upvotes: 0

Related Questions