I want to use a 9-patch drawable to use as the background of layout which I set to be clickable
the drawable I use is actually a selector xml which I wrote like that:
<?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/action_item_pressed" />
<item android:drawable="@drawable/action_item" />
</selector>
and my layout is like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:padding="8dp"
android:background="@drawable/action_item_btn">
<ImageView
android:id="@+id/icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/sword_item"
android:scaleType="centerInside"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="35dp"
android:layout_height="5dp"
android:layout_marginTop="2dp"
style="@android:style/Widget.ProgressBar.Horizontal"/>
</LinearLayout>
the problem is that the action_item drawable is not showing and action_item_pressed drawable is showing when pressing on the view.
any thoughts?
does the way I inflate this view, can cause this?
BTW: in the eclipse graphical layout, it is showing and if I add the view when the application is running, it works.
here’s the way I inflate this view (it calls from the onCreate method):
int achievementsNumber = AchievementsManager.ACTIVE_ACHIEVEMENTS_COUNT;
m_achievementsLayout = new LinearLayout(m_context.getApplicationContext());
m_achievementsLayout.setOrientation(LinearLayout.VERTICAL);
m_achievementsLayout.setGravity(Gravity.CENTER);
LayoutInflater inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for(int i=0; i < achievementsNumber; i++){
inflater.inflate(R.layout.achievement_item, m_achievementsLayout, true);
}
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
addView(m_achievementsLayout, params);
well it seems that inflating this view and adding it to the layout on the
onCreateof the activity is a problem and caused this issue.I moved the inflating and added the view after the onResume and it works.
I guess the reason is that the
ViewGroupI’m adding the inflated view to, doesn’t have a size yet.