how to creat a toggle button for widget app - android

this is the closet post I found to my question How to make a toggle button for a widget android, however, I am looking for a more straight forward solution to this.
Does any one have any straight forward solution to make a toggle button using image button to create a simple widget app?
I would greatly appreciate any advice on that.
I only need a simple toggle button which I can listen for the state change using a service class. Is this possible?
Many thanks in advance.

As per your comments, I am posting sample relevant code below which can be used to change the image used in your widget:
case WifiManager.WIFI_STATE_ENABLED:
remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_on);
break;
case WifiManager.WIFI_STATE_ENABLING:
remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_transition);
break;
case WifiManager.WIFI_STATE_DISABLED:
remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_off);
break;
case WifiManager.WIFI_STATE_DISABLING:
remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_transition);
break;
case WifiManager.WIFI_STATE_UNKNOWN:
remoteViews.setImageViewResource(R.id.widget_normal_imagebutton, R.drawable.ic_widget_wifitimer_error);
break;
For a more thorough tutorial about creating homescreen widgets, you can visit:
http://www.vogella.com/articles/AndroidWidgets/article.html
It explains the whole process including how to update the widget with new information

Related

Build error: Cannot find symbol. Points to navigation actions

I keep getting an error that says that it cannot find the navigation actions. Here is the place that it gives me errors:
public void onItemClick(String item) {
switch (card) {
case ACCESSORIES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToAccessories(item));
case ARMOR_PIECES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToArmorPieceType(item));
case ARMOR_SETS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToArmorSets(item));
case DESTINIES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToDestinyList(item));
case ENEMIES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToEnemyList(item));
case GEMS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToGemList(item));
case LORESTONES:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToLoreSets(item));
case POTIONS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToPotionTypes(item));
case QUESTS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToQuestList(item));
case SKILLBOOKS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToSkillbook(item));
case TRAINERS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToTrainerList(item));
case TWISTS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToTwistList(item));
case WEAPONS:
navController.navigate(TypeOfInfoFragmentDirections.actionTypeOfInfoToWeaponList(item));
It gives me errors on all 13 navigation actions. Here is my gradle: https://pastebin.com/jaK6CK7z
And finally here is the navigation file: https://pastebin.com/8DyR7vpj
As far as I can tell, I have all the elements needed. Any help is greatly appreciated.
I figured out the problem. I was trying to navigate to destinations that were in a nested NavGraph. I found that I did not need to have nested graphs, so taking out the graphs and leaving the fragments was all I needed to do.

How to create Inbox like floating action button in Android with Speed dial animation

I want to create a floating action button that will animate to show inner speed dial buttons like Inbox android app developed by Google.
Example:
I found the expected output by using Floating Action Button Speed Dial library.
The library is available on Jcenter so no additonal repository is required.
Step 1: Add the following dependency
implementation "com.leinardi.android:speed-dial:2.0.0"
Step 2: Add the SpeedDialView to your layout:
<com.leinardi.android.speeddial.SpeedDialView
android:id="#+id/speedDial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:sdMainFabClosedSrc="#drawable/ic_add_white_24dp" />
Step 3: Add the items to the SpeedDialView:
SpeedDialView speedDialView = findViewById(R.id.speedDial);
speedDialView.addActionItem(
new SpeedDialActionItem.Builder(R.id.fab_link, R.drawable.ic_link_white_24dp)
.create());
Step 4: Add the click listeners:
speedDialView.setOnActionSelectedListener(new SpeedDialView.OnActionSelectedListener() {
#Override
public boolean onActionSelected(SpeedDialActionItem speedDialActionItem) {
switch (speedDialActionItem.getId()) {
case R.id.fab_link:
showToast("Link action clicked!");
return false; // true to keep the Speed Dial open
default:
return false;
}
}
});
Source: https://github.com/leinardi/FloatingActionButtonSpeedDial
The FloatingActionButtonSpeedDial is the best library i have found that suits your requirements.
It's quite flexible and you can find that the repository's Main Sample implementing a lot of possible use cases such as add/remove button from the floating menu, change the color of labels and button background etc.
The only drawback I've found in the meantime is that you have to add your buttons programmatically and not in the xml layout file.
Anyway, the first answer is good but he missed a small detail. When you create a button to add to your SpeedDialView:
speedDialView.addActionItem(
new SpeedDialActionItem.Builder(R.id.fab_link, R.drawable.ic_link_white_24dp)
.create());
The R.id.fab_link is the id of the new ActionItem (floating button) so you can reference it afterwards in the onClickListener of the speedDialView:
speedDialView.setOnActionSelectedListener(new SpeedDialView.OnActionSelectedListener() {
#Override
public boolean onActionSelected(SpeedDialActionItem actionItem) {
switch (actionItem.getId()) {
case R.id.fab_add:
// do something
break;
case R.id.fab_link:
// do something else
break;
}
return true; // To keep the Speed Dial open
}
});
It's just an #IdRes, an integer that is expected to be an id resource reference.
So you can do like they did in the official sample here, and create a resource file containing items with type id.
I hope you find this answer helpful.

Validation in onFocusChange

I have multi EditTexts.
How can i validate the EditText from having "" in the EditText and if the EditText is "" then i want the user to must enter a number before it goes to edittext2
How can this be done?
case R.id.P2Throw1Set2:
p212r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p212.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p212r.getText().toString())));
break;
case R.id.P2Throw2Set2:
p222r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p222.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p222r.getText().toString())));
break;
case R.id.P2Throw3Set2:
p232r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p232.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p232r.getText().toString())));
break;
case R.id.P2Throw4Set2:
p242r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) - Integer.parseInt(p242.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p242r.getText().toString())));
break;
You're going to have to disable all but the first edit text and only enable them when the user has provided satisfactory input. Then wrap your parseInt() calls in try/catch blocks like so:
case R.id.P2Throw2Set2:
try
{
p222r.setText(String.valueOf(Integer.parseInt(p2score.getText().toString()) -
Integer.parseInt(p222.getText().toString())));
p2score.setText(String.valueOf(Integer.parseInt(p222r.getText().toString())));
// if the previous lines worked, this will work
p232r.setEnabled(true);
}
catch(NumberFormatException e)
{
// user entered "" or the value was null
// in this case, we leave the next edit text disabled
}
break;
A little bit late answer, but you might take a look onto the declarative form validation techniques and advanced validation implemented in the BARACUS framework for Android applications coping with the problems of validation in Android apps. Since the stuff is open source feel free to use source fragments coping with validation if you don't want to use a framework.

Android : alternative to setVisibility to Display images onscreen

First of all, I am very new at Android and teaching myself so I am not asking you to code it for me. Just give me some terms, topics or a subject to study and I will hit the books and figure it out.
I am making an app where the user sets what appears on screen by pressing a button or selecting an image. The only way I know how to do this is to have an onClickListener set a variable and use a switch statement to display an imageView that corresponds to the variable selected.
example:
//on click listeners set variable userPic1 and userPic2
switch(userPic1){
case 1:
pic11.setVisibility(View.VISIBLE);
pic12.setVisibility(View.GONE);
pic13.setVisibility(View.GONE);
pic14.setVisibility(View.GONE);
break;
case 2:
pic11.setVisibility(View.GONE);
pic12.setVisibility(View.VISIBLE);
pic13.setVisibility(View.GONE);
pic14.setVisibility(View.GONE);
break;
case 3:
pic11.setVisibility(View.GONE);
pic12.setVisibility(View.GONE);
pic13.setVisibility(View.VISIBLE);
pic14.setVisibility(View.GONE);
break;
case 4:
pic11.setVisibility(View.GONE);
pic12.setVisibility(View.GONE);
pic13.setVisibility(View.GONE);
pic14.setVisibility(View.VISIBLE);
break;
}
switch(userPic2){
case 1:
pic21.setVisibility(View.VISIBLE);
pic22.setVisibility(View.GONE);
pic23.setVisibility(View.GONE);
pic24.setVisibility(View.GONE);
break;
case 2:
pic21.setVisibility(View.GONE);
pic22.setVisibility(View.VISIBLE);
pic23.setVisibility(View.GONE);
pic24.setVisibility(View.GONE);
break;
case 3:
pic21.setVisibility(View.GONE);
pic22.setVisibility(View.GONE);
pic23.setVisibility(View.VISIBLE);
pic24.setVisibility(View.GONE);
break;
case 4:
pic21.setVisibility(View.GONE);
pic22.setVisibility(View.GONE);
pic23.setVisibility(View.GONE);
pic24.setVisibility(View.VISIBLE);
break;
}
}
I feel like there is probably a better way to code this, but the bigger problem is in my XML layouts. I have all of these imageViews stacked on top of each other and it is getting difficult to see how they will look because it's just a mess.
Any help is appreciated
http://developer.android.com/reference/android/widget/ViewFlipper.html will make this a bit easier for you to manage, without such unruly code.
Using the ViewFlipper as suggested may help if you're trying to switch between many views. However, if you're really just changing the image and don't need to change your layout instead of creating lots of views, I would simply just set the image on your ImageView (I'm assuming picXX are ImageViews). You can store your IDs in an array or two and get them from there. Something like:
int[] userPics1 = new int[] {R.drawable.pic1, R.drawable.pic2, ... }
and then:
ImageView pic = (ImageView)findViewById(R.id.pic);
pic.setImageResource(userPics1[userPic1]);

Android Activity To Mimic a Manual

Can someone point me in the right direction - I'm trying to create an Android Activity that looks like a technical manual that ALSO can take some user input(I know how to do simple buttons etc.) and the user input part can wait until I have a few basic pages.
My goal (if possible) would be to create a text-heavy activity (like a technical manual) but I'm not sure what the best GENERAL method is for doing this.
To start - rather than having multiple activities I want one large activity that a User may be able to swipe through from left to right (Perhaps use ViewFlipper here??)
But how can I make an Activity that looks like a manual or is Text Heavy??
Thanks!
This is what I did with my application's instructions. I started with ViewFlipper that has next and back buttons. Then as the application grew in size, code got messier and there are a lot of formatting that I needed for the text. So I turned to use WebView and just store the text files as raw assets.
WebView manual=new WebView();
manual.loadData(Utilities.getData(this, R.raw.update),"text/html", "utf-8");
getData method:
public static String getData(Context c,int res) throws IOException{
InputStream ins = c.getResources().openRawResource(res);
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
String html=new String(buffer);
StringBuilder buf = new StringBuilder(html.length());
for (char c1 : html.toCharArray()) {
switch (c1) {
case '#': buf.append("%23"); break;
case '%': buf.append("%25"); break;
case '\'': buf.append("%27"); break;
case '?': buf.append("%3f"); break;
default: buf.append(c1);
break;
}
}
return buf.toString();
}
Then you can attach buttons either to the webview or the application in response to user inputs and load new views.
Advantage:
1. Easy to format and integrate with Android. Very quick development.
2. No need to worry about the view flipper bugs that happens sometimes.
3. It will look like you want.
Disadvantage:
1. Doesn't look too much like an Android Application by itself.

Categories

Resources