Here is my onCreate method. I am trying to set the button to change the background of the background of the layout to be of a different color. However the findViewById is not able to pick up on the layout.
I fixed it by giving it another linear layout child but I would still like to know WHY it didnt work.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
//Below is code in question: Method is not resolved to type.
button1.setOnClickListener(new OnClickListener(){
public void onClick(View view) {
layout.setBackgroundColor(Color.argb(100, 255, 0, 0));
}
});
}
HEre is my XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="linearLayout1"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button" />
</LinearLayout>
Your id
is declared wrong.
It should be
android:id="@+id/linearLayout1"
instead of
android:id="linearLayout1"
And also Try using View.OnClickListener
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
layout.setBackgroundColor(Color.argb(100, 255, 0, 0));
}
});