Clickable image on ListView - java

I want to have a ListView that have an image in every item (star) and that image is clickabe. In other word user can click on star of every row of ListView and I want to define click action for that. How I can do this?

Add an imageView.. make it clickable by adding this to your ImageView tag:
android:clickable="true"
android:focusable = "false"

you can make a ImageButton like
<ImageButton
android:id="#+id/sound_button"
android:layout_x="430px"
android:layout_y="219px"
android:layout_width="48px "
android:layout_height="48px"
android:scaleType="center"
android:src="#android:drawable/volumeicon"
android:background="#drawable/clearbuttonup"
/>
and make a new xml and name it selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
OR you make a Imageview and setproperty of that ImageView isClickable="true"

You can use an ImageButton,
http://developer.android.com/reference/android/widget/ImageButton.html
You can also simply make the image clickable, and set it's onClickListener(),
http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)
The problem with this however is that there will be no visual feedback to the user as the image is clicked. An image button looks and acts like a button, and if you are willing to provide normal, pressed, etc versions of the image, you can attach a state list drawable to the image button to make it look very professional.
http://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html

Related

how to do pop up button animation in android studio

In a calculator app that I am creating, I want buttons to animate in a pop-up way (scaling-up) when the button is clicked. But that is not the issue. I want the button to stay scaled up (bigger size) as long as the user hold the button. It is supposed to go back to its original size (small size) only when the user releases the button. In the same way button colour has to change from white to blue as long as user hold the button(back to white when released). Is there any way to do that? I am not very good at animations in android studio. So please try to explain it in simple words.
I am also attaching a video that I made for prototyping to illustrate it.
Click link below
https://drive.google.com/file/d/13B8LAuNW1ymhmliw52BEIkMbJAJQRbfw/view?usp=sharing
You should create an xml file that have a selector as root element. This element selects the shape in base of the corrent state like pressed, focused or enabled.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/button_pressed" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/button_enabled" />
</selector>
If you want a different shape with different color and size you must create a new drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient
android:startColor="#00CCFF"
android:centerColor="#0000CC"
android:endColor="#00CCFF"
android:angle="90"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<stroke
android:width="2dip"
android:color="#FFFFFF" />
<corners android:radius= "8dp" />
</shape>
If you want to change the size you can use the attribute size.
Then you must implement the on click listener in the mainActivity onCreate file and set the pressed state of the button to true.
In a nutshell you must create one selector drawable for the button and two drawable one for the state pressed="true" and one for pressed="false".
Here I put an exaustive example: https://stackoverflow.com/a/29848987/13198061

How to gray a seekbar when disabled?

I want to make grey my seekbar when I disable it.
I know I can programmatically make this changes by changing color but I'd like to know if it is possible to do this directly in my xml activity layout.
In fact in my code I'd like to just write this:
mySeekBarNuance.setEnabled(true);
And automatically my seekbar becomes grey.
Thanks
Do it like this
In your styles.xml
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> // or the themw you are using
...
...
<item name="android:seekBarStyle">#style/MySeekBarStyle</item>
</style>
Add also a new style
<style name="MySeekBarStyle" parent="android:Widget.SeekBar">
<item name="android:colorControlNormal">#color/gray</item> // put here the color for disable state
</style>
controlColorNormal is used for disabled state and colorAccent for enabled state
You can use a selector as background for the Seekbar
A selector set what you want if condition are met (here : android:state_enabled="true")
In your activity/fragment layout you put the seekbar with a background that is a selector :
<SeekBar
android:id="#+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/selector_seekbar"/>
You make the selector in your drawable folder :
selector_seekbar.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#android:color/darker_gray" />
<item android:state_enabled="true"
android:drawable="#android:color/holo_green_dark" />
</selector>
If enabled, the seekbar will be holo green, otherwise it will be gray
You can do the same thing if the seekbar is focused, pressed etc thaht way.
And you can not only set the color but an entire drawable instead

Set button style programmatically

So I'm programmatically creating new buttons and adding them to a LinearLayout, however I want to initialize these buttons with a predefined style. I've spent some time searching for a solution and trying out answers, but I still can't seem to get it to work.
When I add a new button to the layout, it should look like the buttons (near the top) in this picture.
I've tried creating an xml file in res/values/ and initializing a button with new Button(context, null, R.style.ChoiceButton), but it doesn't work resulting in this happening.
I've also tried the workaround of creating a new layout xml for the button and using (Button)getLayoutInflater().inflate(R.layout.choice_buttton_layout, null), but that also didn't work, resulting in this (two buttons to show lack of margin).
res/values/choice_button.xml
<resources>
<style name="ChoiceButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_marginBottom">7dp</item>
<item name="android:minWidth">250dp</item>
<item name="android:background">#ff27ae60</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:enabled">true</item>
</style>
</resources>
Snippet from Main.java
public void btnAdd_click(View view) {
Button newBtn = new Button(getApplicationContext(), null, R.style.ChoiceButton);
newBtn.setText("new button");
newBtn.setId(Util.generateViewId());
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutTop);
layout.addView(newBtn);
}
activity_main.xml
A bit long to paste in here.
Is there just something I'm missing? Is this even possible?
Ok #kin3tik, I found an old application I made with some custom button.. see what it looks like :
there is my xml for one button :
<Button
android:id="#+id/num1"
android:layout_width="110dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:text="#string/num1"
android:textSize="20sp" />
I created file .xml in my drawable folder custombutton.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/bleuperso"/> // you can put #XXXXXX for the color you want
<corners android:radius="4dp"/>
And I just put the style in java :
Bfrancois.setBackgroundResource(R.drawable.custombutton);
With this you should be able to find yourself ;)

Android - Disable ListView Selection Highlight but Keep OnClick enabled [duplicate]

This question already has answers here:
Android: disabling highlight on listView click
(15 answers)
Closed 6 years ago.
I want to disable the highlight that appears when the user selects a row (listSelector) from code. I don't want to disable the onClick and enabled settings (I still want to listen to clicks, just want to remove the highlight).
Specify android:listSelector="#android:color/transparent" in your ListView XML.
Just create a drawable that has a transparent color in it, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent"/>
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/transparent" />
<item android:state_focused="true" android:drawable="#drawable/list_focused_holo" />
</selector>
And then set by code or by XML:
listView.setSelector(R.drawable.my_transparent_selector);
The javadoc for this method says:
Set a Drawable that should be used to highlight the currently selected item.
and the XML attribute is:
android:listSelector
You can play with all the states, remember that you also have the focus state.
I have done this way:
By adding two properties of ListView.
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent"
Your ListView should looks like below:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent">
</ListView>
Done
try listview.setSelector(new ColorDrawable(Color.TRANSPARENT));
The highlight effect is a style on the listSelector. You can override the listSelector style.
This is a example with a listview : Android: disabling highlight on listView click

Android: Specify two different images for togglebutton using XML

I'm attempting to override the default ToggleButton appearance. Here's the XML that defines the ToggleButton:
<ToggleButton android:id="#+id/FollowAndCenterButton"
android:layout_width="30px"
android:layout_height="30px"
android:textOn="" android:textOff="" android:layout_alignParentLeft="true"
android:layout_marginLeft="5px"
android:layout_marginTop="5px" android:background="#drawable/locate_me"/>
Now, we have two 30 x 30 icons we want to use for the clicked/non-clicked states. Right now we have code that programmatically changes the background icon depending on the state:
centeredOnLocation.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (centeredOnLocation.isChecked()) {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me_on));
} else {
centeredOnLocation.setBackgroundDrawable(getResources().getDrawable(R.drawable.locate_me));
}
}
});
Obviously I'm looking for a better way to do this. I've tried to make a selector for the background image, which would automatically switch between the states:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/locate_me" /> <!-- default -->
<item android:state_checked="true"
android:drawable="#drawable/locate_me_on" /> <!-- pressed -->
<item android:state_checked="false"
android:drawable="#drawable/locate_me" /> <!-- unchecked -->
But this does not work; reading the ToggleButton API (http://developer.android.com/reference/android/widget/ToggleButton.html), it appears that the only inherited xml attributes are
XML Attributes
Attribute Name Related Method Description
android:disabledAlpha The alpha to apply to the indicator when disabled.
android:textOff The text for the button when it is not checked.
android:textOn The text for the button when it is checked.
There does not seem to be the android:state_checked attribute, despite the class having the method isChecked() and setChecked().
So, is there a way to do what I want in XML, or am I stuck with my messy workaround?
Your code is fine. However, the toggle button will display the first item in your selector that it matches, so the default should come last. Arrange the items in the following manner to ensure they will all be utilized:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" /> //currently pressed turning the toggle on
<item android:state_pressed="true" /> //currently pressed turning the toggle off
<item android:state_checked="true" /> //not pressed default checked state
<item /> //default non-pressed non-checked
</selector>

Categories