How do I make an Android CheckBoxPreference unclickable? - java

Here's my code:
<PreferenceCategory
android:summary="Fade information"
android:title="Fade Effects">
<CheckBoxPreference
android:title="Fade In/Out"
android:defaultValue="false"
android:key="fadeIn"/>
<CheckBoxPreference
android:title="Heartbeat"
android:defaultValue="false"
android:key="heartbeat" />
<CheckBoxPreference
android:title="Pulse"
android:defaultValue="false"
android:key="pulse" />
<CheckBoxPreference
android:title="None"
android:defaultValue="true"
android:key="none" />
</PreferenceCategory>
I'm basically trying to figure out how to make those CheckBoxes appear as they are, but having them unclickable by the user.

Using android:enabled="false" is incorrect as this actually completely disables a View (it also greys it out which is the main issue). What you want to do instead is:
android:clickable="false"
This simply stops the user clicking the View but doesn't officially 'disable' it. I think that's more what you are looking for.

Use android:enabled="false" from your XML, or setEnabled() in Java code.

You can use android:selectable="false" in xml if you don't wish to gray out the CheckboxPreference.

Related

How to make a text have a "Message Style Apperance"

So, basically I want a Text on android appear in this "message style" or like a speakbubble.
btw that was the first picture to pop up from the old whatsapp style xD.
Does somebody know hot to do this?
Assuming you want something similar to the right side, here is a solution:
First create a new drawable xml
speech_bubble.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#90ee90" />
<corners android:radius="5dp"/>
</shape>
Then in your activity xml you can use it like so:
activity_main.xml
<TextView
android:padding="5dp"
android:background="#drawable/speech_bubble"
android:text="This is a speech bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
You put a background drawable behind the text view, with a bit of padding on all 4 sides (so there's an offset between the text and the start of the image). Preferably a 9-patch so you can stretch the speech bubble.

Android button background color changes button size

I am using a built-in theme for my Android app:
<style name="AppTheme" parent="android:Theme.Black">
<!-- Customize your theme here. -->
</style>
I am happy with that theme, except I want to change the background color of a button. Here is how it looks by default:
Here's what happens when I add a background color to this button (android:background="#color/play_bg"):
Hey!? It basically changed all the button's size, padding and margins!
So I managed to get the expected result using the backgroundTint property (android:backgroundTint="#color/play_bg"):
Unfortunately, this is only supported since version 21 of the API, which is not acceptable for me.
So two questions:
Why does changing the background messes with the rest of the button's properties?
How do I get the expected result without backgroundTint?
And a bonus question: How can I get the expected result programmatically (I have dynamic buttons in my app, so this would be very useful)?
You can change this color in your Java File. When your main class loads you can take an object of this button and then change color.
Here is how you define this button in Manifest file :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="#+id/btn1"
... />
Now in your Java file when you are adding this XML layout you need to
Button b = (Button)findViewByID(R.id.btn1);
b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);
You may also use COLOR:
COLOR.RED
The code below sometimes does not work for me :-
b.setBackgroundColor(int color)
In my case I will be doing in this process
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#color/play_as"
android:padding="8dp"
android:text="Button" />
Or you can use this link which is more easy way of creating the buttons

How do I use this custom drawable class?

Im fairly new to android programming so im not sure on this.
Im trying to set a png as a background but the image is stretching when i use android:background="#drawable/bkgrnd" in my layout xml file
I found this custom class here http://www.anddev.org/viewtopic.php?p=27178#27178 but im not sure how to use it in my code.
Ive copied the code into its own class file and no errors are present.
How do I set the background to the drawable above using this class?
NB: I have abandoned this approach and gone with something much simpler. The answers below did give clues along with some other stackoverflow questions. Answer is below.
Please try this one it will help you.
android:src="#drawable/bkgrnd"
The result can be achieved using scale drawables instead
bkgrnd.xml (drawable xml file)
<?xml version="1.0" encoding="utf-8"?>`
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/bkgrnd"
android:scaleGravity="clip_horizontal"
android:scaleHeight="100%"
android:scaleWidth="100%"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
layout.xml
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bkgrnd" />
In your XML, declare an ImageView like this
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#drawable/bkgrnd"
android:scaleType="fitXY"
android:layout_alignParentRight="true"
/>

Use activity as a dialog

I want to use an activity as a dialog in my android application. Till now, I've used AlertDialogs but I want to make my app more fancy. I've done a lot of research on this but couldn't find a satisfactory answer.
I created a new activity named DialogActivity.java and dialogactivty for the java and xml files respectively.
So, How do I call this activity as a dialog in some other activity?
AND
How do I give this activity a round cornered rectangle dialog shape?
Thanks in advance!
You can set a dialog theme to activity as
<activity android:theme="#android:style/Theme.Dialog" />
and use
<activity android:theme="#android:style/Theme.Holo.Dialog" /> // api level 11 and above
in manifest for the activity required.
You need to start the activity using startActivity(intent)
From the official documentation,
If you want a custom dialog, you can instead display an Activity as a
dialog instead of using the Dialog APIs. Simply create an activity and
set its theme to Theme.Holo.Dialog in the manifest element:
<activity android:theme="#android:style/Theme.Holo.Dialog" >
That's it. The activity now displays in a dialog window instead of
fullscreen.
Use Theme.Dialog for API 10 or lower.
try this :
android:theme="#android:style/Theme.Dialog">
entry in manifest for the activity you required.
#ChinmayDabke To display with rounded corners, you need to create a new shape using xml and set it as the background drawable.. be sure to also set transparency for your activity. For more detailed information on this, there are many up-to-date topics available - I just wanted to give a quick answer to this old post, since you had asked!
My mistake - you don't need to set as Transparent - I meant to say set no Titlebar!
Example:
Create a Shape for the rounded background, inside your res/drawable folder:
shape_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#color/colorOfActivityHere"/>
<corners
android:radius="12dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
Then set the shape as the Background of your activity's parent layout:
layout_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="#drawable/shape_background"
android:orientation="vertical">
...
</LinearLayout>
And in your Activity.java, set no titlebar:
Activity.java
requestWindowFeature(Window.FEATURE_NO_TITLE);

Java move <Preference> from one PreferenceScreen to another?

I'm trying to do something similar to iOS editable UITableView. I have a list of "added items" if the user clicks on "Add new item" he is taken to a PreferenceScreen with multiple <Preference> elements, onclick the should disappear from this screen and add it to the "Added list" (actually a <PreferenceCategory>), then they should be able to remove them from here or edit their order. This is similar to iOS editable lists.
This is my XML:
<PreferenceScreen android:title="ALL APPS" android:key="app_list">
<!-- A <preference> for each app -->
</PreferenceScreen>
<PreferenceCategory android:title="ADDED APPS" android:key="my_apps_list">
</PreferenceCategory>
I have tried uncountable times and I cannot get it to work.
How can I get it done?
I've experimented around a bit and it's actually very easy when you nest your PreferenceScreens.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference android:title="Check it!" />
<PreferenceScreen android:title="To the other preferences!" android:summary="I really hope this works <3" >
<EditTextPreference android:title="Edit your name" android:summary="Edit your name :)" />
<CheckBoxPreference android:title="Accept answer" />
</PreferenceScreen>
</PreferenceScreen>
PS: Please excuse my hard-coded string literals - it was just for testing! :)
this looks like a very similar question to what i've asked in the past:
android - showing listView in PreferenceActivity
when selecting the items to show , store them in some way (DB ,references ,etc...) and then use the solution i've found .
as you can see , the view used in preference activity is actually a listView , so it's weird to put a listView in a listView . what you need is a special trick , and that's something i've written about .
Please have a look to the accepted answer on this question:
How to remove Android preferences from the screen
Sometimes, if categories are present, you have to
"reference the category containing the preference you wanted to remove and then remove that preference from that category"
like the answer there says. Isn't this also your case?
Hope this helped.

Categories