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);
Related
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));
Am using a CircularFloatingActionMenu library, I have successfully implemented this library, but I want to increase the size of the 4 sub-action button
Please find below the code am using
final ImageView fabIconNew = new ImageView(this);
fabIconNew.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_new));
final FloatingActionButton rightLowerButton = new FloatingActionButton.Builder(this)
.setContentView(fabIconNew)
.build();
SubActionButton.Builder rLSubBuilder = new SubActionButton.Builder(this);
ImageView rlIcon1 = new ImageView(this);
ImageView rlIcon2 = new ImageView(this);
ImageView rlIcon3 = new ImageView(this);
ImageView rlIcon4 = new ImageView(this);
//ImageView rlIcon6 = new ImageView(this);
rlIcon1.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_contact));
rlIcon2.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_currency_info));
rlIcon3.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_exhibition));
rlIcon4.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_faq));
You can set LayoutParams in the SubactionButton.Builder
SubActionButton.Builder rLSubBuilder = new SubActionButton.Builder(this);
LayoutParams params=new LayoutParams(200,200);
rLSubBuilder.setLayoutParams(params);
LayoutParams params=new LayoutParams(80,80);
button1.setLayoutParams(params)
for more reference [circular floating action menu][2]
I want to position a View horizontally centered in a Layout. My code below fails. How can I fix this?
See my code:
LinearLayout LLT = new LinearLayout(context);
LLT.setOrientation(LinearLayout.VERTICAL);
LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//get booktheme by bookID
theme = db.getthemeByID(id);
String themePath = theme.getFilepath();
int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
//imageView
ImageView imageTheme = new ImageView(context);
imageTheme.setLayoutParams(new LayoutParams(500, 700));
imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageTheme.setPadding(0, 20, 0, 10);
imageTheme.setAdjustViewBounds(true);
imageTheme.setImageResource(resid);
LLT.addView(imageTheme);
// add view
VF.addView(LLT);
Use Gravity feature in your LayoutParams as following:
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
imageTheme.setLayoutParams(layoutParams);
Replace your code with this one and try:
LinearLayout LLT = new LinearLayout(context);
LLT.setOrientation(LinearLayout.VERTICAL);
LLT.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//get booktheme by bookID
theme = db.getthemeByID(id);
String themePath = theme.getFilepath();
int resid = getResources().getIdentifier(themePath, "drawable", getPackageName());
//imageView
ImageView imageTheme = new ImageView(context);
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(500, 700);
layoutParams.gravity=Gravity.CENTER_HORIZONTAL;
imageTheme.setLayoutParams(layoutParams);
imageTheme.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageTheme.setPadding(0, 20, 0, 10);
imageTheme.setAdjustViewBounds(true);
imageTheme.setImageResource(resid);
LLT.addView(imageTheme);
// add view
VF.addView(LLT);
have you tried using relative layout instead of linear layout ?
EDIT :
if u want to use relative layouts ,
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)imageview.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageview.setLayoutParams(layoutParams);
( did not test the code btw but i used it in one of my app so this shld do the job )
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;
I have a RelativeLayout
RelativeLayout myLayout = new RelativeLayout(this);
Then, I add 2 ImageView in this Layout:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20, 20);
ImageView img1 = new ImageView(this);
// Here I give the position of img1
ImageView img2 = new ImageView(this);
// And here the position and img2
myLayout.addView(img1, params);
myLayout.addView(img2, params);
setContentView(myLayout);
And here I have a problem: I want to show and click on the 2 ImageViews, but only the img2 is visible on my screen.
Is there a problem with the z-index or something else?
Since you are using RelativeLayout and same params for both ImageView one will be overlapped with the other. So define params for both. Add rule to each param for positioning it. And then give addView().
For eg:
RelativeLayout myLayout = new RelativeLayout(this);
ImageView img1 = new ImageView(this);
RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftArrowParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ImageView img2 = new ImageView(this);
RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rightArrowParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
myLayout.addView(img1, firstImageParams);
myLayout.addView(img2, secImageParams);
setContentView(myLayout);