Auf Preferences in Android Zugreifen

Auf Preferences in Android Zugreifen
 

private TextView textView;
textView = (TextView) findViewById(R.id.tv1);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String string = sharedPreferences.getString("thename", "");
textView.setText(string);

 

Android Preferences setzen mit Menu und PreferenceActivity

Android Preferences setzen mit Menu und PreferenceActivity

 

Es müssen 3 .xml Dateien erschaffen werden: menu1.xml, array.xml und prefs1.xml

menu1.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/preferences"
android:title="Preferences"/>
<item
android:id="@+id/exit"
android:title="Exit App"/>

</menu>

 

 

array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string-array name="list">
<item> Option 1 </item>
<item> Option 2 </item>
</string-array>

<string-array name="values">
<item>1</item>
<item>2</item>
</string-array>

</resources>

 

prefs1.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<EditTextPreference
android:key="The name"
android:summary="Your name:"
android:title="Name" />

<ListPreference
android:entries="@array/list"
android:entryValues="@array/values"
android:key="List Pref1"
android:summary="The List Preference"
android:title="List" />

</PreferenceScreen>

 

 

Der Java Code:

 
PrefTestActivity.java

package de.worldling;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;

public class PrefTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

	@Override
	public boolean onCreateOptionsMenu(android.view.Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu1, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.preferences:
			Intent intent = new Intent(PrefTestActivity.this, Prefs1.class);
			startActivity(intent);
			break;
		case R.id.exit:
			finish();
			break;
		}
		return false;
	}
}

 
Prefs1.java

package de.worldling;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Prefs1 extends PreferenceActivity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.prefs1);

	}

}

Android – Parameters passing zu einer anderen Activity

Android – Parameters passing zu einer anderen Activity

Eine App in der man mit Button Click einen Parameter zu einer anderen Activity weiter recht.

 

 

 

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="40dp"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/etPar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>

<Button
android:id="@+id/bPar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" Pass the parameter" />

</LinearLayout>

 

new_lay.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="40dp">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your parameter: " />

<TextView
android:id="@+id/tvPar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00FFFF"
android:text="..." />

</LinearLayout>

 

Manifest:

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

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

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ParActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewClass"
android:label="@string/app_name" >
>
</activity>
</application>

</manifest>

 

ParActivity.java

package de.worldling;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ParActivity extends Activity implements OnClickListener{

	EditText editText;
	Button button;
	String string;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initStuff();
    }

	private void initStuff() {
		editText = (EditText) findViewById(R.id.etPar);
		button = (Button) findViewById(R.id.bPar);

		button.setOnClickListener(this);
	}

	public void onClick(View v) {
		string = editText.getText().toString();
		Bundle bundle = new Bundle();
		bundle.putString("key1", string);
		Intent intent = new Intent(ParActivity.this, NewClass.class);
		intent.putExtras(bundle);
		startActivity(intent);
	}
}

 
NewClass.java

package de.worldling;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class NewClass extends Activity{

		TextView textView;
		String string;

		@Override
		protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.new_lay);

			textView = (TextView) findViewById(R.id.tvPar);

			Bundle bundle = getIntent().getExtras();
			string = bundle.getString("key1");
			textView.setText(string);
		}

}

 

ToggleButton – Password hiding in Android

ToggleButton – Password hiding in Android

Eine App, die mit dem ToogleButton unser Password anzeigt oder es versteckt.

main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="40dp" >

<EditText
android:id="@+id/etPass"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Type the password"
android:password="true" />

<ToggleButton
android:id="@+id/tbPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checked="true"
android:paddingBottom="10dp"
android:text="ToggleButton"
android:textOff="Showing password"
android:textOn="Hiding password" />

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100" >

<TextView
android:id="@+id/tvPass"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="match_parent"
android:layout_weight="30"
android:text="..." />

<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="70"
android:text="Check" />
</LinearLayout>

</LinearLayout>

 

Unser Java Code:

package de.worldling;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import android.widget.TextView;
import android.widget.ToggleButton;

public class PassProjectActivity extends Activity implements View.OnClickListener {

	EditText editText;
	Button button;
	ToggleButton toggleButton;
	TextView textView;
	String string;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initStuff();
    }

	private void initStuff() {
		editText = (EditText) findViewById(R.id.etPass);
		button = (Button) findViewById(R.id.button1);
		toggleButton = (ToggleButton) findViewById(R.id.tbPassword);
		textView = (TextView) findViewById(R.id.tvPass);

        button.setOnClickListener(this);
        toggleButton.setOnClickListener(this);
	}

	public void onClick(View v) {

		switch (v.getId()){
		case R.id.tbPassword:
			if (toggleButton.isChecked()){
				editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			} else{
				editText.setInputType(InputType.TYPE_CLASS_TEXT);
			}
			break;

		case R.id.button1:
			string = editText.getText().toString();
			if(string.equals("pass")){
				textView.setText(string);
				textView.setTextColor(Color.GREEN);
			}else{
				textView.setText("WRONG");
				textView.setTextColor(Color.RED);
			}
			break;

		}
	}
}

Android GraphicsView – einen Gradient Background setzen und Circles Malen

Android GraphicsView – einen Gradient Background setzen und Circles Malen
 
Heute machen wir mall so etwas in Android:

 

background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<gradient
android:angle="270"
android:endColor="#000000"
android:startColor="#FFFFFF" />

</shape>

Die Activity:

package com.example;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Start1 extends Activity {

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

	}

	static public class GraphicsView extends View {
		public GraphicsView(Context context) {
			super(context);
			setBackgroundResource(R.drawable.background);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint1 = new Paint();
			Paint paint2 = new Paint();
			paint1.setColor(Color.WHITE);
			paint2.setColor(Color.BLACK);	

			Path circle = new Path();
			Path circle2 = new Path();
			Path circle3 = new Path();

			circle.addCircle(300, 300, 200, Direction.CW);
			canvas.drawPath(circle, paint1);
			circle2.addCircle(300, 300, 50, Direction.CW);
			canvas.drawPath(circle2, paint2);
			circle3.addCircle(300, 300, 10, Direction.CW);
			canvas.drawPath(circle3, paint1);
		}

	}
}