Skip to main content

Posts

Showing posts with the label layout

Simple Calculator in Android

This post explains how you can create a simple calculator in android. You can download the code and use. See xml class: <?xml version="1.0" encoding="utf-8"?> </LinearLayout > See Java class: public class Calculator extends Activity implements OnClickListener { EditText etNum1; EditText etNum2; Button btnAdd; Button btnSub; Button btnMult; Button btnDiv; TextView tvResult; String oper = ""; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calculator); // find the elements etNum1 = (EditText) findViewById(R.id.etNum1); etNum2 = (EditText) findViewById(R.id.etNum2); btnAdd = (Button) findViewById(R.id.btnAdd); btnSub = (Button) findViewById(R.id.btnSub); btnMult = (Button) findViewById(R.id.btnM...

Delete A row in ListView with Animation

Listview is simply a group of view that displays a list of scrolling items. "Adapters" are used to insert list items to the list.By default we are able to provide only text as list items.In order to delete a row from listview, copy or download this javacode and try yourself. Also see here : Android – Bullets in ListView ListView in Alphabetic Order in ANDROID Single Selection ListView in android Simple ListView in Android java file : public class SimpleListViewActivity extends Activity { private ListView mainListView; private ArrayAdapter<String> listAdapter; ArrayList<String> all_rowtext = new ArrayList<String>() ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Find the ListView resource. mainListView = (ListView) findViewById(R.id.mainListView); all_rowtext.add("Angel"); all_rowtext.add("Mark"); all_rowtext.add("Coding...

Customized Spinners in Android

See more basics on Android along with interview questions The Android Spinner Control is definitely the equivalent of the drop-down selector . By making use of spinner control you basically obtain the capability to choose from a list without taking up all of the display screen space of a ListView. Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object. You should usually do so in your xml layout with a  <spinner>   element. For example: <Spinner     android:id="@+id/myspinner"     android:layout_width="fill_parent"     android:layout_height="wrap_content" /> Also See....Simple Spinners In Android Here we are going to deal with Customized Spinners.Her...

Using external font in ANDROID

TextView text =(TextView) findViewById(R.id.textview); Typeface face; face = Typeface.createFromAsset(getAssets(), "AMAZB.TTF"); text.setTypeface(face); text.setTextSize(30); text.setTextColor(Color.WHITE);

Expandable ListView in ANDROID

java.class public class Expandablelistview extends ExpandableListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.expandablelistview); SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter( this, createGroupList(), R.layout.group_row, new String[] { "Item" }, new int[] { R.id.row_name }, createChildList(), R.layout.child_row, new String[] {"Sub Item"}, new int[] { R.id.grp_child}); setListAdapter( expListAdapter ); } // hash map for row private List createGroupList() { ArrayList result = new ArrayList(); for (int i = 0; i groupPosition ...

Android – Bullets in ListView

main.xml : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp"> <ListView android:id="@+id/listView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:listSelector="@android:color/transparent" android:divider="@null"/> </LinearLayout> create a bullet.xml in drawable folder: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:height="6dp" android:width="6dp"/> ...

How to write interfaces in java or Android?

what is an Interface? An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements. Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class. MainActivity.java public class MainActivity extends Activity implements Interface { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void testFunctionOne() { System.out.println("Print from testFunctionOne in the Interface"); } @Override public void testFunctionTwo() { Syst...

Using DialogFragments in Android

MainActivity.java : public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FragmentManager fm = getSupportFragmentManager(); dialogfragment testDialog = new dialogfragment(); testDialog.setRetainInstance(true); testDialog.show(fm, "fragment_name"); }} Dialogfragment.java : public class Dialogfragment extends DialogFragment { public void TestDialog() { // Empty constructor required for DialogFragment } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog, container); return view; } } dialog.xml : <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/edit_name" ...

Fragmentation In Android

See more basics on Android along with interview questions Fragments are a portion of an Activity: Load a fragment Activity to call a fragment : public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } activity_main : <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <fragment android:id="@+id/titles" android:layout_width="0px" android:layout_height="match_parent" android:layout_weight="1" class="com.example.googlefragment.TitlesFragment"/> //calls the fragment </LinearLayout> TitlesFragment : ...

Spinners in Android

The Android Spinner Control is definitely the equivalent of the drop-down selector . By making use of spinner control you basically obtain the capability to choose from a list without taking up all of the display screen space of a ListView. Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. You can add a spinner to your layout with the Spinner object. You should usually do so in your xml layout with a  <spinner>   element. For example: <Spinner             android:id="@+id/myspinner"            android:layout_width="fill_parent"         android:layout_height="wrap_content" /> Also See....Custom Spinners In Android Her...

Blinking TextView - Two Methods

METHOD 1 private void blink(){ final Handler handler = new Handler(); new Thread(new Runnable() { public void run() { int timeToBlink = 600; //in milissegunds try { Thread.sleep(timeToBlink); }catch (Exception e) { }handler.post(new Runnable() { public void run() { TextView txt = (TextView)findViewById(R.id.usage); if(txt.getVisibility() == View.VISIBLE) { txt.setVisibility(View.INVISIBLE); }else { txt.setVisibility(View.VISIBLE); } blink(); } }); } }).start(); } METHOD 2 T...

Customize The Title Bar in Android

xml for custom title bar: < ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/titlebar> < TextView style="@style/MyRedTextAppearance" android:padding="7dp" android:id="@+id/txtvew_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#FFFFFF" android:textSize="20dp" android:textStyle="bold" android:gravity="center"/> < /LinearLayout> main.xml: Include the above xml to the main xml as given below... < RelativeLayout xmlns:android="http://schemas.android.com/a...

How to use RadioButtonGroup in Android?

in xml page: < ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > < TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Which company do you like the most?" android:textSize="25dp" android:textStyle="bold" /> < RadioGroup android:id="@+id/menu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checkedButton="@+id/lunch" android:orientation="vertical" > < RadioButton android:id="@+id/google" ...

Option Menu in Android

menu.xml : < ?xml version="1.0" encoding="utf-8"?> < menu xmlns:android="http://schemas.android.com/apk/res/android"> < item android:id="@+id/item1" android:title="Option 1" android:icon="@android:drawable/ic_menu_compass"/> < item android:id="@+id/item2" android:title="Option 2" android:icon="@android:drawable/ic_menu_week"/> < item android:id="@+id/item3" android:title="Option 3" android:icon="@android:drawable/ic_menu_call"/> < item android:id="@+id/item4" android:title="Option 4" android:icon="@android:drawable/ic_menu_zoom"/> < /menu> Click on the menu button to open this menu in java class : public class OptionMenuActivity extends Ac...

Custom AlertBox in Android

in main.xml file: < ?xml version="1.0" encoding="utf-8"?> < 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/button1" android:layout_width="120dp" android:layout_height="wrap_content" android:text="Button" /> < /LinearLayout> in dialog.xml :(xml for custom dialog box) : < ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > < ImageView android:id="@+id/imageView1" android:layout_widt...

Frame by Frame Animation in Android

Call a new xml file which includes the frames that are to be loaded: < ?xml version="1.0" encoding="utf-8"?> < animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> < item android:drawable="@drawable/frame_above95_1" android:duration="30"/> < item android:drawable="@drawable/frame_above95_2" android:duration="30"/> < item android:drawable="@drawable/frame_above95_3" android:duration="30"/> < item android:drawable="@drawable/frame_above95_4" android:duration="30"/> < item android:drawable="@drawable/frame_above95_5" android:duration="30"/> < item android:drawable="@drawable/frame_above95_6" android:duration="30"/> < item android:drawable="@drawable/frame_above95_7" android:duration="...