Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DSC WORKSHOP “ANDROID FOR BEGINNER #2”

DSC WORKSHOP “ANDROID FOR BEGINNER #2”

Zainal Hasan

July 29, 2018
Tweet

More Decks by Zainal Hasan

Other Decks in Programming

Transcript

  1. Views vs ViewGroup Views : • TextView • ImageView •

    Button • EditText • RadioButton • Checkbox • Switch • Rating Bar • etc ViewGroup : • RelativeLayout • LinearLayout • ConstraintLayout • TableLayout • FrameLayout • etc
  2. ViewGroup Example <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/title_text_view1" android:layout_width="wrap_content" android:layout_height="wrap_content"

    android:text="Hello DSC" android:textColor="#4689C8" android:textStyle="bold" android:padding="16dp"/> <TextView android:id="@+id/title_text_view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Lagi DSC" android:textColor="#4689C8" android:textStyle="bold" android:padding="16dp"/> </LinearLayout> Hello Lagi DSC Hello DSC
  3. ViewGroup Example (2) <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/title_text_view1" android:layout_width="wrap_content"

    android:layout_height="wrap_content" android:text="Hello DSC" android:textColor="#4689C8" android:textStyle="bold" android:padding="16dp"/> <TextView android:id="@+id/title_text_view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Lagi DSC" android:textColor="#4689C8" android:textStyle="bold" android:padding="16dp"/> </LinearLayout> Hello DSC Hello Lagi DSC
  4. onClickListener (Style 1) public class MainActivity extends AppCompatActivity { private

    Button tombol; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tombol = (Button) findViewById(R.id.button); tombol.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "Hello DSC", Toast.LENGTH_SHORT).show(); } }); } }
  5. onClickListener (Style 2) public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button tombol; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tombol = (Button) findViewById(R.id.button); tombol.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button : Toast.makeText(this, "Hello DSC", Toast.LENGTH_SHORT).show(); break; } } }
  6. Intent Activity 1 Activity 2 Intent intent = new Intent(Activity1.this,

    Activity2.class); startActivity(intent); startActivityForResult(intent, 123);
  7. Intent With Data // Activity 1 String nama = "Ahok";

    intent.putExtra("data_nama", nama); startActivity(intent); Activity 1 (i have data for you) Activity 2 (where???) // Activity 2 Intent intent = getIntent(); String nama = intent.getStringExtra("data_nama");
  8. Intent With Data (2) Bundle // Activity 1 String nama1

    = "Ahok"; String nama2 = "Djarot"; Bundle bundle = new Bundle(); bundle.putString("data_nama_1", nama1); bundle.putString("data_nama_2", nama2); intent.putExtras(bundle); startActivity(intent); // Activity 2 Intent intent = getIntent(); Bundle bundle = intent.getExtras(); String nama1 = bundle.getString("data_nama_1"); String nama2 = bundle.getString("data_nama_2");