Change height of image button through code in android Studio - android

I am working in Android studio and using TableLayout. i want to display small imageButtons. and height of each button can be changed programmatically. I tried my best. but does not change the height of imageButton, Please anyone help me, what changes can i make in this code. thanks
public void settable() {
index = 0;
LinearLayout le = (LinearLayout) findViewById(R.id.onofffbutton);
float height = le.getHeight();
height= height/9.0f;
for (int a = 0; a < maxrow; a++) {
TableRow row = new TableRow(this);
for (int b = 0; b < col; b++) {
location[a][b] = new ImageView(this);
/*LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
location[a][b].setLayoutParams(layoutParams);*/
if (isOn[a][b]) {
location[a][b].setBackgroundResource(R.drawable.box1);
} else {
location[a][b].setBackgroundResource(R.drawable.box);
}
location[a][b].setMaxHeight(height);
location[a][b].setOnClickListener(Onclick);
location[a][b].setId(index++);
row.addView(location[a][b]);
}
tableLayout.addView(row, a);
}
}

Related

How to get the ImageView that was being click by user inside an Array?

in Android Studio I developed a view that consist of 15 TableRows, and each TableRow contains 15 ImageViews. (Each ImageView store a Bitmap with square shape, and this produce a 15 x 15 square matrix). Relevant coding of View onCreate roughly as below:
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
TableRow[] tr = new TableRow[15];
for (int i = 0; i < 15; i++) {
tr[i] = new TableRow(this);
for (int j = 0; j < 15; j++) {
bmp = BitmapFactory.decodeResource(getResources(),R.drawable.jmpty);
imageViews[j] = new ImageView(this);
imageViews[j].setImageBitmap(bmp);
imageViews[j].setOnClickListener(new doSomething());
tr[i].addView(imageViews[j],oParams);
}
tableLayout.addView(tr[i]);
}
....
My question is how do I know which imageView (Total = 15 x 15 = 225) was being clicked by user by utilizing OnClick event? i.e. If the user click the ImageView located at (imageViews[13] located at TableRow=4), can I return capture this x-y coordinate information (e.g. x=13, y=4) and return to other parts of program?
private class doSomething implements View.OnClickListener {
#Override
public void onClick(View v) {
ImageView iv = (ImageView) v;
......
// what I want:
int x = iv.GetImageViewNumber(); // how can I do this???
int y = iv.GetTableRowNumber(); // how can I do this???
Or is there any other better alternative way of doing this?
You can create a class who inherits from ImageView, and inside this class create 2 attributes int x and int y, So when onClickListener is called you should cast the View to your custom class.
Example:
public calss MyCustomImageView extends ImageView{
public int x;
public int y;
}
TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
TableRow[] tr = new TableRow[15];
for (int i = 0; i < 15; i++) {
tr[i] = new TableRow(this);
for (int j = 0; j < 15; j++) {
bmp = BitmapFactory.decodeResource(getResources(),R.drawable.jmpty);
imageViews[j] = new MyCustomImageView(this);
imageViews[j].setImageBitmap(bmp);
imageViews[j].setOnClickListener(new doSomething());
imageViews[j].x = i;
imageViews[j].y = j;
tr[i].addView(imageViews[j],oParams);
}
tableLayout.addView(tr[i]);
}
#Override
public void onClick(View v) {
MyCustomImageView iv = (MyCustomImageView) v;
int x = iv.x;
int y = iv.y;
}
I hope it will help you, sorry for my English I'm beginner!
to know which imageview has been clicked, you need to identify that view by its ID or Tag. You need to set it in your for loop and then get it when u want to identify in onClick method.
String tagString = "Image";
for (int i = 0; i < 15; i++) {
tr[i] = new TableRow(this);
for (int j = 0; j < 15; j++) {
bmp = BitmapFactory.decodeResource(getResources(),R.drawable.jmpty);
imageViews[j] = new ImageView(this);
imageViews[j].setImageBitmap(bmp);
//setting image tag for each image in "Image1", "Image2"... format
imageViews[j].setTag(tagString+j);
imageViews[j].setOnClickListener(new doSomething());
tr[i].addView(imageViews[j],oParams);
}
tableLayout.addView(tr[i]);
}
Identify the image in your onClick method
#Override
public void onClick(View v) {
Toast.makeText(this, "Clicked items is "+v.getTag().toString(), Toast.LENGTH_SHORT).show();;
}

Programmatically generated TextViews print on each other

I'm generating TextViews in a loop and add them to a Relative layout like this :
RelativeLayout relativeLayout2 = (RelativeLayout) findViewById(R.id.rl_root);
for(int i = 0; i < 5; i++)
{
TextView textvw = new TextView(this);
textvw.setText(Integer.toString(i));
relativeLayout2.addView(textvw);
for(int j = 0; j < 10; j++)
{
TextView textvw2 = new TextView(this);
textvw2 .setText(Integer.toString(j+5));
relativeLayout2.addView(textvw2);
}
}
When I do this, all textViews print on the same place. I want them to appear below each other. I mean first one will be on the top, secone one below the top and like that. How can I do that? Thanks.
Use Linear layout instead of Relative layout and set the orientation to vertical if you want it one below the other.
LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.rl_root);
for(int i = 0; i < 5; i++) { TextView textvw = new TextView(this);
textvw.setText(Integer.toString(i));
relativeLayout2.addView(textvw); for(int j = 0; j < 10; j++) { TextView textvw2 = new TextView(this);
textvw2 .setText(Integer.toString(j+5));
linearLayout2.addView(textvw2); } }

Android GridLayout only shows last child

I am new to Android Development. I have been working with using a GridLayout to display Dynamically inserted ImageViews.
My issue is located in "onFocusWindowChanged" but I pasted my onCreate where I do my assignments of the images.
private List<Behavior> behaviors = null;
private static int NUM_OF_COLUMNS = 2;
private List<ImageView> images;
private GridLayout grid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_behaviors);
XMLPullParserHandler parser = new XMLPullParserHandler();
try {
behaviors = parser.parse(getAssets().open("catagories.xml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
grid = (GridLayout) findViewById(R.id.behaviorGrid);
images = new ArrayList<ImageView>();
grid.setColumnCount(NUM_OF_COLUMNS);
grid.setRowCount(behaviors.size() / NUM_OF_COLUMNS);
for (Behavior behavior : behaviors)
images.add(this.getImageViewFromName(behavior.getName()));
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
View view = (View) findViewById(R.id.scrollView);
int width = (int) (view.getWidth() * .45);
Log.i("ViewWidth", Integer.toString(width));
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.height = width;
lp.width = width;
int childCount = images.size();
ImageView image;
for (int i = 0; i < childCount-1; i++) {
image = images.get(i);
image.setLayoutParams(lp);
grid.addView(image);
}
}
In my (short) previous experience, using
grid.add(View);
worked fine, but now I am only seeing the last child display only. Looking through the debugger I can see that the gridview is being populated with more than just the last element, as well as the last imageview.
Thank you for your help
you should create a GridLayout.LayoutParams for each ImageView:
for (int i = 0; i < childCount-1; i++) {
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
lp.height = width;
lp.width = width;
......
}
GridLayout.LayoutParams contains location information, e.g [column:2, row:3]. In your code, all ImageViews are set the same GridLayout.LayoutParams, so they are located in the same cell(overlapping each other).
When use LinearLayout.LayoutParams instead, there is no location information in it. GridLayout will create a new GridLayout.LayoutParams for each child view, so all ImageViews use their own different GridLayout.LayoutParams and location.
Wish this help. You can read the GridLayout.java and ViewGroup.java for more details.
So I solved my issue, although I'm not sure how-
GridLayout.LayoutParams lp = new GridLayout.LayoutParams();
changed to...
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(x,y);
made it work just as I wanted it. But I'm not sure why- If anyone could explain, please do :)

ImageView pushed out from screen

I'm playing around in Android and what I'm trying to achieve is a 10x10tile boardgame.
I want to read size and width of screen and then I want a square in the middle with a textViews above and below.
This is what I've done so far:
public void init(){
Point size = new Point();
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
w.getDefaultDisplay().getSize(size);
screenWidth = size.x;
screenHeight = size.y;
}else{
Display d = w.getDefaultDisplay();
screenWidth = d.getWidth();
screenHeight = d.getHeight();
}
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.LinearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView TVtop = new TextView(this.getApplicationContext());
TVtop.setHeight((screenHeight-screenWidth)/2);
TVtop.setWidth(screenWidth);
TVtop.setText("TOP");
TextView TVbot = new TextView(this.getApplicationContext());
TVbot.setHeight((screenHeight-screenWidth)/2);
TVbot.setWidth(screenWidth);
TVbot.setText("BOT");
TableLayout tableLayout = new TableLayout(this.getApplicationContext());
//Make a cube
LayoutParams tableParams = new LayoutParams(screenWidth, screenWidth);
tableLayout.setLayoutParams(tableParams);
//Populate tableLayout
for(int i = 0; i < nrOfTiles; i++){
TableRow tableRow = new TableRow(this.getApplicationContext());
for(int j = 0; j < nrOfTiles; j++){
ImageView imgView = new ImageView(this.getApplicationContext());
imgView.setImageResource(R.drawable.cell);
tableRow.addView(imgView);
}
tableLayout.addView(tableRow);
}
linearLayout.addView(TVtop);
linearLayout.addView(tableLayout);
linearLayout.addView(TVbot);
}
}
I've tried diffrent layoutparams but nothing seems to get the work done. :S
BR
Personally, I would use a RelativeLayout and setMargins for the items.
While not a direct answer to your question, the following code will display 15 icons in three rows. This should be enough to explain and get you started.
The main activity.
public class MainActivity extends Activity {
private int mWidth;
private int mTile;
private int mColMax = 5;
private Context mContext;
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
// the screen width is need to work out the tile size
WindowManager w = getWindowManager();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Point size = new Point();
w.getDefaultDisplay().getSize(size);
mWidth = size.x;
}else{
mWidth = w.getDefaultDisplay().getWidth();
}
// how wide (and high) each icon will be to fit the screen.
mTile = (mWidth / mColMax);
setContentView(R.layout.activity_main);
// layout the icons
initUI();
}
/**
* Layout 15 icon images in three rows dynamically.
*/
private void initUI() {
// this is the layout from the XML
ViewGroup layout = (ViewGroup) findViewById(R.id.main_layout);
ImageView iv;
RelativeLayout.LayoutParams params;
int i = 0;
int row = 0;
int col = 0;
do {
params = new RelativeLayout.LayoutParams(mTile,mTile);
params.setMargins((col * mTile), (row * mTile), 0, 0);
iv = new ImageView(mContext);
iv.setAdjustViewBounds(true);
iv.setScaleType(ScaleType.FIT_CENTER);
iv.setImageResource(R.drawable.ic_launcher);
iv.setLayoutParams(params);
layout.addView(iv);
if (col == mColMax) {
row++;
col = 0;
} else {
col++;
}
} while (++i <= 16);
}
}
And the layout XML.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>

Dynamic TextView in Relative layout

I am triying to use dynamic layout for comment part of my project but when i settext of textview dynamicly the output only appears in top of the screen. And it puts the output over the other outputs
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++) {
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
ll.addView(cb);
}
So how can i put the output on the bottom of the screen linearly.
You should use LinearLayout to automatically add one TextView after another.
Assuming you can't live without RelativeLayout, you'll need to dynamically generate ids for all TextView you create in order to put one view under another. Here is example:
public class HelloWorld extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
Random rnd = new Random();
int prevTextViewId = 0;
for(int i = 0; i < 10; i++)
{
final TextView textView = new TextView(this);
textView.setText("Text "+i);
textView.setTextColor(rnd.nextInt() | 0xff000000);
int curTextViewId = prevTextViewId + 1;
textView.setId(curTextViewId);
final RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, prevTextViewId);
textView.setLayoutParams(params);
prevTextViewId = curTextViewId;
layout.addView(textView, params);
}
}
}
You've to provide the location of your newly added view. As #Adinia said, with no position, it will be aligned to the top by default. So you can use the following code to do it with RelativeLayout;
RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container);
for (int i = 0; i < 20; i++) {
TextView dynaText = new TextView(this);
dynaText.setText("Some text " + i);
dynaText.setTextSize(30);
// Set the location of your textView.
dynaText.setPadding(0, (i * 30), 0, 0);
containerLayout.addView(dynaText);
}
If you want to show multiple textviews one after the other, then you should go with LinearLayout.
You may also add Dynamic textview to relative layout. Here with i have attached some code this may help you.
RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl);
for(int i = 0; i < 20; i++)
{
TextView cb = new TextView(this);
cb.setText("YORUMLAR"+yorum[0]+i);
cb.setTextSize(30);
cb.setId(2000+i);
RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100);
if (i != 0 )DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1));
ll.addView(cb,TextViewLayoutParams);
}

Categories

Resources