Android layouts: create board, centered and automatic sized - android

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!

Related

Random image, array and probability android studio

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?

Continue countdown

i want that my countdown continue when i completely close the app but i dont know how to do it. Someone can explain it to me or pass me some link where explain it? Thank
The code for the countdown
public class MainActivity extends Activity {
Button b1;
TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
b1 = (Button) findViewById(R.id.b1);
}
#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 a(View view){
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
tv1.setText("La cuenta llega a 0 en: " + millisUntilFinished / 1000);
}
public void onFinish() {
tv1.setText("Listo!");
}
}.start();
}
}
You need to do following steps :
At the exit point of your app(either it is onBackPress of Home Activity or Ok click of the dialog that ask for exit) find the value of System.currentTimeMillis() and save it to SharedPrefences.
Now on second launch(in onCreate()) just find again value of System.currentTimeMillis(), then get the data saved in SharedPrefences and get the diff. of both values.
Now the diff is the time period that your app was shut down, hope you are looking for the same data.

Extend Navigation Bar into another activity (no fragments)

I read some question regarding this but all the answers are about fragments and there is question similar to this one but the answer is incomplete, I want to reuse a set of layout or codes into multiple activities, i created a baseActivity that extends into Activity with the code below.
I also read that you need to put the code in the onCreateOptionMenu but it is still not working. (the code in baseacitivty xml is working, and homepage xml is working but does not show the navigation_layout)
public class BaseActivity extends Activity {
private ImageButton ibButtonHome;
private ImageButton ibButtonFavorite;
private ImageButton ibButtonRandomize;
private ImageButton ibButtonHistory;
private ImageButton ibButtonLogOut;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_layout);
}
View.OnClickListener Navigation = new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
if (v.equals(ibButtonHome)) {
i.setClass(getBaseContext(), HomePage.class);
} else if (v.equals(ibButtonFavorite)) {
i.setClass(getBaseContext(), Favorite.class);
} else if (v.equals(ibButtonHome)) {
i.setClass(getBaseContext(), HomePage.class);
} else if (v.equals(ibButtonRandomize)) {
i.setClass(getBaseContext(), Randomize.class);
} else if (v.equals(ibButtonHistory)) {
i.setClass(getBaseContext(), History.class);
} else if (v.equals(ibButtonLogOut)) {
//TODO: something code here to not crash on activity exit??
i.setClass(getBaseContext(), MainActivity.class);
}
startActivity(i);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
ibButtonHome = (ImageButton) findViewById(R.id.button_Home);
ibButtonFavorite = (ImageButton) findViewById(R.id.button_favorites);
ibButtonRandomize = (ImageButton) findViewById(R.id.button_randomize);
ibButtonHistory = (ImageButton) findViewById(R.id.button_history);
ibButtonLogOut = (ImageButton) findViewById(R.id.button_logout);
ibButtonFavorite.setOnClickListener(Navigation);
ibButtonRandomize.setOnClickListener(Navigation);
ibButtonHome.setOnClickListener(Navigation);
ibButtonHistory.setOnClickListener(Navigation);
ibButtonLogOut.setOnClickListener(Navigation);
getMenuInflater().inflate(R.menu.menu_filter_menus, 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);
}
Homepage activity
public class HomePage extends BaseActivity {
private CustomAdpaterFoodFeed ExpAdapter;
private ArrayList<FoodFeed> foodFeeds;
private ExpandableListView ExpandList;
//Onclick listener for the Navigation Bar
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
ExpandList = (ExpandableListView) findViewById(R.id.evFoodFeed);
//runs the function and returns the data to foodFeeds
foodFeeds = SetStandardGroups();
//Adapter for ExapadableListView
ExpAdapter = new CustomAdpaterFoodFeed(HomePage.this, foodFeeds);
ExpandList.setAdapter(ExpAdapter);
}
// Dummy data method for pictures and comments
public ArrayList<FoodFeed> SetStandardGroups() {
String names[] = {"Geraldine", "Marielle", "Gina", "Bryan",
"Pat", "Eugene", "Shermaine", "Kook"};
String comments[] = {"TasteGood", "Nah", "DONT EAT HERE", "Cameroon",
"Nice place", "chill", "woah Spain", "lalala"};
int Images[] = {R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher,
R.mipmap.ic_launcher, R.mipmap.ic_launcher
};
ArrayList<FoodFeed> list = new ArrayList<FoodFeed>();
ArrayList<Comments> comments_list;
for (int images : Images) {
FoodFeed gru = new FoodFeed();
gru.setIcon(images);
comments_list = new ArrayList<Comments>();
for (int j = 0; j < 4; j++) {
Comments comments1 = new Comments();
comments1.setName(names[j]);
comments1.setComments(comments[j]);
comments_list.add(comments1);
}
gru.setComments(comments_list);
list.add(gru);
}
return list;
}
#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_page, 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);
}
}
Base activity to extend the navigation drawer in other activities you can follow this link , described well. it is well tested i followed the same :)
http://androiddeveloperdemo.blogspot.in/2014/08/android-navigation-drawer-with-multiple.html
You can get the same action bar in other activities by declaring in AndroidManifest.xml like this
<activity
android:name=".SettingsActivity"
android:label="#string/activity_title"
android:theme="#style/AppTheme" />
For different menu options define a xml file under menu folder in android studio and inflate that file in
onCreateOptionsMenu(Menu) overridden method of your activty

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

Categories

Resources