Images and Checkboxes with the Microsoft Band 2 - android

I am trying to create a tile on the Microsoft Band 2 using Android Studio. I was wondering if it is possible to add images to a button like I would be able to on an android phone. My other question is about checkboxes. Are there checkboxes on the band? If not is there another way to get similar functionality? I need users to be able to click multiple things for a single question. Any help would be greatly appreciated.

For the checkboxes I would do a layout where you have a small text button next to a larger text button within a layout. When the large text button gets clicked, call an update function from your receiver that changes the text of the smaller button (possibly an asterisk or some other character that looks like a bullet point and it seems to appear and disappear). For example, your update function could look like this (slight modification from the example tile code given in the SDK):
private final int bulletTextId = 12;
private final int textButtonId = 21;
private boolean isActiveBullet = false;
private void onButtonClicked(int clickedID) {
switch (clickedID) {
case textButtonId:
String text = "";
isActiveBullet = !isActiveBullet;
if (isActiveBullet) text = "*";
try {
client.getTileManager().setPages(tileId,
new PageData(pageId1, 0)
.update(new TextBlockData(bulletTextId, text))
.update(new TextButtonData(textButtonId, "Text Button")));
} catch (BandIOException e) {e.printStackTrace();}
break;
default:
Log.e("", "Unknown button press received");
}
}
For multiple buttons you might need a map of button to boolean and switch the corresponding ones. If you can't figure that out, comment and I'll follow up.
Originally I was thinking it would make sense to change the background color, but that doesn't seem to be supported by the sdk.
As for using an image for the background, I don't think that is currently supported, just from looking at the function definitions in the sdk source code, but I would actually love to know that for sure as well.
Edit: I found this shortly after posting. It appears you can use bitmaps as masks, but I am not sure how to do that. Hopefully someone will come along and tell us because I would like to know too :)
"8.5.1
Icons Used as
FilledButton
Masks
By defining an Icon bitmap that acts as a mask and
then
superimposing that Icon over a
FilledButton
(see
Negative
Margins
)
, you can create the effect of the Icon image becoming visible when the button is pressed. That is, the
Icon bitmap
is defined to
have transparent pixels for the desired image, and opaque pixels els
e
where
. When the
user presses the
FilledButton
, the
FilledButton
color changes
but
shows through
only
the
transparent portions of
the Icon bitmap. "
And here is other relevant code if you want it:
private PageLayout createButtonLayout() {
return new PageLayout(
new ScrollFlowPanel(15, 0, 260, 105, FlowPanelOrientation.HORIZONTAL)
.addElements(new TextBlock(0, 0, 20, 45, TextBlockFont.MEDIUM)
.setMargins(5, 0, 0, 0)
.setId(bulletTextId))
.addElements(new TextButton(0, 0, 190, 45).setMargins(5, 0, 0, 0)
.setId(textButtonId).setPressedColor(Color.BLUE))
);
}
private BroadcastReceiver messageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() == TileEvent.ACTION_TILE_OPENED) {
} else if (intent.getAction() == TileEvent.ACTION_TILE_CLOSED) {
}
/* ***** THIS IS THE ONLY EVENT WE ACTUALLY CARE ABOUT ***** */
else if (intent.getAction() == TileEvent.ACTION_TILE_BUTTON_PRESSED) {
TileButtonEvent buttonData = intent.getParcelableExtra(TileEvent.TILE_EVENT_DATA);
appendToUI("button is " + isActiveBullet + " ");
onButtonClicked(buttonData.getElementID());
}
}
};

Related

Combine TapGestureRecognizer and PanGestureRecognizer in Xamarin Android

I have the following structure.
Frame frame = new Frame();
Grid grid = new Grid();
ContentView contentView = new ContentView();
contentView.GestureRecognizers.Add(CreateSwipeEffect());
grid.Children.Add(contentView, 0, 0);
frame.GestureRecognizers.Add(CreateFrameTapEffect());
frame.Content = grid;
Frame has available two effects: first we can swipe(PanGesture) and second we can tap(TapGesture). On iOS platform this solution perfectly works. However on Android platform only swipe effect is firing. How can I solve this to have both effects available for Android platform?
I use your code in iOS and Android, yes, it works fine on iOS, and have issue on Android.
But if you add tapGenture and panGesture after you add label or frame control, it can works fine. Maybe some mechanisms of Android are a little different from iOS. Please take a look the following code, I test it on Android and iOS, it all works fine.
public Page17()
{
InitializeComponent();
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += PanGesture_PanUpdated;
var tapGenture = new TapGestureRecognizer();
tapGenture.NumberOfTapsRequired = 1;
tapGenture.Tapped += TapGenture_Tapped;
Frame frame = new Frame();
frame.BackgroundColor = Color.AliceBlue;
Grid grid = new Grid();
Label label= new Label();
label.Text = "this is test!";
label.BackgroundColor = Color.Red;
grid.Children.Add(label,0,0);
frame.Content = grid;
stacklayout1.Children.Add(frame);
frame.GestureRecognizers.Add(tapGenture);
label.GestureRecognizers.Add(panGesture);
}
private void TapGenture_Tapped(object sender, EventArgs e)
{
Console.WriteLine("the tapgesture fire!");
}
private void PanGesture_PanUpdated(object sender, PanUpdatedEventArgs e)
{
Console.WriteLine("the pangesture fire");
}
You are dispatching Tap Event to Frame(parent) and Swip Event to ContentView(childview).
According to Input events overview:
Remember that hardware key events are always delivered to the View
currently in focus. They are dispatched starting from the top of the
View hierarchy, and then down, until they reach the appropriate
destination.
So based on your codes when you are tapping on the Frame, android system thought you want to fire the tap event on ContentView(which doesn't exist) because current focus is the contentview not the frame. So if you have specific needs to do that, you need to try other ways to implement it. If not, please register the tap event on ContentView.

Setting Text Based on values held in ArrayList

This is driving me a little mad since I know this should be very simple but I am not getting the desired affect.
I have the following arraylist
private List<String> tagStringArray = new ArrayList<>();
Then later I have a method that creates dynamic buttons, based on ID values pulled across from my Retrofit instance.
In my method, I have a count to help me set the title of the button but I also add the values of count to an ArrayList for use in another method.
I have taken a snip of relevant information from the method mentioned.
count = 1;
if (!questionNumber.equals("") && !questionNumber.equals(null)) {
for (final Object value : list) {
try {
/*Dynamically create new Button which includes the question number
*/
final AppCompatButton btn_question = new AppCompatButton(getActivity());
/*LayoutParams (int width, int height,float weight)
As LayoutParams is defaulted in px, I have called a method called dpToPX to make sure
the dynamically added EditText is the same size on all devices.
*/
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpToPx(280), dpToPx(45), 1);
btn_question.setBackgroundColor(Color.parseColor("#3B5998"));
btn_question.setTextColor(Color.WHITE);
btn_question.setText("Question "+count);
//set the Tag based on its position in the XML
tagStringArray.add(String.valueOf((count)));
count++;
If a user clicks on say Question 1 Button, I want my fragment to say Question 1, so to try and achieve that, I have tried doing the following:
String tags = String.valueOf(tagStringArray);
tags = tags.substring(1, tags.length() -1);
String[] currentTag = tags.split(",");
if (currentTag[0].contains("1")) {
tv_setQuestions_edit.setText("Question 1");
}else if(currentTag[1].contains("2")) {
tv_setQuestions_edit.setText("Question 2");
}
But this will always set the title to Question 1 and I am not sure what is going wrong.......
If I use the following toast Toast.makeText(getActivity(), Arrays.toString(currentTag), Toast.LENGTH_LONG).show(); it shows [1,2] so I know they are being added ok.
I did look into using tags by doing:
public static int KEY_COUNT=0; public static int KEY_VALUE=1;
btn_question.setTag(KEY_VALUE,value);
btn_question.setTag(KEY_COUNT,count);
But for some reason, when I add more than one tag (as I need a minimum of 2), my dynamic button is missing from the layout. But for some reason when only 1 tag - like this btn_question.setTag(value); is used, it shows up fine (I have a feeling its some issue with my fragment). Therefore I am trying to think of a workaround in the meantime.
Any help or guidance would be really appreciated.
It's because
currentTag[0].contains("1")
is always true. The first item of currentTag always contains "1".
Instead of doing this, why don't you just do String titleForFragment = myButton.getText() in the onClick method for the button? That way, you can set the same onClickListener on all the buttons, and it will reduce the amount of code you need to write.

Need to check array of imageviews for backgroundresource, clickable or imageresource (Android)

I'm creating a memory app (you know where you have to select 2 images and match them). I'm looking for a way to check when there are no images left so I can show a message.
I have this code to check if the images match. I'm working with 2 arrays, 1 holds the imageviews and their position in the grid (playfield) and one holds the reference for which image is on that imageview (cards).
private void checkCards() {
if (cards[value1] == cards[value2]) {
playfield[value1].setBackgroundResource(R.drawable.border_red);
playfield[value2].setBackgroundResource(R.drawable.border_red);
playfield[value1].setClickable(false);
playfield[value2].setClickable(false);
score++;
} else {
playfield[value1].setImageResource(R.drawable.back);
playfield[value2].setImageResource(R.drawable.back);
}
value1 = -1;
value2 = -1;
}
I need to check on one of these conditions:
There are no more imageviews without a red border
There are no more imageviews that are clickable
There are no more imageviews with the R.drawable.back imageresource
However I'm not sure on how to do this? Do any of you have an idea or even a better method?
Thanks in advance!
I would keep a counter of each one, I can think on more complicated solutions, but a pretty straightforward one will be to have a counter and increase that counter when you're assigning the clickables, the borders, etc... e.g:
private void checkCards() {
if (cards[value1] == cards[value2]) {
playfield[value1].setBackgroundResource(R.drawable.border_red);
playfield[value2].setBackgroundResource(R.drawable.border_red);
playfield[value1].setClickable(false);
playfield[value2].setClickable(false);
mCounterClickables--; // one less clickable
mRedDrawables++; // one more Red Drawable
mBlackDrawables--;
score++;
} else {
playfield[value1].setImageResource(R.drawable.back);
playfield[value2].setImageResource(R.drawable.back);
mBlackDrawables++; //
}
value1 = -1;
value2 = -1;
}
Then to validate that you're done:
public boolean imDone() {
return mBlackDrawables == 0 && mCounterClickables== 0
&& mRedDrawables == playfield.length;
}
Does it makes sense?
In this way you don't have to iterate over and over again, you will always have track of what the user has done.
Btw. The initial state of your variables should be something like this:
int mBlackDrawables = 0, mCounterClickables = playfield.length, mRedDrawables = 0;

checking/comparing imagebutton resources

I need to know how to check how to compare or check for an image resource on an image button
First I setup the button.
button1 = (ImageButton) findViewById(R.id.ib1);
button1.setImageResource(R.drawable.smiley);
if( currentTime%2==0 ) {
button1.setImageResource(R.drawable.smiley);
}
else {
button1.setImageResource(R.drawable.smileyhit);
}
Later at some point I need to check if the image resource of the button is the smiley drawable and increase the score.
something like
if( button1.getImageResource() == R.drawable.smiley ) {
score = score + 1;
}
What should I do to compare that? I do not want to use tags. Please help me out!
Use ImageButton.setTag and ImageButton.getTag to identify which image is currently in ImageButton background as:
if( currentTime%2==0 ) {
button1.setImageResource(R.drawable.smiley);
button1.setTag(R.drawable.smiley);
}
else {
button1.setImageResource(R.drawable.smileyhit);
button1.setTag(R.drawable.smileyhit);
}
use button1.getTag to check current image:
if(Integer.parseInt(button1.getTag().toString()) == R.drawable.smiley ) {
score = score + 1;
}
using setTag and getTag you can easily differentiate images.
if( currentTime%2==0 ) {
button1.setImageResource(R.drawable.smiley);
button1.setTag("smiley");
}
else {
button1.setImageResource(R.drawable.smileyhit);
button1.setTag("smileyhit");
}
if(button1.getTag().toString().equalsIgnoreCase("smiley")){
score = score + 1;
}
Maintain an array with currentTime%2==0 in it for each button. One should not depend on the UI to retrieve the state of the app since the UI is recreated at several different points in the life-cycle of an app. All your data which determines the state of the app should be seperated from the UI.
EDIT
Okie.. as you say you have more images, i still would go through array or list rather than depending on UI to get the data.My method would be as follows,
Create constants for each Image resource with an Int value
eg: public static final int image1=1;
create the int array for the number of images and maintain them with initial values.
Each time to you change the image change the corresponding array element accordingly.
To find out which resource is used, check the corresponding array element.
While restoring UI(like onResume) use the array to set the corresponding draw able resource.

Change Text in a same Layout

I'm Making simple app for project
That App contains lot of text so i want,
"when a button is pressed, text should Change in same layout"
like PowerPoint slide.
I want change text only not scroll.
Now i made my app, have lots of Windows or Layouts.
It is not looking good, too much layout in simple app so please help me .
Thanks in advance
Doing this is very easy, I will quickly walk you through the Algorithm:
Set a class level variable called as FLAG initialize it to 1.
Let us assume that FLAG = 1 will represent the first slide. FLAG = 2 the second slide and so on.
Now in your button click you can use a switch case or an if else condition, based on the value of the flag display the relevant text in textview.
Once done, increment the flag, for the next set of sentence(s).
Class level:
int FLAG = 1;
onCreate:
Initialize your textView:
TextView mtv = (TextView)findViewById(R.id.yourid);
Set a button click listener:
private View.OnClickListener slides = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(FLAG ==1)
mtv.setText("First slide");
else if(FLAG ==2)
mtv.setText("Second Slide");
//and so on...
FLAG = FLAG+1;//increment flag
}
};

Categories

Resources