Is it possible to change the background color of a "Floating" Activity?

3

I need something similar to DialogFragment but with a full class, in other words, create a activity floating%. The procedure is as follows:

In my MainActivity pulse on a Item of my ListView and it shows me a contextual menu I select More Information and it open my class MasInformacion loading the data of my Sqlite of that Item , well, with this code I have achieved that MasInformacion is floating (maybe not the best option, if there is another you can better comment to me please) and transparent so that it shows behind my MainActivity , like this:

link (I add the image because I fail to upload them here)

Well, I'm looking for it like this:

link (I add the image because I fail to upload them here)

That is not completely transparent, but has opacity to obscure the 'class' behind it.

MasInformacion : So I do it "Floating"

    DisplayMetrics flotante = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(flotante);

    int width = flotante.widthPixels;
    int height = flotante.heightPixels;

    getWindow().setLayout((int)(width*.9),(int)(height*.7));

...

// Recupero los datos de la DB

       extras = getIntent().getExtras();

        if (estadoEditarPersona()) {
            editTextNombre.setText(extras.getString("nombre"));
            editTextFecha.setText(extras.getString("fecha"));
            editTextZodiaco.setText(extras.getString("zodiaco"));
            editTextEdad.setText(extras.getString("edad"));
            editTextDiasrestantes.setText(extras.getString("diasrestantes"));
            ruta_imagen = extras.getString("ruta_imagen");
            imagenPersona.setImageBitmap(crearThumb());
        }
    }

MainActivity

// menu contextual

   @Override
    public boolean onContextItemSelected(android.view.MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();

        switch (item.getItemId()) {
            case R.id.menu_contextual_mas_informacion:
                masInformacion((int)info.id);
                return true;
            default:
                return super.onContextItemSelected((android.view.MenuItem) item);
        }
    }

...

// metodo masInformacion

    public void masInformacion(int p_id){
            Persona persona;
            try{
                persona = baseDatos.getPersona(p_id);
                // Se dirige a la actividad MasInformacion
                Intent actividad_editarPersona = new Intent(this, MasInformacion.class);

                // Carga los datos para mostrar en MasInformacion
                actividad_editarPersona.putExtra("id", p_id);
                actividad_editarPersona.putExtra("nombre", persona.getNombre());
                actividad_editarPersona.putExtra("fecha", persona.getFecha());
                actividad_editarPersona.putExtra("zodiaco", persona.getZodiaco());
                actividad_editarPersona.putExtra("edad", persona.getEdad());
                actividad_editarPersona.putExtra("diasrestantes", persona.getDiasrestantes());
                actividad_editarPersona.putExtra("ruta_imagen", persona.getRutaImagen());
                startActivityForResult(actividad_editarPersona, CODIGO_RESULT_EDITAR_PERSONA);
            }catch (Exception e){
                Toast.makeText(getApplicationContext(), (getResources().getString(R.string.error_mostrarinformacion)), Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }finally{
                baseDatos.cerrar();
            }
        }

style.xml I charge from AndroidManifest with android:theme="@style/AppTheme.Flotante"/>

    <style name="AppTheme.Flotante">

        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowCloseOnTouchOutside">true</item>
        <item name="android:windowBackground">@drawable/borde_flotante</item>
    </style>

   <!-- probe así pero lo transparente se vuelve completamente negro, cuando "colorPrimary" no es negro-->

    <style name="AppTheme.Flotante">
        <item name="android:windowBackground">@color/colorPrimary</item>
    </style>
    
asked by UserNameYo 28.02.2017 в 15:41
source

2 answers

1

As an example if you want to background color "transparent", containing a, define it in colors.xml :

<color name="transparent">#00000000</color>

and you send it to call from the defined style:

<style name="AppTheme.Flotante">
    <item name="android:windowBackground">@color/transparent</item>
</style>

Remember that you can define another type of opacity as a green color example:

#00FF00

To this color you can define opacity, either without opacity:

#0000FF00

or completely opaque:

#FF00FF00
    
answered by 28.02.2017 / 21:10
source
1

You can see the documentation here: link

Within the onClick () of your item, call this method:

showDialog();

From Google docs:

void showDialog() {
        // DialogFragment.show() will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("dialog");
        if (prev != null) {
            ft.remove(prev);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance();
        newFragment.show(ft, "tag");
    }

And here the custom DialogFragment:

public class MyDialogFragment extends DialogFragment {

    static MyDialogFragment newInstance() {
        return new MyDialogFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.custom_layout, container, false);
        return view;
    }
}

This will show the content of the layout (in this case custom_layout.xml) in a floating window with background with default transparency.

If you want to send information to this fragment do it in the following way.

From Google docs:

static MyDialogFragment newInstance(int num) {
    MyDialogFragment f = new MyDialogFragment();

    // Supply num input as an argument.
    Bundle args = new Bundle();
    args.putInt("num", num);
    f.setArguments(args);

    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNum = getArguments().getInt("num");
}

I've tried the code, it works and it does what you ask for.

If you want to transform the Activity you have to the DialogFragment, use the same layout, pass the onCreate content of your Activity to the fragment onCreateView and copy all the methods and variables.

    
answered by 28.02.2017 в 19:32