My question is how can i put my spinner in an external java class and implement it in all other activities (works as a menu), here is my spinner code:
final Spinner spinner = (Spinner) findViewById(R.id.comboCasino);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setSelection(0, false);
// this will be called when you select any item in this spinner
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
// get the text at that position
switch(position) {
case 0: {
Intent NewPost = new Intent(getApplicationContext(), StartingPoint.class);
startActivity(NewPost);
break; }
case 1: {
Intent NewPost = new Intent(getApplicationContext(), Simmering.class);
startActivity(NewPost);
break; }
case 2: {
Intent NewPost = new Intent(getApplicationContext(), LugnerCity.class);
startActivity(NewPost);
break; }
case 3: {
Intent NewPost = new Intent(getApplicationContext(), Gmunden.class);
startActivity(NewPost);
break;}
case 4: {
Intent NewPost = new Intent(getApplicationContext(), Salzburg.class);
startActivity(NewPost);
break; }
case 5: {
Intent NewPost = new Intent(getApplicationContext(), Linz.class);
startActivity(NewPost);
break; }
case 6: {
Intent NewPost = new Intent(getApplicationContext(), Saalbach.class);
startActivity(NewPost);
break; }
case 7: {
Intent NewPost = new Intent(getApplicationContext(), Innsbruck.class);
startActivity(NewPost);
break;}
case 8: {
Intent NewPost = new Intent(getApplicationContext(), Reutte.class);
startActivity(NewPost);
break; }
case 9: {
Intent NewPost = new Intent(getApplicationContext(), Bregenz.class);
startActivity(NewPost);
break; }
case 10: {
Intent NewPost = new Intent(getApplicationContext(), Kufstein.class);
startActivity(NewPost);
break; }
case 11: {
Intent NewPost = new Intent(getApplicationContext(), Bratislava.class);
startActivity(NewPost);
break; }
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
it shoud be saved for example in menu.java and should be called in every activity, how do i do that correctly? thank you in advance.
If you have the same menu in all your activities, the best way to do this is by creating a superclass that extends Activity and let all your other activities extend this activity.
public class BaseActivity extends Activity { // menu code }
public class StartingPoint extends BaseActivity { //... }
Try creating your own Spinner class:
public class MenuSpinner extends Spinner {
public MenuSpinner(Context context) {
super(context);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.comboCasino, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
setAdapter(adapter);
setSelection(0, false);
.
.
.
}
}
And then add it in your layout
<com.yourpackage.MenuSpinner
android:id="comboCasino"
.
.
. />
If you want more detailled information visit http://developer.android.com/training/custom-views/index.html
Create a constructor for menu.java
menu(Context mcontext){
this.mcontext = mcontext;
}
Now in place of getApplicationContext() call it by passing of object of class where you want to use Spinner property.
Menu object = new Menu(object_of_current_class);
object.ShowSpinner();
Related
I did ListView with some items, now I want when I click on specific item to send me to 'next' activity.
I write this code:
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
Intent intent = new Intent(DrugaStranica.this, MainActivity.class);
startActivity(intent);
}
});
But it is work for all item some, I want specific item to send me to specific activity.
Please use below code it may be helpful to solve your issue.
listViewProduct.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View v, int pos, long id) {
Intent intent;
switch (pos) {
case 0:
intent = new Intent(DrugaStranica.this, TestOneActivity.class);
break;
case 1:
intent = new Intent(DrugaStranica.this, TestTwoActivity.class);
break;
case 2:
intent = new Intent(DrugaStranica.this, TestThreeActivity.class);
break;
case 3:
intent = new Intent(DrugaStranica.this, TestFourActivity.class);
break;
default:
intent = new Intent(DrugaStranica.this, MainActivity.class);
break;
}
startActivity(intent);
}
});
First of all, I am fairly new at this, so bear with me.
I am trying to make a spinner where if you select an item, the layout and the activity will change, but I cant even implement the simplest of spinners.
so far the only code I've got is the one below, but no matter what I put in it doesn't do anything, so I must just not understand it, please be very specific in your answers. thank you. the first answer got me some of the way, but the "spinner spin..... spin.setAdapter(aa)" part is not accepted under OnCreate
String[] generations = { "Gen2", "Gen3", "Gen4", "Gen5", "Gen6","Gen7" };
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,generations);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
Intent intent2 = new Intent(this, gen2.class);
startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(this, Gen3.class);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(this, gen4.class);
startActivity(intent4);
break;
case 4:
Intent intent5 = new Intent(this, gen5.class);
startActivity(intent5);
break;
case 5:
Intent intent6 = new Intent(this, MainActivity.class);
startActivity(intent6);
break;
case 6:
Intent intent7 = new Intent(this, gen7.class);
startActivity(intent7);
break;
}
}
Here is a example, hope it helps you understand it better:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
switch (position){
// Check what position was selected
case 1:
//start activity depending on position
Intent intent = new Intent(this, ActivityOne.class);
startActivity(intent);
break;
case 2:
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
break;
case 3:
Intent intent = new Intent(this, ActivityThree.class);
startActivity(intent);
break;
}
#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.activity_main, menu);
return true;
}
}
I am using a nav drawer with three items(Time sheet, claims, project) and I would like to pass ab to claims. In time sheet I have 3 activities, the ab is in the work_details activity.How can I do to achieve this?
TimeSheet has 3 activities (Information(extend to fragment),work force,work details)
Claims has 2 activities(claims(extend to fragment), cameraMain).
WorkDetails.java
Button btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
builder.setTitle("Data Saved");
builder.setMessage("Are you sure you want to save?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
long ab = ts.insertTimeSheet(name, weather, date, status,b,i);
Bundle bundle =new Bundle();
bundle.putString("ab","ab");
Claims o=new Claims();
o.setArguments(bundle);
}
Claims.java
final Long fk=this.getArguments().getLong("ab");
Button button = (Button) claims.findViewById(R.id.button8);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
startActivity(intent);
}
});
return claims;
}
I refer from Send data from activity to fragment in android but the app crashed when I select claims item.
11-02 09:25:55.910 6116-6116/com.example.project.project
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 6116
java.lang.NullPointerException
at com.example.project.project.Claims.onCreateView(Claims.java:59)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
I think the NullPointException is came from here
fk=data.getLong("ab");
Do I need to add the parameter in MainActivity?
MainActivity.java
Some part of code snippet in mainActivity
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment=new Claims();
break;
case 2:
fragment=new Project();
case 3:
fragment=new Report();
case 4:
fragment=new ViewView();
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
EDITED
Button btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
builder.setTitle("Data Saved");
builder.setMessage("Are you sure you want to save?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
long ab = ts.insertTimeSheet(name, weather, date, status,b,i);
Bundle bundle =new Bundle();
bundle.putLong("ab",ab);
Claims o=new Claims();
o.setArguments(bundle);
}
Claims.java
Bundle bundle=this.getArguments();
fk=bundle.getLong("ab");
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
AlertDialogRadio(a1);
}
});
MainActivity
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment=new Claims();
break;
case 2:
fragment=new Project();
break;
case 3:
fragment=new Report();
break;
case 4:
fragment=new ViewView();
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
if(fragment instanceof Claims)
{
Bundle bundle=new Bundle();
bundle.putString("ab","ab");
fragment.setArguments(bundle);
fragment.setArguments(bundle);
}
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
MainActivity
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment=new Claims();
Bundle bundle=new Bundle();
bundle.putLong("ab",WorkDetails.ab);
fragment.setArguments(bundle);
break;
case 2:
fragment=new Project();
break;
case 3:
fragment=new Report();
break;
case 4:
fragment=new ViewView();
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
WorkDetails
public static long ab=-1;//this is flied ,don't put in the method
Button btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
builder.setTitle("Data Saved");
builder.setMessage("Are you sure you want to save?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
ab = ts.insertTimeSheet(name, weather, date, status,b,i);
}
What you can just do is to place this data in your activity where fragment is placed. example is.
in your activity
long ab = 12.4;
in your fragment
long data = ((YourActivity)getActivity()).ab;
"OR"
make ab like
static long ab = value; // your Details activity
in your details activity and get the value of this
with details activity name like
long data = Details.ab //in your claims activity.
When you are instantiating your Claim Fragment , instantiate a bundle object and assign whatever data that you need to be passed in bundle and set it as argument to the fragment.
Bundle bundle = new Bundle();
bundle.putLong("ab",10L);
new Claims().setargument(bundle);
Then in your Claim fragment's onCreateView() you can access this bundle data as
Bundle bundle = getArguments();
long ab = bundle.getLong("ab");
How to submit spinner selecteditem text via btnup and why selectedReport on btnup cases is declared not used?I have search hard enough to solve this but i still blankon what i need to do in order to get text on spinner to suit the coding.
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
Button bLogout;
ImageView viewImage;
Button b,btnup;
private String selectedReport = null;
} private void createSpinnerDropDown() {
//get reference to the spinner from the XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
//Array list of report to display in the spinner
List<String> list = new ArrayList<String>();
list.add("Crime");
list.add("Bribery");
list.add("Schools problem");
list.add("Homeless");
list.add("Rural Problems");
list.add("Public Transport");
//create an ArrayAdaptar from the String Array
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selectedItem = parent.getItemAtPosition(pos).toString();
switch (parent.getId()) {
case R.id.spinner:
if (selectedReport != null) {
Toast.makeText(parent.getContext(), "Report you select is " + selectedItem,
Toast.LENGTH_LONG).show();
}
selectedReport = selectedItem;
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String selectedReport = spinner.getSelectedItem().toString();
break;
}
}
Declare your views before onCreate in your class:
UserLocalStore userLocalStore;
EditText etName, etAge, etUsername, uploadImageName;
ImageView viewImage;
Button b, bLogout, btnup;
Spinner spinner;
String selectedReport;
In your onCreate, add the following:
String[] list = {"Crime", "Bribery", "Schools Problem", "Homeless", "Rural Problems", "Public Transport"}
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Toast.makeText(this, "Report selected: " + list[position], Toast.LENGTH_LONG).show();
//You can remove this switch statement, if you don't need it.
switch (position) {
case 0:
//Do something
break;
case 1:
//Do something
break;
case 2:
//Do something
break;
case 3:
//Do something
break;
case 4:
//Do something
break;
case 5:
//Do something
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
I am assuming here that you have already set an onClickListener to your buttons in onCreate.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bLogout:
userLocalStore.clearUserData();
userLocalStore.setUserLoggedIn(false);
Intent loginIntent = new Intent(this, Login.class);
startActivity(loginIntent);
break;
case R.id.btnup:
Bitmap image = ((BitmapDrawable) viewImage.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
selectedReport = spinner.getSelectedItem().toString();
break;
}
}
I'm trying to get view from Fragment dynamically added to Activity.
MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
myDrawerList.setOnItemClickListener(new DrawerItemClickListener());
handleIntent(getIntent());
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(
AdapterView<?> parent, View view, int position, long id
) {
// display view for selected nav drawer item
displayView(position);
}
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
String userToken = UserTokenStorageFactory.instance().getStorage().get();
switch (position) {
case 0:
fragment = new AllCategories();
break;
case 1:
fragment = new Subscription();
break;
case 2:
if( userToken != null && !userToken.equals( "" ) ){
Intent myIntent1 = new Intent(this, PrivateOffice.class);
this.startActivity(myIntent1);
break;
}
else {
Intent myIntent1 = new Intent(this, LogIn.class);
this.startActivity(myIntent1);
break;
}
case 3:
if( userToken != null && !userToken.equals( "" ) ){
Intent myIntent1 = new Intent(this, Create_action.class);
this.startActivity(myIntent1);
break;
}
else {
Intent myIntent1 = new Intent(this, LogIn.class);
this.startActivity(myIntent1);
break;
}
case 4:
AlertDialog dialog = onCreateDialog(DIALOG_CHOOSE_CATEGORY);
dialog.show();
break;
default:
break;
}
if (fragment != null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
fTrans = fragmentManager.beginTransaction();
fTrans.replace(R.id.content_frame, fragment);
fTrans.commit();
// update selected item and title, then close the drawer
myDrawerList.setItemChecked(position, true);
myDrawerList.setSelection(position);
setTitle(viewsNames[position]);
myDrawerLayout.closeDrawer(mDrawer);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I need to get view (LinearLayout listv) from layout of AllCategories.java. I tried using getView() with findViewById, but it returns NullPointerException. What shall I do?