set background drawable programmatically android
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#d3d3d3"
tools:context=".MainActivity">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
</RelativeLayout>
MainActivity.java
package com.android.kotlin.changedrawablecolor;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.content.res.AppCompatResources;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
RelativeLayout layout =(RelativeLayout)findViewById(R.id.layout);
//You can use the AppCompatResources.getDrawable() method and set background programmatically of layout using android VectorDrawable or image
//AppCompatResources worked with both images and vectors for almost all android APIs
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable(AppCompatResources.getDrawable(context, R.drawable.ic_png_image));
} else {
layout.setBackground(AppCompatResources.getDrawable(context, R.drawable.ic_png_image));
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#d3d3d3"
tools:context=".MainActivity">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_margin="20dp"
android:orientation="vertical"
tools:context=".MainActivity">
</RelativeLayout>
</RelativeLayout>
MainActivity.java
package com.android.kotlin.changedrawablecolor;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.content.res.AppCompatResources;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
RelativeLayout layout =(RelativeLayout)findViewById(R.id.layout);
//You can use the AppCompatResources.getDrawable() method and set background programmatically of layout using android VectorDrawable or image
//AppCompatResources worked with both images and vectors for almost all android APIs
final int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
layout.setBackgroundDrawable(AppCompatResources.getDrawable(context, R.drawable.ic_png_image));
} else {
layout.setBackground(AppCompatResources.getDrawable(context, R.drawable.ic_png_image));
}
}
}
No comments:
Post a Comment