Random Android Snippets

July 15, 2015 Android

Contents:

  • Making TextView links clickable
  • Getting String object from string resource
  • Setting state of SwipeRefreshLayout
  • Setting Gson date format
  • Getting a formatted today’s date
  • Displaying home as up button in action bar
  • Accessing Activity from Fragment
  • Converting List<String> to comma-separated string
  • Converting HTML to text
  • Specifying whether to use URL encoding in Retrofit
  • Fix RecyclerView showing no items

This can be achieved by setting its movement method to LinkMovementMethod.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import sample.R;

public class MainFragment extends Fragment {
    private TextView message;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        message = (TextView) view.findViewById(R.id.message);
        message.setMovementMethod(LinkMovementMethod.getInstance());
        return view;
    }
}

2. Getting String object from string resource

import android.content.Context;

String description = context.getResources().getString(R.string.description);

3. Setting state of SwipeRefreshLayout

boolean loading = true;
swipeRefreshLayout.post(new Runnable() {
    @Override
    public void run() {
        swipeRefreshLayout.setRefreshing(loading);
    }
});

4. Setting Gson date format

Any ISO-8601 compilant format can be used.

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

final Gson gson = new GsonBuilder()
    .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    .create();

5. Getting a formatted today’s date

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

String today = new SimpleDateFormat("yyyy-MM-dd", Locale.US).format(new Date());

6. Displaying home as up button in action bar

public class MainActivity extends AppCompatActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

7. Accessing Activity from Fragment

Override android.support.v4.app.Fragment.onAttach(Activity activity) method.

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.activity = activity;
}

8. Converting List<String> to comma-separated string

import android.text.TextUtils;

String message = TextUtils.join(", ", words);

9. Converting HTML to text

import android.text.Html;

String message = Html.fromHtml(messageHtml);

10. Specifying whether to use URL encoding in Retrofit

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import retrofit.Callback;
import retrofit.http.GET;
import retrofit.http.Path;

public interface ProfileApi {
    @GET("/{user}/profile")
    void getProfile(
            @Path(value = "user", encode = false) String user,
            Callback<UserProfile> response
    );
}

11. Fix RecyclerView showing no items

Probably you’ve set its item’s layout_height to match_parent.

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerViewItem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

You should set it to wrap_content to prevent the item from occuping the whole screen height.

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recyclerViewItem"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
comments powered by Disqus