How to do a Sub-menu without a new Acitivity? Android programming - java

This is my first question at stackoverflow :-). However, I've started to Android programming some weeks ago and now I have get into a problem that I will describe below.
Problem:
I have a background-image for the whole screen. Inside this LinearLayout I have another LinearLayout with Buttons inside. How can I (without to start a new activity) load another buttons and make the first buttons disappear. The Buttons have a drawable background-image assigned to the specific button.
Like: I have a menu; play, options and quit. When I for example click on Options, the Options should load, and the play-button, options-button and quit-button should disappear.
All I want to become is to remain the background-image without to create a new activity and always can load a bunch of new buttons depending on the button that was clicked on.
For now I have created a new activity for the new buttons that should appear for example: Exit -> Do you want to Exit? Yes or no., then I also have done so the new acitivty loads another xml-layout-resource file, with the same background as before. This is what I want to change to a smoother resolution.

Make two buttons with your second layout. use button1.setVisibility(View.GONE) method to set the visibility of the button. check this link

Related

Android Popup Window vs Android Dialog

I am trying to implement a simple logic in my application where the user is shown a popup (after sometime of application launch). The popup simply shows a TextView with some info message. This message is refreshed every time the application is launched and a new message is shown.
The UI of the popup matches my application UI - here maybe just popup background images is needed. Also one close button (X) is shown at the top right corner of the popup - to close this popup.
Logic of Showing Message: I have a String array having some 100 strings stored in it. I randomly pick one string from this array and populate the popup TextView showing the message. Please suggest if there is any better approach than what I am doing already here. Also is it possible to logic that if one message is picked then the same message is not picked until the other messages are shown at least once?
Logic of Showing Popup: This is what I am not able to implement. I do not want to anchor the popup with any user Event or Button click. I simply wants to show the message after some time - say
Thread.sleep(3000);
Now I have tried to use PopupWindow for this using the below code.
PopupWindow infoPopup;
LinearLayout infoLayout;
TextView infoTextView;
Button infoButton;
infoTextView = new TextView(this);
infoTextView.setText("Testing Popup Text");
infoTextView.setPadding(10,10,10,10);
infoButton = new Button(this);
infoButton.setText("Close");
infoLayout = new LinearLayout(this);
infoLayout.setOrientation(LinearLayout.VERTICAL);
infoLayout.addView(infoTextView);
infoLayout.addView(infoButton);
infoPopup = new PopupWindow(infoLayout,LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
infoPopup.setContentView(infoLayout);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
infoPopup.showAtLocation((CoordinatorLayout)findViewById(R.id.main_content),Gravity.CENTER,100,100);
But this popup is showing error at the last line giving null pointer on my
(CoordinatorLayout)findViewById(R.id.main_content)
parameter.
The issue that I am getting are:
First of all, I am not sure if this is the right approach of showing a custom UI popup. I am aware of AlertDialog but not sure which is the best option to go in this case - Please suggest.
Why the CoordinatorLayout is showing null pointer?
How to implement the top right (X) button logic in this Popup ?
1. Yes there are so many options for showing a custom UI popup in Android. You might select one from PopupWindow, AlertDialog or Dialog Activity. You need to decide which suits you best.
If you need to customize your UI a lot and have to show a list or some complex GUI then I would suggest you launch an Activity with theme.Dialog. Just set the theme of the Activity to something like this android:theme="#android:style/Theme.Holo.Light.Dialog". There's a plenty of tutorials for implementing a dialog Activity.
PopupWindow is another tool to customize your custom pop up anywhere in the screen. If you're showing this popup always in the middle of the screen, then I would like to suggest not to use this. The AlertDialog should work fine.
AlertDialog has many variants and as far as I can assume your problem, this one suits you best. You can have a cross button too in the top-right corner of the dialog (You can set the icons anywhere, as you can provide a custom layout to an AlertDialog). Personally I use this library to provide a custom layout to my AlertDialog. You can have a look at this too.
2. The NullPointerException is simple. Your layout doesn't have any id named main_content. Post your logcat if this doesn't solve your problem. Post the layout too.
3. As I've told you earlier, I use the library to provide a custom layout to an AlertDialog and you can have a look at it too. So after implementing this library you can easily design your own layout with a cross button and implement the onClick functionalities easily.
Hope this helps.
Activity with theme Dialog.
This is not a good idea. It looks like a pop-up, but you can't click outside the pop-up to close it.
PopupWindow
It will stop the application. When the user finish clicking the pop-up, the application can work again.
AlertDialog
The best one, it will not stop the application and can be closed by clicking outside the dialog.

Android - How to add radio buttons to a radiogroup in another Activity? (programatically)

I'm very new to programming with Android, so I'm making a simple text RPG to practice developing for the platform. The player has access to a menu, which shows their items as a group of Radio Buttons. That way one can be selected to Equip/sell/etc. The game is handled in a completely different Activity. I want a random event that can happen in the Game activity to be able to add a new custom Radio button to the Inventory Activity page. On my Inventory Activity page, I've written a simple method:
public void addRadioButtons(){
RadioGroup items=(RadioGroup) findViewById(R.id.invItems);
RadioButton newItem = new RadioButton(this);
newItem.setText("New Rare Item");
newItem.setId(idCounter);
idCounter++;
items.addView(newItem);
}
When the random drop event is calculated in the Game Event, it just does a object.addRadioButtons() method call. This force closes every time, no matter what I try. I've also tried pre-formatting the button before adding it, also to no avail. Am I missing something?
It seems, that the reason in idCounter value. In Android view id must be calculated according to certain rules. Look at this post for more detals.

flip board menu style for android

In my app,first activity if we click one button it should open sencond activity with transparence only at the end some part. So when we are in second activity, the content of first activity should visible some part. Its like exactly Flipboard style menu in android, in flip board if we click top red button we will get another screen in that we can visible previous screen at the bottom like this i need. If any one having ideas please share with me how to do flip board style menu.
Use Fragments for this problem that's what they are made for.
If you want to use API 8, I still recomment Fragments but then you need to use ActionBarSherlock it provides you with all features you need.

Put new component on JXTaskPane

The JXTaskPane used in Java Swing has a title bar. There is a expand/shrink button in it. I would like to be able to add my own actions in the titlebar that would appear beside
the expand/shrink button.
How can I do this? I try myself many times, but do not get the expected result. I made a new JButton and set its location over the title bar coordinates, but it's added in the element.
I have attached a screenshot that shows a help type action in the title bar to show
you what I mean.
That's not possible. I'd recommend perhaps making a new JFrame pop up when you click the button to extend the dropdown menu. Creating your own JFrame popup would also afford you more creativity and customizability when it comes to the components you can put on it. I've not much experienced with this sort of thing, but you could try to do this. It might not be ideal though.

Android activity navigation

I'm developing an app, where there is the main activity, it display some content (texts, images, etc), and a translucent menu on top of it. In this menu, I got link to other "screens" (implemented as Activities), on clicking it, I get correctly to the new activity, but the "activity changing animation" of the device blinks and then get me to the new activity.
I would like to make the navigation experience to be seamless on navigate through the screens, keeping my menu always on top of it. Today, this menu is implemented as a custom view (extending a Linear layout), also this menu will be expandable and collapsible, so the way it is I have to store the menu state through the activities.
PS: The way I've thought was to keep always two running activities, one for the current screen and another one for the menu, keep the menu on top, and just change the activity behind it. But could not find a way to do this...
PPS: Developing for Android 2.1
Anyone got an idea?
You should take a look at Fragments, or try to keep all functionality in one activity, changing only the content view.

Categories

Resources