How to build a horizontal ListView with RecyclerView

Is there a better way to implement this now with RecyclerView now? Yes. When you use a RecyclerView, you need to specify a LayoutManager that is responsible for laying out each item in the view. The LinearLayoutManager allows you to specify an orientation, just like a normal LinearLayout would. To create a horizontal list with … Read more

Navigation Drawer (Google+ vs. YouTube)

Edit #3: The Navigation Drawer pattern is officially described in the Android documentation! Check out the following links: Design docs can be found here. Developer docs can be found here. Edit #2: Roman Nurik (an Android design engineer at Google) has confirmed that the recommended behavior is to not move the Action Bar when opening … Read more

Android toolbar center title and custom font

To use a custom title in your Toolbar all you need to do is remember is that Toolbar is just a fancy ViewGroup so you can add a custom title like so: <android.support.v7.widget.Toolbar android:id=”@+id/toolbar_top” android:layout_height=”wrap_content” android:layout_width=”match_parent” android:minHeight=”?android:attr/actionBarSize” android:background=”@color/action_bar_bkgnd” app:theme=”@style/ToolBarTheme” > <TextView android:layout_width=”wrap_content” android:layout_height=”wrap_content” android:text=”Toolbar Title” android:layout_gravity=”center” android:id=”@+id/toolbar_title” /> </android.support.v7.widget.Toolbar> This means that you can style … Read more

android:drawableLeft margin and/or padding

As cephus mentioned android:drawablePadding will only force padding between the text and the drawable if the button is small enough. When laying out larger buttons you can use android:drawablePadding in conjunction with android:paddingLeft and android:paddingRight to force the text and drawable inward towards the center of the button. By adjusting the left and right padding … Read more

Percentage width in a RelativeLayout

You are looking for the android:layout_weight attribute. It will allow you to use percentages to define your layout. In the following example, the left button uses 70% of the space, and the right button 30%. <LinearLayout android:layout_width=”match_parent” android:layout_height=”wrap_content” android:orientation=”horizontal”> <Button android:text=”left” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_weight=”.70″ /> <Button android:text=”right” android:layout_width=”0dp” android:layout_height=”wrap_content” android:layout_weight=”.30″ /> </LinearLayout> It works the … Read more

How do I get the currently displayed fragment?

When you add the fragment in your transaction you should use a tag. fragTrans.replace(android.R.id.content, myFragment, “MY_FRAGMENT”); …and later if you want to check if the fragment is visible: MyFragment myFragment = (MyFragment)getSupportFragmentManager().findFragmentByTag(“MY_FRAGMENT”); if (myFragment != null && myFragment.isVisible()) { // add your code here } See also http://developer.android.com/reference/android/app/Fragment.html

How to programmatically set drawableLeft on Android button?

You can use the setCompoundDrawables method to do this. See the example here. I used this without using the setBounds and it worked. You can try either way. UPDATE: Copying the code here incase the link goes down Drawable img = getContext().getResources().getDrawable(R.drawable.smiley); img.setBounds(0, 0, 60, 60); txtVw.setCompoundDrawables(img, null, null, null); or Drawable img = getContext().getResources().getDrawable(R.drawable.smiley); … Read more

View’s getWidth() and getHeight() returns 0

The basic problem is, that you have to wait for the drawing phase for the actual measurements (especially with dynamic values like wrap_content or match_parent), but usually this phase hasn’t been finished up to onResume(). So you need a workaround for waiting for this phase. There a are different possible solutions to this: 1. Listen … Read more

Why fragments, and when to use fragments instead of activities?

#1 & #2 what are the purposes of using a fragment & what are the advantages and disadvantages of using fragments compared to using activities/views/layouts? Fragments are Android’s solution to creating reusable user interfaces. You can achieve some of the same things using activities and layouts (for example by using includes). However; fragments are wired … Read more