Skip to main content

Posts

Showing posts from January, 2013

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 Here we go with java source code:
in java file:
public class simplespinnersactivity extends Activity implements OnItemSelectedLi…

Show and Resume Android Soft-Keyboard

Code to show keyboard: InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(yourEditText,InputMethodManager.SHOW_IMPLICIT);Code resume keyboard : InputMethodManager imm = (InputMethodManager)gettSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

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 TextVie…

Calling a Service from Activity in Android

This is a service class: public class serv extends Service { private static final String TAG = "MyService"; AlertDialog alertDialog @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } public void onCreate() { Toast.makeText(getApplicationContext(), "My Service Created",Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped",Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); //player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started",Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); //player.start(); } }Call service from activity: public class MainActivity extends …

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/apk/re…

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 Activity…

ContextMenu in Android

Long click on the button to open the menu.
in java class:
public class ContextmenuActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button b=(Button)findViewById(R.id.button1); registerForContextMenu(b); } //create the menuuu====================== @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater=getMenuInflater(); inflater.inflate(R.layout.contextmenu, menu); } //click to each menu====================== @Override public boolean onContextItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==R.id.item1) …

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_width=&qu…

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="…

Download an Image from Url and Display it in a ImageView

Bitmap bmImg; ImageView img1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.sending_card); img1=(ImageView) findViewById(R.id.imageView1); String url="http://i.123g.us/c/efeb_valen_happy/card/107397.gif"; downloadFile(url); } void downloadFile(String fileUrl){ URL myFileUrl =null; try { myFileUrl= new URL(fileUrl); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection(); conn.setDoInput(true); conn.connect(); InputStream is = conn.getInputStream(); bmImg = BitmapFactory.decodeStream(is); img1.setImageBitmap(…

Read from Asset and Write to a TextView

Code for reading from a text file in asset folder to a textview:


public class Readfile extends Activity { String path; ArrayList data = new ArrayList(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); path = getResources().getString(R.string.directory1); String[] data1 = null; AssetManager am = getResources().getAssets(); try { data1 = am.list(path); } catch (Exception e) { } for (String name : data1) { data.add(name); } Readoperation(); } public void Readoperation() { // TODO Auto-generated method stub TextView txt = (TextView) findViewById(R.id.text); try { path=getResources().getString(R.string.directory1) +"/"+data.get(2); InputStream stream = getAssets().open(path); int size = stream.avai…

Swipe Image Viewer in Android

See more basics on Android along with interview questions public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager); ImagePagerAdapter adapter = new ImagePagerAdapter(); viewPager.setAdapter(adapter); } private class ImagePagerAdapter extends PagerAdapter { private int[] mImages = new int[] { R.drawable.chiang_mai, R.drawable.himeji, R.drawable.petronas_twin_tower, R.drawable.ulm }; @Override public int getCount() { return mImages.length; } @Override public boolean isViewFromObject(View view, Object object) { return view == ((ImageView) object); } @Override public Object instantiateItem(ViewGroup container, int position) { Context context = MainActivity.this; ImageView imageView …

Implementing Share Intent in Android

Create a sharing button and inside the listen for button click call the share intent

Intent sharingIntent = newIntent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); String shareBody = "Insert the share content body"; sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Insert Subject Here"); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); startActivity(Intent.createChooser(sharingIntent, "Share via")); Note: Give internet permission in android manifest file
< uses-permission android:name="android.permission.INTERNET"/>

Showing Selected Grid Image in New Activity (Full Screen)

in main java class:
gridView = (GridView) findViewById(R.id.gridView1); gridView.setAdapter(new ImageAdapter(this)); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent,View v,int position,long id) { System.out.println("====>>"+position); Intent i = new Intent(getApplicationContext(),GidImage.class); // passing array index i.putExtra("id", position); startActivity(i); } });create a new java file with an image view:
Intent i = getIntent(); // Selected image id int position = i.getExtras().getInt("id"); ImageAdapter imageAdapter = new ImageAdapter(this); ImageView imageView =(ImageView) findViewById(…

Function to Generate a Set of Random Numbers in Corona

Call this function to generate a set of five random numbers (maxRandom)... local function generaterandom() local maxRandom = 5 for m = 1, randCount do randomArray[m]=-100 end local i = 1 while i local randNum = math.random (1, maxRandom) local flagNew = false for j = 1 , randCount do if randomArray[j]==randNum then flagNew = true break end end if flagNew==false then randomArray[i]=randNum i=i+1 end end for k=1, randCount do print("Values---->".. randomArray[k]) end end

Disabling the Touch of an Activity

On a button click we are creating the same class as a dialog so that the touch is not received public class MainActivity extends Activity { Dialog overlayDialog; EditText edt; Button bt; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edt=(EditText)findViewById(R.id.editText1); bt=(Button)findViewById(R.id.button1); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub disableAnyInput(); } }); } public void disableAnyInput(){ Toast.makeText(getApplicationContext(), "This will disable any input. Click the backbutton " + "to enable any input.", Toast.LENGTH_LONG).show(); overlayDialog = new Dialog(MainActiv…

Marquee Text in Android

in xml file:
< TextView android:id="@+id/MarqueeText" android:layout_width="wrap_content" android:marqueeRepeatLimit="marquee_forever" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:freezesText="true" android:paddingLeft="15dip" android:paddingRight="15dip" android:scrollHorizontally="true" android:singleLine="true" android:layout_marginTop="300dp" android:text="This is a very long text which is not fitting in the screen so it needs to be marqueed." />

Passing Images between Activities in Android

in First Activity:
Intent intent=new Intent(FirstClass.this, SecondClass.class); Bundle bundle=new Bundle(); bundle.putInt("image",R.drawable.ic_launcher); intent.putExtras(bundle); startActivity(intent); in Second Acticity:
Bundle bundle=this.getIntent().getExtras(); int pic=bundle.getInt("image"); v.setImageResource(pic);another method:
in First Activity:
Drawable drawable=imgv.getDrawable(); Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); Intent intent=new Intent(Passimage.this,myclass.class); intent.putExtra("picture", b); startActivity(intent); in Second Acticity:
Bundle extras = getIntent().getExtras(); byte[] b = extras.getByteArray("picture"); Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length); Image…

Custom GridView in Android

in xml file:
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > < GridView android:id="@+id/gridView1" android:numColumns="auto_fit" android:gravity="center" android:columnWidth="50dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > < /GridView> < /LinearLayout> in cus_gridview file :
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding=…

GridView in Android

in xml file:
< pre class="brush: java"< < ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > < GridView android:id="@+id/gridView1" android:numColumns="auto_fit" android:gravity="center" android:columnWidth="50dp" android:stretchMode="columnWidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > < /GridView> < /LinearLayout> in java file:
public class Gridviewactivity extends Activity { GridView gridView; static final String[] numbers = new String[] { "A", "B", "C", "D", "E", "F", &quo…

Creating A Customized ListView in Android

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 insert other attributes make a customized listview. Copy or download this javacode and try yourself.
Also see here :
Android – Bullets in ListViewListView in Alphabetic Order in ANDROIDSingle Selection ListView in androidSimple ListView in Android java file :
public class CustomListView extends Activity { static final String[] items = new String[]{"Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday",""Saturday"}; ListView listview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listview=(ListView) findViewById(R.id.list);…

Simple ListView in Android

xml page:eazy_list
< 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="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"> < /ListView> < /RelativeLayout> java page:
public class MainActivity extends ListActivity { String[] planets = new String[] { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune","Ceres","Pluto"}; ListView lstv; private ArrayAdapter listAdapter ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); s…

Global IT spending to reach $3.7 trillion in 2013: Gartner

Worldwide IT spending is projected to reach $3.73 trillion in 2013, a 4.2 percent increase from $3.58 trillion in 2012, research firm Gartner Thursday said.
The 2013 outlook for IT spending growth in US dollars has been revised upward from 3.8 percent in the Q3 2012 forecast, Gartner said in a statement.
Much of this spending increase is the result from projected gains in the value of foreign currencies versus the dollar, it said adding that in constant currency, spending growth in 2013 is expected to be 3.9 percent.
"Uncertainties surrounding prospects for an upturn in global economic growth are the major retardants to IT growth.
This uncertainty has caused the pessimistic business and consumer sentiment throughout the world," Gartner Managing Vice President Richard Gordon said.
However, much of this uncertainty is nearing resolution, and as it does, Gartner expects accelerated spending growth in 2013 compared to 2012, he added.
Worldwide devices spending whic…

Working with images in Corona

Loading an Image :
syntax:
display.newImage( filename [, baseDirectory] [, left, top] )
example:
newImage = display.newImage( "image.png", 10, 20 ) myImage2 = display.newImage( "image.png" )Image Autoscaling:
The default behavior of display.newImage() is to auto-scale large images.To control manually we can use boolean value.
syntax:
display.newImage( [parentGroup,] filename [, baseDirectory] [, x, y] [,isFullResolution] )
example:
newImage = display.newImage( "image.png", 10, 20, true )Images with Dynamic Resolution :
syntax:
display.newImageRect( [parentGroup,] filename [, baseDirectory] w,h )
example:
newImage = display.newImageRect( "Image.png", 100, 100 )

Calculate Quadratic equation in c language

Simple program to get Quadratic equation in c language
#include< stdio.h> #include< math.h> int main(){ float a,b,c; float d,root1,root2; printf("Enter a, b and c of quadratic equation: "); scanf("%f%f%f",&a,&b,&c); d = b * b - 4 * a * c; if(d < 0){ printf("The roots are complex number.\n"); printf("The roots of quadratic equation are: "); printf("%.3f%+.3fi",-b/(2*a),sqrt(-d)/(2*a)); printf(", %.3f%+.3fi",-b/(2*a),-sqrt(-d)/(2*a)); return 0; } else if(d==0){ printf("Both the roots are equal.\n"); root1 = -b /(2* a); printf("The root of quadratic equation is: %.3f ",root1); return 0; } else{ printf("The roots are real numbers.\n"); root1 = ( -b + sqrt(d)) / (2* a); root2 = ( -b - sqrt(d)) / (2* a); printf("The roots of quadratic equation are: %.3f , %.3f",root1,root2); } return 0; } S…

Tech Apple admits flaw in Do Not Disturb feature, will be fixed after 7 Jan

It’s now confirmed that Apple’s Do Not Disturb feature which is available for iPhone, iPod Touch and iPad on iOS 6 doesn’t work. The buzz first started on MacRumours, where users complained that the Do Not Disturb was working ahead of the time that they had set it for.
Read more from here

http://www.firstpost.com/tech/apple-admits-flaw-in-do-not-disturb-feature-will-be-fixed-after-7-jan-576817.html