Added ImageDrawable to listview

This commit is contained in:
Michael Schättgen 2016-08-21 21:55:04 +02:00
parent 59402d30ff
commit f11145ce95
9 changed files with 82 additions and 43 deletions

View file

@ -19,10 +19,12 @@ android {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
compile 'me.dm7.barcodescanner:zxing:1.9'
compile 'com.android.support:cardview-v7:24.1.1'
compile 'com.android.support:recyclerview-v7:24.1.1'

View file

@ -1,5 +1,6 @@
package me.impy.aegis;
import android.graphics.Color;
import android.os.Handler;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
@ -10,9 +11,13 @@ import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.amulyakhare.textdrawable.TextDrawable;
import com.amulyakhare.textdrawable.util.ColorGenerator;
import java.security.Key;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
@ -37,18 +42,21 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
public static class KeyProfileHolder extends RecyclerView.ViewHolder {
TextView profileName;
TextView profileCode;
ImageView profileDrawable;
KeyProfile keyProfile;
KeyProfileHolder(View itemView) {
super(itemView);
profileName = (TextView) itemView.findViewById(R.id.profile_name);
profileCode = (TextView) itemView.findViewById(R.id.profile_code);
profileDrawable = (ImageView) itemView.findViewById(R.id.ivTextDrawable);
}
public void setData(KeyProfile profile) {
this.keyProfile = profile;
profileName.setText(profile.Name);
profileCode.setText(profile.Code);
profileDrawable.setImageDrawable(generateTextDrawable(profile));
}
public void updateCode() {
@ -63,6 +71,19 @@ public class KeyProfileAdapter extends RecyclerView.Adapter<KeyProfileAdapter.Ke
}
profileCode.setText(otp.substring(0, 3) + " " + otp.substring(3));
}
private TextDrawable generateTextDrawable(KeyProfile profile)
{
if(profileName == null)
return null;
ColorGenerator generator = ColorGenerator.MATERIAL; // or use DEFAULT
// generate color based on a key (same key returns the same color), useful for list/grid views
int profileKeyColor = generator.getColor(profile.Name);
TextDrawable newDrawable = TextDrawable.builder().buildRound(profile.Name.substring(0, 1), profileKeyColor);
return newDrawable;
}
}
// Provide a suitable constructor (depends on the kind of dataset)

View file

@ -26,7 +26,7 @@ public class ScannerActivity extends Activity implements ZXingScannerView.Result
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
//handleDummyResult();
handleDummyResult();
mScannerView = new ZXingScannerView(this) {
@Override

View file

@ -4,45 +4,63 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="16dp">
<ImageView android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/ivTextDrawable"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:gravity="bottom"
android:id="@+id/relativeLayout"
android:layout_alignParentStart="false">
<RelativeLayout
android:layout_width="match_parent"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_alignParentBottom="false"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:gravity="bottom"
android:id="@+id/relativeLayout"
android:layout_alignParentStart="false">
android:id="@+id/profile_name"
android:text="Post title"
android:textSize="20sp"
android:layout_below="@+id/profile_code"
android:layout_alignLeft="@+id/profile_code"
android:layout_alignStart="@+id/profile_code"
android:ellipsize="end"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/profile_code"
android:textSize="36sp"
android:textColor="?android:attr/textColorPrimary"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/profile_code"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:textSize="36sp"
android:textColor="?android:attr/textColorPrimary"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/profile_name"
android:text="Post title"
android:layout_below="@+id/profile_code"
android:textSize="20sp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</LinearLayout>