I am trying to make a spinner that will display a different text view each time an item from the list has been selected. When I run my code, I am able to switch between the different items, but the text is not updating based on the selection. I have looked at a variety of similar questions but none of their solutions have done what I am looking for.
Here is my code from the main activity:
Spinner spinner;
TextView example;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
example = (TextView) findViewById(R.id.example);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.medication_array,android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
}
#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);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case 0:
example.setText("Depression");
break;
case 1:
example.setText(R.string.ssriexample);
break;
case 2:
example.setText(R.string.snriexample);
break;
case 3:
example.setText(R.string.tcaexample);
break;
case 4:
example.setText(R.string.moiexample);
break;
case 5:
example.setText(R.string.otherexample);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Any tips would be apprieated as this is my first time coding in android studios and it's taking a little bit of time to get used to.
From the way your code is posted, it looks like your listeners are not attached to the spinner. Call spinner.setOnItemSelectedListener(new listener) or spinner.setOnItemSelectedListener(this)
Related
I created Listview and menu options in android application with App Constants to minimize the number of classes
Ex:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new ArrayAdapter<String>(this,R.layout.list_item,prgmNameList));
lv.setOnItemClickListener(this);
}
OnClickListener
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position)
{
case 0:
Intent intent = new Intent(this, First.class);
intent.putExtra(AppConstants.MAIN_ACTIVITY_TAG,lv.getItemAtPosition(position).toString());
startActivity(intent);
break;
}
But i need apply app constants to Menu Options too
But i don't know how to do that:
Here is the code for Menu Options
// create action options for options
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.news, 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_about) {
Intent intent = new Intent(this,Extras.class);
startActivity(intent);
}
if (id == R.id.action_advertise) {
return true;
}
if (id == R.id.action_contact) {
return true;
}
if (id == R.id.action_help) {
return true;
}
return super.onOptionsItemSelected(item);
}
I want to do same as listview but for menu options.
Please help me solve this problem.
Just put your string in string.xml .
<string name="menu_name">Show result</string>
and you directly access it in xml .
<menu 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"
tools:context=".ContainerActivity">
<item
android:id="#+id/menu_action_show"
android:layout_width="wrap_content"
android:icon="#drawable/youricon"
android:title="#string/menu_name"
app:showAsAction="always"/>
You can also access all resources using java code with context :
String menu_name=getString(R.id.menu_name);
I have several questions with random topic . First, I 'm trying to make an app that take a random picture of an array and show it on a image view . What I want to do next, and i cant, is that set visible a button according to the displayed image appears. How can i do?
My code:
public class MainActivity extends Activity {
Button b1, b2,b3,b4,b5;
ImageView iv1,iv2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1=(ImageView)findViewById(R.id.iv1);
iv2=(ImageView)findViewById(R.id.iv2);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);
b3=(Button)findViewById(R.id.b3);
b4=(Button)findViewById(R.id.b4);
b5=(Button)findViewById(R.id.b5);
}
#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);
}
public void Accion1 (View view){
final TypedArray imgs = getResources().obtainTypedArray(R.array.RandomImages);
final Random rand = new Random();
final int rndInt = rand.nextInt(imgs.length());
final int rID = imgs.getResourceId(rndInt, 0);
iv2.setImageResource(rID);
b1.setVisibility(View.INVISIBLE);
b2.setVisibility(View.VISIBLE);
}
public void Accion2(View view){
b2.setVisibility(View.INVISIBLE);
switch (iv2.getId()){
case 1:
iv2.setImageResource(R.drawable.imagen1);
b3.setVisibility(View.VISIBLE);
break;
case 2:
iv2.setImageResource(R.drawable.imagen2);
b4.setVisibility(View.VISIBLE);
break;
case 3:
iv2.setImageResource(R.drawable.imagen3);
b5.setVisibility(View.VISIBLE);
break;
}
}
}
My other two questions are, it is possible to have 3 array , each with pictures, and do a random? first make a random array with those 3 and then another to take a picture of that array.
The other question is, it is possible to make an image has more or less probabilities to appear in a random?
I'm new to Android Development, and I'm creating a simple app to train.
In this app, I would like to have a button in the Action Bar Menu that, if pressed, would change Action Bar color. I somewhat managed to achieve this in my main activity with a static variable and the switchActionBarColor() method listed above. But my problem is when I go to a new activity : the action bar changes color and I did not press the menu button. And when I go back to my main Activity, it is not coloured anymore. I would like the Action Bar style to persist through the application, and I feel my method is not suited at all. What would be the best way to do this, according to you ? Thanks
public class MainActivity extends Activity {
protected static int color = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the add_contact button */
public void addContact(View view) {
Intent intent = new Intent(this, AddContactActivity.class);
startActivity(intent);
}
#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_activity_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.change_color:
switchActionBarColor();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void switchActionBarColor() {
ActionBar bar = getActionBar();
if (bar == null)
return ;
switch (color) {
case 0:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_1)));
break ;
case 1:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_2)));
break ;
case 2:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_3)));
break ;
case 3:
bar.setBackgroundDrawable(new ColorDrawable(getResources()
.getColor(R.color.actionbar_background_default)));
break ;
}
color++;
if (color == 4)
color = 0;
return ;
}
}
You can use SharedPreferences (https://developer.android.com/reference/android/content/SharedPreferences.html) to save and load the indicator for the color and set the color of the ActionBar in onResume of the Activity.
I spent a week trying to make this, but with no result.
I want to make something like what's in the image.
I want to make table of buttons (the user chooses 5x5, 10x10, or
something else)
The table should be centered horizontally and vertically
Each button in the table cell should be sized relatively to the
device screen
Each button should be a rectangle.
EDIT:
Here my code:
public class MainActivity extends ActionBarActivity {
private int game_counter=1;
private int nof_columns = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GridView table_layout = new GridView(this);
table_layout.setNumColumns(this.nof_columns);
int button_size=5;
for(int rows=1; rows<=this.nof_columns; rows++) {
TableRow rowT = new TableRow(this);
for(int columns=1; columns<=this.nof_columns; columns++){
Button bn = new Button(this);
bn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onclick_function(v);
}
});
bn.setId(100 + rows * this.nof_columns + columns);
bn.setWidth(button_size);
bn.setHeight(button_size);
bn.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
MyTags tag = new MyTags();
bn.setTag(tag);
rowT.addView(bn);
}
//rowT.setPadding(-5,-5,-5,-5);
rowT.setMinimumHeight(button_size);
rowT.setMinimumWidth(button_size);
table_layout.addView(rowT);
//table_layout.setPadding(-70,-70,-70,-70);
}
setContentView(table_layout);
}
#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);
}
}
I found the solution.
It's mentioned in Defining a percentage width for a LinearLayout?
Later I'll post here my code with the board.
Thanks!
I found a code from here
it is like
public class MainActivity extends ListActivity implements
SwipeActionAdapter.SwipeActionListener
{
protected SwipeActionAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter<String> stringAdapter = new ArrayAdapter<String>(
this,
R.layout.row_bg,
R.id.text,
new ArrayList<String>(Arrays.asList(content))
);
mAdapter = new SwipeActionAdapter(stringAdapter);
mAdapter.setSwipeActionListener(this)
.setListView(getListView());
setListAdapter(mAdapter);
mAdapter.addBackground(SwipeDirections.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirections.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
.addBackground(SwipeDirections.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
.addBackground(SwipeDirections.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);
}
#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);
}
#Override
protected void onListItemClick(ListView listView, View view, int position, long id){
Toast.makeText(
this,
"Clicked "+mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
}
#Override
public boolean hasActions(int position){
return true;
}
#Override
public boolean shouldDismiss(int position, int direction){
return direction == SwipeDirections.DIRECTION_NORMAL_LEFT;
}
#Override
public void onSwipe(int[] positionList, int[] directionList){
for(int i=0;i<positionList.length;i++) {
int direction = directionList[i];
int position = positionList[i];
String dir = "";
switch (direction) {
case SwipeDirections.DIRECTION_FAR_LEFT:
dir = "Far left";
break;
case SwipeDirections.DIRECTION_NORMAL_LEFT:
dir = "Left";
break;
case SwipeDirections.DIRECTION_FAR_RIGHT:
dir = "Far right";
break;
case SwipeDirections.DIRECTION_NORMAL_RIGHT:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Test Dialog").setMessage("You swiped right").create().show();
dir = "Right";
break;
}
Toast.makeText(
this,
dir + " swipe Action triggered on " + mAdapter.getItem(position),
Toast.LENGTH_SHORT
).show();
mAdapter.notifyDataSetChanged();
}
}
}
I want to remove the string section and want to display apps.
I want to use SwipeActionAdapter to replace listview from my app which i used earlier to show app packages but unable to understand how to do that with this..
Go a bit easy on me if this is a very noob-ish question. I m a begginer.
I think you are misunderstanding the purpose of the Adapter.
The Adapter is the component that feeds the data into your ListView.
The SwipeActionAdapter is meant to wrap around your existing implementation of Adapter and provide callbacks that you can use to perform actions when someone swipes an item in your ListView.
You should probably go through a bit more tutorials. I can greatly recommend the Busy Coders guide to Android development by Commonsware.
I'm certain you'll find what you need there.