In this blog explain how to set image left,right ,bottom,top in TextView in android programmatic.
first, you have create an activity inside src folder..
scond ,you have create onCreate() method inside MainActivity.java class.
show below.
setCompoundDrawablesWithIntrinsicBounds (int left, int top, int right, int bottom)
MainActvity .java
..............................................................................................................................................
package com.androidbeginner.testcode;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout lm = (LinearLayout) findViewById(R.id.layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
// set image left in textview
TextView text=new TextView(this);
text.setText("textview");
text.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dwon_arrow, 0, 0,0);
text.setLayoutParams(params);
text.setBackgroundColor(Color.RED);
lm.addView(text);
// set image top in textview
TextView text1=new TextView(this);
text1.setText("textview");
text1.setLayoutParams(params);
text1.setBackgroundColor(Color.RED);
text1.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.dwon_arrow,0,0);
lm.addView(text1);
// set image right in textview
TextView text2=new TextView(this);
text2.setText("textview");
text2.setLayoutParams(params);
text2.setBackgroundColor(Color.RED);
text2.setCompoundDrawablesWithIntrinsicBounds( 0, 0,R.drawable.dwon_arrow,0);
lm.addView(text2);
// set image bottomt in textview
TextView text3=new TextView(this);
text3.setText("textview");
text3.setLayoutParams(params);
text3.setBackgroundColor(Color.RED);
text3.setCompoundDrawablesWithIntrinsicBounds( 0, 0,0,R.drawable.dwon_arrow );
lm.addView(text3);
}
}
0 comments