Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Create array of strings in xml file Android example


AD MOB

Example source code for Android developers how create array of strings in *.xml file.
You can using array of strings in Spinner ( combobox - dropdown list ) or ListView etc.

<resources>
    <string-array name="colors">
        <item>red</item>
        <item>orange</item>
        <item>yellow</item>
        <item>green</item>
        <item>blue</item>
    </string-array>
</resources>

397LW NO topic_id




AD

Další témata ....(Topics)


133

Draw Rect Android basic example | draw-rect-android-basic-example


drawRect(), Paint, setStroke(), setStyle(), Canvas example source code.

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}

		@Override
		protected void onDraw(Canvas canvas) {

			canvas.drawColor(Color.CYAN);
			Paint p = new Paint();
			// smooths
			p.setAntiAlias(true);
			p.setColor(Color.RED);
			p.setStyle(Paint.Style.STROKE); 
			p.setStrokeWidth(4.5f);
			// opacity
			//p.setAlpha(0x80); //
//drawRect (float left, float top, float right, float bottom, Paint paint) 
			canvas.drawRect(10, 10, 30, 30, p);
		}

	}
}
390

Android runtime change switch Activity theme | android-runtime-change-switch-activity-theme



static boolean mbThemeLight = false;

 @Override
    public void onCreate(Bundle savedInstanceState) {
         if(mbThemeLight)
        setTheme(android.R.style.Theme_Light);
        super.onCreate(savedInstanceState);
        // bla bla bla..........
    }

private void switchTheme(){
 mbThemeLight = true;
 this.recreate();
}
58

HorizontalScrollView ScrollView horizontal vertical Android xml example | horizontalscrollview-scrollview-horizontal-vertical-android-xml-example


HorizontalScrollView ScrollView LinearLayout horizontal vertical Android xml basic example and image.

[caption id="attachment_889" align="alignleft" width="200" caption="horizontal scrollview horizontalscrollview android"]horizontalscrollview-android[/caption]


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
	android:layout_width="fill_parent" android:layout_height="fill_parent"
	xmlns:android="//schemas.android.com/apk/res/android">

	// horizontal and vertical Scrollview
	<ScrollView android:id="@+id/ScrollView01"
		android:layout_height="135px" android:layout_width="wrap_content"
		android:scrollbars="horizontal|vertical">

		// HorizontalScrollview
		<HorizontalScrollView android:id="@+id/HorizontalScrollView01"
			android:layout_height="fill_parent" android:layout_width="wrap_content">

			<LinearLayout android:id="@+id/LinearLayout02"
				android:layout_width="wrap_content" android:orientation="vertical"
				android:layout_height="fill_parent">
				<LinearLayout android:orientation="horizontal"
					android:layout_width="fill_parent" android:layout_height="fill_parent"
					android:layout_weight="1">
					<TextView android:text="red" android:gravity="center_horizontal"
						android:background="#aa0000" android:layout_width="200px"
						android:layout_height="fill_parent" android:layout_weight="1" />
					<TextView android:text="green" android:gravity="center_horizontal"
						android:background="#00aa00" android:layout_width="200px"
						android:layout_height="fill_parent" android:layout_weight="1" />
					<TextView android:text="blue" android:gravity="center_horizontal"
						android:background="#0000aa" android:layout_width="200px"
						android:layout_height="fill_parent" android:layout_weight="1" />
					<TextView android:text="yellow" android:gravity="center_horizontal"
						android:background="#aaaa00" android:layout_width="200px"
						android:layout_height="fill_parent" android:layout_weight="1" />
				</LinearLayout>

				<LinearLayout android:orientation="vertical"
					android:layout_width="fill_parent" android:layout_height="fill_parent"
					android:layout_weight="1">
					<TextView android:text="row one" android:textSize="15pt"
						android:layout_width="fill_parent" android:layout_height="wrap_content"
						android:layout_weight="1" />
					<TextView android:text="row two" android:textSize="15pt"
						android:layout_width="fill_parent" android:layout_height="wrap_content"
						android:layout_weight="1" />
					<TextView android:text="row three" android:textSize="15pt"
						android:layout_width="fill_parent" android:layout_height="wrap_content"
						android:layout_weight="1" />
					<TextView android:text="row four" android:textSize="15pt"
						android:layout_width="fill_parent" android:layout_height="wrap_content"
						android:layout_weight="1" />
				</LinearLayout>


			</LinearLayout>
		</HorizontalScrollView>
	</ScrollView>
</LinearLayout>



Other sample


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello world! First TextView" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        </LinearLayout>



    </HorizontalScrollView>

</LinearLayout>

129

Bitmap set contrast and brightness Android example | bitmap-set-contrast-and-brightness-android



////www.apache.org/licenses/LICENSE-2.0
        private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private ColorMatrix mCM = new ColorMatrix();
        private Bitmap mBitmap;
        private float mSaturation;
        private float mAngle;
//..........
mBitmap = BitmapFactory.decodeResource(context.getResources(),
                                                   R.drawable.my_image);
//..........
        private static void setContrastScaleOnly(ColorMatrix cm, float contrast) {
            float scale = contrast + 1.f;
               float translate = (-.5f * scale + .5f) * 255.f;
            cm.set(new float[] {
                   scale, 0, 0, 0, 0,
                   0, scale, 0, 0, 0,
                   0, 0, scale, 0, 0,
                   0, 0, 0, 1, 0 });
        }
       private static void setContrast(ColorMatrix cm, float contrast) {
            float scale = contrast + 1.f;
               float translate = (-.5f * scale + .5f) * 255.f;
            cm.set(new float[] {
                   scale, 0, 0, 0, translate,
                   0, scale, 0, 0, translate,
                   0, 0, scale, 0, translate,
                   0, 0, 0, 1, 0 });
        }
        
        private static void setContrastTranslateOnly(ColorMatrix cm, float contrast) {
            float scale = contrast + 1.f;
               float translate = (-.5f * scale + .5f) * 255.f;
            cm.set(new float[] {
                   1, 0, 0, 0, translate,
                   0, 1, 0, 0, translate,
                   0, 0, 1, 0, translate,
                   0, 0, 0, 1, 0 });
        }

       @Override protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;
            float x = 20;
            float y = 20;
            
            canvas.drawColor(Color.WHITE);
            
            paint.setColorFilter(null);
            canvas.drawBitmap(mBitmap, x, y, paint);
            
            ColorMatrix cm = new ColorMatrix();
            
            mAngle += 2;
            if (mAngle > 180) {
                mAngle = 0;
            }
            
            //convert our animated angle [-180...180] to a contrast value of [-1..1]
            float contrast = mAngle / 180.f;
            
            setContrast(cm, contrast);
            paint.setColorFilter(new ColorMatrixColorFilter(cm));
            canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);
            
            setContrastScaleOnly(cm, contrast);
            paint.setColorFilter(new ColorMatrixColorFilter(cm));
            canvas.drawBitmap(mBitmap, x, y + mBitmap.getHeight() + 10, paint);
            
            setContrastTranslateOnly(cm, contrast);
            paint.setColorFilter(new ColorMatrixColorFilter(cm));
            canvas.drawBitmap(mBitmap, x, y + 2*(mBitmap.getHeight() + 10),
                              paint);
            
            invalidate();
        }
    }

283

Get Set Screen Orientation Android Code | get-set-screen-orientation-android-code



// in AndroidManifest.xml
<Activity
 android:screenOrientation="portrait" // or landscape

// in MyActivity.java
boolean mbOrientacionLandscape = false;
int nOrientation =	getResources().getConfiguration().orientation;
if(mbOrientacionLandscape==true){
	 getResources().getConfiguration();
	if(nOrientation != Configuration.ORIENTATION_LANDSCAPE)
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
	
}
else{
	if(nOrientation != Configuration.ORIENTATION_PORTRAIT)
	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}



Editace: 2013-12-09 10:55:08
Počet článků v kategorii: 397
Url:create-array-of-strings-in-xml-file-android-example

AD
11 Share
Share
Tweet