Reputation: 1
I have two xml layouts.
One with a Button.
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.seluhadu.colorpicker.MainActivity">
<Button
android:id="@+id/button"
android:elevation="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="1dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="123dp"/>
</RelativeLayout>
And one with a Relative Layout with just an id.
maintwo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.seluhadu.colorpicker.MainActivity">
</RelativeLayout>
I have one MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
I want to click in the Button in the main.xml and change the background color of the Maintwo.xml layout.
I tried to call findViewById
but it is in another xml, so it does not work because setContentView is main.xml
How do I call the id of the other xml to my main Activity?
How do make an onClick listener for the Button and get the id of maintwo.xml to change the background?
Upvotes: 0
Views: 419
Reputation: 192023
Assuming you have two Activities, you'll need to set the button click to call startActivity to your second Activity, meanwhile passing the color data through the intent, preferably as an hexadecimal string such as #ff00ff00
for green.
How do I pass data between Activities in Android application?
After that, you may use findViewById for the second layout and set its color
Also, see Color.parseColor()
Upvotes: 1
Reputation: 1211
use this
View inflatedView = getLayoutInflater().inflate(R.layout.other_layout, null);
RelativeLayout rl = (RelativeLayout) inflatedView.findViewById(R.id.your_view_id);
rl.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
More information about inflating layouts can be found here.
Upvotes: 0