Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android Forge:

Artyom Dorosh
February 19, 2016
68

Android Forge:

Artyom Dorosh

February 19, 2016
Tweet

Transcript

  1. Android Developer@IT4Medicine About me #AndroidForge Artyom Dorosh • 2 years

    of experience • 3 years in development • experience in UI/UX • co-coordinator of “IT KPI” • in love with hackathons
  2. #AndroidForge public class MainActivity extends AppCompatActivity { @Bind(R.id.email) EditText email;

    @Bind(R.id.password) EditText password; @Bind(R.id.sign_in) Button signIn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } }
  3. #AndroidForge <EditText android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/email" android:inputType="textEmailAddress" /> <EditText android:id="@+id/password"

    android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/password" android:inputType="textPassword" /> <Button android:id="@+id/sign_in" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/sign_in" />
  4. #AndroidForge // #1, or The Best of The Worst meaningOfLife.setVisibility(universe.getAnswer()

    == 42 ? View.VISIBLE : View.GONE); // #2. In case you doesn't use RetroLambda button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); // #3 setting a lot of data to TextViews name.setText(text != null ? text : "GTFO");
  5. Define a ViewModel #AndroidForge public class LoginModel extends BaseObservable {

    private String email; private String password; @Bindable public String getEmail() { return email; } @Bindable public String getPassword() { return password; } ... }
  6. #AndroidForge public class LoginModel extends BaseObservable { private String email;

    private String password; public void setEmail(String email) { this.email = email; notifyPropertyChanged(BR.email); } public void setPassword(String password) { this.password = password; notifyPropertyChanged(BR.password); } ... }
  7. Define your binding adapters #AndroidForge @BindingAdapter({"bind:loadImage", "bind:loadError"}) public static void

    loadImage(ImageView imageView, String link, Drawable error) { Glide.with(imageView.getContext()).load(link).error(error).into(imageView); }
  8. Gonna bind them all #AndroidForge public class MainActivity extends AppCompatActivity

    { private ActivityMainBinding binding; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_main); LoginModel loginModel = new LoginModel(); binding.setModel(loginModel); } }
  9. Layout itself #AndroidForge <layout xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="model" type="me.dnihze.databindingdemo.models.LoginModel" />

    </data> <LinearLayout> <EditText android:id="@+id/email" ... android:text="@{model.email}" /> <EditText android:id="@+id/password" ... android:text="@{model.password}" /> <Button android:id="@+id/sign_in" ... android:onClick="@{model.onClick}" android:text="@string/sign_in" /> </LinearLayout> </layout>
  10. Includes #AndroidForge <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:bind="http://schemas.android.com/apk/res-auto"> <data> <variable

    name="user" type="com.example.User"/> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/name" bind:user="@{user}"/> <include layout="@layout/contact" bind:user="@{user}"/> </LinearLayout> </layout>
  11. Expression language #AndroidForge • Mathematical + - / * %

    • String concatenation + • Logical && || • Binary & | ^ • Unary + - ! ~ • Shift >> >>> << • Comparison == > < >= <= • instanceof • Grouping () • Literals - character, String, numeric, null • Cast • Method calls • Field access • Array access [] • Ternary operator ?:
  12. Collections #AndroidForge <data> <import type="android.util.SparseArray"/> <import type="java.util.Map"/> <import type="java.util.List"/> <variable

    name="list" type="List&lt;String>"/> <variable name="sparse" type="SparseArray&lt;String>"/> <variable name="map" type="Map&lt;String, String>"/> <variable name="index" type="int"/> <variable name="key" type="String"/> </data>
  13. String Literals #AndroidForge // Base text android:text=‘@{map["firstName"]}' // Other ways

    to pass string android:text=“@{map[`firstName`}" android:text="@{map[&quot;firstName&quot;]}"
  14. Binding adapters #AndroidForge @BindingAdapter({"bind:loadImage", "bind:loadError"}) public static void loadImage(ImageView imageView,

    String link, Drawable error) { Glide.with(imageView.getContext()).load(link).error(error).into(imageView); }
  15. Custom type conversation #AndroidForge <View android:background="@{isError ? @color/red : @color/white}"

    android:layout_width="wrap_content" android:layout_height="wrap_content"/>
  16. Colour is just an int, not a drawable #AndroidForge @BindingConversion

    public static ColorDrawable convertColorToDrawable(int color) { return new ColorDrawable(color); }
  17. Google + -> +Artyom Dorosh Twitter -> @dniHze VK ->

    @dorosh_artem Fb -> @Dorosh That’s all. Thank U. #AndroidForge