iOS AND ANDROID
Topics:
Choose a tagIn WordPress, Shortcodes are simple functions that can be used globally in the content editor. WordPress has a few default shortcodes and also provides a nice tutorial on how to make your own. By default, however, WordPress does not have a shortcode that displays the website URL. This can be a very useful code to have if you are developing on one server and transferring it to another for production.
Creating one is simple, just add the following lines to your functions.php file:
function website_url_sc() {
return get_bloginfo('url');
}
add_shortcode('url','website_url_sc');
With this shortcode, simple use [url] to display the website URL provided in your admin.
I recently ran into a small issue when trying to programmatically set a margin to a FrameLayout.
I would have expected this to have worked:
someFrameLayout = (FrameLayout) findViewById(R.id.some_frame_layout); MarginLayoutParams marginParams = new MarginLayoutParams(someFrameLayout.getLayoutParams()); marginParams.setMargins(newLeftMargin, newTopMargin, newRightMargin, newBottomMargin); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginParams); someFrameLayout.setLayoutParams(layoutParams);
But for some reason, nothing happened. I did a little research online and I ended up finding the (really odd) fix. I’m hoping that this is a bug and not intentional, but apparently there needs to be some form of gravity set to be able to set a margin in a FrameLayout.
You’re going to need to add one more line of code to the previous example to get it working:
someFrameLayout = (FrameLayout) findViewById(R.id.some_frame_layout); MarginLayoutParams marginParams = new MarginLayoutParams(someFrameLayout.getLayoutParams()); marginParams.setMargins(newLeftMargin, newTopMargin, newRightMargin, newBottomMargin); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginParams); layoutParams.gravity = Gravity.NO_GRAVITY; //This is the new line someFrameLayout.setLayoutParams(layoutParams);
I’ve found the best results when I’m applying the gravity in the direction that the margin is coming from, but usually NO_GRAVITY will work just fine.
It looks like they also fixed this issue for newer devices. I’m not sure which version it was fixed as I only tested it on a 4.2 device (which didn’t need this fix), and a 2.3.6 device (which needed this fix).
When I code a website, I like to use as little linked files as possible to reduce the amount of calls made to the server. With pretty much every website, browser-specific CSS styles are needed to adjust for the differences between the way, for example, Internet Explorer and Firefox may output code. Rather than using multiple CSS files, I prefer to use CSS hacks within the same file. There are two ways you can specify these hacks – through selectors or through attributes in a shared selector.
/* IE6 and below */
* html #uno { color: red }
/* IE7 */
*:first-child+html #dos { color: red }
/* IE7, FirefoxF, Saf, Opera */
html>body #tres { color: red }
/* IE8, FirefoxF, Saf, Opera (Everything but IE 6,7) */
html>/**/body #cuatro { color: red }
/* Opera 9.27 and below, Safari 2 */
html:first-child #cinco { color: red }
/* Safari 2-3 */
html[xmlns*=""] body:last-child #seis { color: red }
/* Safari 3+, Chrome 1+, Opera9+, FirefoxF3.5+ */
body:nth-of-type(1) #siete { color: red }
body:first-of-type #ocho { color: red }
/* Safari 3+, Chrome 1+ */
@media screen and (-webkit-min-device-pixel-ratio:0) {
#diez { color: red }
}
/* iPhone / mobile webkit */
@media screen and (max-device-width: 480px) {
#veintiseis { color: red }
}
/* Safari 2 - 3.1 */
html[xmlns*=""]:root #trece { color: red }
/* Safari 2 - 3.1, Opera 9.25 */
*|html[xmlns*=""] #catorce { color: red }
/* Everything but IE6-8 */
:root *> #quince { color: red }
/* IE7 */
*+html #dieciocho { color: red }
/* Firefox only */
#veinticuatro, x:-moz-any-link { color: red }
#veinticinco, x:-moz-any-link, x:default { color: red }
These are my most-used hacks since Internet Explorer tends o read things much more differently than other browsers.
/* IE6 only */
#once { _color: blue }
/* IE6, IE7 */
#doce { *color: blue; /* or #color: blue */ }
/* Everything but IE6 */
#diecisiete { color/**/: blue }
/* IE6, IE7, IE8 */
#diecinueve { color: blue\9; }
/* IE7, IE8 */
#veinte { color/*\**/: blue\9; }
/* IE6, IE7 -- use this instead of !important */
#veintesiete { color: blue !ie; }
For a more in depth look at these hacks, check out BrowserHacks.com.
I recently ran into a problem with UI scaling issues in one of our projects running on an HTC One device. We were alerted to this problem by one of our users through feedback. The code that handled scaling was pretty old and did not contain a case to scale for devices with an XXHigh screen density. Because of this, certain UI components were scaled to that of a normal screen of Medium density by default.
To determine if this was in fact the cause of the problem, I first tried to replicate the user’s experience in the android emulator. I created a new Device Definition and Android Virtual Device to match the HTC One Specs and launched the app, but I was unable to replicate the user’s problem. After some digging through code, I discovered that the emulator was returning DisplayMetrics.DENSITY_XHIGH when accessing the device’s Display Metrics from code instead of DisplayMetrics.DENSITY_XXHIGH. Figuring this was a problem with the android emulator, Brandon found a post on StackOverflow which explained the problem as well as a simple solution to force the emulator to return DisplayMetrics.DENSITY_XXHIGH: Any suggestions on getting xxhdpi working in the android emulator?
After following the instructions outlined in the post, the emulator returned the screen density correctly allowing me to replicate and eventually solve the user’s problem.
Solution from StackOverflow:
In the Android SDK Tools version 21.1, the xxhdpi screen resolution for the emulator display is not yet supported. However, you could do a manual override of the LCD properties of the emulator. This will ensure that the correct resources are being dispatched for the Xperia™ Z emulator. To do so, please execute the following in your command line after the Xperia™ Z emulator has completed the boot sequence: adb shell setprop qemu.sf.lcd_density 480 adb shell stop adb shell start The emulator will then restart. After it has rebooted the emulator should use the correct screen density and UI scaling. We recommend you to execute these commends using a batch file or a shell script if you are using these settings often.
This site has been in my toolbox for quite a while now and still comes in handy when I can’t automate the compression of JS files. You can either copy and paste the JS or upload the whole file and get back the minified JS. It’s amazing to be able to reduce a file’s size by 70% to 80% and still have it work as intended.
If you need reasons for minifying a JS file, here you go:
Every activity in android already has a method you can override to support gesture detection. This method is the onTouchEvent method. The onTouchEvent method is called by Android when a user touches the screen. The event passes a MotionEvent object, which tells us everything we need to know about the touch.
I will use an example that will detect a left and right fling.
private static int MINIMUM_FLING_DIST = 100;
private int iX, iY; //Position when pressed
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
iX = event.getX();
iY = event.getY();
break;
case MotionEvent.ACTION_MOVE:
float dX = event.getX();
float dY = event.getY();
//===== Fling Right
if((dX-iX) > MINIMUM_FLING_DIST){
//Right fling
}
//===== Fling left
else if((dX-iX) < -MINIMUM_FLING_DIST){
//Left fling
}
break;
}
}
Here we’re really just seeing if the user dragged a certain distance before we determine the fling. If you have a more advanced gesture, it would also be accomplished by this method, it will just require a bit more thought.
By default, WordPress orders custom posts by date. This is not very desirable if you are not using them as normal blog posts and want to order the admin page by title, menu order, etc. You can change the order by adding a few lines of code to your theme’s functions.php file:
function set_custom_post_types_admin_order($wp_query) {
if (is_admin()) {
$post_type = $wp_query->query['post_type'];
if ( $post_type == 'name_of_post_type') {
$wp_query->set('orderby', 'menu_order');
$wp_query->set('order', 'ASC');
}
}
}
add_filter('pre_get_posts', 'set_custom_post_types_admin_order');
Just change name_of_post_type to the one you want to reorder.
Here are some parameters that you can order your posts by:
‘ID’ – Order by post id.
‘author’ – Order by author.
‘title’ – Order by title.
‘name’ – Order by post name (slug).
‘date’ – Order by date.
‘modified’ – Order by date modified.
‘parent’ – Order by parent id.
‘menu_order’ – Order by page order specified in edit page.
Wanted to share one of the websites I end up using a lot due to my inability to remember every htaccess command from memory. here’s a list of the commands documented there:
Detect and redirect iPhone
How to create a password for a .htpasswd file using PHP
How to find the full path to a file using PHP
Htaccess redirect
htpasswd – The file to store passwords
Prevent hotlinking
Redirection
Password Protection with htaccess
Hope you find it as useful as I do!
HTML5 gave us the long-awaited feature of adding watermark text to form input and textarea fields. If you haven’t tried it yet, here is the simple code:
<input type="text" placeholder="Your Name">
The placeholder attribute will work in the latest versions of Firefox, Safari, Chrome and Internet Explorer 10. For Internet Explorer 9 and below, there is a simple jQuery fix that also utilizes the placeholder attribute so you re not dealing with duplicate content. Huge props to Niko Hagenburger for this simple solution.
Add this code to your javascript file:
$("[placeholder]").focus(function() {
var input = $(this);
var watermark = input.attr("placeholder")
if (input.val() == watermark) {
input.val("");
input.removeClass("placeholder");
}
}).blur(function() {
var input = $(this);
if (input.val() == "" || input.val() == watermark) {
input.addClass("placeholder");
input.val(input.attr("placeholder"));
}
}).blur();
$("[placeholder]").parents("form").submit(function() {
$(this).find("[placeholder]").each(function() {
var input = $(this);
if (input.val() == input.attr("placeholder")) {
input.val("");
}
})
});
This code selects all elements that have the placeholder attribute and adds it to the value attribute. The second part of the script strips the value out before submit if it directly matches the placeholder value.
Note that you can use a submit or click function for the last part of of this code – the example shows submit as in Nico’s original, but I used click so that my jQuery validator would fire off.
Check out the original post from Nico to find out more about this script and how to style it.
If you’ve ran into this article, you’re most likely dealing with a JSON-RPC API. To begin with, JSON-RPC is a standardized protocol that’s encoded in JSON. If you’ve never dealt with JSON before, you’re going to have to read up on that first.
For each JSON-RPC call, there is both a request and a response. A request has a method name, an array of parameters, and an ID. A response has a result, an error, and an ID (to match the request’s id).
Here’s a class that will make it easy to create requests.
public class JSONRPC_Request {
private String mMethod, mID;
private Object[] mParams;
private JSONObject mPostJSON;
private static final String METHOD = "method";
private static final String PARAMS = "params";
private static final String ID = "id";
/**
* This class is to act as a structure for an outgoing JSON_RPC call.
* @param method This is the method from the API to use.
* @param params The ordered params for the API call.
* @param ID The ID of the call.
*/
public JSONRPC_Request(String method, Object[] params, String ID){
mMethod = method;
mParams = params;
mID = ID;
mPostJSON = createJSON();
}
/**
* @return Returns a JSONObject that represents the JSONRPC_Post object.
*/
public JSONObject createJSON(){
JSONObject postJSON = new JSONObject();
try {
//Method
postJSON.put(METHOD, mMethod);
//Params
JSONArray params = new JSONArray();
for(Object param : mParams){
params.put(param);
}
postJSON.put(PARAMS, params);
//ID
postJSON.put(ID, mID);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return postJSON;
}
/**
* This will act as a serialization for this object.
* This simply prints out the JSONObject's toString.
*/
@Override
public String toString(){
return mPostJSON.toString();
}
}
When the request needs to be sent to the server, just call the toString method and send the result.
When you receive the response from the server (which should be a string), stuff that string into this class’s constructor. If you need to get the specific JSON data for either the result, the error, or the ID, call the appropriate method.
public class JSONRPC_Response {
Object mResult;
private JSONObject mError;
private String mID;
private static final String RESULT = "result";
private static final String ERROR = "error";
private static final String ID = "id";
/**
* Creates a JSONObject from the String, then calls
* the other constructor.
*/
public JSONRPC_Response(String response) throws JSONException{
this(new JSONObject(response));
}
/**
* Constructor: Breaks up values from response for easy access.
*/
public JSONRPC_Response(JSONObject response){
//===== Result
try {
mResult = response.get("result");
} catch (JSONException e) {
mResult = null;
}
//===== Error
try {
mError = response.getJSONObject("error");
} catch (JSONException e) {
mError = null;
}
//===== ID
try {
mID = response.getString("id");
} catch (JSONException e) {
mID = null;
}
}
//================ Getters ================
/**
* Returns the result from the JSON_RPC call
*/
public Object getResult(){
return mResult;
}
/**
* Returns the error; null if no error.
*/
public JSONObject getError(){
return mError;
}
/**
* Returns the ID of the JSON_RPC call
*/
public String getID(){
return mID;
}
}