I'm trying to change background of ImageView when i click on it , i learned that it could be done by creating XML file and make a selector so i did that but when i execute i see the background has already changed that's my codes :
change.XML file in drawable folder :
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true"> <color android:color="#00FF00"/> </item> </selector> content_main.xml in Layout folder :
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.othma.test2.MainActivity" tools:showIn="@layout/activity_main"> <ImageView android:layout_width="200dp" android:layout_height="200dp" android:id="@+id/imageView" android:src="@drawable/str" android:maxWidth="@dimen/abc_button_padding_horizontal_material" android:contentDescription="@string/imgstart" android:layout_centerHorizontal="true" android:layout_marginTop="82dp" android:background="@drawable/change" /> </RelativeLayout> Java Code :
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { //imgV = (ImageView) findViewById(R.id.imageView); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
No comments:
Post a Comment