Wednesday, December 17, 2014
How to use & access In android
First create object:-
WebAPIRequest web = new WebAPIRequest();
WebUrl weburl = new WebUrl();
Call Url and Execute :-
url = weburl .setURL() + "CompanyList";
new get_alert_activity().execute(url);
public class get_alert_activity extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
try {
} catch (Exception e) {
e.getMessage();
}
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
response = web.performGet(params[0]);
Log.i("Response Display Extraactivity", response);
} catch (Exception e) {
e.getMessage();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
try {
Log.i("Test", response);
getjsontoListviewCompanyList(response);
datalistview dlv = new datalistview();
lv.setAdapter(dlv);
} catch (Exception e) {
e.getMessage();
}
}
}
public void getjsontoListviewCompanyList(String result) {
try {
jsonArray = new JSONArray(result);
for (i = 0; i <= jsonArray.length(); i += 1) {
list.add(jsonArray.getString(i));
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
-------How to Use split Function and access String data-----------------------------------
JSONObject jobj;
jobj=new JSONObject(result);
String str1=jobj.getString("exam_get_questionsResult");
str2 = str1.split(";");
len=str2.length;
totallen=len;
Log.i("lengthofstring",String.valueOf( str2.length) );
newquestion = new String[len*6];
for(int i=0 ; i < str2.length;i++)
{
quetiondata = str2[i].split(",");
for(int k=0;k<6;k++)
{
newquestion[j]=quetiondata[k];
Log.i("Question",j + newquestion[j]);
j++;
}
}
-------End Split Function Code--------------------------------
}
} catch (Exception e) {
// TODO: handle exception
Toast.makeText(getApplicationContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
Log.i("Result Response", e.getMessage());
}
}
public class datalistview extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return list.get(pos);
}
@Override
public long getItemId(int pos) {
// TODO Auto-generated method stub
return pos;
}
@Override
public View getView(int pos, View convertedView, ViewGroup viewgroup) {
// TODO Auto-generated method stub
LayoutInflater inflater = getLayoutInflater();
inflater = LayoutInflater.from(CompanyList.this);
convertedView = inflater.inflate(R.layout.list, null);
TextView tv = (TextView) convertedView
.findViewById(R.id.branch_name);
tv.setText(list.get(pos));
return convertedView;
}
}
Sunday, September 7, 2014
Android interview questions
Android interview questions : Interview Important question answer in Android
-------------------------------------------------------------------------------------------------------
01) What is android?
Ans :- Android is a stack of software for mobile
devices which has Operating System, middleware and some key
applications. The application executes within its own process and its
own instance of Dalvik Virtual Machine
02) What is activity?
Ans:- A single screen in an application, with supporting Java code.
03) What is intent in Android?
ans :- A class (Intent) will describes what a
caller desires to do. The caller will send this intent to Android's
intent resolver, which finds the most suitable activity for the intent.
04) What Programming languages does Android support for application development?
ans:- Android applications supports using Java Programming Language. which is coded in Java and complied using Android SDK.
05) What is a resource?
ans:- A user defined JSON, XML, bitmap, or other
file, injected into the application build process, which can later be
loaded from code.
06) What is APK format?
ans:- The APK file is compressed
AndroidManifest.xml file with extension .apk, Which have application
code (.dex files), resource files, and other files which is compressed
into single .apk file.
07) What are the features of Android?
ans:-
-> Components can be reused and replaced by the application framework.
-> Optimized DVM for mobile devices
-> SQLite enables to store the data in a structured manner.
-> Supports GSM telephone and Bluetooth, WiFi, 3G and EDGE technologies
-> The development is a combination of a device emulator, debugging tools, memory profiling and plug-in for Eclipse IDE
-> Optimized DVM for mobile devices
-> SQLite enables to store the data in a structured manner.
-> Supports GSM telephone and Bluetooth, WiFi, 3G and EDGE technologies
-> The development is a combination of a device emulator, debugging tools, memory profiling and plug-in for Eclipse IDE
Monday, August 4, 2014
Create a Simple Calculator In Android
--------------------------------------------------------------------------------------
Note:- First Create New Android Application(Project) And After Followed Following Step's
--------------------------------------------------------------------------------------
Step:- 1
XML: Go To Activity_main.xml OR main.xml File And Add Following xml Code ....
<?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" android:paddingTop="15dp">
<RelativeLayout android:layout_height="wrap_content"
android:id="@+id/relativeLayout1" android:layout_width="match_parent">
</RelativeLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout1">
<TextView android:text="Enter first values" android:id="@+id/txt1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textStyle="bold"></TextView>
<EditText android:text="" android:id="@+id/editText1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:singleLine="true"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout2">
<TextView android:text="Enter second value" android:id="@+id/txt2"
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text="" android:id="@+id/editText2"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:singleLine="true"></EditText>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout3"
android:padding="15dp">
<TextView android:text="Answer = " android:id="@+id/txt3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="30dp"></TextView>
<TextView android:text="" android:id="@+id/txt4"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="20dp"></TextView>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="@+id/linearLayout4"
android:paddingTop="20dp" android:paddingLeft="55dp">
<Button android:text="+" android:id="@+id/btn1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
<Button android:text="-" android:id="@+id/btn2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
<Button android:text="*" android:id="@+id/btn3"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:text="/" android:id="@+id/btn4"
android:textSize="40dp"></Button>
<Button android:text="%" android:id="@+id/btn5"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="40dp"></Button>
</LinearLayout>
</LinearLayout>
--------------------------------------------------------------------------------------
Step:- 2
JAVA:- Go To src Folder --> Go To Java Package --> Go To .java File And Write Following java Code....
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class calculatertest extends Activity {
/** Called when the activity is first created. */
TextView tv1;
EditText ed1,ed2;
Button sum,sub,mul,div,mod;
int v1, v2 ,ans;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed1=(EditText)findViewById(R.id.editText1);
ed2=(EditText)findViewById(R.id.editText2);
tv1=(TextView)findViewById(R.id.txt4);
sum=(Button)findViewById(R.id.btn1);
sub=(Button)findViewById(R.id.btn2);
mul=(Button)findViewById(R.id.btn3);
div=(Button)findViewById(R.id.btn4);
mod=(Button)findViewById(R.id.btn5);
sum.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1+v2;
tv1.setText(String.valueOf(ans).toString());
}
});
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1-v2;
tv1.setText(String.valueOf(ans).toString());
}
});
mul.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1*v2;
tv1.setText(String.valueOf(ans).toString());
}
});
div.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1/v2;
tv1.setText(String.valueOf(ans).toString());
}
});
mod.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v1=Integer.parseInt(ed1.getText().toString().trim());
v2=Integer.parseInt(ed2.getText().toString().trim());
ans=v1%v2;
tv1.setText(String.valueOf(ans).toString());
}
});
}
}
--------------------------------------------------------------------------------------
Step:- 3
Now Run Your Android Application(Project) and Check Your Project Output.......
OUTPUT:- Screen Short
Wednesday, July 23, 2014
How To Create Email Validation....
Create login application where you will have to validate EmailID (UserName). Till the username and password is not validated , login button should remain disabled.
--------------------------------------------------------------------------------------
Note:- First Create New Android Application(Project) And After Followed Following Step's
Step:- 1
XML: Go To Activity_main.xml File And Add Following Code....
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F0F0"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text=" Login Form"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#176CEC"
android:textStyle="bold" />
<EditText
android:id="@+id/editText_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:ems="10"
android:hint="Enter Email Id"
android:inputType="textEmailAddress"
android:padding="12dp" />
<Button
android:id="@+id/btn_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#176CEC"
android:text="Check Valid Email"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
Step:- 2
JAVA:- Go To Activity_main.java File And Write Following Code....
package com.chirag.emailvalidation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.Menu;
public class MainActivity extends Activity {
EditText emailEditText;
Button btn_click;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEditText = (EditText) findViewById(R.id.editText_email);
btn_click=(Button)findViewById(R.id.btn_signup);
btn_click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
final String email = emailEditText.getText().toString();
if (!isValidEmail(email))
{
emailEditText.setError("Invalid Email");
Toast.makeText(getApplicationContext(),
"Invalid Email", Toast.LENGTH_LONG).show();
btn_click.setEnabled(true);
}
else
{
//emailEditText.setError("Valid Email");
Toast.makeText(getApplicationContext(),
"Valid Email", Toast.LENGTH_LONG).show();
btn_click.setEnabled(false);
}
}
});
}
// validating email id
private boolean isValidEmail(String email) {
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pattern = Pattern.compile(EMAIL_PATTERN);
Matcher matcher = pattern.matcher(email);
return matcher.matches();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Step:- 3
Now Run Your Android Application(Project) and Check Your Email Is Validate Or Not.......
OUTPUT:- Screen Short
Sunday, July 20, 2014
How to Use DatePicker Control
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" >
<Button
android:id="@+id/changeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click To Change Date" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current/Selected Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
Activity_main.java
package com.chirag.datepicker;
import com.android.datepicker.R;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
public class DatePickerExample extends Activity {
private Button changeDate;
public EditText ed;
private int year;
private int month;
private int day;
static final int DATE_PICKER_ID = 709;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
changeDate = (Button) findViewById(R.id.changeDate);
ed=(EditText)findViewById(R.id.editText1);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// Show current date
ed.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// Button listener to show date picker dialog
changeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// On button click show datepicker dialog
showDialog(DATE_PICKER_ID);
}
});
ed.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
showDialog(DATE_PICKER_ID);
}
});
}
@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_PICKER_ID:
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
return new DatePickerDialog(this, pickerListener, year, month,day);
}
return null;
}
private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener()
{
// when dialog box is closed, below method will be called.
@Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
ed.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
}