Tuesday, 9 June 2015

User Interface with XML

Linear Layout: 

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. android:orientation is used to align direction. All element are stacked one after the other. A LinearLayout respects margins between children and the gravity (right, center, or left alignment) of each child.

Example: create a new Project


activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                              android:layout_width="match_parent"     
 android:layout_height="match_parent"   
 android:paddingLeft="16dp"   
 android:paddingRight="16dp"  
 android:orientation="vertical" >    
<EditText         
 android:layout_width="match_parent"    
 android:layout_height="wrap_content"   
 android:hint="To" />     
<EditText      
  android:layout_width="match_parent"        
  android:layout_height="0dp"  
  android:layout_weight="1"      
  android:gravity="top"         
  android:hint="SMS Body" />  
<Button      
  android:layout_width="100dp"    
  android:layout_height="wrap_content"    
  android:layout_gravity="right"       
  android:text="Send" />
</LinearLayout>

EditText: an EditText is used to get inputs from the user.

android:hint="To"
is used to assign hints to TditText

MainActivity.java

package com.linearlayout.edittext.button;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {
EditText ed_to, ed_body;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed_to = (EditText) findViewById(R.id.ed_to);
        ed_body = (EditText) findViewById(R.id.ed_body);
        Button sendbtn = (Button) findViewById(R.id.send);


        sendbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String to= ed_to.getText().toString();
                String body= ed_body.getText().toString();
                Toast.makeText(getApplicationContext(), "YOU Typed: "+to+ " AND "+ body, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Listener On Button

View.OnClickListener: Interface definition for a callback to be invoked when a view is clicked more about Listner.

Result Of the above example




No comments:

Post a Comment