Is it possible to add a button to a scroll view? - android

I have a simple ScrollView in a layout that displays an About Box in a dialog format. So it just pops up on the phone screen in a dialog. The only way for the user to close the box is to click the back button (it is an intent).
Do I have a way of adding a close button to the box or could I have the user hit the box on the screen with their finger and close it?
Here is my xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:screenOrientation="portrait"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip" >
<TextView
android:id="#+id/about_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about_text" />
</ScrollView>

The only way for the user to close the box is to click the back button
That's what any Android user would want to do. Anyway, if you want to provide the "close with button" functionality just keep in mind this: ScrollView cannot contain more than one item, so you will have to wrap both TextView and Button inside a LinearLayout or something.
But, my suggestion is that you should build an AlertDialog, which can be automatically configured with an "OK" button which will close the dialog.

Related

Android - Overlay with Button Not Responding Correctly

I have an overlay that covers my entire screen to provide a simple message when the user launches the app for the very first time. On this overlay is a single button that dismisses the overlay (by setting its visibility to gone). I have a catch-22 that I don't know how to resolve.
First, the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main_rootview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="Activity.MainActivity"
android:orientation="vertical">
<include
android:id="#+id/activity_main_content"
layout="#layout/main_content"/>
<RelativeLayout
android:id="#+id/activity_main_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#B3000000">
<Button
android:id="#+id/button dismiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:text="#string/ok"/>
</RelativeLayout>
</RelativeLayout>
The very first time they launch the app, I set this overlay to be visible. I want them to be able to click the button, which will set the visibility of the overlay to View.GONE. As this code stands, they can click the button and all is well. However, if they click anywhere else on the overlay, the click passes through the overlay and the content behind the overlay responds as if the overlay was not present. I want this overlay to absorb the click, and only respond to the button.
So, the obvious response is to add clickable="true" to the overlay, right? But then, the button itself doesn't respond to the click and the user is stuck on the overlay, thereby defeating the entire purpose of the overlay. Sometimes, if I wait long enough, the button click will occur, but if this happens at all, it is an unacceptably long delay before the button will respond.
How can I get the button to respond to a click and at the same time have the overlay consume all other tap events? Thanks for any help.
Perhaps it is not the cleanest solution, but you can set onclickListener event on the overlay.
for example
RelativeLayout overlay = (RelativeLayout)findViewById(R.id.activity_main_overlay);
overlay.setOnClickListener(new OnClickListener(){
});
//button click
Button btn = findViewById(R.id.button_dismiss);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//button action
}
);

In android, how to drop down edit text fields when a button is pressed

I have a button Add new address and when it is pressed, I want to show EditText fields to collect the new address details. Is there any layout to do that. Or hiding the text fields when the Button is unpressed, is that the only way to do this?
Define the edit box in a layout as below -
<LinearLayout
android:id="#+id/exp_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/exp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="10dp"
/>
</LinearLayout>
And now use the layout id to get the view like below.
LinearLayout l=(LinearLayout)findViewById(R.id.exp_linear_layout);
And just toggle the visibility on button click event -
l.setVisibility(View.GONE) and vice versa.
I hope it will help u.
There is no built in framework to do it. You can do this by setting View.SetVisibility() to visible or gone. Initially the button is visible but textfield is invisible. When user click on the button, you can set this button visibility invisible or gone and visible the text fields.

Adding CheckBox to ListView row view makes the row not clickable

I am trying to create a ListView similar to the Gmail app, where each row has a CheckBox on the left to facilitate multiple selection and actions via the contextual action bar, while at the same time, allow clicking each row to transition to another activity.
Originally, before adding the CheckBox the rows would indicate their selection with a light blue background color from the Holo theme I'm using and call onListItemClick in my ListFragment when clicked and onItemLongClick in my OnItemLongClickListener when long clicked.
When I add the CheckBox to my layout .xml file for the row view, the background no longer changes color and I no longer receive click or long click events. If I add android:longClickable="true" to the top ViewGroup in my view .xml then I start getting long click events again. But android:clickable="true" does not get me click events. And neither allows the background blue selection indication to return. Only the CheckBox itself provides visual touch indication with a blue background when touched.
What do I need to do to get the normal visual selection indication as well as click events on the ListView rows with the CheckBox in the view?
Here's the relevant portion of my view .xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="true"
android:orientation="horizontal" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingRight="12dp"
android:paddingTop="5dp" >
...
</LinearLayout>
</LinearLayout>
I wrote a blog post on this subject awhile back that you may find helpful. The problem is, in fact, that adding focusable elements (like CheckBox or Button) disables the ability to click the overall list item and their focusability has to be disabled. You should not add any clickable flags to the main layout either.
While adding checkbox in CAB please make it focusable="false", this will solved the problem of only one time called onItemCheckedStateChanged.

Dim background like when displaying a dialog

I would like to obtain the same effect as when a modal dialog is displayed, i.e. dimmed background, can't interact with it, but to have a layout element, or more, active "on top" of the rest. For example: I tap on an icon and a panel with detailed information appears from the left, and you can do some stuff on that panel, the rest of the screen is dimmed, inactive. How can I do this? Thanks.
You can start a new Activity with the background set to transparent on some elements, for example:
<ScrollView
android:gravity="left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
There is also an example of a TranslucentActivity in the Android API Demos:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/TranslucentActivity.html

Android: when list has ended than it should show button

Iamtrying to have a button at the bottom of the list, so when u scroll down and the list has ended than there will be a button "Get more", which will populate the list with more items.
But the button is not visible. this is how my xml file looks like.
Maybee this occurs because of the scroller,, maybe the scroller only works for the list.
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button android:id="#+id/search_browser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get more ads" />
</LinearLayout>
That's not really the correct way of doing it.
In your case, the button will ALWAYS be visible at the bottom of the list.
If you want it to be visible only when user scrolls at the bottom, use:
yourListView.addFooterView(someFooterView)
This footer view can (logically) be any view (button, layout), inflated, or created at runtime.
P.S. http://developer.android.com/reference/android/widget/ListView.html#addFooterView(android.view.View)
P.S.S. In your case, you need to remove your button declaration in XML, leaving only ListView.
And in code do
Button button = new Button(this);
button.setText("Get more ads");
((ListView) findViewById(R.id.list).addFooterView(button);
Try the following:
android:layout_height="0dip"
android:layout_weight="1"
instead of
android:layout_height="fill_parent"
for your ListView.
I believe however that is more appropriate to have an auto-growing list.

Categories

Resources