User Interface - Android
User Interface - Android
using Fragments
• Create Android UI
• Working with Layout
• Create Custom Layouts
• Work with UI Components and Events
• Material Design Toolbar
• Tab Layout, Recycler View and Card View
• Android Menus
android:layout_x
This specifies the x-
coordinate of the view.
android:layout_y
This specifies the y-
coordinate of the view.
tabLayout=(TabLayout)findViewById(R.id.tabLayout);
viewPager=(ViewPager)findViewById(R.id.viewPager);
tabLayout.addTab(tabLayout.newTab().setText("Home"));
tabLayout.addTab(tabLayout.newTab().setText("Sport"));
tabLayout.addTab(tabLayout.newTab().setText("Movie"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
} Prof. Urmi Desai, SCET CO
public class Pager extends FragmentPagerAdapter {
public static Toast makeText(Context makes the toast containing text and
context, CharSequence text, int duration.
duration)
public void show() displays toast.
public void setMargin (float changes the horizontal and vertical
horizontalMargin, float verticalMargin) margin difference.
Constant Description
public static final int displays view for the long duration
LENGTH_LONG of time.
public static final int displays view for the short
LENGTH_SHORT duration of time.
Toast.makeText(getApplicationContext(),"Hello World",Toast.LENGTH_SHORT).show();
Another code:
Toast toast=Toast.makeText(getApplicationContext(),"Hello World",Toast.LENGTH_SHORT);
toast.setMargin(50,50);
toast.show();
Prof. Urmi Desai, SCET CO
Handling View Events
• For Button-
• Button btnOpen = (Button) findViewById(R.id.btnOpen);
• btnOpen.setOnClickListener(new View.OnClickListener() {
• public void onClick(View v) {
• DisplayToast(“You have clicked the Open button”);
• } });
• For Checkbox-
• CheckBox checkBox = (CheckBox) findViewById(R.id.chkAutosave);
• checkBox.setOnClickListener(new View.OnClickListener()
• {
• public void onClick(View v) {
• if (((CheckBox)v).isChecked())
• DisplayToast(“CheckBox is checked”);
• else
• DisplayToast(“CheckBox is unchecked”); } CO
Prof. Urmi Desai, SCET });
Handling View Events
• For radiobutton
• RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
• radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
• {
• public void onCheckedChanged(RadioGroup group, int checkedId) {
• RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1);
• if (rb1.isChecked()) {
• DisplayToast(“Option 1 checked!”);
• } else {
• DisplayToast(“Option 2 checked!”); } } });
• For togglebutton
• ToggleButton toggleButton =
• (ToggleButton) findViewById(R.id.toggle1);
• toggleButton.setOnClickListener(new View.OnClickListener()
• {
• public void onClick(View v) {
• if (((ToggleButton)v).isChecked())
• DisplayToast(“Toggle button is On”);
• else
• Prof. Urmi
DisplayToast(“Toggle button is Off”); } Desai, SCET});
CO
•
• @Override
• protected void onCreate(Bundle savedInstanceState) {
• super.onCreate(savedInstanceState);
• setContentView(R.layout.activity_main);
• Button button = findViewById(R.id.button);
• EditText editText = findViewById(R.id.editText);
• button.setOnClickListener(new View.OnClickListener() {
• @Override
• public void onClick(View view) {
• String s=editText.getText().toString();
• Toast.makeText(getApplicationContext(),s,Toast.
LENGTH_SHORT).show();
• }
• });
• } Prof. Urmi Desai, SCET CO
• There is another way to handle view events. Using the
Button as an
• example, you can add an attribute called onClick to it:
• <Button android:id=”@+id/btnSave”
• android:layout_width=”fill_parent”
• android:layout_height=”wrap_content”
• android:text=”save”
• android:onClick=”btnSaved_clicked”/>
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View
view, int position, long l) {
// TODO Auto-generated method stub
String value=adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHO
RT).show();
Prof. Urmi Desai, SCET CO
}
Strings.xml
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
Prof. Urmi Desai, SCET CO
List View
(this,android.R.layout.select_dialog_item,language);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoComplet
eTextView);
actv.setThreshold(1);//will start working from first
character
actv.setAdapter(adapter1);
Prof. Urmi Desai, SCET CO
AutoCompleteTextView