Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MohammadSamandari/55e08954b9d6f999fe915d0e66274140 to your computer and use it in GitHub Desktop.
Save MohammadSamandari/55e08954b9d6f999fe915d0e66274140 to your computer and use it in GitHub Desktop.
Android Drawable and Styles and Theme Creating Dark Theme , Using Styles , Using Selectors and ImageView

Drawable - Styles - Theme

drawable resource

In Android, graphics are often handled by a resource called a Drawable. In the following step you learn how to create a certain type of Drawable called a ShapeDrawable, and apply it to your ImageButton elements as a background.

Create a ShapeDrawable

A ShapeDrawable is a primitive geometric shape defined in an XML file by a number of attributes including color, shape, padding and more. It defines a vector graphic, which can scale up and down without losing any definition.

  1. Choose New > Drawable resource file.
  2. Change xml code of the new resource to :
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
        <stroke 
            android:width="2dp"
            android:color="@color/colorPrimary"/>
</shape>

Style your View elements

  1. The Android platform supplies a large collection of styles and themes.
  2. Using styles can reduce the amount of code needed for your UI components.
  3. A style can specify common properties such as height, padding, font color, font size, and background color.
  4. A style should not include layout-related information.
  5. A style can be applied to a View, Activity, or the entire app. A style applied to an Activity or the entire app must be defined in the AndroidManifest.xml file.
  6. To inherit a style, a new style identifies a parent attribute in the XML.

Themes and final touches

  1. When you apply a style to a collection of View elements in an activity or in your entire app, that is known as a theme.
  2. To apply a theme, you use the android:theme attribute.
  • To apply a theme to an activity instead of the entire application, place the theme attribute in the tag instead of the tag. For more information on themes and styles, see the Style and Theme Guide.
Add theme button to the menu
  • The Android framework provides a theme that is designed exactly for this: The DayNight theme.
  • The DayNight theme uses the AppCompatDelegate class to set the night mode options in your Activity. To learn more about this theme, visit this blog post.
  1. In your styles.xml file, modify the parent of AppTheme to "Theme.AppCompat.DayNight.DarkActionBar".
  2. in the code to change the theme:
if(item.getItemId()==R.id.night_mode){
    // Get the night mode state of the app.
    int nightMode = AppCompatDelegate.getDefaultNightMode();
    //Set the theme mode for the restarted activity
    if (nightMode == AppCompatDelegate.MODE_NIGHT_YES) {
        AppCompatDelegate.setDefaultNightMode
                          (AppCompatDelegate.MODE_NIGHT_NO);
} else {
   AppCompatDelegate.setDefaultNightMode
                         (AppCompatDelegate.MODE_NIGHT_YES);
}
// Recreate the activity for the theme change to take effect.
recreate();
  1. don't forget about the savedinstancestate because we are recreating the activity.
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="@color/colorPrimary"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="@color/colorPrimary"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_background" android:state_pressed="false" />
<item android:drawable="@drawable/button_background_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/button_background" android:state_enabled="false" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- State when a row is being pressed, but hasn't yet been activated (finger down) -->
<item android:state_pressed="true" android:color="@color/colorButtonPressedState" />
<item android:state_enabled="false" android:color="@color/colorAccent" />
<!-- Default, "just hangin' out" state. -->
<item android:color="@color/colorPrimary" />
</selector>
package mohammad.samandari.scorekeeper;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "Lord";
// Member variables for holding the score
private int mScore1;
private int mScore2;
private TextView mScoreText1, mScoreText2;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScoreText1 = findViewById(R.id.score_1);
mScoreText2 = findViewById(R.id.score_2);
}
public void updateScore (View view) {
switch (view.getId()) {
case R.id.decreaseTeam1:
mScore1--;
break;
case R.id.decreaseTeam2:
mScore2--;
break;
case R.id.increaseTeam1:
mScore1++;
break;
case R.id.increaseTeam2:
mScore2++;
break;
default:
break;
}
mScoreText1.setText(String.valueOf(mScore1));
mScoreText2.setText(String.valueOf(mScore2));
}
@Override
public boolean onCreateOptionsMenu (Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
// Change the label of the menu based on the state of the app.
int nightMode = AppCompatDelegate.getDefaultNightMode();
if(nightMode == AppCompatDelegate.MODE_NIGHT_YES){
menu.findItem(R.id.night_mode).setTitle(R.string.day_mode);
} else{
menu.findItem(R.id.night_mode).setTitle(R.string.night_mode);
}
return true;
}
@Override
public boolean onOptionsItemSelected (@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.night_mode:
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
item.setTitle(R.string.night_mode);
}
recreate();
}
return true;
}
@Override
public void onSaveInstanceState (@NonNull Bundle outState) {
outState.putInt("mScore1", mScore1);
outState.putInt("mScore2", mScore2);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState (@NonNull Bundle savedInstanceState) {
mScore1 = savedInstanceState.getInt("mScore1");
mScore2 = savedInstanceState.getInt("mScore2");
mScoreText1.setText(String.valueOf(mScore1));
mScoreText2.setText(String.valueOf(mScore2));
super.onRestoreInstanceState(savedInstanceState);
}
}
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="ScoreButtons" parent="TextAppearance.AppCompat.Button">
<item name="android:background">@drawable/button_background_state</item>
<item name="android:tint">@drawable/button_text_state</item>
</style>
<style name="PlusButton" parent="ScoreButtons" >
<item name="android:src">@drawable/ic_plus</item>
<item name="contentDescription">@string/plus_button_description</item>
</style>
<style name="MinusButton" parent="ScoreButtons">
<item name="android:src">@drawable/ic_minus</item>
<item name="contentDescription">@string/minus_button_description</item>
</style>
<style name="ScoreText">
<item name="android:textAppearance">@style/TextAppearance.AppCompat.Display3</item>
</style>
<style name="TeamText">
<item name=
"android:textAppearance">@style/TextAppearance.AppCompat.Display1
</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment