0

I'd like to to make possible that layout with options to change the background color of MainActivity. I don't know how to do this- how tell the program to change the background color of other view, in this case: View v from MainActivity. Also, there is this error on "android.view.View.setBacktroundColor(int)" in the SettingsActivity

MainActivity.java:

package com.example.nowy;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);






    final Button buttonOptions = (Button) findViewById(R.id.buttonOptions);
    buttonOptions.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent optionsIntent = new Intent(MainActivity.this,
                    SettingsActivity.class);

            // Use the Intent to start the HelloAndroid Activity
            startActivity(optionsIntent);

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;


}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);


}
}

SettingsActivity.java:

package com.example.nowy;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;

public class SettingsActivity extends Activity {

 SeekBar seekRed, seekGreen, seekBlue, seekAlpha;
 View targetView;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.mainlayout);

 targetView = (View)findViewById(R.id.textView1);

 seekRed = (SeekBar)findViewById(R.id.seekred);
 seekGreen = (SeekBar)findViewById(R.id.seekgreen);
 seekBlue = (SeekBar)findViewById(R.id.seekblue);
 seekAlpha = (SeekBar)findViewById(R.id.seekalpha);

 seekRed.setOnSeekBarChangeListener(seekChangeListener);
 seekGreen.setOnSeekBarChangeListener(seekChangeListener);
 seekBlue.setOnSeekBarChangeListener(seekChangeListener);
 seekAlpha.setOnSeekBarChangeListener(seekChangeListener);
 }

 private SeekBar.OnSeekBarChangeListener seekChangeListener
 = new SeekBar.OnSeekBarChangeListener(){

  @Override
  public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
   // TODO Auto-generated method stub
   updateBackgroundColor();
  }

  @Override
  public void onStartTrackingTouch(SeekBar arg0) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onStopTrackingTouch(SeekBar arg0) {
   // TODO Auto-generated method stub

  }};

  private void updateBackgroundColor(){

  int red = seekRed.getProgress();
  int green = seekGreen.getProgress();
  int blue = seekBlue.getProgress();
  int alpha = seekAlpha.getProgress();

  targetView.setBackgroundColor(
    ((alpha << 24) & 0xFF000000)
    + ((red << 16) & 0x00FF0000)
    + ((green << 8) & 0x0000FF00)
    + (blue & 0x000000FF));

 }
}

mainlayout.xml:

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/mainlayout"
 >
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="hello"
 />
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="RED"
 />
<SeekBar
 android:id="@+id/seekred"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:max="255"
 />
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="GREEN"
 />
<SeekBar
 android:id="@+id/seekgreen"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:max="255"
 />
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="BLUE"
 />
<SeekBar
 android:id="@+id/seekblue"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:max="255"
 />
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="ALPHA"
 />
<SeekBar
 android:id="@+id/seekalpha"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:max="255"
 />
</LinearLayout>

activity_main.xml:

<RelativeLayout 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="com.example.nowy.MainActivity" 

    >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />



    <Button
        android:id="@+id/buttonOptions"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="Opcje" />

</RelativeLayout>
Inkwizytor
  • 7
  • 1
  • 7

1 Answers1

0

You can do the following:

First initialize the Layout in MainActivity.java then make sure to make the layout static:

public static LinearLayout linearlayout;

Then in your SecondActivity or Fragment do the following:

After making the variable static you can now successfully change the background color like this:

linearlayout.setBackgroundColor(getResources().getColor(R.color.yourcolor));

In fragment:

linearlayout.setBackgroundColor(view.getResources().getColor(R.color.yourcolor));

or

linearlayout.setBackgroundColor(Color.parseColor("#ffffff"));
LW001
  • 2,452
  • 6
  • 27
  • 36
  • answer is right. Just to add more on his answer the reason you get that NullPointerException error is that bc you did not initialize your layout. Like he said, you need to initialize your layout first before invoking its methods. – Yosidroid Sep 02 '21 at 17:00