I have the following CardView:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="expandCollapse">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:text="Heading Goes Here" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_action_down" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Description Goes Here"
android:visibility="gone" />
</LinearLayout>
</android.support.v7.widget.CardView>
Now, there are many of these cardviews, so I want to make my expandCollapse function work with all of them.
So basically, when the CardView is clicked, I want it to show or hide the Description Goes Here TextView depending on it's current visibility. It will either be GONE or VISIBLE. I also want it to change the ImageView arrow based on the current visibility.
Basically I just want this card to expand/collapse the textview inside of it. I know how do to this, but I don't know how to select the child TextView dynamically inside of my function.
How exactly do I select this TextView when the card is clicked, considering I will not be specifying an id for it, since I want this to work with many different CardView having the exact same layout?
I figured it out.
I simply added a tag to the TextView with android:tag="desc"
Then I used TextView textView = view.findViewWithTag("desc"); in my onClick function.
Related
HOw to Add close button to top right to cardview in android.
like this:
See this
i want to place my close button to the top right corner of the dialog so that the when button clicked thi will set the visibilty of cardview to gone. any better method to do this ?
You can create popup with top-right outside close button using below code snippet:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dim_10"
android:layout_marginTop="#dimen/dim_20"
android:layout_marginRight="#dimen/dim_10"
android:layout_marginBottom="#dimen/dim_20">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/dim_25"
android:orientation="vertical"
android:paddingStart="#dimen/dim_5"
android:paddingTop="#dimen/dim_18"
android:paddingEnd="#dimen/dim_5"
android:paddingBottom="#dimen/dim_40">
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/text_description"
android:textColor="#color/color_black"
android:textSize="#dimen/text_size_24"/>
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/iv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|top"
android:background="#color/color_black"
android:padding="#dimen/dim_5"
android:src="#drawable/close_popup" />
</FrameLayout>
Using FrameLayout u can achieve that by adjusting the margins for the Close button view
I'm trying to display a dynamic list on a widget and so far i've been able to display my 3 headers (which are simply textview inside a linearlayout) but for my items (2 textview in one linearlayout inside another linear layout) the view displays Loading... instead of the content (a task label).
I've already bumped getViewTypeCount to 2 in my RemoteViewsFactory to match the number of views returned by the getViewAt.
I can't figure out what is wrong inside this layout that would cause a problem:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:clickable="true"
android:focusable="true"
style="#style/SelectableItemBackground"
android:id="#+id/widget_item"
>
<LinearLayout
android:paddingLeft="22dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="•"
android:paddingRight="6dp"
android:textSize="17dp"
android:textColor="#color/colorText" />
<TextView
android:id="#+id/widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#color/colorText" />
</LinearLayout>
Thank you for reading :)
To anyone reading this, i have been able to solve it by suppressing the style attribute
I have a RelativeLayout with both an ImageView and a ViewPager. It seems as though the ViewPager sits on top of the ImageView because I had an onClick listener set up for my ImageView and when I implemented the ViewPager the ImageView no longer responds to clicks. Is there any way to "click through" to the ImageView below? In general, is it possible to ignore the "top" view and allow the "bottom" view to respond to click events?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/feed_image_content_description"
android:scaleType="centerCrop" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop">
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/custom_viewpagertitlestrip"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
You may want to try to tell ViewPage (and its children) they are not clickable by setting layout attribute android:clickable="false" (I'd also perhapss set android:focusable="false" and android:focusableInTouchMode="false" from XML or call setClickable(false) from code.
i am beginner to android programming and trying to build a simple app program.
whenever i try to make more than one views they stack on each other.
I am trying to make a text field and a button, but when i run it, the text field and button overlap each other however, i want them to be separated by some distance.
i am using the following code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.example.new1.MainActivity" >
<EditText android:id="#+id/edit_message"
android:layout_weight= "1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send" />
</RelativeLayout>
i am checking this code on Samsung Galaxy S2.
does anybody knows the solution to this problem and can guide me where i am doing it wrong.
Either use LinearLayout so that elements stack horizontally or vertically, or use attributes such as android:layout_toRightOf="#id/edit_message" to control placement within a RelativeLayout.
Find out more about LinearLayout here: http://developer.android.com/guide/topics/ui/layout/linear.html and RelativeLayout here: http://developer.android.com/guide/topics/ui/layout/relative.html
RelativeLayout positions views relative to each other. So if you do not specify the relationship between view, all the views will be put one above the other. You can either use LinearLayout with orientation attribute or define relationship between views. Following can be your working code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="#+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="#string/edit_message"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/edit_message"
android:text="#string/button_send" />
</RelativeLayout>
Here android:layout_toRightOf="#id/edit_message" lets your button to be positioned to right of your edittext
Depending on what you want, a horizontal layout or vertical, you need a fitting layout xml.
You use relativeLayout, where you have to specify the parents to layout like:
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
If your beginner better take a look at a linear layout in vertical or horizontal mode.
You dont have to specify this then.
Like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/bResults"
android:text="Try Command"
android:layout_weight="20" />
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/tbPassword"
android:layout_weight="80"
android:checked="true"
/>
</LinearLayout>
This will put your two buttons nexto each other
So what I am trying to do is, to set an OnLongClickListener to a ListView Button. I managed to put an OnClickListener to it and I also tried to put an OnLongItemClick to my whole ListView, but the problem is, that the OnLongItemClick is never called when I press the certain Button in the Item.
I was able to put the OnClickListener to the Buttons in the XML-File of the ListView-Rows by using android:onClick="myOnClickListener", but this doesn't work with the OnLongClickListener.
I also tried setting the OnLongClickListener to the button in the normal OnClickListener, but that's not what I really want because it just works after a normal click on it.
Is there any way to set the OnLongClickListener to the Buttons programmatically? They have to be unique in each row tho. Btw im not extending ListActivity.
Here is the OnClickListener for the Button:
//I attached this in my XML-File to the Button
public void handlerForPlus(View v)
{
//The LinearLayout where all my Items are in
LinearLayout myItemLayout = (LinearLayout)v.getParent();
//A random EditText in my Layout
editTextCountItem = (EditText) myItemLayout.getChildAt(0);
//The Button i want to add the OnLongClickListener to
buttonItemPlus = (Button)myItemLayout.getChildAt(1);
//Here i set the OnLongClickListener to the button, but I want to do this before it gets clicked normally.
buttonItemPlus.setOnLongClickListener(this);
editTextCountItem.setText((Integer.toString(Integer.parseInt(editTextCountItem.getText().toString()) + 1)));
myItemLayout.refreshDrawableState();
}
Here is my XML-File for the LinearLayout of the ListView-Items:
<?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:weightSum="40" >
<EditText
android:id="#+id/etAnzahl"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="5"
android:text="0" />
<!-- This is the Button i want to set a OnLongClickListener to. -->
<Button
android:id="#+id/bItemPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="25"
android:onClick="handlerForPlus"
android:text="Item" />
<Button
android:id="#+id/bItemMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:onClick="handlerForMinus"
android:text="-" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:weightSum="10"
android:layout_weight="5"
android:orientation="vertical" >
<TextView
android:id="#+id/tvPriceItemSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="PreisS" />
<TextView
android:id="#+id/tvPriceItemLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="PreisL" />
</LinearLayout>
</LinearLayout>
So does anybody know what could solve my Problem?
Best Regards, Alex.
buttonItemPlus.setOnLongClickListener(this); has the effect for entire ListView. What I think you're looking for is buttonItemPlus.setOnItemLongClickListener(this); which has the effect for particular TextView in your ListView.