I need something like DialogFragment
but with a full class, in other words create a activity
float. The procedure is the next:
When I click MainActivity
on an Item of my ListView
and it shows me a contextual menu , I select More Information and my class opens MasInformacion
loading the data of my Sqlite
of that Item , well, with this code I have managed to MasInformacion
make it float (perhaps it is not the best option , if there is a better one you can tell me please) and transparent so that it shows behind my MainActivity
, like this:
Well, I want it to be like this:
Not be completely transparent, but have opacity to obscure the 'class' behind it.
MasInformacion
: This is how I make 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 load it from AndroidManifest
withandroid: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>
You can see the documentation here: https://developer.android.com/reference/android/app/DialogFragment.html
Inside the onClick() of your item call this method:
FromGoogle docs:
And here the custom DialogFragment:
With this you will show the content of the layout (in this case custom_layout.xml) in a floating window with background with the default transparency.
If you want to send information to this fragment do it in the following way.
FromGoogle docs:
I have tested the code, it works and does what you ask.
If you want to transform the Activity you have to the DialogFragment, use the same layout, pass the content of your Activity's onCreate to the fragment's onCreateView and copy all the methods and variables.
As an example if you want a "transparent" background color, containing a , define it in
colors.xml
:and you call it from the defined style:
Remember that you can define another type of opacity, for example a green color:
To this color you can define opacity, or without opacity:
or completely opaque: