Android TextView is not clickable - android

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.

Related

TextView is gone and Button is visible

Textview items are not visible. The Button below them is.
Tried changing the LinearLayout with RelativeLayout, changed the match_parent and content with 0dp. None of them worked. Only the button shows in the right spot as though the TextViews are there but invisible. These views are called in the onCreate() of the Activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".EnterData">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvDateIn"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
tools:text="Date"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvDescIn"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
tools:text="Description"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvAmountIn"
tools:text="California"
android:layout_marginTop="15dp"
android:layout_marginStart="15dp"
android:textColor="#000"
android:textSize="17sp"/>
<Button
android:id="#+id/btnSendData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:text="Send Data"/>
</LinearLayout>
You try to use android:text instead of tools:text. tools:textonly visible in the Android Studio.
Or you should use yourTextView.setText("yourText") if you are using tools:text.
Instead of using tool:text in xml use android:text="abc" in textView.
As tool:text is used for android studio layout preview . It does not show text when you run your application .
While android:text is used to set text to textview, button etc.
Yep, it was all working and just placing android:text showed it.
Thank you very much.

Android layout not clickable when include ImageView and TextView

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.

Android add background behind imagebutton, edittext, imagebutton

I'm trying to add a footer containing an imagebutton, an textview and another imagebutton to my activity (I want to make an app like whatsapp and it should nearly look the same). I have tried many ways, but all didnt work.
I tried to make a footer like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chatBottom"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:background="#D8D8D8"
android:layout_alignParentBottom="true" >
<ImageButton
android:id="#+id/emojiButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:src="#drawable/emoji_button"
android:background="#D8D8D8"
android:padding="10dp"
android:contentDescription="#string/empty"/>
<EditText
android:id="#+id/enterMessage"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5dp"
android:padding="5dp"
android:layout_toLeftOf="#+id/sendMessage"
android:layout_toRightOf="#id/emojiButton"
android:background="#drawable/textfield_multiline_activated_holo_dark"
android:hint="" />
<ImageButton
android:id="#+id/sendMessage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#D8D8D8"
android:contentDescription="#string/empty"
android:padding="10dp"
android:src="#drawable/action_send_message" />
and add it using include in xml or via addView() in java, but nothing worked properly. Most important is that the footer has its on background colour (always when I tried to set a colour to the layout where the footer is in, the whole activity got this colour) and that the textview resizes automatically.
Best regards, Oliver

enabling scrolling for textview

I have a following xml for my activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Activity1" >
<RelativeLayout
android:id="#+id/titleStoryRelativeLayout"
android:layout_weight="#string/titleWeight"
android:layout_width="fill_parent"
android:layout_height="0dip">
<TextView
android:id="#+id/titleStory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/contentTextViewLayout"
android:layout_weight="#string/contentTextViewWeight"
android:layout_width="fill_parent"
android:layout_height="0dip">
<TextView
android:id="#+id/textViewStory1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/footerStoryRelativeLayout"
android:layout_weight="#string/footerWeight"
android:layout_width="fill_parent"
android:layout_height="0dip>
<Button
android:id="#+id/buttonPrevious"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:onClick="loadPrevious"
android:text="#string/stringButtonPrevious"/>
<Button
android:id="#+id/buttonNext"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:onClick="loadNext"
android:text="#string/stringButtonNext"/>
Here I am loading my Textview (#+id/textViewStory1) dynamically from my code with some text. The problem is that during loading, few lines go beyond the screen. I am planning to enable scrolling so as user can scroll down to view those missing lines. But the scrolling should only be limited till the last line and not beyond. Any suggestion on how can I set that?
Set these settings for your textview:
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
You can then set it to be scrollable in java:
TextView.setMovementMethod(new ScrollingMovementMethod());
Try to use ScrollView (official documentation)
You can limit the Height of TextView to match the number of lines you want to see, after that put your TextView in a ScrollView
I had done a simple example to demonstrate this...
<ScrollView android:layout_height="30dp"
android:layout_width="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:textSize="16sp"
android:id="#+id/tv1"
/>
</ScrollView>
Observe how I fixed ScrollView's height and matched TextView's height to it.
I had answered this exact question before here , please check for existing answers on SO before asking them again.

Register context menu for android gridview

I'm trying to register a context menu for my gridview. At the moment nothing happens.
Amazingly I couldn't find the answer here.
My gridview is inside main.xml relativelayout:
<?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="match_parent">
<LinearLayout
android:id="#+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:id="#+id/cameraButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_camera"
android:onClick="cameraButtonOnClick"/>
<ImageButton
android:id="#+id/addGalleryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_gallery"
android:onClick="addGalleryButtonOnClick"/>
<ImageButton
android:id="#+id/addDirButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_dir"
android:onClick="addDirButtonOnClick"/>
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="spacingWidthUniform"
android:gravity="center"
android:layout_above="#id/bottom_bar"
/>
The gridview is filled with thmbnail.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.ninovanhooff.projectv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/thumbview"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:background="#drawable/thumb_selector"
android:src="#drawable/sample_0" />
<!-- <TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:focusable="false"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:background="#drawable/label_bg"/>
<com.ninovanhooff.projectv.ThreeStateCheckBox
android:id="#+id/tricheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
app:label=""
app:shadow="false"
app:state="0"
android:layout_alignParentBottom="true">
</com.ninovanhooff.projectv.ThreeStateCheckBox>
-->
</RelativeLayout>
Note that only the imagebutton is preserved to simplify problem
Contextmenu is registered like this:
adapter = new ThumbnailAdapter(this);
gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(adapter);
registerForContextMenu(gridView);
I suspect it's got something to do with android:focusable or android:clickable. But don't know which to try. I tried focusable = false and clickable = false on both the relativelayout and linearlayout of main.xml, but no effect.
So, I got this to work and my suspicion was right. Other elements where stealing the touch events away from intended gridview element. Make sure to remove clickable objects like buttons and checkboxes from your layout to find the culprit. In my case, I had to remove both ImageButton and ThreeStateCheckbox. Another mistake was that I added onclicklisteners to the ImageButton rather than the direct child of gridview (thumbnail.xml). The recommended approach is to use gridview.setOnItemClickListener(listener)).
Now, this works, but I still need my image and checkbox back. ImageButton was replaced by an ImageView, and I added android:focusable="false" to the checkbox to prevent it from consuming the click event. And everything works fine.
If you can't figure this stuff out, the hierarchyviewer can be enlightening. Maybe you forgot about some tiny element which could steal your touch events. Good luck!

Categories

Resources