Monday, March 3, 2014
Work With Spinners in ANDROID
Step 01
Make an xml file in res->values name it as arrays
<string-array name="douse">
<item>Half</item>
<item>One</item>
<item>One and Half</item>
<item>Two</item>
<item>Two and half</item>
</string-array>
Step 02
Cording of java file
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Addpills extends Activity implements AdapterView.OnItemSelectedListener{
Spinner spin_douse;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addpills);
//load page
spin_douse =(Spinner)findViewById(R.id.sp_I_douse_addpills);//get spinner to java from xml
ArrayAdapter adapter_01 = ArrayAdapter.createFromResource(this, R.array.douse, android.R.layout.simple_spinner_item);
//add the string array which is on [res->values->arrays.xml] using arrayadapter to the spinner
spin_douse.setAdapter(adapter_01);
spin_douse.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,
long l) {
//cord what happen when you select value on spinner
// TODO Auto-generated method stub
TextView res_text=(TextView)view;
Toast.makeText(this, "You select "+res_text.getText(), Toast.LENGTH_SHORT).show();
//make a message when click the selection
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@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;
}
}
Make an xml file in res->values name it as arrays
<string-array name="douse">
<item>Half</item>
<item>One</item>
<item>One and Half</item>
<item>Two</item>
<item>Two and half</item>
</string-array>
Step 02
Cording of java file
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Addpills extends Activity implements AdapterView.OnItemSelectedListener{
Spinner spin_douse;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addpills);
//load page
spin_douse =(Spinner)findViewById(R.id.sp_I_douse_addpills);//get spinner to java from xml
ArrayAdapter adapter_01 = ArrayAdapter.createFromResource(this, R.array.douse, android.R.layout.simple_spinner_item);
//add the string array which is on [res->values->arrays.xml] using arrayadapter to the spinner
spin_douse.setAdapter(adapter_01);
spin_douse.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i,
long l) {
//cord what happen when you select value on spinner
// TODO Auto-generated method stub
TextView res_text=(TextView)view;
Toast.makeText(this, "You select "+res_text.getText(), Toast.LENGTH_SHORT).show();
//make a message when click the selection
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@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;
}
}
How to use RadioButton Group in ANDROID
XML cord for radiobuttons and radiobutton group
<RadioGroup
android:id="@+id/rg_I_selectloging_activity_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb_I_delear_activity_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Delear" />
<RadioButton
android:id="@+id/rb_I_rep_activity_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rep" />
</RadioGroup>
Java Cord for use radiobuttons
// convert xml id of radiobutton group to java
RadioGroup rg=(RadioGroup)findViewById(R.id.rg_I_selectloging_activity_a);
loging=(Button)findViewById(R.id.bt_I_loging_activitymain_a);
loging.setOnClickListener(new View.OnClickListener() {
final String value = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();
// get the selected radio buttons value as a string
if(value.equalsIgnoreCase("Delear")){
// check the string values is equal for another string value
Intent openStartingPoint = new Intent("android.intent.action.DEALERMENU");
startActivity(openStartingPoint);
// load a page
}
else if(value.equalsIgnoreCase("Rep")){
Intent openStartingPoint = new Intent("android.intent.action.REPMENU");
startActivity(openStartingPoint);
}
});
---------------------------------------------------------------------------------------------------------------
if you want trigger some thing in radiobutton click
can use this cord
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
// do operations specific to this selection
break;
case R.id.radio1:
// do operations specific to this selection
break;
case R.id.radio2:
// do operations specific to this selection
break;
}
}
});
Pass Data Between two XML files in ANDROID using Java
In here i'm going to insert a text to edittext , and then click a button.Then the page move to another page and display the first text in the second page's text box
My first pages java file name is MainActivity.java and it's xml file name is activity_main.xml
in there I create a edittext(R.id.editText1) and a button (R.id.button1)
My secound pages java file name is Display.java it's xml file name is display.xml in there I creat a edittext(R.id.editText2)
----------------------------------------------------------------------------------------------------------
//this is the cord for the first java class (MainActivity.java)
package com.myapp.activity_01;
import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
public static String st="com.myapp.activity_01";//story my package name
EditText e1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//
e1=(EditText) findViewById(R.id.editText1);//get the edittext xml id to java file
b1=(Button) findViewById(R.id.button1); //get the button xml id to java file
b1.setOnClickListener(this); //set the on clicklistner
}
@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;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Display.class);
Toast.makeText(getApplicationContext(), "Button Clicked! success!!!! ", Toast.LENGTH_LONG).show();
//to display massage as the button is click
String s=e1.getText().toString();//get the edittext's content to string variable
i.putExtra(st,s);//pass the string value to the new activity
startActivity(i);//start new activity
}
}
-------------------------------------------------------------------------------------------------------------------
//this is the cord for the first java class (Display.java)
package com.myapp.activity_01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
public class Display extends Activity {
EditText e1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display); //display the layout
e1=(EditText) findViewById(R.id.editText2);//get the edittext xml id to java file
Intent i=getIntent();
String s1=i.getStringExtra(MainActivity.st);//get the pass string , to string varlable
e1.setText(s1);//set the passed text value for the edit text
}
}
My first pages java file name is MainActivity.java and it's xml file name is activity_main.xml
in there I create a edittext(R.id.editText1) and a button (R.id.button1)
My secound pages java file name is Display.java it's xml file name is display.xml in there I creat a edittext(R.id.editText2)
----------------------------------------------------------------------------------------------------------
//this is the cord for the first java class (MainActivity.java)
package com.myapp.activity_01;
import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
public static String st="com.myapp.activity_01";//story my package name
EditText e1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//
e1=(EditText) findViewById(R.id.editText1);//get the edittext xml id to java file
b1=(Button) findViewById(R.id.button1); //get the button xml id to java file
b1.setOnClickListener(this); //set the on clicklistner
}
@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;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Display.class);
Toast.makeText(getApplicationContext(), "Button Clicked! success!!!! ", Toast.LENGTH_LONG).show();
//to display massage as the button is click
String s=e1.getText().toString();//get the edittext's content to string variable
i.putExtra(st,s);//pass the string value to the new activity
startActivity(i);//start new activity
}
}
-------------------------------------------------------------------------------------------------------------------
//this is the cord for the first java class (Display.java)
package com.myapp.activity_01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
public class Display extends Activity {
EditText e1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display); //display the layout
e1=(EditText) findViewById(R.id.editText2);//get the edittext xml id to java file
Intent i=getIntent();
String s1=i.getStringExtra(MainActivity.st);//get the pass string , to string varlable
e1.setText(s1);//set the passed text value for the edit text
}
}
Subscribe to:
Posts (Atom)