Random image, array and probability android studio - android

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?

Related

App Constant in application

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);

How to get a spinner to display textview

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)

Silly errors (beginner)

I have a first activity ("Home"), with two buttons: one is called About and leads to activity About and the second is named List and leads to the activity List.
Manifest.xml should be fine, but I get a load of tiny petty errors I can't fix up by myself, regrettably.
Home.class is the following
Public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Button AboutButton = (Button)findViewById(R.id.About);
AboutButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view); {
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}
Button ListButton = (Button)findViewById(R.id.List);
ListButton.setOnClickListener(new View.onClickListener());{
#Override
public void onClick(View view); {
Intent openList = new Intent(Home.this, List.class);
startActivity(openList);
}
}
}
#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_home, 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);
}
}
while About.class is like this
public class About extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
public void onClick(View view); {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#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_about, 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);
}
}
and List is like this:
public class List extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
}
}
#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_list, 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 get lot of red light bulbs, saying, for instance that ")" or ";" is expected or (worse) onClickListener cannot be resolved
Last but not least: I copied this code online and I was wondering why after "View" there is a "view"; what does it mean?
I copied your code and I see fails everywhere... let me explain you what's going on ...
HOME CLASS
1.- You have to remove the ")"
2.- You don't have to ";" when you call onClick()
3.- When you are don on your onClick() NOW you have to close it, you missed the ");"
AboutButton.setOnClickListener(new View.OnClickListener()){ //<-- Just remove one
#Override
public void onClick(View view); { //<-- Remove this ";"
Intent openAbout = new Intent(Home.this, About.class);
startActivity(openAbout);
}
}//Here goes ");"
4.- The ListButton has the same issues so just fix it as you will fix the first one.
ABOUT CLASS
1.-On this case you have the setOnClickListener() ok, BUT why you have two onClick(View view)? It's not necessary just remove one of them.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
Button ReturnButton = (Button) findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent returnhome = new Intent(About.this, Home.class);
startActivity(returnhome);
}
});
}
LIST CLASS
1.-Well in your List class you have made the same error as the first one... Your onClickListener() it's wrong.
2.-Once again you included an unnecessary ")" on new View.OnClickListener() just remove it,
3.-Another fail that I'm seeing is that you are trying to make an Intent but you are refering that you are on About.this and you are NOT. You are on List class so you have ot put List.this because the first parameter refers :
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
More information about Intents
4.- You need to close again the setOnClickListener()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Button ReturnButton = (Button)findViewById(R.id.Return);
ReturnButton.setOnClickListener(new View.OnClickListener()){ //<-- remove one ")"
#Override
public void onClick(View view) {
Intent returnhome = new Intent(About.this, Home.class); //<-- Remove About.this and put List.this
startActivity(returnhome);
}
}//<-- Close the setOnClickListener() with ");"
}
It's okay guy, this is your first question and I'll answer it, but NOW as I've made the favor to take my time and explain to you what was wrong on your code take your time to :
How do I as a question on StackOverflow
Learn some Android basics
And the most IMPORTANT THING
DO NOT COPY PASTE AN INTERNET CODE if you don't know the basics, I mean you can copy paste the code, but you'll face with this problem every time you do this, so first of all read the tutorial, make an examples, and you'll improve every day.

how to get my android USSD program to work

am building a simple dialler to help me check my account balance but for some unknown reasons am getting some errors, i have a button on my xml which i have set its onclick element to sendMessage1 , but am getting error on my code with the phoneNum[1] telling me cannot reslove symbol phoneNum[1]. this is my code
/**Called when the user clicks the Send button */
public void sendMessage1(View view){
//example phoneNum[1] = "*556";
String encodedHarsh = Uri.encode("#");
startActivity(new Intent
("android.intent.action.DIAL",
Uri.parse("tel:"+ phoneNum[1]+ encodedHarsh)));
//Do something in response to button
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mtn);
}
#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_mtn, 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);
}
You have to do it doing this :
String encodedHarsh = "*" + "556" + Uri.encode("#");
startActivity(new Intent("android.intent.action.CALL", Uri.parse("tel:" + encodedHarsh)));
Note, don't forget to add uses-permisions on manifest

Android layouts: create board, centered and automatic sized

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!

Categories

Resources