Pour obtenir un dégradé de couleur allant jusque dans la barre de status :
Gradient drawable file drawable/bg_toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="0"
android:startColor="#11998e"
android:endColor="#38ef7d" />
</shape>
add this in your values/style.xml
<item name="android:windowBackground">@drawable/bg_toolbar</item>
<item name="toolbarStyle">@style/Widget.Toolbar</item>
<item name="android:statusBarColor">#00000000</item>
make a new file for your toolbar Gradient values/toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Widget.Toolbar" parent="@style/Widget.AppCompat.Toolbar">
<item name="contentInsetStart">0dp</item>
<item name="android:background">@drawable/bg_toolbar</item>
</style>
</resources>
And add background android:background="#ffffff"
in your Activity Layout file.
Pour définir les couleurs, utilisez les sites :
http://www.jokconcept.net/codes-couleurs-hexdecimal.php ou
http://www.kitsgraphiques.net/generateur-code-couleur-hexadecimal.html et http://www.kitsgraphiques.net/convertisseur-rvb-hexadecimal.html
Pour des fenêtres arrondies :
Définir la forme dans un fichier xml à placer dans res/drawable/ Par exemple « rectangle.xml »
<?xml version="1.0" encoding="UTF-8"?>
<!-- rectangle aux bords arrondis, avec un dégradé en fond et un bord blanc -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="7dp" />
<stroke android:width="2dp" android:color="#ffffff" />
<gradient android:angle="270" android:startColor="#cc333333"
android:endColor="#cc000000" />
</shape>
Puis dans le layout, ajouter ce rectangle en fond de textview ou autre :
android:background="@drawable/rectangle"
Pour utiliser des styles :
cf. http://www.alonsoruibal.com/using-styles-on-android-layouts/
Définir les styles utilisés (res/values/styles.xml) :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="General">
<item name="android:textColor">#fff</item>
</style>
<style name="General.Text">
<item name="android:textSize">20dp</item>
<item name="android:padding">7dp</item>
</style>
</resources>
Puis les utiliser dans le layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/napply_widget"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp" >
<TextView
android:id="@+id/nap_time"
android:text="Erase me"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="@drawable/rectangle"
style="@style/General.Text"/>
</RelativeLayout>
Pour insérer des commentaires dans un fichier xml : <!– commentaires –>
Pour tracer une ligne de séparation dans un layout :
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>
Pour écrire dans un champ à l’écran :
TextView ResText;
super.onCreate(savedInstanceState);
setContentView(R.layout.ecran_hash);
ResText = (TextView) findViewById(R.id.afficres);
ResText.setText("Ceci est un texte");
d’un projet, les trois différents thèmes proposés :
- Holo Light
- Holo Dark
- Holo Light
Sont décrits à l’adresse : http://developer.android.com/design/style/themes.html
Pour « effacer » l’écran avant l’affichage d’un message ou d’un Toast :
Créez un Layout vide (exemple black.xml) et invoquez le avant d’afficher le message d’alerte : setContentView(R.layout.black);
Pour afficher en plein écran l’application :
Placer la ligne « Theme » suivante dans le fichier manifest.
<activity android:name=".Game" android:label="@string/app_name"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
Ou dans le code, insérez après le « on create » :
//--Retrait du titre (nom et icône de l'application)
requestWindowFeature(Window.FEATURE_NO_TITLE);
//-- Pour cacher la barre de statut
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
Pour retirer le clavier et la zone de saisie sur un écran :
//-- On cache le bouton et la zone de saisie
button.setVisibility(View.GONE);
editText.setVisibility(View.GONE);
//-- On retire le clavier
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Pour afficher ou masquer des boutons dans un layout :
Utilisez la propriété VISIBLE ou INVISIBLE
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonlocal = (Button) findViewById(R.id.buttonlocal);
button = (Button) findViewById(R.id.buttonUrl);
if (isOnline())
button.setVisibility(View.VISIBLE);
else
button.setVisibility(View.INVISIBLE);
Pour faire défiler un texte dans un TextView (scrolling) :
<TextView
android:id="@+id/word_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:text="This is a very long text with a lot of test" />
Et dans le code java placer les lignes :
private TextView ResText;
ResText = (TextView) findViewById(R.id.word_text);
ResText.setText(sTexte); //-- sTexte contient le texte à faire défiler
ResText.setEllipsize( android.text.TextUtils.TruncateAt.MARQUEE);
ResText.setSingleLine(true);
ResText.setMarqueeRepeatLimit(-1); //-- Défile indéfiniment
ResText.setSelected(true);
Pour revenir au début du texte scrollé :
scrollable.scrollTo(0, 0);
Pour obliger l’écran à rester actif :
he best way to do this is to use the FLAG_KEEP_SCREEN_ON
in your activity (and only in an activity, never in a service or other app component). For example:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); }
The advantage of this approach is that unlike wake locks (discussed in Keep the CPU On), it doesn’t require special permission, and the platform correctly manages the user moving between applications, without your app needing to worry about releasing unused resources.
Another way to implement this is in your application’s layout XML file, by using the android:keepScreenOn
attribute:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true"> ... </RelativeLayout>
Using android:keepScreenOn="true"
is equivalent to using FLAG_KEEP_SCREEN_ON
. You can use whichever approach is best for your app. The advantage of setting th
Pour fixer l’orientation de l’écran (portrait ou landscape) :
add this Add android:screenOrientation="portrait"
in your manifest file where you declare your activity like this
<activity android:name=".yourActivity"
....
android:screenOrientation="portrait"/>
if you want to do using java code
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Soit en résumé :
super.onCreate(savedInstanceState); //-------------------------------------------- getSupportActionBar().hide(); setContentView(R.layout.activity_main); //-- Pour cacher la barre de statut getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); //-- Maintien écran allumé getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); //----------------------------------
Références : https://developer.android.com/training/scheduling/wakelock.html.
Création de menus (activés par la touche « menu » de votre téléphone) :
Créez les icônes du menu (48 x 48 pixels) (exemple icoraz, iconet, icoquit) dans le dossier drawable
Créez un Layout qui contiendra votre menu exemple :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/raz"
android:title="Raz"
android:icon="@drawable/icoraz">
</item>
<item android:id="@+id/nettoyer"
android:title="Nettoyer"
android:icon="@drawable/iconet">
</item>
<item android:id="@+id/quitter"
android:title="Quitter"
android:icon="@drawable/icoquit" />
</menu>
Ajouter ensuite, dans le code de votre activity, le code suivant :
/////////////////////// MENU ///////////////// //Méthode qui se déclenchera lorsque vous appuierez sur le bouton menu du téléphone public boolean onCreateOptionsMenu(Menu menu) { //Création d'un MenuInflater qui va permettre d'instancier un Menu XML en un objet Menu MenuInflater inflater = getMenuInflater(); //Instanciation du menu XML spécifier en un objet Menu inflater.inflate(R.layout.menu, menu); return true; } //Méthode qui se déclenchera au clic sur un item public boolean onOptionsItemSelected(MenuItem item) { //On regarde quel item a été cliqué grâce à son id et on déclenche une action switch (item.getItemId()) { case R.id.raz: Toast.makeText(ChercheAnnoncesActivity.this, "Suppression de toutes les annonces mémorisées.", Toast.LENGTH_SHORT).show(); PlaceDataSQL.onDelete(db); return true; case R.id.nettoyer: Toast.makeText(ChercheAnnoncesActivity.this, "Suppression des annonces sauf celles sauvegardées.", Toast.LENGTH_SHORT).show(); return true; case R.id.quitter: //Pour fermer l'application il suffit de faire finish() finish(); return true; } return false;}
Votre commentaire