Android layout not clickable when include ImageView and TextView - android

I have a layout like following. There are total five similar tabs. When I set onClickListener to the LinearLayout tab_dashboard in code, nothing fired. I have to set click listener to the ImageButton and TextView as well to make the whole area clickable. I tried to set all child items clickable="false" and duplicateParentState="true", no luck. I also tried to change the ImageButton to ImageView, same issue. Anyone has idea on this?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/tab_dashboard"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/white"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="#+id/dashboard_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#android:color/white"
android:src="#drawable/ic_dashboard" />
<TextView
android:id="#+id/dashboard_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="#string/dashboard"
android:textSize="10sp" />
</LinearLayout>
<!-- Four more similar tabs -->
</LinearLayout

To the TextView and ImageView add android:clickable="false" and android:focusable="false"
In your activity add following code.
((ImageView) findViewById(R.id.dashboard_button)).setOnClickListener(null);
((ImageView) findViewById(R.id.dashboard_button)).setFocusable(false);
((TextView) findViewById(R.id.dashboard_text)).setOnClickListener(null);
((TextView) findViewById(R.id.dashboard_text)).setFocusable(false);

I resolved the issue by replacing the android:layout_width="0dp" and android:layout_weight="1" with other way that achieve the same effect. I guess the android:layout_width="0dp" may the cause of this issue. Will do more investigation later.

Related

Create a clickable item like button with a particuliar view inside

I'm starting in android but I'm working on a project that asks me to do the same thing as the pictures below. However I don't know how to analyze this view to be able to produce something similar.
From what I see in the activity I have a "button" and when I click on it, the elements present in the "button" change. Could someone explain to me how to cut out the view and the elements to do the same thing? Thank you in advance for your answer.
Pictures :
based view
view after click on "button"
I gonna give you a rough idea..
its not a button its a layout containing 2 textviews, 2 imageviews and one seekbar and i will suggest you to use a linearlayout.add a clicklistner on it.
to change the elements after clicking on layout you have to get the references of its elements textview etc as mentioned and change them by yourself. ex- seekbar.setprogress(30) approximately as your picture after click.
xml..
Linearlayout
textview
imageview and textview
imageview
seekbar
\Linearlayout
#Miina Neko Use card view inside cardview use a linear layout (or anyother layout you like) inside cardview use below code i made a layout for you
<androidx.cardview.widget.CardView
app:cardCornerRadius="#dimen/_5sdp"
app:cardElevation="#dimen/_5sdp"
app:cardBackgroundColor="#color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:padding="#dimen/_5sdp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/refresh_btn"
android:layout_width="#dimen/_10sdp"
android:layout_height="match_parent" />
<TextView
android:layout_marginLeft="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="arrrarrr-raaar"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="#dimen/_3sdp"
android:layout_marginBottom="#dimen/_3sdp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_marginRight="#dimen/_5sdp"
android:src="#drawable/refresh_btn"
android:layout_width="#dimen/_10sdp"
android:layout_height="#dimen/_10sdp" />
<ImageView
android:layout_marginLeft="#dimen/_5sdp"
android:src="#drawable/refresh_btn"
android:layout_width="#dimen/_10sdp"
android:layout_height="#dimen/_10sdp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:layout_marginRight="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_5sdp"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminateDrawable="#android:drawable/progress_indeterminate_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
NOTE: "#dimen/_sdp" is a library i am sharing link and chage your drawable accrouding to your choice and make ids and in java or kotlin change then programmatically when clicked or any action performed like in second picture hope answer your question..
SDP library link https://github.com/intuit/sdp

TextView and EditText overflow

I've got a need to display and TextView (label), EditText(Input) and TextView(suffix) in a line.
The issue I have is - if the label TextView is too long it The EditText and Suffix TextView and not drawn.
Here's my code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="0"
android:padding="5dp"
android:text="Label"
android:id="#+id/label_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="1"
android:id="#+id/input_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColorHint="#color/dark_grey"
android:textColor="#color/black"
android:hint="text.." />
<TextView
android:padding="5dp"
android:layout_weight="0"
android:text="Suffix.."
android:background="#color/lite_grey"
android:id="#+id/suffix_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
So what I want is - even if the label is over 2-3 lines, for an EditText to appear followed by another TextView.
Any pointers would be appreciated.
Thanks!
Edit:
I've added some 'diagrams' so you can understand what I want to do a bit better.
The label to the EditText can be arbitrary length(2/3 lines), so whenever the label finishes I want an EditText to start (which is followed by another TextView showing a suffix..)
..
![This shows a 2 line label and TextView..]
Edit 2: Thanks for the responses guys!! I think #questioner has really understood what I want to do i.e. I want your 3 views to move to other lines so that they behave like they were one view - and as he has suggested i'll have to find a library for this!
How about this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="5dp"
android:text="Label"
android:id="#+id/label_text_view"
android:layout_width="100dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content"/>
<EditText
android:layout_weight="1"
android:id="#+id/input_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColorHint="#color/dark_grey"
android:textColor="#color/black"
android:hint="text.."/>
<TextView
android:padding="5dp"
android:text="Suffix.."
android:layout_gravity="center_vertical"
android:background="#color/lite_grey"
android:id="#+id/suffix_text_view"
android:layout_width="100dp"
android:layout_height="wrap_content"/>
</LinearLayout>
If you want your 3 views to move to other lines so that they behave like they were one view - you should use external library for that.
Why not use a RelativeLayout instead of LinearLayout and then use android:layout_below="#+id/..." or android:layout_above="#+id/..." also android:layout_toRightOf="#+id/..." or android:layout_toLeftOf="#+id/..."
Set
android:layout_weight="1"
for each view so that they all occupy the same amount of horizontal space.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1">
Try this or please expand a little more.

Android - Background reset after show/hide views with animation

I am showing and hiding views on a particular actions using some animation, I used LayoutTransition and it seems to work fine except one thing, while the animation is going, the background of some TextViews changes and reset to the default white background!!
Following is the code I am using to apply animation after hide/show.
mRightTransitioner = new LayoutTransition();
mRightTransitioner.setDuration(1000);
vg_rightContainer.setLayoutTransition(mRightTransitioner);
Edited: even without animation i tried, same problem, the background resets once view's position changes
Edited: here how i declared the views in the XML
<LinearLayout
android:id="#+id/ll_req_reasonwrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<EditText
android:id="#+id/et_req_reasons"
android:layout_width="400dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:gravity="right|center_vertical"
android:inputType="text"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_preasons" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_timewrapper"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:baselineAligned="false"
android:gravity="right"
android:orientation="horizontal"
android:visibility="gone" >
<LinearLayout
android:id="#+id/ll_req_endtimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/ftv_req_endtime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_endtime" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_req_starttimewrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="50dp" >
<TextView
android:id="#+id/ftv_req_starttime"
android:layout_width="100dp"
android:layout_height="match_parent"
android:background="#drawable/comments_textbox"
android:freezesText="true"
android:gravity="right|center_vertical"
android:inputType="numberDecimal"
android:paddingRight="8dp" />
<TextView
android:layout_width="100dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="#string/str_req_starttime" />
</LinearLayout>
</LinearLayout>
for instance: if i hide 'll_req_reasonwrapper', then the LinearLayout 'll_req_timewrapper' moves up, then the backgrounds of the textViews like 'ftv_req_starttime' becomes white!!
Please help.
Edited Answer:
if your are using linear layout then if the child is not being displayed, it is not going to be laid out and not going to be the part of the measurements ( as it looks like ). I recommend to use the RelativeLayout.
You can try
android:background="#null"
for your buttons. if your are using some custom class for button then add
.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
for this specific views.
Need more code. The fact that disabling the LayoutTransition does not solve your issue, means that the animation is not the issue.
How do you reinstantiate child views or populate your view. Are you removing views with setVisibility(View.GONE) and then setting them back with setVisibility(View.VISIBLE)
OR
Are you removing childAt(i) and then dynamically adding custom views
I'm sorry I couldnt add a comment since I dont have the reputation for that but I think I am able to answer your question if you post more code.

Android TextView is not clickable

I have this small piece of code, and I started using relative layouts and the textview is no longer clickable. I am posting the XML below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/fav_list_selector"
android:orientation="horizontal"
android:padding="5dip">
<ImageButton
android:id="#+id/pinImage"
android:layout_alignParentLeft="true"
android:src="#drawable/ic_9_av_make_available_offline"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/favBusItem"
android:layout_toRightOf="#+id/pinImage"
android:layout_toLeftOf="#+id/transpoImage"
android:layout_centerVertical="true"
android:textSize="12sp"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:clickable = "true"
android:textColor="#000000">
</TextView>
<ImageButton
android:id="#+id/transpoImage"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/pinImage"
android:src="#drawable/ic_9_av_make_available_offline"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</RelativeLayout>
I suspect that the textview is not clickable because of the ImageButton.
Well, I figured it out myself.. Anyway here is the answer in case anyone else hits the same issue:
add this attribute for your imagebutton
android:focusableInTouchMode="false"
also add
android:descendantFocusability="blocksDescendants"
for your parent layout
This worked for me
use android:clickable="true", in this part of your code:
<ImageButton
android:id="#+id/transpoImage"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/pinImage"
android:src="#drawable/ic_9_av_make_available_offline"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
Use an OnClickListener in your class and hook it up to your TextView.

Unable to specify the alignComponent for an ImageButton

I'm trying to set my TextView to the left of an ImageButton, but I can't seems to find this option.
I was expecting to use something like android:layout_alignLeft, but this option is missing.
I've tried to google the issue, but couldn't find any relative results.
Without it my TextView overlaps the ImageButton and I want to avoid it.
UPDATE
The full xml code is too complex, but here is the important part of it:
<FrameLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:background="#color/white"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_toLeftOf="#+id/frameLayoutBalanceClosed">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/button_edit_nickname"
android:id="#+id/card_closed_control_editNickname" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/card_closed_description_nickname"
android:layout_margin="8dp" android:layout_gravity="left"/>
</FrameLayout>
I think what you need is a RelativeLayout. You can specify your TextView to the left of your ImageView with it's specifications. Your code would look something like this:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/imagebutton1" />
<ImageButton
android:id="#+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
The reason FrameLayout isn't working is because it's purpose is to overlay items on top of each other, which wouldn't work at all!
If that isn't what you're looking for, you could also use a TableLayout in which items are arranged in columns.

Categories

Resources