Skip to content

Commit

Permalink
Handle also known as creation dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
YassinAJDI committed Jul 30, 2018
1 parent 622cc5c commit d4a0a95
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,25 @@
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.flexbox.FlexboxLayout;
import com.squareup.picasso.Picasso;
import com.udacity.sandwichclub.R;
import com.udacity.sandwichclub.databinding.ActivityDetailBinding;
import com.udacity.sandwichclub.model.Sandwich;
import com.udacity.sandwichclub.ui.sandwichlist.SandwichListViewModel;

import java.util.List;

public class DetailActivity extends AppCompatActivity {

public static final String EXTRA_POSITION = "extra_position";
Expand Down Expand Up @@ -87,10 +95,34 @@ private void populateUI(Sandwich sandwich) {

// sandwich main name
mBinding.textSandwichName.setText(sandwich.getMainName());

// sandwich origin
String placeOfOrigin = sandwich.getPlaceOfOrigin();
if (placeOfOrigin.isEmpty()) {
mBinding.textOrigin.setVisibility(View.GONE);
} else {
mBinding.textOrigin.setText(placeOfOrigin);
}

// also known as
List<String> names = sandwich.getAlsoKnownAs();
if (!names.isEmpty()) {
for (String name : names) {
TextView textView = new TextView(this);
textView.setText(name);
textView.setBackground(ContextCompat.getDrawable(this, R.drawable.chip_shape));
TextViewCompat.setTextAppearance(textView, R.style.Chips);
FlexboxLayout.LayoutParams layoutParams = new FlexboxLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 8, 8);
textView.setLayoutParams(layoutParams);
mBinding.flexbox.addView(textView);
}
}

// sandwich description
mBinding.descriptionTv.setText(sandwich.getDescription());
// sandwich origin
mBinding.textOrigin.setText(sandwich.getPlaceOfOrigin());


mBinding.executePendingBindings();
}
Expand Down
96 changes: 46 additions & 50 deletions app/src/main/res/layout/activity_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,13 @@
tools:text="@tools:sample/cities" />

<com.google.android.flexbox.FlexboxLayout
android:id="@+id/linearLayout"
android:id="@+id/flexbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
app:alignContent="stretch"
app:alignItems="stretch"
app:flexWrap="wrap"
Expand All @@ -111,55 +112,50 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_origin">

<TextView
android:id="@+id/textView"
style="@style/Chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/chip_shape"
android:text="Also known as" />

<TextView
android:id="@+id/chip2"
style="@style/Chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/chip_shape"
android:text="Name 2" />

<TextView
android:id="@+id/chip3"
style="@style/Chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/chip_shape"
android:text="Name 3" />

<TextView
android:id="@+id/chip4"
style="@style/Chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/chip_shape"
android:text="Name 2" />

<TextView
android:id="@+id/chip5"
style="@style/Chips"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/chip_shape"
android:text="Name 3" />
<!--<TextView-->
<!--android:id="@+id/textView"-->
<!--style="@style/Chips"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginBottom="8dp"-->
<!--android:background="@drawable/chip_shape"-->
<!--android:text="Also known as" />-->

<!--<TextView-->
<!--android:id="@+id/chip2"-->
<!--style="@style/Chips"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginBottom="8dp"-->
<!--android:background="@drawable/chip_shape"-->
<!--android:text="Name 2" />-->

<!--<TextView-->
<!--android:id="@+id/chip3"-->
<!--style="@style/Chips"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginBottom="8dp"-->
<!--android:background="@drawable/chip_shape"-->
<!--android:text="Name 3" />-->

<!--<TextView-->
<!--android:id="@+id/chip4"-->
<!--style="@style/Chips"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginBottom="8dp"-->
<!--android:background="@drawable/chip_shape"-->
<!--android:text="Name 2" />-->

<!--<TextView-->
<!--android:id="@+id/chip5"-->
<!--style="@style/Chips"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_marginBottom="8dp"-->
<!--android:background="@drawable/chip_shape"-->
<!--android:text="Name 3" />-->

</com.google.android.flexbox.FlexboxLayout>
</LinearLayout>
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<item name="android:textAppearance">@style/TextAppearance.AppCompat.Body2</item>
<item name="android:capitalize">words</item>
<item name="android:textColor">#ff232F34</item>
<item name="android:layout_marginEnd">8dp</item>
<item name="android:background">@drawable/chip_shape</item>
</style>

<style name="DividerHorizontal">
Expand Down

0 comments on commit d4a0a95

Please sign in to comment.