How to highlight a TextView or LinearLayout when click it? - android

I have a LinearLayout that contains three TextViews. I want to highlight a TextView or the whole Layout when user clicks on the TextView. Is they any way to make it happen?
Thanks.

I supperpose 2 Layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
>
<LinearLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
>
...
</LinearLayout>
</LinearLayout>
findViewById(R.id.root).setOnClickListener(...);

I couldn't get Roman's methods working, but found my answer here instead. Use an XML resource as the background drawable and voila! it worked like a charm.

There are a number of ways of doing this.
The simplest way of doing this is by playing around with various View attributes such as android:focusable, android:focusableInTouchMode, android:clickable, and TextView attributes such as android:selectAllOnFocus.
You could also customize the appearance of your views by setting their backgrounds to StateListDrawables (a.k.a. <selector> drawables).

Related

How to have a scrollable floating Cardview like this?

Does anyone know how to have a floating Cardview like this?
http://chairnerd.seatgeek.com/images/autocomplete_checkout.gif
The background image should be able to change programmatically and the cardviews should be scrollable. And the position of the first Cardview should be somewhere below the image. Thanks in advance!
I figured it out myself and I will post my solution here in case anyone run into the same situation.
Here how the layout file should look like:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:background="#color/bgGrey">
<ImageView
android:layout_width="match_parent"
android:layout_height="125dp"
app:srcCompat="#drawable/soccer"
android:id="#+id/imageView"
android:scaleType="centerCrop"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="120dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="6dp">
EDIT: Within the LinearLayout, something like a place holder should be added. Otherwise a part of the content at the end would not be shown. So I used a textview to do so.
<TextView
android:layout_width="match_parent"
android:layout_height="120dp" />
Note: The height here should match the marginTop in the LinearLayout
Yes it is a cardView directly on a ScrollView, or a ListView simply with the item's layout with background transparency.
The scrollview/listview is placed on a FrameLayout or RelativeLayout. Either there is a padding/margin on top, or a "stub" first element which is transparent".
Bellow (declared first in the parent layout) the scrollview/listview you can place an image or any other static component whatsoever.
And above you can place other floating components (like the Check-out button on your example)

How to add a subview to an ImageView in Android

I come from an iOS background. For some reason, I cannot figure out how to add a view to another view.
I have two ImageViews that I am creating programatically as follows:
ImageView imageView;
ImageView imageHolder;
Now, I want to do something like this:
imageHolder.addView(imageView);
How do I accomplish this? Did a lot of Googling but no use.
As pskink said, you can only add views programatically to something that is a ViewGroup. You can add to a LinearLayout, for example:
LinearLayout layout = (LinearLayout)findViewById(R.id.linear_layout);
layout.addView(new EditText(context));
That probably won't help your scenario, though. To place an image on top of another you could use a Relative Layout. You'd typically set this up in the XML layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/backgroundImage"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/foregroundImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/backgroundImage"
android:layout_alignLeft="#id/backgroundImage" />
</RelativeLayout>
Then you can specify the images in code if you don't know what they're going to be beforehand:
((ImageView)findViewById(R.id.backgroundImage)).setImageResource(R.drawable.someBackgroundImage);

Android RelativeLayout Background Color

For a long time I am reading posts from stackoverflow because they are very helpful and google seems to think that also.
Since yesterday I have a problem with a row in a ListView in Android. I want to show an image and the area between the image and the bottom of the element should be filled with a grey color.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/ConversationsBadgeLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#c0c0c0"
android:orientation="vertical" >
<QuickContactBadge
android:id="#+id/ConversationsBadge"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
<!--
...
A LinearLayout with TextViews, shouldn't be interesting for this
...
-->
</RelativeLayout>
My Problem ist that the it seems that the inner layout only wraps the content but doesn't fill_parent. When I set for example 100dp it works but that is not what i want.
It would be nice if you could help me with that. I tried much workarround like using LinearLayouts and TextViews but nothing worked.
You can set the background color to the ListView itself, then everything in it will have the background you want.
BTW, I recommend using LinearLayout instead of RelativeLayout in your case.
The layout file should contain exactly one outermost element and it should have both android:layout_width="match_parent" and android:layout_height="match_parent" Your RelativeLayout has android:layout_height="wrap_content"
i think you can use you can take background color in another xml file and put in drawable folder and also using Linear Layout is very useful to your problem

Setting ListView heights with 2 ListViews in 1 LinearLayout

Here is my xml file for my layout:
<com.handmark.pulltorefresh.library.PullToRefreshScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pull_to_refresh_scrollview_feat"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewFriends"
android:layout_width="match_parent"
android:layout_height="1100dp" >
</ListView>
<ListView
android:id="#+id/listViewTrending"
android:layout_width="match_parent"
android:layout_height="1100dp" >
</ListView>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
For some reason, the only way to show both ListViews is by setting height in actual dp's. I can't use wrap_content or layout_weights.
Is this a limitation of using multiple ListViews? Or am I doing it wrong?
I simply scrapped this idea and used a MergeAdapter and got what I was looking for.
I assume PullToRefreshScrollView is some sort of ScrollView. You should not use a ListView inside a ScrollView; they just do not play well together. Not only must you must set an explicit layout height for the list(s), but the two views will get in each other's way in dealing with touch events.
If you promote the LinearLayout to the top of the container hierarchy, you can set the following attributes for each ListView:
. . .
android:layout_height="0dp"
android:layout_weight="1"
. . .
They should then take up the same vertical space.
I have not tried this, but could you set layout_height=0dp and layout_weight=1 to your ListViews and see what happens?
Hope it helps.

LinearLayout, RelativeLayout, etc. margins do not work as expected

Margins in group layouts do not seem to work.
For example,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="40dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="I'm a button" />
</LinearLayout>
should display a button with 40p margins on all sides. However, it has 80p margins on the right and bottom.
Am I doing something wrong?
Is this a bug?
A workaround would be to use gravity, but this only works with even margins.
BTW, there is a similar question posted here but has not been answered.
android:padding="40dp" on the LinearLayout or android:layout_margin="40dp" on the Button will give you the effect you want. Padding defines the space between a views edges and its content, layout margin defines extra space on the sides of a view.
The problem is actually the way FrameLayout interprets margins. setContentView() attaches your "main" layout to a FrameLayout, which is the actual root of the view hierarchy (you can see that with Hierarchy Viewer) and is offered to you by the phone.
Margins are managed by the parent layout, so in this case that main FrameLayout. I don't know if it's a feature or a bug, but that's how this layout interprets margins.
So well, the solution was already posted while I was typing: use padding instead.
if you need set margin for a layout, simply wrap it with another linear or relative layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_margin="40dip"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="I'm a button" />
</LinearLayout>
</LinearLayout>
Wrapping the Linear Layout with another layout is the best strategy.

Categories

Resources