Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android − black background turn into grey

I'm explicitly defining a black color to the background of my LinearLayout (using @android:color/black in my XML), but sometimes (often after a screen rotation), the black is turning into a grey color (or a transparent black).

This problem appears on many devices: both on emulator, Acer Liquid (Android 2.2) and Galaxy Nexus (Android 4.1).

Screenshots: Buggy view // Not buggy view

Here is my XML and activity code:

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:minHeight="150dp">

            <ImageView
                android:id="@+id/projectview_description_image"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:contentDescription="@string/projectview_description_image_description"
                android:scaleType="centerCrop"
                android:src="@drawable/project_nophoto" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="vertical"
                android:id="@+id/projectview_description_overlay"
                android:paddingTop="5dp"
                android:paddingLeft="10dp"
                android:paddingBottom="5dp"
                android:background="@android:color/black"
                android:height="35dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/projectview_description_title"
                    android:textAllCaps="true"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="bold"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/projectview_description_baseline"
                    android:textColor="@android:color/white"
                    android:textStyle="italic" />

            </LinearLayout>

        </RelativeLayout>

        <!-- This LinearLayout background is buggy -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:background="@android:color/black">

            <ImageView
                android:layout_width="175dp"
                android:layout_height="25dp"
                android:layout_gravity="center_vertical"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:scaleType="fitCenter"
                android:src="@drawable/project_status_finish" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="right"
                android:paddingRight="10dp"
                android:text="@string/projectview_description_website"
                android:textColor="@color/website_link"
                android:textSize="15dp"
                android:id="@+id/projectview_description_website" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/projectview_description_about_container"
            android:padding="10dp"
            android:paddingBottom="0dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/projectview_description_about"
                android:textAllCaps="true"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/projectview_description_about_text" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/projectview_description_questions_container" />

    </LinearLayout>

</ScrollView>

Activity:

public class ProjectViewActivity extends Activity implements OnClickListener {
    private boolean displayMenu = false;
    private Intent shareIntent;
    private FavoriteSqlite db;
    private I4pProjectTranslation project;

    @TargetApi(14)
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if(displayMenu) {
            // Inflate menu only if it hasn't been done before
            if(menu.size() == 0) {
                // Inflating the menu
                MenuInflater inflater = getMenuInflater();
                inflater.inflate(R.menu.projectview, menu);

                // Creating share intent
                Intent prepareShareIntent = new Intent(Intent.ACTION_SEND);
                prepareShareIntent.putExtra(Intent.EXTRA_TEXT, UriHelper.getProjectUrl(project));
                prepareShareIntent.putExtra(Intent.EXTRA_SUBJECT, project.getTitle());
                prepareShareIntent.setType("text/plain");
                shareIntent = Intent.createChooser(prepareShareIntent, getResources().getText(R.string.projectview_menu_share_dialog));
            }

            // Defining favorite state
            MenuItem favoriteItem = menu.getItem(0);
            if(db.isFavorite(project))
                favoriteItem.setTitle(R.string.projectview_menu_favorites_remove);
            else
                favoriteItem.setTitle(R.string.projectview_menu_favorites_add);
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case android.R.id.home:
            if(getIntent().getData() != null) {
                Intent intent = new Intent(this, HomepageActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            } else 
                finish();
            break;
        case R.id.projectview_favorite:
            Toast t;
            if(db.isFavorite(project)) {
                db.removeFavorite(project);
                t = Toast.makeText(this, getResources().getString(R.string.projectview_toast_favorites_remove, project.getTitle()), Toast.LENGTH_SHORT);
            } else {
                db.addFavorite(project);
                t = Toast.makeText(this, getResources().getString(R.string.projectview_toast_favorites_add, project.getTitle()), Toast.LENGTH_SHORT);
            }
            t.show();
            break;
        case R.id.projectview_share:
            startActivity(shareIntent);
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @TargetApi(11)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT < 11)
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.loading);
        db = new FavoriteSqlite(this);

        if(Build.VERSION.SDK_INT >= 11)
            getActionBar().setDisplayHomeAsUpEnabled(true);

        project = (I4pProjectTranslation) getLastNonConfigurationInstance();
        if(project != null)
            displayProject();
        else {
            String projectLang;
            String projectSlug;

            Uri data = getIntent().getData();
            if(data != null) {
                List<String> path = data.getPathSegments();
                projectLang = path.get(0);
                projectSlug = path.get(2);
            } else {
                Bundle extras = getIntent().getExtras();

                if(extras.containsKey("project_title"))
                    setTitle(extras.getString("project_title"));

                projectLang = extras.getString("project_lang");
                projectSlug = extras.getString("project_slug");
            }

            ProjectViewHandler handler = new ProjectViewHandler(this);
            ProjectViewThread thread = new ProjectViewThread(handler, projectLang, projectSlug);

            thread.start();
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        return project;
    }

    public void setProject(I4pProjectTranslation p) {
        project = p;
    }

    @TargetApi(11)
    public void displayProject() {
        setContentView(R.layout.projectview_description);
        displayMenu = true;
        if(Build.VERSION.SDK_INT >= 11)
            invalidateOptionsMenu(); // Rebuild the menu

        setTitle(project.getTitle());

        LinearLayout overlay = (LinearLayout) findViewById(R.id.projectview_description_overlay);
        overlay.getBackground().setAlpha(127);

        if(project.getProject().getPictures().size() > 0) {
            ImageView image = (ImageView) findViewById(R.id.projectview_description_image);
            image.setImageBitmap(project.getProject().getPictures().get(0).getImageBitmap());
        }

        TextView title = (TextView) findViewById(R.id.projectview_description_title);
        title.setText(project.getTitle());

        TextView baseline = (TextView) findViewById(R.id.projectview_description_baseline);
        baseline.setText(project.getBaseline());

        TextView website = (TextView) findViewById(R.id.projectview_description_website);
        if("".equals(project.getProject().getWebsite()))
            website.setVisibility(View.GONE);
        else
            website.setOnClickListener(this);

        if("".equals(project.getAboutSection())) {
            LinearLayout aboutContainer = (LinearLayout) findViewById(R.id.projectview_description_about_container);
            aboutContainer.setVisibility(View.GONE);
        } else {
            TextView aboutText = (TextView) findViewById(R.id.projectview_description_about_text);
            aboutText.setText(project.getAboutSection());
        }

        LinearLayout questions = (LinearLayout) findViewById(R.id.projectview_description_questions_container);
        for(Question question : project.getProject().getQuestions()) {
            if(question.getAnswer() != null) {
                LinearLayout questionLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.projectview_question, null);

                TextView questionView = (TextView) questionLayout.findViewById(R.id.projectview_question_question);
                TextView answerView = (TextView) questionLayout.findViewById(R.id.projectview_question_answer);

                questionView.setText(question.getQuestion());
                answerView.setText(question.getAnswer().trim());

                questions.addView(questionLayout);
            }
        }
    }

    public void onClick(View arg0) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(project.getProject().getWebsite()));
        startActivity(intent);
    }
}

Thanks for help!

like image 634
Kyriog Avatar asked Oct 01 '12 10:10

Kyriog


People also ask

How do I get rid of black background on Android?

It's easy to turn off dark mode if you don't like it. Go to Settings > Display and toggle off Dark Theme.

Why is my background black on my Android phone?

Check the Grayscale settings. Under Android Accessibility settings, you'll find the Grayscale setting under Screen colors under the Vision menu. If this is enabled, disable it to see if it fixes your screen color issue.

How do I change my android background from black to white?

Open your device's Settings app . Select Accessibility. Under "Display," select Color inversion. Turn on Use color inversion.

How do I change my Google background from black to white?

In the menu, find Settings and tap on it. Tap on General. Find the Theme option, tap it, and you will get to choose between Light and Dark.


1 Answers

This is due to a bug in the framework's caching mechanism. The easiest workaround is to use a color that's almost black, such as #ff010101.

like image 135
Romain Guy Avatar answered Nov 27 '22 04:11

Romain Guy