Load and classify fonts from assets dynamically - java

i was wondering if someone knows or has a way to dynamically load all fonts .ttf and .otf from assets folder and classify it by family, i have a static way (which is not completely done), i have a method that returns a List with all the font families, hardcoded, so i have this other method to return a typeface given the font family (string) given and it's style:
public Typeface getTypeface(Context context, String name, int textStyle)
{
String fontFileName = "";
switch (name)
{
case "ComicNeue-Angular":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "ComicNeue":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "Impact":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "LiberationSans":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
case "Roboto":
{
switch (textStyle)
{
case Typeface.NORMAL: {} break;
case Typeface.BOLD: {} break;
case Typeface.ITALIC: {} break;
case Typeface.BOLD_ITALIC: {} break;
}
} break;
}
return Typeface.createFromAsset(context.getAssets(), fontFileName);
}
i want to add all fonts and classify it by family, so i can load a spinner with all font families. so i don't have to add lines to this code if i add another font family to the folder. (I have to add like 7 more font families to the project)
any ideas?
thanks in advance!

Related

switch-case does not work properly

i the below posted code, when I leave the field ip blank/empty and give values to the other fields, the toast always gives message the KATimer is invalid or missing.
i expected to see a toast showing with a message indicating the empty field,but the below code, if any field is empty, it always says KATimer is invalid or empty.
why that is happeneing, i am missing something
Code:
btnStubView_Connect:
btnStubView_Connect.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isValidMQTTConfigs(etStubView_ip) &&
isValidMQTTConfigs(etStubView_port) &&
isValidMQTTConfigs(etStubView_ClientID) &&
isValidMQTTConfigs(etStubView_KATimer)) {
Log.d(TAG, "#btnStubView_ConnectListener(): all entries are valid");
setCSession(cbStubView_CS.isChecked()); // set the current state of the cleanSession checkBox.
addToContentValues();
Log.d(TAG, "#btnStubView_ConnectListener(): all entries added toContentValues");
} else {
Log.w(TAG, "#btnStubView_ConnectListener(): one or more entry(s) is invalid or left blank.");
}
}
});
isValidMQTTConfigs:
protected boolean isValidMQTTConfigs(View view) {
// TODO Auto-generated method stub
boolean valid = false;
String viewName = "";
switch(view.getId()) {
case R.id.etSubView_ip:
viewName = "IP";
if (isDuly( ((EditText) view).getText().toString())) {
this.setIP(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_port:
viewName = "Port";
if (isDuly( ((EditText) view).getText().toString())) {
this.setPort(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_clientID:
viewName = "clientID";
if (isDuly( ((EditText) view).getText().toString())) {
this.setClienID(((EditText) view).getText().toString());
return valid = true;
}
case R.id.etSubView_KATimer:
viewName = "KAtimer";
if (isDuly( ((EditText) view).getText().toString())) {
this.setKATimer(((EditText) view).getText().toString());
return valid = true;
}
}
Log.w(TAG, "#checkMQTTConfigs(): " + viewName + " is invalid or missing");
Toast.makeText(getActivity(), viewName + " is invalid or missing", Toast.LENGTH_SHORT).show();
return valid;
}
isDuly:
private boolean isDuly(String text) {
// TODO Auto-generated method stub
if (text.trim().equals("")) {
return false;
} else {
return true;
}
}
You are not using break; after every case which causes the cases below to execute even if you do not want them to. For eg, this is correct :-
switch(int){
case 1:
break;
case 2:
break;
}
and this will cause unexpected output though it is not wrong:-
switch(int){
case 1:
case 2:
}
Omitting break will cause execution of case 2 after executing case 1.
You seem to be missing several break statements.
Switch(X){
case 1: doOne();
case 2: doTwo();
case 3: doThree();
}
the waterfall flow makes sure that if X is 2, both doTwo and doThree will be executed. If X is 1, all three methods will be executed.
If you want only the linked method to be called, change the code into:
Switch(X){
case 1: doOne(); break;
case 2: doTwo(); break;
case 3: doThree(); break;
}

How to set values for Radio button in android?

I am getting the value from DB and setting it to the respective button in the below format. Is there any optimised way to do the same. All these radio buttons are inside a radio group.
if (bundlevalue.get(3).equalsIgnoreCase("Mr.")) {
rg_nametitle.check(R.id.mr);
} else if (bundlevalue.get(3).equalsIgnoreCase("Mrs.")) {
rg_nametitle.check(R.id.mrs);
} else if (bundlevalue.get(3).equalsIgnoreCase("Ms.")) {
rg_nametitle.check(R.id.ms);
} else {
rg_nametitle.check(R.id.messrs);
}
You can try as follows...
String value = bundlevalue.get(3)
Resources res = getResources();
if (value.equalsIgnoreCase("Mr.") || value.equalsIgnoreCase("Mrs.") || value.equalsIgnoreCase("Ms.")) {
String[] splitedValue = value.toLowerCase ().split(".");
int id = res.getIdentifier(splitedValue[0], "id", getContext().getPackageName());
rg_nametitle.check(id);
} else {
rg_nametitle.check(R.id.messrs);
}
In case if you use XML attribute like this :
<RadioGroup
...
...
android:checkedButton="#+id/IdOfTheRadioButtonInsideThatTobeChecked"
... >....</RadioGroup>
or you can use switch-case statement like this :
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
Use switch statement. Although, there is nothing big difference in using if-else or switch, you can go ahead with whichever is more readable to you.
public enum Title
{
Mr, Mrs, Ms;
}
String title = bundlevalue.get(3).equalsIgnoreCase("Mr.");
switch(Title.valueOf(title)) {
case Mr:
rg_nametitle.check(R.id.mr);
break;
case Ms:
rg_nametitle.check(R.id.ms);
break;
case Mrs:
rg_nametitle.check(R.id.mrs);
break;
default:
break;
}

How to detect if ondraglistener is already finish? (android)

I need to stop this service after a listener is already activated by using stopSelf(); However the service ends even though there is no Drag event... How can i detect if there is a Drag event happend before I can call StopService?
mainButton.setOnTouchListener(new MyTouchListener());
vTopLeft.setOnDragListener(new MyDragListener());
vTopRight.setOnDragListener(new MyDragListener());
vBottomLeft.setOnDragListener(new MyDragListener());
vBottomRight.setOnDragListener(new MyDragListener());
Log.d("tok", "add mview");
stopSelf(); ---> this should wait after draglistener is finish.
MyDragListener.java
Public class MyDragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
String containerName = null;
int ID = 0;
int Dragaction = event.getAction();
int gravity2 = 1234567879;
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
owner.setVisibility(View.GONE);
FrameLayout container = (FrameLayout) v;
container.addView(view);
LocationSerializable ls = new LocationSerializable();
switch (v.getId()) {
case R.id.topLeft:
gravity2 = Gravity.LEFT | Gravity.TOP;
ls.setGravity(gravity2);
break;
case R.id.topRight:
gravity2 = Gravity.RIGHT | Gravity.TOP;
ls.setGravity(gravity2);
break;
case R.id.bottomLeft:
gravity2= Gravity.BOTTOM | Gravity.LEFT;
ls.setGravity(gravity2);
break;
case R.id.bottomRight:
gravity2 = Gravity.RIGHT | Gravity.BOTTOM;
ls.setGravity(gravity2);
break;
default:
throw new RuntimeException("Unknown ID");
}
LinearLayout containerParent = (LinearLayout) v.getParent();
LinearLayout conatainerGrandMother = (LinearLayout) containerParent.getParent();
conatainerGrandMother.setVisibility(View.GONE);
case DragEvent.ACTION_DRAG_ENDED:
default:
break;
}
return false;
}
}
Stop your service in case DragEvent.ACTION_DRAG_ENDED:
below is a sample template.
#Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
break;
case DragEvent.ACTION_DRAG_ENDED:
// option 1
Context.stopService();
// option 2
// stopService(new Intent(Activity.this,MailService.class));
//stopSelf();
// other alternative is
// v.getContext().stopService(new Intent(v.getContext(),DragDropButtonMainService.class));
break;
}
return true;
}
According to android docs
After the user releases the drag shadow, and after the system sends out (if necessary) a drag event with action type ACTION_DROP, the system sends out a drag event with action type ACTION_DRAG_ENDED to indicate that the drag operation is over.

why getEdgeFlags() always return 0

I want to detect the touch edge in onTouchEvent function for my view, but getEdgeFlags() always return 0 in my Nexus S 4.1.1, anyone can help me? thanks advance.
#Override
public boolean onTouchEvent(MotionEvent event) {
int edgeFlags = event.getEdgeFlags();
//edgeFlags alway 0!
switch (edgeFlags) {
case MotionEvent.EDGE_LEFT:
Log.i("tag", "EDGE_LEFT");
break;
case MotionEvent.EDGE_RIGHT:
Log.i("tag", "EDGE_RIGHT");
break;
case MotionEvent.EDGE_TOP:
Log.i("tag", "EDGE_TOP");
break;
case MotionEvent.EDGE_BOTTOM:
Log.i("tag", "EDGE_BOTTOM");
break;
default:
Log.i("tag", "" + edgeFlags);
break;
}
return super.onTouchEvent(event);
}
I solved the problem with add four button to trigger four direction!

Android Multi-touch event problems...No new touch events while dragging?

So what I'm trying to make is a little space game, You control the ship with a graphic on-screen joystick using touch and drag. I have gotten this to work fine. The problem arises once I started to try and add the ability to touch the top portion of the screen to fire a weapon. For some reason, if you are currently dragging the joystick it seems to ignore the other touch input and doesn't do anything (But it works fine once I stop holding the joystick).
This is my both my first time working with java, and with Android so its probably something stupid, but iv been stuck on it for a few days.
Anyway, here my code.
The below is from my
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
#Override
public boolean onTouchEvent(MotionEvent event) {
fingers= event.getPointerCount(); //Returns 1 or 2 properly if 2 fingers on screen
playership.onTouchEvent(event); //Pass the event along to the spaceship object
return true;
//return super.onTouchEvent(event); //no idea what this does, but it seems to disable the drag event
}
Below is the playership.onTouchEvent(event); function
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
if(event.getY()<(Panel.mHeight-150))
{
Laser1.reset(mX,mY,(int)event.getX(),(int)event.getY());
}
break;
}
joy1.onTouchEvent(event);
return true;
}
And below is the joy1.onTouchEvent(event);
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
//Offset between mouse(x,y) and center
double offx=oX-event.getX();
double offy=oY-event.getY();
double d=Math.sqrt((offx*offx)+(offy*offy));
switch (eventaction )
{
case MotionEvent.ACTION_DOWN:
if(d<dragrange) //Start Dragging if clicked
{
offx=offx/d;
offy=offy/d;
Dragging=true;
reset = false;
dX=(int) (offx*dragrange);
dY=(int) (offy*dragrange);
}
break;
case MotionEvent.ACTION_MOVE:
if(Dragging)//if joy is already being draged, update it.
{
offx=offx/d;
offy=offy/d;
if(d>dragrange)
{
dX=(int) (offx*dragrange);
dY=(int) (offy*dragrange);
}
else
{
dX=(int) (offx*d);
dY=(int) (offy*d);
}
reset = false;
}
break;
case MotionEvent.ACTION_UP:
if(Dragging)
{
Dragging=false;
}
break;
}
return true;
}
So yeah, the problem is that if the joystick is in the middle of a case MotionEvent.ACTION_MOVE it seems to block any other touch events from properly registering.
You need to handle detecting the other finger. More specifically handling
MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP
otherwise you're ignoring other fingers. Here is some code from one of my projects that does exactly what you're looking for:
public void onTouchEvent(MotionEvent event)
{
int ptrId = -1;
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
down(event.getPointerId(0), (int)event.getX(), (int)event.getY());
break;
case MotionEvent.ACTION_UP:
up(event.getPointerId(0));
break;
case MotionEvent.ACTION_POINTER_DOWN:
ptrId = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
int ptrIdx = event.findPointerIndex(ptrId);
down(ptrId, (int)event.getX(ptrIdx), (int)event.getY(ptrIdx));
break;
case MotionEvent.ACTION_POINTER_UP:
ptrId = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
up(ptrId);
break;
case MotionEvent.ACTION_MOVE:
for(int i = 0; i < event.getPointerCount(); ++i)
if(event.getPointerId(i) == inputPad.id())
{
inputPad.position(event.getX(inputPad.id()));
player.velocity(inputPad.delta());
player.stand();
if(enemy != null) {
Fighter.collide(player, enemy);
enemy.update();
}
player.update();
break;
}
break;
}
}
See here and here for more explanation.

Categories

Resources