Android creating ordinary menu with ActionBarSherlock - android

I have in my activity ActionBarSherlock action bar. It is created with method onCreateOptionsMenu(Menu menu). How to leave this menu available like actionbar and create ordinary menu with some buttons?

Create a layout for your application. For example, create a button for your menu. And you can also use graphical UI android eclipse plugin to position it.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/testButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/testTitle"/>
</RelativeLayout>
In your activity class, which extends com.android.Activity,
Button testButton;
testButton = (Button) findViewById(R.id.testButton);
testButton .setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code to perform on click of the menu button
}
});

Related

How can i get below image type view of Floating Action Button in android?

I know how to set floating action button like shown in bellow image. But i need to show different layout on click or anytime when i want to show that layout. second layout is attached
How can i achieve this?
Refer this link Floating Action Button Toolbar https://github.com/AlexKolpa/fab-toolbar
There you need to add dependency in the project.
compile 'com.github.alexkolpa:floating-action-button-toolbar:0.5.1'
In your XML file,
<com.github.alexkolpa.fabtoolbar.FabToolbar
android:id="#+id/fab_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
tb:tb_anim_duration="500"
tb:tb_button_gravity="end"
tb:tb_container_gravity="center"
>
<ImageView
android:id="#+id/attach"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_attachment_white_48dp"
android:layout_marginLeft="#dimen/icon_margin"
android:layout_marginRight="#dimen/icon_margin"
/>
<!-- More buttons can be added here -->
To hide or show the Toolbar, simply call hide() and show().
FabToolbar fabToolbar = ((FabToolbar) findViewById(R.id.fab_toolbar));
findViewById(R.id.attach).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
fabToolbar.hide();
}
});

Android use MenuItem to hide LinearLayout

I am new on android.
What I want to do is, when I press the icon on Actionbar the LinearLayout is showing.
Here's the picture
As you can see, there's a magnifier icon.
when I press that icon, the search bar below it will gone.
And when i press the icon again, it'll show again.
I already trying use setVisibility, but its show error.
Here's my Java code
MenuItem searchBar;
searchBar = (MenuItem) findViewById(R.id.search);
searchBar.setVisibility(View.GONE);
}
public void toggle_contents(View v){
if(searchBar.isShown()){
Fx.slide_up(this, searchBar);
searchBar.setVisibility(View.GONE);
}
else{
searchBar.setVisibility(View.VISIBLE);
Fx.slide_down(this, searchBar);
}
}
And Here's my XML code
<LinearLayout
android:id="#+id/search"
android:background="#color/gray74"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="7dip"
android:paddingBottom="7dip" >
<EditText
android:id="#+id/search_global"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="#drawable/searchbackground"
android:text="Search"
android:textColor="#color/gray74" />
</LinearLayout>
Thanks before
First: A MenuItem is not a View and consequently it's not part of your layout. Therefore you shouldn't be using:
MenuItem searchBar = (MenuItem) findViewById(R.id.search);
But instead:
MenuItem searchBar = menu.findItem(R.id.search);
Second: When you make changes to your menu, you should call invalidateOptionsMenu().
By doing this you basically tell the menu that some changes have been made to it. This will make the menu recreate itself and request that the system call onPrepareOptionsMenu().

Android apply animation to button background

I have a button which currently has an animation assigned to it on click:
Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.startAnimation(animRotate);
}
});
Here is an example of the button in XML:
<Button
android:id="#+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="#string/btnText"
android:textColor="#ffffff"
android:textSize="18dp"
android:padding="12dp"
android:drawableTop="#drawable/btnIcon"
android:layout_weight="1" />
The animation works spot on, however on the button itself there is a text value and a drawable value, and I would like to be able to target the drawable and animate that only. I have searched all over the net and everything appears to be targeting objects by their id without any background/drawable selection.
Any feedback is welcome, even if its just to inform me that it can't be done.
Many thanks.
You may want to create a FrameLayout with the TextView and the Button and animate only the Button.

How to make a button press once and then not pressable anymore?

I have a button
<Button
android:layout_above="#id/choice2"
android:layout_centerHorizontal="true"
android:textColor="#FFFF00"
android:textSize="25sp"
android:gravity="center"
android:background="#drawable/loginbutton"
android:layout_height="30dp"
android:layout_width="fill_parent"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="#string/q1a1"
android:id="#+id/choice1">
</Button>
and when you press this button, it adds 10 to a counter/score keeper. How do I make this button able to be pressed once, but then not pressable again after that? Users are able to cheat and press the button multiple times to add more to there score.
in your onCreate method, do it like this:
final Button choice1 = (Button) findViewById(R.id.choice1);
choice1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
choice1.setEnabled(false);
}
});
You can either hide or disable it
Button mybutton;
mybutton.setVisibility(View.GONE); // hide it
mybutton.setClickable(false); // disable the ability to click it
Disable the button when it is pressed (in the code that is also handling the 'normal' action of the button).

Buttons are hiding but not showing when focus is moving in Android

I have some odd requirement. I have some menu buttons.when i am clicking on the buttons some other 3 buttons should visible. But when the focus is moving to another menu button, this 3 buttons should hide or become invisible. i did the first requirement. But unable to do the second. I take the three buttons in a relative layout.
<RelativeLayout android:id="#+id/relativelayout_inventory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/relativelayout_menu"
android:layout_toRightOf="#id/relativelayout_checkout"
android:layout_marginTop="10px"
android:layout_marginLeft="18px"
android:visibility="invisible"
>
<Button android:id="#+id/stckupdt"
android:background="#drawable/stckupdt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
<Button android:id="#+id/pushoffer"
android:background="#drawable/stckstatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/stckupdt"
android:layout_marginTop="10px"
>
</Button>
</RelativeLayout>
And in the java file, i write the code like below..
final Button button_inventory = (Button)findViewById(R.id.inventory);
final RelativeLayout view_inventory = (RelativeLayout)findViewById(R.id.relativelayout_inventory);
button_inventory.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
view_inventory.setVisibility(View.VISIBLE);
}
});
So you are intend to do like Windows menu? I don't know why you need to do that on a phone, but you better look at Touch Event to OnClickListener: Handling UI Events

Categories

Resources