quarta-feira, 8 de setembro de 2010

My Custom Android View "StarsViewKMB"

StarsViewKMB

This view created by me, allows to set and display stars rates as showing below:

You can enable or disable trackball/touch changes, you can change star type to (leaf, dollar, star), you can allow to cancel (the red icon), you can get and set stars values between 0 and 10, -1 value it's in the cancel case.
Currently do not have the option to resize the pictures, has only two sizes of images. Big (to edit with trackball or touch) and small to display the star value.

Methods

setIconType(int value)

Valid values are:

StarsViewKMB.ICON_TYPE_STAR
StarsViewKMB.ICON_TYPE_DOLLAR
StarsViewKMB.ICON_TYPE_LEAF

setStars(int value)

Valid values are:

A number between 0 and 10 that allows to change selected stars, -1 it's a special value when cancel is allowed.

setIsDoubleSize(boolean value)

True for big icons, false to small icons.

setAllowCancel(boolean value)

True if allow cancel option, it displays a red icon before stars. False to disable cancel option.

setIsEditable(boolean value)

True if allows the user to change stars value using trackball or touch events. False to not allow user to change stars value.

int getIconType()
boolean isAllowingCancel()
boolean isDoubleSize()
boolean isEditable()
int getStars()


XML Layout Code


You can easily implement the view in your layout using xml code as below:

<kmb.StarsViewKMB android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:paddingTop="5px"
android:paddingBottom="5px"
doubleSize="true"
allowCancel="false"
editable="false"
iconType="0"
value="6" />

Creating StarsViewKMB by Code

  • Set the content view.
  • Get the main LinearLayout where you want to put the StarsViewKMB
  • Create the StarsViewKMB object.
  • Configure the view using the custom methods.
  • Add the StarsViewKMB to the main LinearLayout

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main_layout);

StarsViewKMB starsViewKmb1 = new StarsViewKMB(this);
starsViewKmb1.setIsDoubleSize(false);
starsViewKmb1.setStars(3);
starsViewKmb1.setIconType(StarsViewKMB.ICON_TYPE_STAR);
starsViewKmb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

StarsViewKMB starsViewKmb2 = new StarsViewKMB(this);
starsViewKmb2.setIsDoubleSize(false);
starsViewKmb2.setStars(2);
starsViewKmb2.setIconType(StarsViewKMB.ICON_TYPE_DOLLAR);
starsViewKmb2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

StarsViewKMB starsViewKmb3 = new StarsViewKMB(this);
starsViewKmb3.setIsDoubleSize(false);
starsViewKmb3.setStars(1);
starsViewKmb3.setIconType(StarsViewKMB.ICON_TYPE_LEAF);
starsViewKmb3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

mainLayout.addView(starsViewKmb1);
mainLayout.addView(starsViewKmb2);
mainLayout.addView(starsViewKmb3);
}

Nenhum comentário:

Postar um comentário