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]);
Related
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.
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.
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
I have 4 buttons that when click starts a new activity class. This works fine using switch case. But I want a drawable to show in an Image View based on which button was clicked. I'm not sure how to do this. Could someone give me and idea of how to research this for some sample code.
case R.id.button1:
intent = new Intent(Gallery.this, Photos.class);
intent.putExtra("iv", R.drawable.photo1);
break;
case R.id.button2:
intent = new Intent(Gallery.this, Photos.class);
intent.putExtra("iv", R.drawable.photo2);
break;
This works as far as switching to the Photos activity but the drawable doesn't show in the Imagae View. I have this code in the Photos Activity.
iv = (ImageView)findViewById(R.id.photo);
Bundle extras = getIntent().getExtras();
extras.getInt("iv");
I do not get any errors I just don't have the photo showing up.
Thank you for any help you can give.
Looks like the only thing you're missing is the call to actually set the image - everything else looks fine. Make the last lines more like:
int imgId = extras.getInt("iv", 0);
if (imgId > 0) {
iv.setImageResource(imgId);
}
You were most of the way there. Android has a great set of tutorials for coming up to scratch with all of the basic view components.
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.