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
Related
I have 2 layouts which contain the same buttons
layout_1.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
and
layout_2.xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Please assume these are all valid layouts etc.(I am just adding the relevant code.).
So in my fragment ,I inflate and use layout_1.xml in onCreateView.I want to toggle between the 2 scenes using button_1.
I can set the listener for button_1 in layout_1.xml during the onCreateView().
The problem is trying to set a listener on that button in the second view.i.e. the listener does not activate for the second scene(with layout_2.xml).And hence i canot toggle between the 2 scenes.Is there a way to achieve this?
It would actually appear that a proper way to do this would be to on the second scene you define an action to be performed as such:
mSecondScene.setEnterAction(new Runnable() {
#Override
public void run() {
((Button) mSecondScene.getSceneRoot().findViewById(R.id. button_1)).setOnClickListener( ... );
}
This will allow you to set your ClickListener on the View without the data binding to a generic click listener method. Then you can perform the Transition to the second scene and viola.
In general, it is not a good idea to have multiple views with the same id. This is what caused the confusion here.
Note: Below is the solution used by OP that was suitable for their specific needs:
One simple solution is to use the onClick attribute in the XML file. You can assign the same onClick method to multiple items. Like this:
And in your activity.java add this:
public void buttonClicked(View v){
Log.d("TAG","Button clicked!!"
// do stuff here
}
2nd option:
When you set a listener for one button with the id of button_1, it does not set the listener for both buttons, it only sets it for the first one. If you want to set the same listener for both, all you need to do is to assign these button different ids and then assign them the same listener.
This is what you should do:
Listener myListener = new Listener(){.. blah blah....};
((Button) findViewById(R.id.some_id)).setListerner(myListener);
((Button) findViewById(R.id.some_other_id)).setListerner(myListener);
3rd option:
findViewById(R.id.id_of_layout1).findViewById(R.id.button_1)
findViewById(R.id.id_of_layout2).findViewById(R.id.button_1)
in this case, you need add some id to your layout files, for example: layout_1.xml:
<RelativeLayout
android:id="+id/id_of_layout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/button_1"
android:text="button2"
android:background="#android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
I am making cart of an e-com application. While implementing the cart items i want to show a particular Layout like this. I Have implemented the row, the thing at which i stuck is, The increment (+) and decrement (-) Layout's to make them look similar as in the image. I am not able to decide how to do this,
I have tried and implemented two different imageviews but didnt work i want the same layout as in the image.
Can anybody help me out with this?
Thanks in advance.
You can use two button for '-' and '+' with selector layout, then u will be able to design exactly as needed. use text view for middle , update textview number in onclick method of those buttons.
Try this code which i have written for my application and its working
mIncrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
qtyCount++;
mQuantity.setText("" + qtyCount);
}
});
mDecrement.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (qtyCount > 1) {
qtyCount--;
mQuantity.setText("" + qtyCount);
}
}
});
Where mIncrement and mDecrement are textview. and mQuantity is the center textView which will increment and decrement on click of + and - respectively
What you have to do , It's tricky little bit .just use this one at your encircle area ::
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:background="#drawable/your custom background" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff0000" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00ff00">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000ff" >
</LinearLayout>
Here your custom background contains + , - ,and oval shape as per your requirement.
Now you can directly access click event of that 3 linear layout . Got it !!!
I need a way of turning a TableRow into a Button in android. I have tried to set up an onCLickListener() and I have tried nesting a TableRow inside a Button but that just crashes the app.
Edit:
I deleted the android:onCLick="onClick" like you said and that got rid of the crashing but nothing happens when I click the table row.
My code:
tableRow1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent openInfoTR1 = new Intent("android.intent.action.MENU");
fromTableRow = 1;
startActivity(openInfoTR1);
System.out.println("Confirmed click");
}
});
<TableLayout
android:id="#+id/tlDisplayTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/trTableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:visibility="invisible"
android:clickable="true">
<TextView
android:id="#+id/tvDisplayedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_weight="5"/>
</TableRow>
</TableLayout>
Every element that inherits from View can have attached an OnClickListener. No need to wrap it inside a button.
However, you'll have to look at how events are propagated through your layout. E.g. if you have clickable elements within your TableRow, the click events will normally be consumed by that elements and will not reach your OnClickListener. There are different ways to intercept or modify that behaviour, but you'd have to post your code to get more specific help.
EDIT:
The exception in your app comes from the line android:onClick="onClick" in your layout file. As you set the onClick listener programmatically, you do not need this. android:onClick="onClick" is a shortcut that would expect a method void onClick(View view) directly within your Activity (not, as you have it, as part of the OnClickListener implementation).
I have created an activity in which there is four button in center . i want that when i click on any of button then there should be open another Xml file in same. i don't want to move on another activity . i want to show on same Activity with new Xml. This new Xml hide previous one.
first xml is:-
<include
android:id="#+id/header"
layout="#layout/header"
android:layout_alignParentTop="true">
</include>
<Button
style="#style/homePageBtnStyle"
android:id="#+id/howToUse"
android:layout_centerHorizontal="true"
android:layout_marginTop="110dp"
android:text="#string/howToUse"
/>
<Button
style="#style/homePageBtnStyle"
android:id="#+id/purposeOfApp"
android:layout_alignLeft="#+id/howToUse"
android:layout_marginTop="10dp"
android:text="#string/purposeOfApp"
android:layout_below="#+id/howToUse"
/>
<Button
style="#style/homePageBtnStyle"
android:id="#+id/rateThisApp"
android:layout_alignLeft="#+id/purposeOfApp"
android:layout_marginTop="10dp"
android:text="#string/rateThisApp"
android:layout_below="#+id/purposeOfApp"
/>
and second xml is:-
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:text="#string/howtousetext"
android:id="#+id/textInstruction"
android:textColor="#color/btn_border"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:
android:background="#color/themeColor"
/>
Best thing you can do is to use ViewFlipper. You can include multiple layouts in a single ViewFlipper and you can navigate from one layout to another using showNext() and showPrevious().
And since you have only two view, this becomes very simple.
call setContentView with the second xml id again and call invalidate().
try this one:
Button button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Test1Activity.this.setContentView(R.layout.main2);
}});
I m a novice in android.. i want to add a button or menu item in message inbox or email. how can i do so.
i want to add a button or menu item in
message inbox or email
If you mean you want to add a button or menu item to somebody else's application (Email, Gmail, Messaging, etc.), you can't -- sorry!
Well this is a very broad question because there are many ways to add a button, and it really just depends on where you would like it to appear on the screen, and when. But suppose you wanted a button at the bottom of the screen that was always there, you might do something like this:
In your layout xml you will do something like this:
?xml...
<LinearLayout ...
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical" >
.... other layout items (lists, images, ext) ....
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orienation="horizontal" >
<Button
android:layout_height="wrap_content" <!--the button is only as tall as it needs to be -->
android:layout_width="fill_parent"
android:layout_weight="1" <!-- when width is "fill parent" and weight is "1" the element will share equal space with other elements with weight "1" -->
android:text="Ok"
android:id="#-id/ok_button" ></Button>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1"
android:text="Cancel"
android:id="#-id/cancel_button" ></Button>
</LinearLayout>
</LinearLayout>
Then, in onCreate() in your activity, you will need to make objects associated with the buttons, and define their behavior
Button okButton = (Button) findViewById(R.id.ok_button);
Button cancelButton = (Button) findViewById(R.id.cancel_button;
okButton.setOnClickListener(new onClickListener() {
public void onClick()
{
//do something
}
});
cancelButton.setOnClickListener(new onClickListener() {
public void onClick()
{
//do something
}
});
I hope this helps you out