Create Button and TextView dynamically - Android sample
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
TextView textView = new TextView(this);
textView.setText("Text View ");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
layout.addView(textView, p);
Button buttonView = new Button(this);
buttonView.setText("Button");
buttonView.setOnClickListener(mThisButtonListener);
layout.addView(buttonView, p);
}
private OnClickListener mThisButtonListener = new OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Hello !",
Toast.LENGTH_LONG).show();
}
};
}
396LW NO topic_id
AD
Další témata ....(Topics)
Kvalitní mobilní telefon pro rok 2012 by měl mít tyto, nebo ještě lepší parametry.
Značka Samsung , HTC, LG, Motorola, Sony Ericsson, ZTE Blade
Verze operačního systému Android 2.2 a vyšší.
Procesor a jeho frekvence Qualcomm 800MHz a vyšší.
Paměť RAM 512MB a více.
Paměť ROM 512MB.
Displej a jeho rozlišení 4,3 palce rozlišení 480x800 pixelů pro snadné ovládání a čtení a schopnost zobrazit 16 milionů barev.
Technologie 3G data, WiFi, BlueTooth, GPS přijímač, fotoaparát 3 Mpix, autofokus, diodový blesk.
Výdrž baterie minimálně 1 den a více.
3G internet.
Značka Samsung , HTC, LG, Motorola, Sony Ericsson, ZTE Blade
Verze operačního systému Android 2.2 a vyšší.
Procesor a jeho frekvence Qualcomm 800MHz a vyšší.
Paměť RAM 512MB a více.
Paměť ROM 512MB.
Displej a jeho rozlišení 4,3 palce rozlišení 480x800 pixelů pro snadné ovládání a čtení a schopnost zobrazit 16 milionů barev.
Technologie 3G data, WiFi, BlueTooth, GPS přijímač, fotoaparát 3 Mpix, autofokus, diodový blesk.
Výdrž baterie minimálně 1 den a více.
3G internet.
Adding TableRow with a View TextView (EditText Button) programmically dynamically to TableLayout Android sample basic example.
MainClass.java
main.xml
MainClass.java
public class MainClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout tl = (TableLayout)findViewById(R.id.tableLayout1);
TableRow row = new TableRow(this);
TextView tv = new TextView(this);
tv.setText("This is text");
tl.addView(row);
row.addView(tv);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableLayout android:layout_width="fill_parent"
android:id="@+id/tableLayout1" android:layout_height="wrap_content">
</TableLayout>
</LinearLayout>
public class MainActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
RadioGroup radioGroup = new RadioGroup(this);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
layout.addView(radioGroup, p);
RadioButton radioButtonView = new RadioButton(this);
radioButtonView.setText("RadioButton");
radioButtonView.setOnClickListener(this);
radioGroup.addView(radioButtonView, p);
RadioButton radioButtonView2 = new RadioButton(this);
radioButtonView2.setText("RadioButton2");
radioButtonView2.setOnClickListener(mThisButtonListener);
radioGroup.addView(radioButtonView2, p);
}
public void onClick(View view) {
try {
String s = ((RadioButton) view).getText().toString();
Toast.makeText(MainActivity.this, "This is: " + s,
Toast.LENGTH_LONG).show();
}
catch (Exception e1) {
e1.printStackTrace();
}
}
private OnClickListener mThisButtonListener = new OnClickListener() {
public void onClick(View v) {
String s = ((RadioButton) v).getText().toString();
Toast.makeText(MainActivity.this, "Hello from 2!" + s,
Toast.LENGTH_LONG).show();
}
};
}
onSaveInstanceState, onRestoreInstanceState life cycle if screen orientation changed from log file.
Diagram of life cycle onSaveInstanceState, onRestoreInstanceState
[caption id="attachment_1169" align="alignleft" width="229" caption="Life cycle onRestoreInstanceState"][/caption]
// starts activity
15:27:12.801: INFO/onCreate(1828): onCreate()
15:27:12.811: INFO/onStart(1828): onStart()
15:27:12.821: INFO/onResume(1828): onResume()
// activity is running
15:27:33.651: DEBUG/dalvikvm(307): GC_EXPLICIT freed 186K, 53%
free 2770K/5831K, external 981K/1038K, paused 99ms
// change emulator state Ctrl+F11 landscape, portrait
15:27:40.427: INFO/ActivityManager(74): Config changed:
{ scale=1.0 imsi=310/260 loc=en_US touch=3 keys=2/1/2 nav=3/1 orien=2 layout=18 uiMode=17 seq=64}
// saved all variable values if need
15:27:40.581: INFO/onSaveInstanceState(1828): onSaveInstanceState()
15:27:40.602: INFO/onPause(1828): onPause()
15:27:40.612: INFO/onStop(1828): onStop()
15:27:40.631: INFO/onDestroy(1828): onDestroy()
// activity goes back to onCreate !!!!!!!!!
15:27:40.692: INFO/onCreate(1828): onCreate()
15:27:40.701: INFO/onStart(1828): onStart()
// restore all saved values of variables
15:27:40.711: INFO/onRestoreInstanceState(1828): onRestoreInstanceState()
// you can using saved values by onSaveInstanceState() in onResume
15:27:40.721: INFO/onResume(1828): onResume()
Diagram of life cycle onSaveInstanceState, onRestoreInstanceState
[caption id="attachment_1169" align="alignleft" width="229" caption="Life cycle onRestoreInstanceState"][/caption]
Errors:
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include screenLayout.
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include uiMode.
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include screenSize.
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include smallestScreenSize.
E/Ads(333): You must have AdActivity declared in AndroidManifest.xml with configChanges.
Solution:
Try to using a lower version of the GoogleAdMobAdsSDK ADS SDK in project:
GoogleAdMobAdsSDK-4.0.4
How add SDK to project
Add activity to AndroidManifest.xml
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include screenLayout.
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include uiMode.
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include screenSize.
E/Ads(333): The android:configChanges value of the com.google.ads.AdActivity must include smallestScreenSize.
E/Ads(333): You must have AdActivity declared in AndroidManifest.xml with configChanges.
Solution:
Try to using a lower version of the GoogleAdMobAdsSDK ADS SDK in project:
GoogleAdMobAdsSDK-4.0.4
How add SDK to project
Add activity to AndroidManifest.xml
// .............. blah
<uses-sdk android:minSdkVersion="4"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:icon="@drawable/dicts_ico" android:label="@string/app_name"
>
<meta-data
android:value="a12345_your_number"
android:name="ADMOB_PUBLISHER_ID" />
<activity android:name=".MainStartMenu"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Google ads -->
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"/>
// ......... blah
Editace: 2011-10-22 07:54:35
Počet článků v kategorii: 396
Url:create-button-dynamically-android-sample