how to add close button to top right to cardview in android - java

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

Related

how can i align my floating button to the bottom of the page in android studio

//Parent View
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/back4">
//Relative Layout Child of ScrollView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp">
//Grid View with Parent Relative Layout. Grid View not allowing floating button to align bottom
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="50dp">
</GridView>
//Floating Button adding to Bottom of screen
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/addNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/gridview"
android:layout_alignParentEnd="true"
android:src="#drawable/addbutton"/>
//End of Relative Layout
</RelativeLayout>
//End of Scroll View
Make some changes to your FloatingActionButton XML. Hope this works for you!
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/addNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:src="#drawable/addbutton" />
Also, there is no need of vertically scrolling ScrollView as GridView is also a vertically scrolling widget(GridView).

Toggle child TextView inside CardView based on visibility

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.

Bypassing stacked views with click listeners

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.

Putting a GestureOverlayView in a ScrollView so that gestures can still be recorded - Android

I'm trying to put a GestureOverlayView inside a scrollview so that I can scroll down and a GestureOverlayView will be there. However, the GestureOverlayView isn't recording my gestures. When I try to gesture on it, the entire window just scrolls.
Is there a way to disable the scrolling when I am touching the GestureOverlayView and still have it record my gestures?
Here's my xml roughly. I have a scrollview that houses a webview and a gestureoverlayview. I want to be able to scroll through the webview and have the gestureoverlayview be at the bottom of the webview. Then I want to be able to make a gesture. Right now, the gestureoverlayview is underneath the webview just fine. But I can't make a gesture, because the screen just scrolls rather than recognizing my gestures.
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2" />
<RelativeLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="200dp" >
<GestureOverlayView
android:id="#+id/gesture"
android:layout_width="650dp"
android:layout_height="140dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:eventsInterceptionEnabled="true"
android:fadeEnabled="false"
android:fadeOffset="5"
android:gestureColor="#color/black"
android:gestureStrokeAngleThreshold="0.1"
android:gestureStrokeLengthThreshold="0.1"
android:gestureStrokeSquarenessThreshold="0.1"
android:gestureStrokeType="multiple"
android:gestureStrokeWidth="1.2"
android:orientation="vertical"
android:requiresFadingEdge="vertical" >
</GestureOverlayView>
</LinearLayout>
</ScrollView>

Java/Android: When I put a ScrollView - I get a soft keyboard. Can't make a good layout

I have a problem with the layout. I want to place one textedit and one button at the bottom of my activity. And then I want a TextView component that fills all free space on my activity's layout.
I have a big text in the bigText component that's why I need to use the ScrollView.
I have a few questions:
I don't understand, why I get a soft keyboard when I place the ScrollView component? But I don't get this keyboard without the scrollview! If I turn off the soft keyboard, like this:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );
Then how I can use my EditText?
Could you help me with my layout? The problem is: when I use the ScrollView - it fills all phone's display I even see my button and EditText which should place below the ScrollView (i don't see completely bottom of my layout). How can I fix this?
And that's my fail attempt of layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/bigText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search string: " />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:gravity="center_vertical|center_horizontal" >
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go >>" />
</LinearLayout>
</LinearLayout>
I'm not sure why adding a ScrollView would make the soft keyboard pop up, but it's easily remedied. Turn it off programmatically like this:
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
IBinder binder = view.getApplicationWindowToken();
if (binder != null) {
manager.hideSoftInputFromWindow(binder, 0);
}
where view can be your ScrollView or a TextView or just about anything else
Simply:
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );

Categories