Android studio tutorial this time to continue the previous article to learn graphic user interface part two, where on the one yesterday we have discussed objects such as TextView, EditText, Button. In this article the objects that I will discuss include the RadioButton, CheckBox, ImageButton, and ListView. Where the four objects that will be discussed later are the components that are often used by developers to create applications based on mobile android. From it I hope you can understand the basics of GUI programming are very well as a provision to create a more complete application. In any discussion of the object will be accompanied with java and xml file as the layout.
RadioButton
RadioButton objects in android works as the choices are in one group, where only one option can be activated only in the group. More details you can directly use the editor android practice studio by creating a new project and given the name RadioButton, where the object is to have a java file named RadioButtonActivity.java, and a layout named activity_radio_button.xml.
package com.dsoccerupdate.radiobutton; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import android.widget.RadioGroup.OnCheckedChangeListener; public class RadioButtonActivity extends Activity { RadioButton pil1; RadioButton pil2; RadioButton pil3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio_button); RadioGroup pilihangroup = (RadioGroup) findViewById(R.id.grouppilihan); pil1 = (RadioButton) findViewById(R.id.pilihan1); pil2 = (RadioButton) findViewById(R.id.pilihan2); pil3 = (RadioButton) findViewById(R.id.pilihan3); pilihangroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { if (pil1.isChecked()) { Toast.makeText(getBaseContext(), "Pilihan 1 Checked!", Toast.LENGTH_LONG).show(); } else { if (pil2.isChecked()) { Toast.makeText(getBaseContext(), "Pilihan 2 Checked!", Toast.LENGTH_LONG).show(); } else { Toast.makeText(getBaseContext(), "Pilihan 3 Checked!", Toast.LENGTH_LONG).show(); } } } }); } }Source code activity_radio_button.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <RadioGroup android:id="@+id/grouppilihan" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> <RadioButton android:id="@+id/pilihan1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:text="Pilihan 1"/> <RadioButton android:id="@+id/pilihan2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:text="Pilihan 2"/> <RadioButton android:id="@+id/pilihan3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:text="Pilihan 3" android:checked="true"/> </RadioGroup> </LinearLayout>CheckboxAn android checkbox object in the form of a menu of options similar to the RadioButton, butfrom there find selected option or all options at once. For example, create a new project and name CheckBox, then add one java file with the name CheckBoxActivity.java and a layout with activity_check_box.xml name.Source code CheckBoxActivity.javapackage com.dsoccerupdate.checkbox; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.CheckBox; import android.widget.Toast; public class CheckBoxActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_check_box); CheckBox checkBox = (CheckBox) findViewById(R.id.checkboxcontoh); checkBox.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((CheckBox)v).isChecked()) Toast.makeText(getBaseContext(), "CheckBox di check", Toast.LENGTH_LONG).show(); else Toast.makeText(getBaseContext(), "Check tidak di cek", Toast.LENGTH_LONG).show(); } }); CheckBox checkstar = (CheckBox) findViewById(R.id.checkstylebintang); checkstar.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (((CheckBox)v).isChecked()) Toast.makeText(getBaseContext(), "Checkstar di cek", Toast.LENGTH_LONG).show(); else Toast.makeText(getBaseContext(), "Checkstar tidak di cek", Toast.LENGTH_LONG).show(); } }); } }Source code activity_check_box.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <CheckBox android:id="@+id/checkboxcontoh" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="CheckBoxContoh"/><CheckBox android:id="@+id/checkstylebintang" android:layout_width="fill_parent" android:layout_height="wrap_content" style="?android:attr/starStyle"/> </LinearLayout>Image Button Image button is an object on android in the form of button that can contain pictures which layout can be adjusted in the program or xml layout. Other objects that can be used to be an image button are Image Button. For more details you create a new project with the name Button in the project image that there is a file named ButtonBergambarActivity.java and a layout with activity_button_bergambar.xml name.Source code ButtonBergambarActivity.java
package com.dsoccerupdate.imagebutton; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.ImageButton; import android.widget.Toast; public class ButtonBergambarActivity extends Activity {ImageButton btnimage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_button_bergambar); Button btn = (Button) findViewById(R.id.imageTextButton6); btn.setText("Android"); btn.setCompoundDrawablesWithIntrinsicBounds( 0, R.drawable.ic_launcher, 0, 0); btnimage = (ImageButton) findViewById(R.id.imageButton); btnimage.setOnClickListener(imagebuttonListener); } private View.OnClickListener imagebuttonListener = new View.OnClickListener() { public void onClick(View view) { if (view.getId()==R.id.imageButton) { Toast.makeText(getBaseContext(), "Image Button di Click", Toast.LENGTH_SHORT).show(); } } }; }Source code activity_button_bergambar.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <Button android:id="@+id/imageTextButton6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/> <Button android:id="@+id/imageTextButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_launcher" android:drawableBottom="@drawable/ic_launcher" android:drawableLeft="@drawable/ic_launcher" android:drawableRight="@drawable/ic_launcher" android:text="Android"/> <ImageButton android:id="@+id/imageButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/> <Button android:id="@+id/imageTextButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableTop="@drawable/ic_launcher" android:text="Android"/> <Button android:id="@+id/imageTextButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:text="Android"/> <Button android:id="@+id/imageTextButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableRight="@drawable/ic_launcher" android:text="Android"/> <Button android:id="@+id/imageTextButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableBottom="@drawable/ic_launcher" android:text="Android"/> </LinearLayout>ListView ListView is the object a list or a list containing items option. For example, you create a new project with the name ListView in which there is a java file with the name ListViewActivity.java and a layout with activity_list_view.xml name. Source code ListViewActivity.javapackage com.dsoccerupdate.listview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class ListViewActivity extends Activity { ListView listbuah; String[] buahbuahan= { "Belimbing", "Jambu", "Mangga", "Rambutan", "Sirsak" }; @Override public void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, buahbuahan); listbuah =(ListView) findViewById(R.id.ListView1); listbuah.setAdapter(adapter); listbuah.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { int index = arg2; Toast.makeText(getBaseContext(), "List yang di pilih: " + buahbuahan[index], Toast.LENGTH_SHORT).show(); } }); } }Source code activity_list_view.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/ListView1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </RelativeLayout>Thus android tutorial to learn Graphic User Interface part two I can explain, actually there are many components contained in the android studio that can be used to create or develop applications based on Android. So this article merely as a basis for continued where you can learn from various existing sources. Stay tuned to the latest updates about the android programming by using android studio because there will always be the latest articles.
0 comments:
Post a Comment