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>
Related
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 a RelativeLayout inside of a ScrollView that contains a Button and some TextViews and EditTexts.
In my xml layout file, I am defining android:onClick but it always takes two clicks of the button to fire the event. The button always gets the focus on the first click and fires the onClick event on the second click. I have tried setting focusable and focusableInTouchMode both to false but the behavior doesn't change.
Here is my layout file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".DensityActivity" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
...
<TextView
...
<TextView
...
<EditText
...
<Button
android:id="#+id/ad_button_calculate"
android:layout_width="112dp"
android:layout_height="wrap_content"
android:layout_below="#id/ad_edit_obs_temp"
android:layout_alignParentRight="true"
android:layout_marginTop="20pt"
android:paddingLeft="6pt"
android:onClick="onClick"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/button_calculate" />
<TextView
...
</RelativeLayout>
</ScrollView>
Any ideas or suggestions as to why the focusable and focusableInTouchMode don't seem to do anything?
I thought it might be my onClick() method not doing what it should so I reduced it to something simple just to see and it behaves the same. Here is my simplified onClick():
public void onClick(View view) {
new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();
}
OK, I found it. It was, of course, my own mistake.
At the end of my onCreate method I was doing this:
// Set the focus to the calculate button so the keyboard won't show up automatically
Button calcButton = (Button)findViewById( R.id.ac_button_calculate );
calcButton.setFocusable( true );
calcButton.setFocusableInTouchMode( true );
calcButton.requestFocus();
So of course, no matter what I did in my xml file, I was overriding it in my code.
Instead, I used this to hide the keyboard:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN );
Which works great.
I'm a novice on the Android platform when it cames to development. However I'm going further from basic Views and I'd like to create something like the following buttons:
This is what I want to achieve. I first tought that a Button with a custom background would have sufficed. However I don't know any way to make that small darker line with the text inside. All of the image reacts like a button and gets highlighted when you touch it.
Can you help me?
If you look at the source code for Apollo you can see ArtistsFragment is not made up of Buttons but rather an inflated RelativeLayout created by a subclass of the SimpleCursorAdapter class.
Since any view can have an OnClickListener you can make any create a layout to look however you want and still have it act like a button:
// Or load it as an item from an existing layout.
View myView = this.getLayoutInflater().inflate(R.layout.anything);
myView.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// Do stuff.
}
});
Every segment with an image could be a Layout with the background set to the appropriate image. Then, you just put the button inside of the layout.
You have to use Framelayout or RelativeLayout. For example:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/your_drawabele" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="your_text" />
</FrameLayout>
In my android project, I need to add controls dynamically into my main activity screen. I created one xml (row.xml) which is added on button click on main screen. I want to capture events from the controls (button) given in row.xml.
Can anybody help me where and how to capture onClick events from newly added layouts?
Also, I want to add many child layout elements, do I need to write separate onClick methods for all the child views added dynamically?
row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_Time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:orientation="horizontal" >
<EditText
android:id="#+id/editText_FromTime"
android:layout_width="216dp"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:hint="#string/hintFromTime" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/button_Delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnDelete" />
</LinearLayout>
</LinearLayout>
So, when I click on Add Time slot button, I get a newly created row with two elements.
I want to delete this row when I click on Delete button. Do I need to have a viewID also to delete this newly-added-view?
Create an onclicklistener in the list adapter and set it to the buttons in the getView method of the adapter. That should work.
You can keep track of your controls as Java variables - don't worry about dynamic android xml. Consider declaring them all at the top, outside of methods.
One way to avoid adding a new OnClickListener for each control is let your class implement OnClickListener then use view.setOnClickListener(this). Alternatively create a subclass which overrides onClick(View) and use setOnClickListener(MyListener).
You can use Layout.removeView(View) to remove controls, as long as you keep track of them.
I have a relative layout consisting of few custom views, When I click on one of the views say "NavigationSelector" the other view say "AssistanceSelector" must be modified. I have added setOnClickListener event for object of NavigationSelector but I have observed that anywhere I click on entire relative layout the NavigationClickListener is called.
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<com.dashboard.customviews.NavigationSelector
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/navigationSelector">
</com.dashboard.customviews.NavigationSelector>
<com.dashboard.customviews.AssistanceSelector
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/assistanceSelector">
</com.dashboard.customviews.AssistanceSelector>
Activity.java:
NavigationSelector objNavigationSelector=null;
objNavigationSelector=(NavigationSelector)findViewById(R.id.navigationSelector);
objNavigationSelector.setOnClickListener(new NavigationClickListener(this));
class NavigationClickListener implements OnClickListener
public void onClick(View v) {
switch (v.getId()) {
case R.id.navigationSelector: //do required stuff
break;
...
}
Add a tag android:clickable="false" to the layout items you don't want to be clicked.
Hope it helps!
I think this is because your two views share the same position. Fix your layout params, both have android:layout_alignParentLeft="true"