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.
Related
I have a fragment defined by the following xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_example"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_example"/>
</RelativeLayout>
Now I would like to reuse the ImageButton (and other buttons I will add) in another fragment, ideally
without copying its definition
by keeping the definition in xml and not adding it programmatically
I.e. the goal is to overlay the same set of buttons in different fragments.
Is there a way to define all buttons in a separate xml file and load them programmatically on fragment creation?
Yes, You can.
Define all buttons in different xml say layout_buttons.xml
and add them in each fragment layout using
<include layout="#layout/layout_buttons" />
Simple question, i have this code
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
...
<LinearLayout
android:id="#+id/insideLinearLayout1"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
<LinearLayout
android:id="#+id/insideLinearLayout2"
...
<TextField>
android:text='beforeClick'
...
<Button
android:id="#+id/button1"
android:onClick="updateExpression"
...
</Linear Layout
</LinearLayout>
</HorizontalScrollView>
The idea, is to change the textField text property, when clicking the button on the same layout!
Ok for 2 is easy, just need the reference of each button and each textfield and change use the get and set method!
But i want to add more layouts dynamically, and grab each one's reference would be a hard task.
So two questions :
How can i grab the id of the Linear layout, that have the clicked button?
I want to handle the 'insideLinearLayout(1 or 2 depending on the clicked button)'id in the 'updateExpression'!
How can i add more layouts with the same widgets as the ones created manually?
Thank you in advance.
Best of codings!
1)Your onClick function is passed the view that was clicked. Every view has a getParent() function. You can use it to get the LinearLayout, then get the id.
2)Create them with the new keyword then add them to the parent layout. For something like this I would probably make a custom compund view holding everything you want to instantiate at once, so you can treat the entire set of widgets as one.
I put admob ads in my app. They appear as a footer view in a ListView. Strangely, the ad doesn't respond to the first click. I need to click it a second time for it to respond. Thereafter, it responds normally to all subsequent clicks.
I don't have a click listener assigned to the view that holds it. I don't have an OnItemClickListener assigned to the listview either. This is what the view looks like that I inject the admob view into (and this inflated view is added as the ListView footer).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp" >
<!-- I add the Ad to this inner layout at runtime. -->
<LinearLayout
android:id="#+id/body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="5dp">
</LinearLayout>
</LinearLayout>
...
View parent = inflateContainerFromXml(R.layout.foo);
parent.addView(makeAdMobView());
ListView lv = ...;
lv.addFooterView(parent);
I realize the nested layout above looks slightly weird but it's because of a visual style I'm trying to achieve.
Any idea what could be causing this? I think the admob view creation code should be ok, because I use it elsewhere in the app in a plain linearlayout and click handling behaves normally there.
Thanks
I had the same problem; solved by adding
android:descendantFocusability="blocksDescendants"
in the root layout of the listview item
I am creating a custom widget to display a rotating list of items. I have declared the layout in newsview.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/tblLink"
style="#style/newsviewstyle"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/newsviewimg"
style="#style/newsviewimgstyle"
android:layout_margin="5dp" />
<TextView
android:id="#+id/newsviewtitle"
android:layout_toRightOf="#id/newsviewimg"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:textColor="#000000" />
<TextView
android:id="#+id/newsviewurl"
android:layout_toRightOf="#id/newsviewimg"
android:layout_below="#id/newsviewtitle"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:textColor="#000000" />
</RelativeLayout>
The idea is in my main.xml, I have a ViewFlipper that I would like to use animate the transitions between each item. How could I instantiate "n" unique items declared above, so they can bee added to the ViewFlipper. How would I be able to get access to the layout declared from a class outside the main activity (and also outside the package containing the main application, i.e. a view controller).
As a note, collecting the list of items takes place in a controller outside the main activity of my application. I would like to keep everything as much as possible in the controller for possible reuse later on.
How could I instantiate "n" unique items declared above, so they can bee added to the ViewFlipper.
Use getLayoutInflater().inflate(), the same way you would in a custom Adapter. In fact, on Honeycomb, there is AdapterViewFlipper to handle this very scenario.
How would I be able to get access to the layout declared from a class outside the main activity (and also outside the package containing the main application, i.e. a view controller).
I'm sorry, but this sentence did not parse for me.
On my screen I have a list view and a button. my list has like 8 item. I would like my screen to scroll if both these items does not fit in. I don't want my list to have scroll but the complete layout including both list & button. If I use the below layout it only shows on item inside the list and I have to scroll within the list to go to next item.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#android:id/list"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:background="#drawable/round_background" />
<Button android:text="Search" android:id="#+id/carSearchButton"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
You can't put a ListView inside a ScrollView. Of GridView, or whatever View that handles scrolling on the same axis as the ScrollView does. That way the framework wouldn't know which View should handle the scrolling event. This layout won't produce an error when you compile it, but it won't work properly.
What you should do here: dump the outer ScrollView, you don't need it. Only use a ListView, and add the button to the ListView, using .addFooter(), that's the easiest way. This way your button'll appear as a list element, but you don't have to mess around with a custom adapter.
Scythe kind of answers my question but I wanted more then one one control below the list also on another screen I wanted 2 lists. So in order to have the scroll bar working with list view I had to fix the height of the list.