简体   繁体   中英

border-width and border color using java code in android?

I want to set border-width and border-color in edit text using Java code in android, I am not using any xml file for design, please help i am new bee in android development

this is activity class

public class MainActivity extends ActionBarActivity implements OnCheckedChangeListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Button myButton = new Button(this);
    myButton.setText("ButtonBox4");
    myButton.setId(1);
    //myButton.setBackgroundColor(Color.YELLOW);
    RelativeLayout myLayout = new RelativeLayout(this);
    Resources r4 = getResources();
    int px4 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 300, r4.getDisplayMetrics());

    myLayout.setMinimumHeight(px4);

    Resources r5 = getResources();
    int px5 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 1022, r5.getDisplayMetrics());

    myLayout.setMinimumWidth(px5);

    RelativeLayout.LayoutParams CheckParams = 
            new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);

    CheckBox checkBox = new CheckBox(this);
    checkBox.setOnCheckedChangeListener(this);
    checkBox.setId(3);
    checkBox.setText("calender6");
    CheckParams.addRule(RelativeLayout.ABOVE, myButton.getId());
    CheckParams.addRule(RelativeLayout.CENTER_VERTICAL);
    CheckParams.setMargins(656, 169, 0, 0);

    Resources r6 = getResources();
    int px6 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 40, r6.getDisplayMetrics());

    checkBox.setHeight(px6);

    Resources r7 = getResources();
    int px7 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 120, r7.getDisplayMetrics());

    checkBox.setWidth(px7);

    EditText myEditText = new EditText(this);
    myEditText.setHint("TextBox2");
    myEditText.setId(2);
    myEditText.setBackgroundColor(Color.rgb(102, 255, 255));

    RelativeLayout.LayoutParams textParams = 
            new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,   
                RelativeLayout.LayoutParams.WRAP_CONTENT);

    textParams.addRule(RelativeLayout.ABOVE, myButton.getId());
    textParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    textParams.setMargins(300, 69,0 ,0);

    Resources r = getResources();
    int px = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 200, r.getDisplayMetrics());

    myEditText.setWidth(px);

    Resources r1 = getResources();
    int px1 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 40, r1.getDisplayMetrics());

    myEditText.setHeight(px1);

    Resources r2 = getResources();
    int px2 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 200, r2.getDisplayMetrics());

    myButton.setWidth(px2);

    Resources r3 = getResources();
    int px3 = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 40, r3.getDisplayMetrics());

    myButton.setHeight(px3);

    RelativeLayout.LayoutParams buttonParams = 
            new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    buttonParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
    buttonParams.setMargins(437, 199, 0, 0);

    myLayout.addView(myButton, buttonParams);
    myLayout.addView(myEditText, textParams);
    myLayout.addView(checkBox, CheckParams);  
    setContentView(myLayout);
}

manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dynamicviews"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>


</manifest>

Use shape. For example:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#4fa5d5"/>
</shape>

Create xml with this text in drawable res and then set it to background in your TextView.

Here's your border width: android:width="1dip"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM