I have a preference activity which gets it's layout from the following XML. This makes a button appear at the bottom of the preferences.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.stealthcopter.nexus.nexusgl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button android:text="This is a button on top of all preferences."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
However when open a nested PreferenceScreen inside it reverts to the showing just a listview, is there a simple way to keep the layout the same between the preference screens?
I tried PreferenceScreen.setLayoutResource(R.id.layout.main); but that just changes the view displayed before it is opened
Update
I fixed (Kludged) this by changing the nested PreferenceScreens into normal preferences and overriding their onclick methods to clear the preferences from the listview and reload the preferences from an appropriate XML file.
I believe that this is another symptom of the bug described here
A work around is to reset the layout each time a preference screen is opened.
What are you trying to achieve with the custom preference screen? The is no preference to take into account here? I also I'm pretty sure that preference layout have to be in a 'xml' folder not the 'layout'
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" />
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.
In Android, i noticed that you can have a fixed view on top of another. For example, when you open your browser, and tap the search box, a keyboard prompt pops up (on top of a listview). However, notice that you can still scroll up and down on the listview without the keyboard going away. Like:
would someone please explain (preferrably some sample code in addition) how this works?
What i'm trying to do is just have a custom listview that always has a floating navigation bar on top of the listview and also on the bottom of the list view (it's not actually a header/footer of the listview, it's more like a header/footer of the screen). It would be similar to the example i just described, where the user can interact with both the navigation bar as well as the listview "underneath" the nav bar.
I am somewhat new to Android development, so please be nice and provide a little bit of details if you would :) much thanks in advance!!
whoops. looks like someone had a similar issue:
Layout Layers? Z-Axis?
and this post http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html explains how FrameLayout works and also how works, which is an even better alternative.
FrameLayout lays object in a different Z-axis, so this is the solution i was looking for.
There are many ways to achieve that, the simpler i can think of is using linear layout :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/header">
//Here you add whatever you want in your "header"
</LinearLayout>
//create your listview
<ListView
android:id="#+id/content_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:layout_marginRight="10dip"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:layout_marginBottom="10dip"
android:scrollbars="none"
android:paddingBottom="5dp"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/footer">
//Here you add whatever you want in your "footer"
</LinearLayout>
Im trying to have a searchbox on the top of my list view. But I want this searchbox to disapear sometimes and the listview to resize to regain space. Is there a way I can do that without reloading another and different layout ???
is there a way to add and remove a component from the current view ?I have been playing with setvisibility but it doesnt resize anything.
Please, if you know, give code example ! :)
I did this with a layout like this
<RelativeLayout android:id="#+id/editFrame"
android:layout_width="wrap_content"
android:layout_below="#id/imageAttachments"
android:layout_height="wrap_content"
>
<EditText android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
></EditText>
</RelativeLayout>
<ListView android:id="#+id/ListView01"
android:layout_below="#id/editFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
></ListView>
Then, in the code, do the following:
findViewById(R.id.editText).setVisibility(View.GONE);
to free up the space, or
findViewById(R.id.editText).setVisibility(View.VISIBLE);
to show the search box.
Instead of the EditText, one can as well use any other single control or a layout for a combination of controls.
Setting its visibility to GONE will make the surrounding editFrame layout (can as well be a FrameLayout) shrink to zero size and reclaim the space for the ListView (which is set to be layout directly below the editFrame layout).
How does Android determine whether to move the layout up when showing the softkeyboard?
Note: I am aware that the activity property android:windowSoftInputMode="adjustResize|adjustResize|adjustUnspecified"
exists, as described here http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
, but in my case it doesn't seem to have any effect. This is my problem:
I have two activities, pretty much the same layout, but the first one is using a ListView that holds a list of buttons. The second activity holds a scrollview with buttons.
The rest is the same, same number of buttons, same height of elements, etc.
Now, when I press the search button to open the search input bar, in my first activity, the entire layouts gets moved up.
While on the second activity, the layout is not being moved up but the softkeyboard just displays on top of it. This is actually how I want it to behave. How can I achieve the same with my activity that's using the ListView?
In my manifest, initially I didn't specify any android:windowSoftInputMode attribute, but even if I do, it doesn't make any difference; I tried all three values (adjustPan, adjustResize, adjustUndefined, without any difference).
This is my layout:
1) http://pastebin.com/5zzVxjbK
2) http://pastebin.com/KFtPuHvP
Interestingly though: when I set my ListView visibility in my layout 1 (left) to View.INVISIBLE, then the layout doesn not get moved up!
I can achieve this with a RelativeLayout and setting android:windowSoftInputMode="adjustPan" like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<include android:id="#+id/Header" layout="#layout/header"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#id/Header"
android:layout_above="#+id/Footer" />
<include android:id="#id/Footer" layout="#layout/footer"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
You may be able to achive this by setting the footer gravity (or layout_gravity) to "bottom", if you want to keep the LinearLayout, but I am not sure.