android:clickable="true" mean's that it's not clickable? - java

I have a ListView with some custom sections in it. Each section has it's own header View. I want the elements in the list to be clickable, but obviously do not want the section headers to be clickable. So in the xml for the section headers I added android:clickable="false".
When debugging I noticed that the section headers were still responding to my setOnItemClickListener(). Then I tried setting android:clickable="true" in the XML. And sure enough, the section header views no longer respond to the clicks...
So what is the deal here? Why is it that setting clickable = true telling it that it is NOT clickable? Am I misunderstanding something here? Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:background="#android:color/transparent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingLeft="30dp"
android:clickable="true" />
If I set that clickable="false" at the bottom, then this view begins to respond to the setOnItemClickListener()...

When you set the OnItemClickListener, the event onItemClicked will only be called if the child of the ListView does not have the OnClickListener set. Setting clickable to true will provide the child view (in this case your TextView) with an empty OnClickListener. Since the TextView's OnClickListener is set the OnItemClickListener will not be called.

I think you should not write android:clickable="true" when it was a child of the list item view. If you have a selector for the listitem, just setbackground on the root tag.

Related

Click arrow to reveal more content in application

i am trying to achieve a behavior where user click an arrow that can reveal more content such as more description abort something. It can a recycler view as well where more things can be added dynamically and the list will expand.Right now i do not have any idea how it can be achieved. I tried searching on the internet for solutions and saw a widget called spinner but i do not think it can help me achieve my desired behavior. YouTube does apply similar behavior as well
Below are the pictures which will make my question clear. Any help would be appreciated Thank You
Before clicking the arrow pic 1
After clicking the arrow pic 2
In your layout.xml include a nested layout that includes a Textview that holds the additional information and set android:visibility="gone". Use an OnClickListener to the button that is meant to expand the view. In the onClick method check if the view is visible or not. If it's not you make it visible, otherwise you set it to gone again.
layout:
...
<ImageView
android:id="#+id/chevron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/chevron"
/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="your additional info here"
android:visibility="gone"/>
...
In your Activity:
ImageView yourView = findViewById(R.id.chevron);
..
yourView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getVisibility() == View.Gone) {
view.setVisibility(View.Visible);
} else {
view.setVisibility(View.Gone);
}
}
});
I used for this purpose ExpandbleLayout from this github library
ExpandableLayout. In readMe of the github repo you can find example of using it, you can get similar experience with as in your example, without need to manually create View for arrow and handling the animation.
You can use it like this :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.github.aakira.expandablelayout.ExpandableRelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:ael_expanded="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text goes here"
android:textSize="28sp" />
</com.github.aakira.expandablelayout.ExpandableRelativeLayout>
And in your java/kotlin code : do additional logic to expand/collapse call : expandableLayout.toggle();.
All the credit goes to the author of the library.
https://github.com/AAkira/ExpandableLayout

How add multiple TextView inside an ImageButton?

Is it possible to add multiple TextView inside one ImageButton with colour background ?
The core need is to have a button with the action text on it, and a subtext nearby explaining the action or giving other information related to the action. This subtext can vary from time to time.
Considering this requirement, one solution is to have a normal button and a subtext below, not clickable. But I find it messy. A better approach which I like is, on iOS for instance, to have a clickable UIView containing the action as bold text and the explanation as light text. See the image bellow containing 4 buttons :
How to achieve the same on Android with Java ? The closest I can have is to have an ImageButton bellow a TextView, and it does not sound right.
Is possible to nest TextViews inside an ImageButton ? If not, what is the best alternative ?
I hope this may be useful it explains how to position a textView within and in front of a imageView in the XML.
TextView inside of ImageButton/ImageView XML - Android Dev
Obviously make sure each view has a unique id/name which you can assign as shown here on this link
Sorry I cannot explain specifically myself but it has been a while since developing in Java for Android.
I dont know why you want this behaviour but you can make a container for your views and add a click listener to the whole view. you can also use it anywhere.
an example of this would be.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:background="#drawable/container_background"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="0.33"
/>
add a selector background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/text_pressed" />
<item android:color="#color/normal" />
</selector>
and the listener
findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

there is some way to make my own item in xml?

i work on android-studio project, and my question is about if
there is some way i can define in xml type of some item that contain
few edit text and buttons, and open listview that contain the item i create?
somthing like :
<item
<Editext(some setting)/>
<Edittext 1(some setting)/>
<Button(some setting)/>
/>
and then some adapter that adjust or something like that, that i can add to ListView.
i saw in youtube some videos that try to explain that but i get stock.. i dno't really get this.
This took my a while to figure out, but it works.
Create your Layout file just however you want. Save it as res/layout/something.txt. Example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:text="hello"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text1"/>
<TextView
android:text="world"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text2"/>
</LinearLayout>
Use it in your code as follows:
LinearLayout mainLinearLayout = new LinearLayout(getApplicationContext());
// inflate your container
View view = inflater.inflate(R.layout.something, null);
// you can make changes to the elements like this:
TextView txt1 = (TextView) view.findViewById(R.id.text1);
TextView txt2 = (TextView) view.findViewById(R.id.text2);
view.setTag(tag);
// add to main layout
mainLinearLayout.addView(view);
Where tag is an unique integer to identify the View. You don't need to set the tag if you're using the container only once.
I didn't use an Adapter when I made this. I just used a ScrollView to make a continous list of entries. Change this to your needs.

how to open a popup at the bottom, above another View in Android?

In my Android App I have a view and on it a pop-up is opened at certain conditions.
How can I set it's position to be at the bottom? (anyhow above the other view).
Today's code just deals with Visibility 'gone' or 'hide'.
This is the popUp Xml:
<RelativeLayout
android:id="#+id/alerterLayout"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:layout_marginTop="90dp"
android:background="#drawable/popup_alerter_base"
android:gravity="right"
android:padding="0dp">
<!--
========================================================================
* Title Text
=======================================================================
-->
<LinearLayout
android:id="#+id/alerterTitleLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="2dp"
android:orientation="horizontal" >
...
Assuming you are talking about using PopupWindow, you can use showAtLocation(View v, int gravity, int x, int y) to get it in the desired spot with respect to whichever View you want.
You can play with the params to get the exact result you need.
You can take the layout inside the same xml file as in which you have defined your view, on top of which you need to show the pop-up. Then you can try to make the pop-up layout visible and invisible whenever you want to. Keep the initial visibility of Pop-up layout as GONE.

Android ListView Alternating Selector

I currently have a ListView which contains a Radiobutton and a TextView on each row. The ListView uses a custom adapter, and I've given the ListView the android:choiceMode="singleChoice" directive, to insure only a single element could be chosen from the list. However, I can't seem to highlight the selected item.
Here is the XML declaration of the ListView:
<ListView android:id="#+id/employees_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="2dp"
android:paddingRight="1dp"
android:background="#drawable/borderlist"
android:choiceMode="singleChoice"
android:listSelector="#48ad82"
android:layout_below="#id/employees_header">
</ListView>
Here is the XML declaration of the ListView's rows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row_employee"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:descendantFocusability="blocksDescendants">
<RadioButton android:id="#+id/radio_employee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"/>
<TextView
android:id="#+id/text_employeename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_toRightOf="#id/radio_employee"
android:layout_marginTop="3dp"
android:layout_marginLeft="5dp"
android:focusable="false" />
</RelativeLayout>
Since I want to alternate the row's colors, I set a selector on each row by programming, I have this bit of code in the custom adapter's getView:
// Alternate row colors.
if (position % 2 == 0) {
v.setBackgroundResource(R.drawable.selector_list1);
} else {
v.setBackgroundResource(R.drawable.selector_list2);
}
Here is the XML declaration for selector_list1, selector_list2 is identical except for the for the first drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:state_pressed="false" android:drawable="#color/white" />
<item android:state_pressed="true" android:drawable="#color/lightgreen_listpressed" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/lightgreen_listpressed" />
</selector>
State_pressed works, but I tried using state_activated, state_selected, state_enabled, state_checked and nothing works out to change the color of the selected list item. Am I doing something wrong?
EDIT 1: If I simply throw my selectors out of the window (IE not use them), selection coloring works with only the android:listSelector="#48ad82" directive I have in the ListView declaration. Why doesn't this work with the selectors?
EDIT 2: Tried all the different states again, including state_focused when removing the listSelector directive entirely. Still no results.
EDIT 3: I suppose the issue is that the ListView itself allows the selection, the RelativeLayout view never really gets "selected" persay, thus setting the selector on it is useless. If that's the case, where and how am I supposed to set the selectors to have them do what I want them to do?
EDIT 4: I tried setting colors on the adapter based on this tutorial: http://eureka.ykyuen.info/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/ No results, I still don't get any selection color unless I call android:drawSelectorOnTop="true" which hides all the elements in the ListView element.
EDIT 5: I'm going to drop this for now and just remove the alternating colors altogether. None of the explanations I have found on StackOverflow or Google talk about this issue. The only way to make this "work" is to call android:drawSelectorOnTop="true", which does change the color of the element currently selected in the ListView, however, it hides everything in that one row.

Categories

Resources