I have 2 layouts for my application but I have written all my Logic in MainActivity.JAVA
I want to use MainActivity.JAVA for my other layout (XML) file also
How can I do that?
This is my MainActivity.XML (Layout File)
Now I want Blue Button on Right to resize the new layout for that
New XML (with no Java file) only Layout file
Now I want to use the same Java file (MainActivity.JAVA) For this new layout file
You have to use a Fragment
or another solution is that include
layout. You said you have one common activity and in this activity you need to include two XML layout then you can set like this:-
You can use the include element to have sub-XML files:
main.xml
<LinearLayout ... >
<include layout="@layout/file1" />
<include layout="@layout/file2" />
</LinearLayout>
file1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/toolbar_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<ImageView
android:id="@+id/iv_toolbar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:padding="10dp"
android:visibility="gone" />
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center_horizontal"
android:padding="10dp"
android:singleLine="true"
android:text="MyApp"
android:textColor="@color/white"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
file2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_splash"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="@color/colorPrimaryDark"
tools:context="com.inducesmile.androidmusicplayer.SplashActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="16dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logomusic"
android:contentDescription="@string/app_name"/>
</LinearLayout>
</LinearLayout>
If you have two XML files, you can merge both of them by including one into another.
For example: if you have two files named a.xml and b.xml, then you can include the b.xml in a.xml using include tag like
<include layout="@layout/b.xml" />
Then call setContentView (R.layout.a) in your MainActivity.java
Or, you can make use of Fragments.
In your main activity layout, you could use a FrameLayout
, which you can then add a fragment to. Then in the Java file, you can use getSupportFragmentManager().beginTransaction().replace()
method to inflate the layout XMLs as and when you like.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.