Skip to content

Instantly share code, notes, and snippets.

@dnkm
Last active August 29, 2015 14:18
Show Gist options
  • Save dnkm/70bbadf12bf46090f3e9 to your computer and use it in GitHub Desktop.
Save dnkm/70bbadf12bf46090f3e9 to your computer and use it in GitHub Desktop.
Android Tutorial Chapter 1 - Getting Started
protected void onCreate(...) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setTextSize(40);
tv.setText("blah");
setContentView(tv); // adds tv as the root view of the activity's layout
}
<application ...>
...
<activity
android:name="com...ExampleActivity"
android:label="@string/title_activity_example"
androidparentActivityName="com...ParentActivity">
<!-- for 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com...ParentActivity" />
</activity>
</application>
/*
* Using id
*/
EditText txt = (EditText) findViewById(R.id.edit_message);
/*
* Starting another activity
*/
public final static String EXTRA_MESSAGE = "com.mycompany.firstapp.MESSAGE";
Intent intent = new Intent(this, RecipientClass.class);
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
/*
* Receiving the Intent (RecipientClass)
*/
protected void onCreate(...) {
Intent intent = getIntent();
String message = intent.getStringExtra(SenderActivity.EXTRA_MESSAGE);
}
/*
* Sample Activity
*/
public Class SampleActivity extends ActionBarActivity {
// key methods to override
// - onCreate() : declare essential properties to retain through pause-stop-resume lifecycle.
// - onCreateView() : return View (or null in case there's no UI)
// - onPause() : commit all changes
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlacehoderFragment()).commit();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handles action bar item clicks.
// Home/Up buttons are automatically handled so long as you specify a parent activity in AndroidManifest.xml
int id = item.getItemId();
if (id == R.id.action_settings) { return true; }
return super.onOptionsItemSelected(item);
}
// placeholder fragment
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_example, container, false);
return rootView;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment