I have a RelativeLayout with 4 ImageViews that should have 2 on top, one on each side of the Layout, and 2 ImageViews below the first two, one on each side of the screen.
I have 2 problems, the two ImageViews that are supposed to be below, do not go below. And The 2 ImageViews that are aligned to be on the right of the screen should be one on top of the other but seem to be about 25dp out.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
RelativeLayout tl = (RelativeLayout)v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.WHITE);
ImageView imageTopL = new ImageView(getActivity());
imageTopL.setImageResource(R.drawable.bell_dl_256);
imageTopL.setPadding(50, 0, 0, 0);
imageTopL.setId(0);
ImageView imageBottomL = new ImageView(getActivity());
imageBottomL.setImageResource(R.drawable.bell_dl_256);
imageBottomL.setPadding(50, 0, 0, 0);
imageBottomL.setId(1);
ImageView imageBottomR = new ImageView(getActivity());
imageBottomR.setImageResource(R.drawable.bell_dr_256);
imageBottomR.setPadding(0, 0, 50, 0);
imageBottomR.setId(2);
ImageView imageTopR = new ImageView(getActivity());
imageTopR.setImageResource(R.drawable.bell_dr_256);
imageTopR.setPadding(0, 0, 50, 0);
imageTopR.setId(3);
tl.addView(imageTopL);
tl.addView(imageTopR);
tl.addView(imageBottomL);
tl.addView(imageBottomR);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageTopL.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageTopR.getLayoutParams();
params.addRule(RelativeLayout.RIGHT_OF, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params = (RelativeLayout.LayoutParams)imageBottomL.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageBottomR.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.RIGHT_OF, imageBottomL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
return v;
// try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
RelativeLayout tl = (RelativeLayout) v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.WHITE);
ImageView imageTopL = new ImageView(this);
imageTopL.setId(1);
imageTopL.setImageResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
imageTopL.setAdjustViewBounds(true);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params1.setMargins(50,10,0,0);
imageTopL.setLayoutParams(params1);
imageTopL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Top Left",Toast.LENGTH_SHORT).show();
}
});
ImageView imageTopR = new ImageView(this);
imageTopR.setId(2);
imageTopR.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params2.setMargins(0,10,50,0);
imageTopR.setLayoutParams(params2);
imageTopR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Top Right",Toast.LENGTH_SHORT).show();
}
});
ImageView imageBottomL = new ImageView(this);
imageBottomL.setId(3);
imageBottomL.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params3.addRule(RelativeLayout.BELOW, imageTopL.getId());
params3.setMargins(50,10,0,0);
imageBottomL.setLayoutParams(params3);
imageBottomL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Bottom Left",Toast.LENGTH_SHORT).show();
}
});
ImageView imageBottomR = new ImageView(this);
imageBottomR.setId(4);
imageBottomR.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params4.addRule(RelativeLayout.BELOW, imageTopR.getId());
params4.setMargins(0,10,50,0);
imageBottomR.setLayoutParams(params4);
imageBottomR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Bottom Right",Toast.LENGTH_SHORT).show();
}
});
tl.addView(imageTopL);
tl.addView(imageTopR);
tl.addView(imageBottomL);
tl.addView(imageBottomR);
return v;
}
Related
I'm creating a ScrollView, nesting in a RelativeLayout (which contains tables). Everything is properly added to the layout. The problem is: vertically scrolling works fine, but horizontal scrolling does NOT work at all. I already tested various combinations of MATCH_PARENT, WRAP_CONTENT and FILL_PARENT. No try even worked close. So my question is: What am I missing here?
Here is the code:
public class PlayerView extends View {
private RelativeLayout relativeLayout;
private TableLayout tableLayout;
private int player_loop;
private int player_count;
public PlayerView(Context context, int player_count){
super(context);
setPlayer_count(player_count);
}
public ScrollView create_scrollView(){
ScrollView scrollView = new ScrollView(getContext());
ScrollView.LayoutParams scroll_params = new ScrollView.LayoutParams(
ScrollView.LayoutParams.MATCH_PARENT,
ScrollView.LayoutParams.MATCH_PARENT
);
scrollView.setLayoutParams(scroll_params);
scrollView.addView(create_relativeLayout());
return scrollView;
}
public RelativeLayout create_relativeLayout(){
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
return relativeLayout;
}
public void create_Player_Table_Layout(){
for(player_loop = 1; player_loop <= player_count; player_loop++){
relativeLayout.addView(player_table(getResources().getString(R.string.dummy_player_name) + player_loop, player_loop));
}
}
public TableLayout player_table(String playername, int playernumber){
tableLayout = new TableLayout(getContext());
tableLayout.setId(playernumber * 1000);
if (playernumber > 1) {
//TABLE PLACEMENT
RelativeLayout.LayoutParams tbl_params_New = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tbl_params_New.addRule(RelativeLayout.RIGHT_OF, (playernumber - 1) * 1000);
tableLayout.setLayoutParams(tbl_params_New);
}
//Add Playername
TableRow row_playername = new TableRow(getContext());
TextView view_name = new TextView(getContext());
TableRow.LayoutParams view_name_params = new TableRow.LayoutParams();
view_name_params.setMargins(20,20,20,20);
view_name.setLayoutParams(view_name_params);
view_name.setGravity(Gravity.CENTER);
view_name.setText(playername);
view_name.setTextSize(20);
view_name.setId(playernumber * 100);
row_playername.addView(view_name);
tableLayout.addView(row_playername);
//Add Lifepoints
TableRow row_lifepoints = new TableRow(getContext());
TextView view_lifepoints = new TextView(getContext());
TableRow.LayoutParams view_lifepoints_params = new TableRow.LayoutParams();
view_lifepoints_params.setMargins(20, 0, 20, 20);
view_lifepoints.setText("40");
view_lifepoints.setTextSize(40);
view_lifepoints.setGravity(Gravity.CENTER);
view_lifepoints.setId(playernumber * 100 + 10);
view_lifepoints.setLayoutParams(view_lifepoints_params);
row_lifepoints.addView(view_lifepoints);
tableLayout.addView(row_lifepoints);
Log.d("Test", "Player count:" + player_count);
for(int opponent_loop = 1; opponent_loop <= player_count; opponent_loop++){
tableLayout.addView(commander_damage_from_player(player_loop, opponent_loop));
}
return tableLayout;
}
private TableRow commander_damage_from_player(int player_loop, int opponent_loop){
TableRow row = new TableRow(getContext());
TextView textView = new TextView(getContext());
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();
layoutParams.setMargins(20, 0, 40, 20);
textView.setLayoutParams(layoutParams);
textView.setText("0 | " + getResources().getString(R.string.dummy_player_name)+ " " + opponent_loop);
textView.setTextSize(20);
textView.setId(player_loop + 100 + opponent_loop);
row.addView(textView);
return row;
}
private void setPlayer_count(int player_count){
this.player_count = player_count;
}
}
And for documentation the calling class:
public class Home extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PlayerView playerView = new PlayerView(this, 8);
setContentView(playerView.create_scrollView());
}
}
The only way you can do this is by creating a horizontalscrollview to be the child of the scrollview. If you make the minor modification to your create_relativeLayout() to create a horizontalscroll view then it will work correctly. I have seen this happen in other examples.
public HorizontalScrollView create_relativeLayout(){
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(getContext());
relativeLayout = new RelativeLayout(getContext());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeLayout.setLayoutParams(relativeParams);
create_Player_Table_Layout();
horizontalScrollView.addView(relativeLayout);
return horizontalScrollView;
}
My code:
public class MyBottomDialog extends Dialog {
LinearLayout container;
public MyBottomDialog(Context context) {
super(context);
container = new LinearLayout(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(params);
container.setOrientation(LinearLayout.VERTICAL);
// container.setBackgroundColor(Color.TRANSPARENT);
int padding = context.getResources().getDimensionPixelSize(R.dimen.navigation_padding_bottom);
container.setPadding(padding, padding, padding, padding);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(container);
// setContentView(button);
setCancelable(true);
setCanceledOnTouchOutside(true);
getWindow().setGravity(Gravity.BOTTOM);
// getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
getWindow().setWindowAnimations(R.style.BottomDialogAnimation);
}
public void addMenuItems(String[] menuItemTitles, int[] menuItemTextColors) {
for (int i = 0; i < menuItemTitles.length; i++) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
// button.setText(menuItemTitles[i]);
// button.setTextColor(menuItemTextColors[i]);
// container.addView(button);
addMenuItem(menuItemTitles[i], menuItemTextColors[i]);
}
}
public void addMenuItem(String menuItemTitle, int menuItemTextColor) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
Button button = produceButton();
button.setText(menuItemTitle);
button.setTextColor(menuItemTextColor);
container.addView(button);
}
private Button produceButton() {
Button button = new Button(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER);
// button.setBackground(getContext().getResources().getDrawable(R.drawable.bottom_menu_button2));
float[] outerRadii = new float[]{24, 24, 24, 24, 24, 24, 24, 24};
RoundRectShape rect = new RoundRectShape(outerRadii, null, null);
ShapeDrawable drawable = new ShapeDrawable(rect);
drawable.getPaint().setColor(Color.WHITE);
drawable.getPaint().setStyle(Paint.Style.FILL);
button.setBackground(drawable);
return button;
}
}
then I use it:
MyBottomDialog dlg = new MyBottomDialog(this);
dlg.addMenuItems(new String[]{"delete", "cancel"}, new int[]{android.R.color.holo_red_light, android.R.color.holo_blue_light});
dialog.show();
but there is no any text in these two button in dialog, why?
I have this code, but the textview are all without Margin right and i don't know why.
I add some lines of onCreate for declaration.
And the functions about the creation of the textview.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearlayout);
mEditText = (EditText) findViewById(R.id.input_materia);
mButton = (Button) findViewById(R.id.add_materia);
mButton.setOnClickListener(onClick());
TextView textView = new TextView(this);
textView.setText("New text");
/*
* add textview from editext
*/
private OnClickListener onClick() {
return new OnClickListener() {
#Override
public void onClick(View v) {
mLayout.addView(createNewTextView(mEditText.getText().toString()));
}
};
}
private TextView createNewTextView(String text) {
final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lparams.setMargins(0, 0, 30, 0);
final TextView textView = new TextView(this);
textView.setLayoutParams(lparams);
textView.setText(text);
textView.setGravity(Gravity.CENTER);
return textView;
}
some helps?
Have you tried referencing Relative Layout Params or Linear Layout Params depending on what is set in your XML?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
I have a pop up that displays an image upon clicking a button. I got to display the pop up but the image is so small and I want it to occupy the 90% of the screen and add a close button to it.
Here is my code:
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.animalbite);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int fullScreenWidth = display.getWidth();
int dialogWidth = (int) (fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
imageView.setLayoutParams(params);
//tv.setText("Hi this is a sample text for popup window");
layout.addView(imageView, params);
popUp.setContentView(layout);
Button ViewBmi = (Button) findViewById(R.id.ViewBmi);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10 ,10);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Here is the display:
ANy ideas?
Try this...
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params,params2;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
final int height = (display.getHeight()*90)/100;
final int width = (display.getWidth()*90)/100;
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
params = new LayoutParams(width,height-50);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView, params);
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
layout.addView(button,params2);
popUp.setContentView(layout);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
popUp.dismiss();
click = true;
}
});
Button ViewBmi = (Button) findViewById(R.id.button1);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10, 10);
popUp.update(10, 20, width,height);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Try This....
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
--->params2.setMargins(100, 0, 0, 0);
layout.addView(button,params2);
I'd do it like this:
height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;
Try:
params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
...
layout.addView(imageView, params);
This way the ImageView will be as wide, as possible.
If the underlying dialog is not wide enough, then you can set it's width also full screen:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);
Or if you want it to be for example 90% of the screen, then:
// get the width of the whole screen
Display display = getWindow().getDefaultDisplay();
int fullScreenWidth = display.getWidth();
//then set 90% of it to the width of the Dialog:
int dialogWidth = (int) fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
I am trying to keep two customviews and i kept fadein fadeout but i am not able to see both custom views am able to see only one custom view if i click on fadein i m able to see only one view but not both. but i am not able to see both views at a time please tell me where i did mistake.
MyView myview;
MyView1 myview1;
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myview=new MyView(this);
myview.setBackgroundColor(color.white);
setContentView(myview);
myview = new MyView(this);
myview.setBackgroundColor(Color.WHITE);
myview1=new MyView1(this);
myview1.setBackgroundColor(color.white);
setContentView(myview1);
myview1 = new MyView1(this);
myview1.setBackgroundColor(Color.WHITE);
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(0);
relativeLayout.setBackgroundColor(Color.WHITE);
myview = new MyView(this);
myview.setId(004);
RelativeLayout.LayoutParams lp6 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myview.setLayoutParams(lp6);
relativeLayout.addView(myview,lp6);
myview1 = new MyView1(this);
myview1.setId(8);
RelativeLayout.LayoutParams lp008 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myview1.setLayoutParams(lp008);
relativeLayout.addView(myview1,lp008);
Button button1 = new Button(this);
RelativeLayout.LayoutParams lp1=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
button1.setText("click");
relativeLayout.addView(button1,lp1);
button1.setOnClickListener(this);
setContentView(relativeLayout);
}
public class MyView extends View {
public MyView(Context c) {
super(c);
}
protected void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(),R.drawable.apple);
canvas.drawBitmap(_scratch, 840, 450, null);
}
}
public class MyView1 extends View{
public MyView1(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
Bitmap scratch=BitmapFactory.decodeResource(getResources(), R.drawable.ant);
canvas.drawBitmap(scratch, 840, 450, null);
}
}
public void onClick(View v) {
if(myview1.getVisibility() == View.VISIBLE) {
Animation out = AnimationUtils.makeOutAnimation(this, true);
myview1.startAnimation(out);
myview1.setVisibility(View.INVISIBLE);
} else {
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
myview1.startAnimation(in);
myview1.setVisibility(View.VISIBLE);
}
}
}
Use Views setVisibility method