How do i set a text field in Eclipse to work in conjunction with an item selected in a Spinner? EXAMPLE- If the user selects the second item (out of lets say 3 in the drop down menu) i want them to be able to use the text field to the fight of the spinner to set a value to that item selected. Think of it like a translation tool. If the person selects English out of the menu of languages and then types in an english word (in the text field) i want it to be able to CONVERT that to whatever they chose in the second spinner (Spanish) once they hit the "convert" button.
I apologize if this question seems more in need of a java/eclipse lesson then code help, but i appreciate any help at all :)
MainActivity
package com.overworldinnovations.datatool;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.os.Build;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private Spinner spinner1, spinner2;
private Button buttonConvert;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsOnSpinner2();
addListenerOnButton();
addListenerOnSpinnerItemSelection();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
//List<String> list = new ArrayList<String>();
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener());
/*list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);*/
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,
"OnClickListener : " +
"\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) +
"\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
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.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.overworldinnovations.datatool.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/buttonConvert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:text="Convert" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Please Select A Data Type To Be Converted"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/spinner1"
android:layout_marginTop="45dp"
android:entries="#array/type_arrays"
android:prompt="#string/data_prompt" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="60dp"
android:entries="#array/type_arrays"
android:prompt="#string/data_prompt" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/spinner2"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner1"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="numberSigned" />
</RelativeLayout>
strings
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Data Tool</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="convert">Convert</string>
<string name="data_tool_is_an_application_that_converts_binary_to_decimal_d">Data Tool is an application that converts Binary to Decimal :D</string>
<string name ="data_prompt">Choose a data type</string>
<string-array name = "type_arrays">
<item >Decimal</item>
<item >Binary</item>
<item >Hexidecimal</item>
</string-array>
</resources>
CustomOnItemSelectedListener
package com.overworldinnovations.datatool;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Well, i cannot tell you about your conversion thingy but i will tell you how to listen to do something based on a selected spinner entry and how to manipulate textfields. The rest you have to code :-)
Here comes a class that implements a spinner, and does something based on the selected option in the spinner. It can be made very different too but this should explain things easy.
public class MyClass extends Fragment implements AdapterView.OnItemSelectedListener {
// Your Spinner
public Spinner spinner;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
final View view = inflater.inflate(R.layout.tab2, container, false);
// Customize from here
final TextView textview = (TextView) view.findViewById(R.id.textView);
final Button button = (Button) view.findViewById(R.id.btn_calculate);
setSpinnerContent( view );
// attach an OnClickListener
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Integer selectedOption = spinner.getSelectedItemPosition();
if (selectedOption==0) {
myString = "Option 1 selected";
}
if (selectedOption==1) {
myString = "Option 2 selected";
}
if (selectedOption==2) {
myString = "Option 3 selected";
}
textview.setText(myString);
}
}
);
return view;
}
private void setSpinnerContent( View view )
{
spinner = (Spinner) view.findViewById( R.id.spinner );
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter( adapter );
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { // ItemsSelected
//An alternate way to listen to the selected items
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
The spinner gets content from an String XML resource like this:
<string-array name="planets_array">
<item>Euro</item>
<item>US-Dollar</item>
<item>Yen</item>
</string-array>
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String val = spinner.getSelectedItem().toString();//Getting Value of Selected Item
txtPerc.setText(val);//TextView
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Do Something Like This.. And please Clear Your Question.
Hope Its Help..
Related
I am making an activity with 3 spinners, First spinner only have one value for now but second spinner have total of six values. I want populate 3rd spinner on base of items selected on second spinner (because first spinner only have 1 item). Can any one explain how can I do it?
I thought about doing it with switch case but I don't know how I can store spinner values in variable. I also searched for it on google and found onItemSeleted() method but I cant understand how to use it.
XML file :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ENOTES">
<TextView
android:id="#+id/Department_text"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="108dp"
android:text="Department"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/department_text2"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="80dp"
android:text="Subject"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/sem_spinner" />
<TextView
android:id="#+id/Sem_text"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="80dp"
android:text="Semester"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dept_spinner" />
<Spinner
android:id="#+id/dept_spinner"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Department_text" />
<Spinner
android:id="#+id/sem_spinner"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="28dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Sem_text" />
<Spinner
android:id="#+id/subject_spinner"
android:layout_width="144dp"
android:layout_height="50dp"
android:layout_marginStart="48dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="28dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/department_text2" />
<Button
android:id="#+id/get_notes_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.888"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.959" />
</androidx.constraintlayout.widget.ConstraintLayout>
java :
package com.example.project;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import java.util.List;
public class ENOTES extends AppCompatActivity
{
Spinner dept_spinner, sem_spinner, subject_spinner;
Button get_notes_button;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enotes);
//Variable Initializations
dept_spinner = findViewById(R.id.dept_spinner);
sem_spinner = findViewById(R.id.sem_spinner);
subject_spinner = findViewById(R.id.subject_spinner);
get_notes_button = findViewById(R.id.get_notes_button);
//Setting dept_spinner adapter
ArrayAdapter<CharSequence> ad_dept = ArrayAdapter.createFromResource(this,
R.array.department_names, android.R.layout.simple_spinner_item);
ad_dept.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dept_spinner.setAdapter(ad_dept);
//setting sem_spinner adapter
ArrayAdapter<CharSequence> ad_sem = ArrayAdapter.createFromResource(this,
R.array.sem_names, android.R.layout.simple_spinner_item);
ad_sem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sem_spinner.setAdapter(ad_sem);
}
}
User selected item listener to get selected option for first two spinner and then show data in third spinner according to that
String a;
String b;
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// assign data to 'a'
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// assign data to 'b'
setDatatoSpinner3(a,b)
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
function setDatatoSpinner3(String a, String b){
// your data logic here
}
Updated code according to your code
package com.example.project;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import java.util.List;
public class ENOTES extends AppCompatActivity
{
Spinner dept_spinner, sem_spinner, subject_spinner;
Button get_notes_button;
String dept_selection = "", sem_selection = "";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enotes);
//Variable Initializations
dept_spinner = findViewById(R.id.dept_spinner);
sem_spinner = findViewById(R.id.sem_spinner);
subject_spinner = findViewById(R.id.subject_spinner);
get_notes_button = findViewById(R.id.get_notes_button);
//Setting dept_spinner adapter
ArrayAdapter<CharSequence> ad_dept = ArrayAdapter.createFromResource(this,
R.array.department_names, android.R.layout.simple_spinner_item);
ad_dept.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dept_spinner.setAdapter(ad_dept);
//setting sem_spinner adapter
ArrayAdapter<CharSequence> ad_sem = ArrayAdapter.createFromResource(this,
R.array.sem_names, android.R.layout.simple_spinner_item);
ad_sem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sem_spinner.setAdapter(ad_sem);
dept_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
dept_selection = ad_dept.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
sem_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
sem_selection = ad_sem.get(position);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
}
function updateSubjects(String dept, String sem){
//setting sem_spinner adapter
if(dept.equals('CIVIL') && sem.equals('1st') ){
ArrayAdapter<CharSequence> ad_subj = ArrayAdapter.createFromResource(this,
R.array.civil_1st_sem, android.R.layout.simple_spinner_item);
} else if(dept.equals('CIVIL') && sem.equals('2nd') ){
ArrayAdapter<CharSequence> ad_subj = ArrayAdapter.createFromResource(this,
R.array.civil_2nd_sem, android.R.layout.simple_spinner_item);
}
ad_subj.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subject_spinner.setAdapter(ad_subj);
}
}
I'm trying to make a app in Android Studio where you have defined URLs in the spinner. And when you choose a item in the spinner it should load the URL into spinner.
I have gotten some help with this, but now I face another problem: I'm getting:
cannot resolve symbol method loadUrl java.lang.strin
My XML code:
<resources>
<string name="app_name">Obligatorisk1</string>
<string-array name="spinner">
<item value="http://www.vg.no">VG</item>
<item value="http://www.dagbladet.no">Dagbladet</item>
<item value="http://www.nettavisen.no">Nettavisen</item>
</string-array>
<string name="omAppen">Om Appen</string>
<string name="title_activity_main2">Om Appen</string>
My main activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener{
private Button omAppenKnapp;
Spinner spinner;
WebView wb1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
wb1 = (WebView) findViewById(R.id.webView);
wb1.getSettings().setJavaScriptEnabled(true);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this,
R.array.spinner, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int i, long l) {
String urlName = adapterView.getItemAtPosition(i).toString();
String urlValue = "http://www.google.com";
switch (urlName) {
case "VG":
urlValue = "http://www.vg.no";
break;
case "Dagbladet":
urlValue = "http://www.dagbladet.no";
break;
case "Nettavisen":
urlValue = "http://www.nettavisen.no";
break;
}
view.loadUrl(urlValue);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
wb1.loadUrl("http://www.google.com");
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
and layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webView"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/spinner" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/omAppen"
android:id="#+id/buttonOmOss"
android:onClick="visOmAppen"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Try this :
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected_url = spinner.getSelectedItem().toString();
view.loadUrl(selected_url);
}
EDIT
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button omAppenKnapp;
Spinner spinner;
WebView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.spinner, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
view = (WebView) findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new browser());
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selected_url = spinner.getSelectedItem().toString();
view.loadUrl(selected_url);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
You can do something like this:
public class MainActivity extends AppCompatActivity {
Spinner spinner;
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.spinner, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String urlName = adapterView.getItemAtPosition(i).toString();
String urlValue = "http://www.google.com";
switch (urlName)
{
case "VG":
urlValue ="http://www.vg.no";
break;
case "Dagbladet":
urlValue = "http://www.dagbladet.no";
break;
case "Nettavisen":
urlValue = "http://www.nettavisen.no";
break;
}
webView.loadUrl(urlValue);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
webView.loadUrl("http://www.google.com");
}
});
}
}
What i want to do is to add a spinner and a button, in spinner 2 songs are placed when i selected a song it plays and same as for other one but when i run this code it will do nothing just display a spinner and button and i only select a song and when i clicked a play button it do no respond.
import android.media.MediaPlayer;
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.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
public int soundSelection;
MediaPlayer mp;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
btn=(Button)findViewById(R.id.button1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.soundEntries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (soundSelection) {
case 1:
if (mp != null && mp.isPlaying())
mp = MediaPlayer.create(MainActivity.this, R.raw.bewafa);
mp.start();
mp.setVolume(100,100);
mp.setLooping(true);
Toast t = Toast.makeText(MainActivity.this,"bewafa",Toast.LENGTH_LONG);
t.show();
break;
case 2:
// if(mp.isPlaying())
mp = MediaPlayer.create(getApplicationContext(), R.raw.dildobaa);
mp.start();
Toast th = Toast.makeText(getApplicationContext(), "dildobaa", Toast.LENGTH_LONG);
th.show();
break;
}
}
});
}
/*Spinner Functions*/
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
soundSelection=(Integer)parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
#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_main, 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);
}
}
This is the Xml code:
<LinearLayout
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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:entries="#array/soundEntries"
android:layout_height="wrap_content"
android:id="#+id/spinner1">
</Spinner>
<Button
android:layout_width="wrap_content"
android:text="Play"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:id="#+id/button1" />
This is Strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Test</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string-array name="soundEntries">
<item>bewafa</item>
<item>dildobaa</item>
</string-array>
Do below changes to your code:
implement AdapterView.OnItemSelectedListener:
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener {
Change switch cases to 0 and 1. (The count starts with 0)
Comment out the line: if (mp != null && mp.isPlaying())
Change onItemSelected() callback method to:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
soundSelection = pos;
}
Hi i am beginner and i want to write simple on click event so i come up with these code in Android SDK and Eclipse :
package com.example.aplic;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("yourtext");
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#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 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
and :
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.aplic.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="110dp"
android:layout_toRightOf="#+id/textView1" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="96dp"
android:contentDescription="#string/abc_action_mode_done"
android:src="#drawable/abc_ab_bottom_solid_dark_holo" />
</RelativeLayout>
The problem is when i launch the code on emulator it hang and reset and when i launch it in the real device , it close the program, I dont know what is wrong with it , do i use wrong event ?
PS: When i delete the onclick event part, it works fine but it don't react on the click.
activity_main.xml :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.deomanapp.MainActivity"
tools:ignore="MergeRootFrame" />
It looks like the button belongs to fragment_main.xml.
From your comment
The name is fragment_main.xml
Its not hanging. Its a crash. You are probably getting NullPointerException.
Change to
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
TextView tx = (TextView) rootview.findViewById(R.id.textView1);
tx.setText("yourtext");
}
});
return rootView;
}
I'm having some issues with my school project. I can't get the text from the textbox. I searched for a solution but nothing found.. I'll be grateful if somebody help me :)
So here is my Java code:
package com.src.vicnote;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class NewNoteActivity extends ActionBarActivity {
Button saveButton;
EditText textData;
Context context;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
saveButton = (Button) this.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textData = (EditText) findViewById(R.id.editText);
text = textData.getText().toString();
//text = "this is sparta!";
Log.d("ADebugTag", "string: \"" + text + "\" end of note text");
new SaveClass(text/*, context.getApplicationContext()*/);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_note, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new_note,
container, false);
return rootView;
}
}
}
And my XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.NewNoteActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/buttonSave"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/buttonSave"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</RelativeLayout>
Also I want to ask you what will happen if the text is cyrillic? Will be there any problem?
Try initializing textData outside of your OnClickListener:
textData = (EditText) findViewById(R.id.editText);
saveButton = (Button) this.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener() { //...
I was also stuck at finding method to get text from edittext. I got resolved this issue by doing the below,
String selQuantity = (((TextView)findViewById(R.id.etxtQuantity)).getText()).toString();
Try this. It works.