Im trying to set a width and height for my cardView, so once I download an image from the database, a cardView is added by it stretches the image and can't control the height, im trying to do it programatically. This is what i've got.
public void CreateCardView(final Users u) throws IOException
{
CardView.LayoutParams params = new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT,
CardView.LayoutParams.WRAP_CONTENT
);
params.setMargins(10,10,10,20);
params.width = 500;
mLoginFormView = findViewById(R.id.containerGrid);
CardView card = new CardView(this);
card.setLayoutParams(params);
card.setRadius(9);
card.setCardElevation(9);
LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
LinearLayout lin = new LinearLayout(this);
lin.setOrientation(LinearLayout.VERTICAL);
lin.setLayoutParams(par);
final ImageButton Name = new ImageButton(this);
Name.setAdjustViewBounds(true);
Name.setScaleType( ImageView.ScaleType.CENTER);
mLoginFormView.bringToFront();
// Name.setBackground(bdrawable);
Name.setAdjustViewBounds(true);
//Name.setImageBitmap(i.getImage().get(0));
lin.bringToFront();
mLoginFormView.addView(card);
card.setElevation(15);
System.out.println("Hello?");
//Name.setImageBitmap( scaled);
TextView username = new TextView(this);
TextView sport = new TextView(this);
username.setText(u.getUsername());
sport.setText(u.getEmail());
lin.addView(Name);
lin.addView(username);
lin.addView(sport);
card.addView(lin);
card.setElevation(15);
Instead of this code:
CardView.LayoutParams params = new CardView.LayoutParams(
CardView.LayoutParams.WRAP_CONTENT,
CardView.LayoutParams.WRAP_CONTENT
);
params.setMargins(10,10,10,20);
params.width = 500;
Try this code:
card.setLayoutParams(new LayoutParams(width, height));
Related
I have to create Dynamically list of Imageview in a row, There is an other Imageview (dot Drawable) under Each Imageview.
see below pic which is my desired view.
Here is my source.
for (int i = 0; i < 9; i++) {
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView catImg = new ImageView(getApplicationContext());
catImg.setImageResource(bottDrawableArr[i]);
catImg.setId(View.generateViewId());
int dp = (int) utiles.convertDpToPixel(27, getApplicationContext());
params1.height = dp;
params1.width = dp;
ImageView dotImg = new ImageView(getApplicationContext());
dotImg.setImageResource(R.mipmap.dot);
dotImg.setId(View.generateViewId());
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
dockBg.addView(catImg, params1);
dockBg.addView(dotImg, params2);
}
But I am Unable to get my desire view,
while my views show like this
I tried to put dot under each image but unable to achieve that.
The best way is working with some LinearLayout, something like this:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 9; i++) {
LinearLayout layoutChild = new LinearLayout(this);
layoutChild.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
ImageView catImg = new ImageView(getApplicationContext());
catImg.setImageResource(bottDrawableArr[i]);
catImg.setId(View.generateViewId());
int dp = (int) utiles.convertDpToPixel(27, getApplicationContext());
params1.height = dp;
params1.width = dp;
ImageView dotImg = new ImageView(getApplicationContext());
dotImg.setImageResource(R.mipmap.dot);
dotImg.setId(View.generateViewId());
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
layoutChild.addView(catImg, params1);
layoutChild.addView(dotImg, params2);
layout.addView(layoutChild);
}
dockBg.addView(layout);
Note: it's better to delete the unnecessary properties like setting some rules...
params2.addRule(RelativeLayout.BELOW, dotImg.getId());
This line of code looks like you are trying to add a rule to put the dotImg below the dotImg. Replacing that with catImg.getId() should fix your issue.
It might be a good idea to consider a RecyclerView though, Or if it is a static array (always the same items) then you could make a new layout file and just inflate it when you need it.
I would like to have an image on android that appears only when I click a button. So I created it in an onClick method, and set its ressource and position with the following code :
public void createAddButton() {
//create the image
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.add_16);
//setting image position : width and height
imageView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) );
}
But this only allows to define width and height of the image. If I want to define other layout parameters (android:layout_gravity or android:marginLeft for example, what should I do ?
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
lp.bottomMargin = ...
lp.leftMargin = ...
lp.rightMargin = ...
lp.topMargin = ...
imageView.setLayoutParams(lp);
You have to use LayoutParams object to access those layout properties as below...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
params.leftMargin = 10;
imageView.setLayoutParams(params);
Here's an image of my problem:
The two small images in the Green and Red rectangles are the images I want centered. They should fall directly in the middle of both of thier parent's respectively.
Here's why it's not working (As far as I can tell):
I've set the width of the wrapper to 0 in order to avoid conflicts with the weight set for each wrapper. This lets me divide the screen right down the middle and divide content on either side. Unfourtunately, I think it also means that each image is trying to center itself on the 0dp portion of the wrapper rather than the dynamic width generated by the weight.
Here's the code, see for yourself:
public void mergeHotels(Hotel keepHotel, Hotel absorbHotel, ArrayList<Hotel> absorbArray){
Context context = getApplicationContext();
//get the current player and let him choose what to do with stock first
Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
//create the dialog for exchanging stocks
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
AlertDialog.Builder stockExchangeDialog = new AlertDialog.Builder(ctw);
stockExchangeDialog.setTitle(getResources().getString(R.string.stock_exchange_dialog_title));
LinearLayout dialogLayout = new LinearLayout(context);
dialogLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout dialogHeader = new LinearLayout(context);
dialogHeader.setOrientation(LinearLayout.HORIZONTAL);
dialogHeader.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 50, 1.0f));
View blankSpace = new View(context);
blankSpace.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f));
LinearLayout tileOneWrapper = new LinearLayout(context);
tileOneWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
tileOneWrapper.setBackgroundColor(Color.GREEN);
ImageView tileOne = new ImageView(context);
String tileOneResourceName = "tile_" + keepHotel.getName().toLowerCase();
int tileOneResourceId = getResources().getIdentifier(tileOneResourceName, "drawable", getPackageName());
tileOne.setBackgroundResource(tileOneResourceId);
LinearLayout.LayoutParams tileOneParams = new LinearLayout.LayoutParams(40, 40);
tileOneParams.gravity = Gravity.CENTER;
tileOne.setLayoutParams(tileOneParams);
tileOneWrapper.addView(tileOne);
LinearLayout tileTwoWrapper = new LinearLayout(context);
tileTwoWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
tileTwoWrapper.setBackgroundColor(Color.RED);
ImageView tileTwo = new ImageView(context);
String tileTwoResourceName = "tile_" + absorbHotel.getName().toLowerCase();
int tileTwoResourceId = getResources().getIdentifier(tileTwoResourceName, "drawable", getPackageName());
tileTwo.setBackgroundResource(tileTwoResourceId);
LinearLayout.LayoutParams tileTwoParams = new LinearLayout.LayoutParams(40, 40);
tileTwoParams.gravity = Gravity.CENTER;
tileTwo.setLayoutParams(tileTwoParams);
tileTwoWrapper.addView(tileTwo);
dialogHeader.addView(blankSpace);
dialogHeader.addView(tileOneWrapper);
dialogHeader.addView(tileTwoWrapper);
LinearLayout dialogBody = new LinearLayout(context);
dialogBody.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < players.length; i++) {
LinearLayout playerStockDetails = new LinearLayout(context);
playerStockDetails.setOrientation(LinearLayout.HORIZONTAL);
TextView playerName = new TextView(context);
playerName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
TextView playerMoney = new TextView(context);
playerMoney.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
TextView playerTileOneQuantity = new TextView(context);
playerTileOneQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
playerTileOneQuantity.setBackgroundColor(Color.LTGRAY);
TextView playerTileTwoQuantity = new TextView(context);
playerTileTwoQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
playerTileTwoQuantity.setBackgroundColor(Color.CYAN);
playerName.setText(players[i].getName());
playerMoney.setText(String.valueOf(players[i].getMoney()));
playerTileOneQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(keepHotel.getName()))));
playerTileTwoQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(absorbHotel.getName()))));
playerStockDetails.addView(playerName);
playerStockDetails.addView(playerMoney);
playerStockDetails.addView(playerTileOneQuantity);
playerStockDetails.addView(playerTileTwoQuantity);
playerStockDetails.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
dialogBody.addView(playerStockDetails);
}
dialogLayout.addView(dialogHeader);
dialogLayout.addView(dialogBody);
stockExchangeDialog.setView(dialogLayout);
stockExchangeDialog.show();
}
So how do I get it to center the images based on the dynamic-width (based on weight), rather than the static width I declared in LayoutParams?
Thanks in advance for your help!
JRadTheBad
Try this..
Add this line in your LinearLayout LayoutParams
tileOneWrapper.gravity = Gravity.CENTER;
and also
tileTwoWrapper.gravity = Gravity.CENTER;
here are my text and image and text views(i1, t1, t2). how can i format the width and height of these views dynamically in my class file.
ImageView i1 = new ImageView(this);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
RelativeLayout rl1 = new RelativeLayout(this);
RelativeLayout.LayoutParams innerLP1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
innerLP1.addRule(RelativeLayout.CENTER_IN_PARENT);
innerLP1.topMargin=(30);
t1.setText(name);
rl1.setLayoutParams(innerLP1);
rl1.addView(t1);
set layout parameters to those views
RelativeLayout.LayoutParams view_params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageview.setLayoutParams(view_params );
textview.setLayoutParams(view_params );
Use the txt.SetHeight() method.
Use setHeight() & setWidth() methods. For example:
ImageView i1 = new ImageView(this);
i1.setHeight(xxx);
i1.setWidth(xxx);
I have an image in "src\main\res\drawable\gate_logo.png" which looks like this :
My Java code looks like this :
LinearLayout Demo_Layout = new LinearLayout(context);
Demo_Layout.setId(View.generateViewId());
// Demo_Layout.setBackgroundColor(Color.rgb(88, 188, 218));
Demo_Layout.setBackgroundColor(Color.rgb(98, 198, 238));
Demo_Layout.setHorizontalGravity(Gravity.CENTER);
Demo_Layout.setVerticalGravity(Gravity.CENTER);
addView(Demo_Layout, LinearLayout.LayoutParams.MATCH_PARENT,328);
TextView textView = new TextView(context);
textView.setId(View.generateViewId());
textView.setGravity(Gravity.CENTER);
textView.setTextSize(20);
textView.setText(Html.fromHtml("<P><Br><big>GATE Demo</big><P>[ Graphic Access Tabular Entry ]<Br><small>An Interception-resistant Authentication System</small>"));
// Demo_Layout.addView(textView, LinearLayout.LayoutParams.MATCH_PARENT, 328);
// Demo_Layout.addView(textView, 600, 328);
// int resID = getResources().getIdentifier("gate_logo.png", "drawable", "package.name");
int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(resID);
// gateLogoView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// gateLogoView.setScaleType(ImageView.ScaleType.FIT_XY);
gateLogoView.setLayoutParams( new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
gateLogoView.setX(1);
gateLogoView.setY(1);
LinearLayout.LayoutParams vp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
gateLogoView.setLayoutParams(vp);
gateLogoView.setImageResource(resID);
// Demo_Layout.addView(gateLogoView, 600, 328);
Demo_Layout.addView(gateLogoView);
LinearLayout Button_Layout = new LinearLayout(context);
Button_Layout.setId(View.generateViewId());
Button_Layout.setBackgroundColor(Color.rgb(168, 98, 188));
Button_Layout.setHorizontalGravity(Gravity.CENTER);
Button_Layout.setVerticalGravity(Gravity.CENTER);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 138);
lp1.addRule(RelativeLayout.BELOW, Demo_Layout.getId());
addView(Button_Layout, lp1);
Button Registration_Button=new Button(context);
Registration_Button.setText("Registration");
Registration_Button.setAllCaps(false);
Registration_Button.setOnClickListener(Registration_Demo_Listener);
Button_Layout.addView(Registration_Button);
Button Login_Button=new Button(context);
Login_Button.setText("Login");
Login_Button.setAllCaps(false);
Login_Button.setOnClickListener(Login_Demo_Listener);
Button_Layout.addView(Login_Button);
Button Auto_Demo_Button=new Button(context);
Auto_Demo_Button.setText("Auto Demo");
Auto_Demo_Button.setAllCaps(false);
Auto_Demo_Button.setOnClickListener(Auto_Demo_Listener);
Button_Layout.addView(Auto_Demo_Button);
I also have a half size smaller version of the image in the same directory : gate_logo_s.png
I've tried different combinations, but why is the logo image not showing up ?
Screenshot look like this :
Replace these lines:
int resID = getResources().getIdentifier("gate_logo_s.png", "drawable", context.getPackageName());
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(resID);
with these:
ImageView gateLogoView = new ImageView(context);
gateLogoView.setImageResource(R.drawable.gate_logo);