Android: layout created programmatically doesnt appear? - android

I think this question has been asked alot but I havent been able to find an answer through all similar posts. There's no error, but when the app runs nothing appears. So here is my code in my activity.
public class MainGameActivity extends AppCompatActivity {
int width = 2;
int height = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
RelativeLayout mainLayout = (RelativeLayout) View.inflate(this, R.layout.activity_main_game, null);
TableLayout tableLayout = new TableLayout(this);
tableLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
Table gameTable = new Table(width, height);
ArrayList<TableRow> tableRowArray = new ArrayList<TableRow>();
for (int i = 0; i < height; i++) {
tableRowArray.add(new TableRow(this));
tableRowArray.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < width; j++) {
Button button = new Button(this);
button.setText(i + "." + j);
button.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRowArray.get(i).addView(button);
}
tableLayout.addView(tableRowArray.get(i));
}
mainLayout.addView(tableLayout);
setContentView(mainLayout);
}
}
and here is my XML, there's basically nothing:
<?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:id="#+id/activity_main_game"
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.khangbuin.flyingdiscscanner.MainGameActivity">
</RelativeLayout>
Thank you in advance for answering.

Related

Graphical representation of the current value into the total value

Suppose I have a total value of 2000, while my current value is 180. I would like to present it graphically as follows:
How could I do that? I was looking for many charts, but I did not find such a solution anywhere.
Is there any open source that provides such graphical representation of values?
You can use TableLayout to achieve this.
Your layout will look like this:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
Then in your code you could populate your table like so:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int totalValue = 2000;
int currentValue = 180;
int rowsNumber = 10;
int columnsNumber = 10;
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
for (int i = 0; i < rowsNumber ; i++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
tableRow.setBackgroundResource(R.drawable.yourRowDrawable);
for (int j = 0; j < columnsNumber; j++) {
ImageView imageView = new ImageView(this);
imageView.setImageResource(this.getCellDrawableId(i,j,totalValue ,currentValue ));
tableRow.addView(imageView, j);
}
tableLayout.addView(tableRow, i);
}
}
}
private int getCellDrawableId(int i, int j,int totalValue ,int currentValue ){
if(/*some logic here*/)
return R.drawable.greenCell;
return R.drawable.emptyGrayCell;
}
P.S.: Where R.drawable.greenCell, R.drawable.emptyGrayCell, R.drawable.yourRowDrawable are appropriate drawable to represent your grid.
Also I did not test this code, only writeen it here like snippet, so some errors may be present here

Android : Locating the item at center position of the screen after selecting it ,In the horizontalscrollview

I am trying to scroll item to the middle position of the screen after selecting it.I have used Horizontalscrollview and LinearLayout inside it to add item. I show in the XML
XML
<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:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/updown"
android:layout_weight="0.8"
android:fadingEdgeLength="0dp" >
<LinearLayout
android:id="#+id/horizontalbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/tlbackground"
android:baselineAligned="true"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
Class
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
LinearLayout l1 = (LinearLayout)findViewById(R.id.horizontalbar);
for(int i = 0; i < 20; i++)
{
Button b = new Button(this);
b.setText("t"+i);
ll.addView(b);
}
}
}
I want something like in figure
before click
after click
How can I Scroll that selected item to the middle in HorizontalScrollview?
After long try , I have got it with this
for (int i = 0; i < 20; i++) {
final Button b = new Button(this);
b.setText("t" + i);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int scrollX = (b.getLeft() - (screenWidth / 2)) + (b.getWidth() / 2);
hsl.smoothScrollTo(scrollX, 0);
}
});
ll.addView(b);
}
This is how your onCreate should look like:
hsl = (HorizontalScrollView) findViewById(R.id.scrollView);
LinearLayout ll = (LinearLayout) findViewById(R.id.horizontalbar);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
final int width = size.x;
for (int i = 0; i < 20; i++) {
final Button b = new Button(this);
b.setText("t" + i);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int center = (width - b.getWidth())/2;
hsl.scrollTo(b.getLeft() - center, b.getTop());
}
});
ll.addView(b);
}
The only thing i didn't discover is why i need to substract additional 200px (in my case). Probably some padding or something else. Instead of 200, you could discover how many dips is it and call function:
public static int convertDipToPixels(Context c, float dips) {
return (int) (dips * c.getResources().getDisplayMetrics().density + 0.5f);
}

Creating Multiple ImageButtons in a Single Activity Programatically

I have a single image myimage.png which is placed in my res-drawable folder.
I have to create 50 ImageButtons in an activity all using this same image.
And then when a user clicks i need to pop out a toast saying button number i was clicked.
Here's what I have done:
public class AllImageButtons extends Activity {
int screendimesionx;
int screendimesiony;
ImageButton imageButton;
ImageButton allImageButtons[] = new ImageButton[50];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.allbuttonimages);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
screendimesiony = metrics.heightPixels;
screendimesionx = metrics.widthPixels;
createButtonsAndAddListener();
}
public void createButtonsAndAddListener() {
for (int i = 0; i < 50; i++) {
imageButton = (ImageButton) findViewById(R.id.myimage);
float buttonimagey = imageButton.getDrawable().getBounds().height();
float buttonimagex = imageButton.getDrawable().getBounds().width();
float xspaceforeachbuttonimage = screendimesionx/50;
LayoutParams par = (LayoutParams)imageButton.getLayoutParams();
par.leftMargin = (int) (i*xspaceforeachbuttonimage);
par.topMargin = 0;
imageButton.setLayoutParams(par);
allImageButtons[i] = imageButton;
allImageButtons[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(AllbuttonimagesForSelectionActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT)
.show();
}
});
}
}}
and then there is the associated xml file allbuttonimages.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/myimage" />
</LinearLayout>
This xml layout displays only the last ImageButton from the loop.
My Question:
How can i dynamically create all 50 ImageButtons from the same image in a single view ?
You are not adding the buttons to the parent .
use addView(yourImageButton) to add your image button to activity
The button that is visible now is the one in xml
You should create new imageButton using new
eg: ImageButton imgBtn = new ImageButton(this)
Then set the properties and addView to your parent Layout
You will not get 50 buttons with the one defined in xml
You have one button in layout and you are calling findViewById for that.
You need to create new buttons each time and add it to the linearlayout.
public void createButtonsAndAddListener() {
for (int i = 0; i < 50; i++) {
imageButton = new ImageButton(this);
imageButton.setImageResource(R.drawable.myimage);
float buttonimagey = imageButton.getDrawable().getBounds().height();
float buttonimagex = imageButton.getDrawable().getBounds().width();
float xspaceforeachbuttonimage = screendimesionx/50;
LayoutParams par = (LayoutParams)imageButton.getLayoutParams();
par.leftMargin = (int) (i*xspaceforeachbuttonimage);
par.topMargin = 0;
imageButton.setLayoutParams(par);
allImageButtons[i] = imageButton;
allImageButtons[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(AllbuttonimagesForSelectionActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT)
.show();
}
});
((LinearLayout)findViewById(R.id.ll)).addView(imageButton);
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:id="#+id/ll">
</LinearLayout>
Your code wont add images to the layout. Get the parent layout in code and add images to it using addView. Between you do not need the image button in the xml. You can do some thing like this:
LinearLayout parent = (LinearLayout)findViewById(R.id.the_parent_layout);
for(int i = 0; i< 50; i++){
ImageButton image = new ImageButton(context);
//set whatever properties you want
//then add to the parent
parent.addView(image); // you can also specify the layout params here
}
Try This....
for (int i = 0; i < 50; i++) {
ImageButton b1 = new ImageButton(myrefmenu);
b1.setId(100 + i);
b1.setImageResource(R.drawable.imagename);
// b1.setText(adapt_objmenu.city_name_array[i]);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if (i > 0) {
lp.addRule(RelativeLayout.RIGHT_OF, b1.getId() - 1);
}
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(AllbuttonimagesForSelectionActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT)
.show();
}
});
b1.setLayoutParams(lp);
relative.addView(b1);
}

Add a dynamic tablelayout in a relativelayout above a existing button

i'm trying to add a tablelayout in a relativelayout, but i'm havind some problems to put this tablelayout above a existing button, i can only but it below it.
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:stretchColumns="0,1"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Button" />
the lp.addRule(RelativeLayout.ABOVE, idx); isn't working
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the size of the screen to set the size of the buttons according
// to the level
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int level = 3;
int width = size.x - (size.x / level);
int btsize = width / level;
// Get the main Layout
RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.main);
//
TableLayout tLayout = new TableLayout(this);
int id = 0;
for (int i = 0; i < level; i++) {
// Create the TableRow
TableRow tRow = new TableRow(this);
for (int j = 0; j < level; j++) {
id++;
Button bt = new Button(this);
bt.setId(id);
bt.setWidth(btsize);
bt.setHeight(btsize);
bt.setMinimumWidth(0);
bt.setMinimumHeight(0);
tRow.addView(bt); // add button into the row
}
tLayout.addView(tRow);// add row into the table
}
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int idx = findViewById(R.id.button1).getId();
lp.addRule(RelativeLayout.ABOVE, idx);
rLayout.addView(tLayout,lp);// add table into the relativelayout
}
you can try this...
put this code in your oncreate() method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relay = (RelativeLayout)findViewById(R.id.rlayout);
LayoutParams layoutprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
TableLayout table=new TableLayout(this);
relay.addView(table);

Android RelativeLayout problem

I have some problem with RelativeLayout positioning:
Now I have this: http://dl.dropbox.com/u/18411570/now.png
And I would like this: http://dl.dropbox.com/u/18411570/expectation.png
Here's my code:
public class ActivityTwo extends Activity {
RelativeLayout myRelativeLayout = null;
ArrayList<ArrayList<EditText>> spreadsheet = new ArrayList<ArrayList<EditText>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.relativesprsheet);
myRelativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout);
//FIRSTROW
ArrayList<EditText> firstRow = new ArrayList<EditText>();
char columnTitle = 'A';
int prev_id = 0;
for(int i = 0; i < 5; ++i) {
EditText eText = new EditText(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
eText.setWidth(100);
eText.setId(getUniqueCellId(columnTitle,i + 1));
if(i==0) {
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,eText.getId());
eText.setText("A1");
}
else {
params.addRule(RelativeLayout.RIGHT_OF,prev_id);
eText.setText(Character.toString(columnTitle)+Integer.toString(1));
}
prev_id = getUniqueCellId(columnTitle,i + 1);
firstRow.add(eText);
myRelativeLayout.addView(eText,params);
columnTitle++;
}
spreadsheet.add(firstRow);
//SECONDROW
ArrayList<EditText> second_row = new ArrayList<EditText>();
EditText a2 = new EditText(this);
a2.setId(getUniqueCellId('A',2));
a2.setWidth(100);
a2.setText("A2");
RelativeLayout.LayoutParams a2params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a2params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 1));
myRelativeLayout.addView(a2, a2params);
second_row.add(a2);
EditText bigView = new EditText(this);
bigView.setId(getUniqueCellId('B', 2));
bigView.setWidth(300);
bigView.setText("B2");
RelativeLayout.LayoutParams b2params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
b2params.addRule(RelativeLayout.RIGHT_OF, getUniqueCellId('A', 2));
b2params.addRule(RelativeLayout.ALIGN_TOP,getUniqueCellId('A', 2));
b2params.addRule(RelativeLayout.ALIGN_BOTTOM,getUniqueCellId('A', 4));
b2params.addRule(RelativeLayout.ALIGN_RIGHT,getUniqueCellId('D',5));
myRelativeLayout.addView(bigView, b2params);
second_row.add(bigView);
spreadsheet.add(second_row);
//THIRDROW
ArrayList<EditText> third_row = new ArrayList<EditText>();
EditText a3 = new EditText(this);
a3.setId(getUniqueCellId('A',3));
a3.setWidth(100);
a3.setText("A3");
RelativeLayout.LayoutParams a3params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a3params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 2));
myRelativeLayout.addView(a3, a3params);
third_row.add(a3);
spreadsheet.add(third_row);
//FOURTHROW
ArrayList<EditText> fourth_row = new ArrayList<EditText>();
EditText a4 = new EditText(this);
a4.setId(getUniqueCellId('A',4));
a4.setWidth(100);
a4.setText("A4");
RelativeLayout.LayoutParams a4params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a4params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 3));
myRelativeLayout.addView(a4, a4params);
fourth_row.add(a4);
spreadsheet.add(fourth_row);
//FIFTHROW
ArrayList<EditText> fifth_row = new ArrayList<EditText>();
EditText a5 = new EditText(this);
a5.setId(getUniqueCellId('A',5));
a5.setWidth(100);
a5.setText("A5");
RelativeLayout.LayoutParams a5params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
a5params.addRule(RelativeLayout.BELOW, getUniqueCellId('A', 4));
myRelativeLayout.addView(a5, a5params);
fifth_row.add(a5);
char c2 = 'B';
for(int i = 1; i < 5; ++i) {
EditText temp5Text = new EditText(this);
temp5Text.setId(getUniqueCellId(c2,5));
temp5Text.setWidth(100);
temp5Text.setText(Character.toString(c2)+"5");
RelativeLayout.LayoutParams temp5Textparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
temp5Textparams.addRule(RelativeLayout.RIGHT_OF, getUniqueCellId((char)(c2-1), 5));
temp5Textparams.addRule(RelativeLayout.ALIGN_TOP, getUniqueCellId((char)(c2-1), 5));
myRelativeLayout.addView(temp5Text, temp5Textparams);
fifth_row.add(temp5Text);
c2++;
}
spreadsheet.add(fifth_row);
}
private int getUniqueCellId(char c, int i) {
return Integer.valueOf(c) * 10369 + i;
}
}
And here is the layout xml:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
</ScrollView>
</HorizontalScrollView>
I can't understand, why the first row is broken and the fifth is not, and how could i fix it... please help me if you can, thx!
Edit1: I didn't mention that I would like to do it programmatically Edit2: I changed the links, I hope it will work for everybody
I suggest you use your XML layout file for this. Set up things so they are laid out properly, and then hook up non-layout stuff like click handlers or changing text later.

Categories

Resources