So I have 3 different buttons on my layout. Now as I was creating my layout for phones etc. I decided that having 3 buttons on that page was a waste of space. Those buttons already have a ton of code logic behind them, to set their visibility, replace strings depending on situation etc.
My question is, is there a simple way to change them to a menu, is it possible to simply copy the layout XML I had for them on the main page, and paste it inside a menu?
That menu would open from a simple button, and then present all 3 buttons inside of the menu. So they aren't taking up space the entire time. Will this break the code I already have? Imagine I have the following code. Will it still
bLogin.setText(getResources().getString(R.string.Exit));
bLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
And so on. I'm concerned with the menus behavior, will it close when I click one of the options, will the visibility attributes mess up the men, etc.
You could add a fourth button (let's call it menuButton) that is used to show/hide the other three buttons. You can then add the OnClickListener to the menuButton and set the visibility of the other three buttons from VISIBLE to GONE and the other way around.
So:
Button menuButton = (Button)findViewById(R.id.menuButton);
menuButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (buttonsVisible) {
otherButton1.setVisibility(View.GONE);
...
} else {
...
}
}
});
You could use menu or action bar, which would simplify things for you (you wouldn't have to worry about showing/hiding your buttons, etc). Check the docs. Also, if you're on Android 3.0+, it is recommended to migrate to action bar:
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Of course, some work is required, but in the end, you will have a much more flexible solution (eg. if you will later want to add another action...)
Related
Possible way of hiding menuItem base on the screen(Activity) is in the foreground. I have four (4) menuItem and I want to show two (2) in the action bar and force 2 into the overflow menu, in some screen I want to show three (3) and have one (1) in the overflow menu,and in some screen the page tile is long I do not want it truncated but instead I want to show only one (1) menuItem in the action bar and force the other three (3) into the overflow menu.
I need a generic way to do this.
I need to do this programmatically I don't need the xml answer of IfRoom, I have a BaseActivity which extends ActionBar and I have BaseActivityHelper where I have my menu layout inflated, all my other activity extends the BaseActivity.
I would love to share my code but I'm not allowed to do so, the big question is if you are the one faced with this situation how will you do it.
The docs say:
If you want to modify the options menu based on events that occur during the activity lifecycle, you can do so in the onPrepareOptionsMenu() method. This method passes you the Menu object as it currently exists so you can modify it, such as add, remove, or disable items. (Fragments also provide an onPrepareOptionsMenu() callback.)
So, you can grab your menu item in onPrepareOptionsMenu and call it's setShowAsAction(int actionEnum) with the appropriate option (SHOW_AS_ACTION_ALWAYS, SHOW_AS_ACTION_IF_ROOM, or SHOW_AS_ACTION_NEVER)
I know that there are many many posts for my question , but they don't work really, as I've tried many solutions.
I would like that my title Bar (1) stays on the top normally AND my navigation bar (2) is hidden or blocked.
I'm developing an application where the back button, home button, and app button are not allowed to be clicked. (the three buttons are locked or hidden)
I already use :
#Override
public void onBackPressed() {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
But the other buttons stay available. Please see the attached picture to get a better understanding.
Click here to see my screenshot
I was viewing a different question Google Maps Android API v2 - Interactive InfoWindow (like in original android google maps) and as you can see he's using the requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); to make the map v2 use the full screen. I tried this but the my location button is partially covered by the action bar on mine unlike his. Does anyone know how to move it down slightly?
Thanks!
Unfortunately you don't have any control over this button. Obviously, you can search for this button id in android sources and change layout params manually from the code, but I definitely wouldn't do that.
This button is super easy to implement yourself. So I recommend to disable built-in "locate me" button and implement your own which will be positioned right below the action bar.
To disable it use:
googleMap.setMyLocationEnabled(false);
New button onClick method content:
googleMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(googleMap.getMyLocation().getLatitude(),
googleMap.getMyLocation().getLongitude())));
You can just get the view by its id and re-set the top margin Apparently the id is always 2, but this isn't documented and your app will break when/if the id ever changes. Like Pavel said, it's safer to make your own location button and wire it up.
Unfortunately, calling ActionBar.getHeight() in your onCreate will always return 0 since layout hasn't finished. The way to get the real heigh is by using a listener on the ViewTreeObserver:
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
int actionBarHeight = getActionBar().getHeight();
// TODO: set margin of views dependent on actionbar height
// remove the listener
mainLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
}
Let me preface my question by saying I have done a lot of research in creating dynamic buttons within an android app, and most are simply wrong or have a different view of dynamic than I do. If I missed something then just post the link and I'll check it out.
What I'm looking for is a way to create a button within my app based on information I gather from internet sources. For instance, when someone creates a post on a forums that I care to see, the app will find this, parse it for me, and return some info. Since I can't fit all this info on screen for each post that shows up, I want to create a button dynamically that previews this info. Such as name of user, date, and short description (as a preview that by clicking, will give all of the inforamtion in a separate activity). For the sake of this post, lets pretend I get this info from a text-entry location (not from an actual internt forum post).
First and foremost, how do I create the button dynamically? The other half of my question is less important to me. I would like to do this programmatically. Links to tutorials are great.
Secondly, and less important... Once I have created this button dynamically, how can I get custom views of the button based on a predictable format.
If anything is unclear, just ask and I'll try to clarify. Thanks for all your help!
In my opinion the right approach is to have an Adapter that will map the data to a certain view (a button in your case).
What adapter you choose will be depending on how you decide to fetch and store the data from internet.
When there are new posts you will be adding them to the data source (a database, a list, etc...) and you will call notifyDataSetChanged which will refresh the list, dynamically creating as many views as needed to display all the data.
I think this answers your question. The idea is to programatically create a button, and then add it to the current layout. Somethiug like this:
Button newButton = new Button(this);
newButton.setText("Click Me");
newButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
...
}
});
container.addView(newButton);
Where container is the layout that will hold the button (i.e. will become it's parent). You can also add layout settings to the button if desired.
Well, you can create a new Button, and set an onClickListener as so:
Button button = new Button(context);
button.setText("New Button");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
If you have anything else you need to set, such as an ID, you can call the method as you wish. You will need then add it to your layout as so:
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
layout.addView(button);
There is of course more you can do with it, but that should get you started, and anything else you need you should be able to find by perusing the docs.
If you need help with anything more specifically, then just comment and I will attempt to elaborate.
Basically I have a ListPreference to allow a user to change the X position of some text on my Live Wallpaper.
It contains 4 entries: top, middle, bottom and manually input X. The first 3 options are no problem, I simply get the SharedPreferences in my WallpaperService class and check if they are top, middle or bottom and change the position corresponding to their choice.
However, the last option is proving more difficult, what I want to do is have an EditText alert box popup when the user clicks the "Manually input X" ListPreference item so they can enter a value for X. I just cant figure out how to make the alert popup from clicking that specific List element.
You probably want to create a custom ListPreference. Basically you want to extend from ListPreference (see original here), and provide a custom protected void onPrepareDialogBuilder(Builder builder), in which you provide the additional "custom" list item and the onclick to handle the selection of the "custom" entry.
Note that I keep saying "custom" because it would be a best practice to make this class as reusable as possible.
Override onPreferenceTreeClick() in your PreferenceActivity and compare the preference it gives to the one you want to do something for.