I have this code below that creates a square with an image, textview and 2 image buttons.
It looks like this:
I want it so that the text is always in 1 line. The width of the square needs to expand so it includes the full text. In the image, the text gets cropped. The two little buttons (info and settings) needs to be in the corners of the square. Also there is another problem, the text is not vertically centered and it should be.
I can't quite get it to look right. Does anyone know how to fix this?
Thanks
public static LinearLayout createNodeWrapper(Activity context, Fish fish) {
ImageView imageview = createImageView(context, fish);
TextView nametv = createNameView(context, fish.getName());
ImageButton info = createButton(context, R.drawable.ic_action_info);
ImageButton settings = createButton(context, R.drawable.ic_action_settings);
LinearLayout namewrapper = createNameWrapper(context);
namewrapper.addView(info);
namewrapper.addView(nametv);
namewrapper.addView(settings);
LinearLayout contentwrapper = createContentWrapper(context, fish);
contentwrapper.addView(imageview);
contentwrapper.addView(namewrapper);
LinearLayout fullwrapper = createFullWrapper(context, fish);
fullwrapper.addView(contentwrapper);
return fullwrapper;
}
private static ImageView createImageView(Context context, Fish fish) {
ImageView imageview = new ImageView(context);
LinearLayout.LayoutParams params = fish.getImageParams();
params.gravity = Gravity.CENTER;
params.setMargins(20, 20, 20, 20);
imageview.setLayoutParams(params);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
return imageview;
}
private static TextView createNameView(Context context, String name) {
TextView nametv = new TextView(context);
nametv.setText(name);
nametv.setTextAppearance(context, R.style.node);
nametv.setPadding(10, 0, 10, 10);
nametv.setGravity(Gravity.CENTER);
nametv.setSingleLine(true);
LinearLayout.LayoutParams textparams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textparams.gravity = Gravity.CENTER;
nametv.setLayoutParams(textparams);
return nametv;
}
private static ImageButton createButton(Context context, int resId) {
ImageButton imageButton = new ImageButton(context);
imageButton.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageparams.gravity = Gravity.CENTER;
imageButton.setLayoutParams(imageparams);
imageButton.setBackgroundResource(resId);
return imageButton;
}
private static LinearLayout createNameWrapper(Context context) {
LinearLayout wrapper = new LinearLayout(context);
wrapper.setOrientation(LinearLayout.HORIZONTAL);
wrapper.setPadding(0, 0, 0, 0);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
wrapper.setLayoutParams(params);
return wrapper;
}
private static LinearLayout createContentWrapper(Context context, Fish fish) {
LinearLayout wrapper = new LinearLayout(context);
wrapper.setOrientation(LinearLayout.VERTICAL);
wrapper.setPadding(3, 0, 3, 3);
LinearLayout.LayoutParams params = fish.getParams();
wrapper.setLayoutParams(params);
wrapper.setMinimumWidth(MIN_NODE_WIDTH);
return wrapper;
}
private static LinearLayout createFullWrapper(Context context, Fish fish) {
LinearLayout wrapper = new LinearLayout(context);
wrapper.setBackgroundResource(R.drawable.category_bg);
wrapper.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = fish.getParams();
params.setMargins(10, 10, 10, 10);
wrapper.setLayoutParams(params);
return wrapper;
}
For the text, try this answer: https://stackoverflow.com/a/2142968/2629065 it will scale down the text until it fits the parameters you give it.
You padded the top, which adds to the height of the item, offsetting the gravity alignment. Remove the top padding and use a margin, or pad the bottom.
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;
}
I am trying to set padding to my linear layouts so that when I press 'create row' they have a gap between them each.
I tried adding marginTop and marginBottom to my LinearLayout but when I run the code, the layout is still spawn in together. not with a gap from the margin I set.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/verticalLayout"
android:layout_below="#id/button"
android:background="#color/colorPrimaryDark"
android:layout_marginTop="50dp"
>
(the above image shows 3 linear layouts I added by pressing create row)
Edit: I tried adding a marginRight to it, just to see if anything worked on it, and it did.
public class MainActivity extends AppCompatActivity {
LinearLayout verticallayout,horizontalLayout; //got
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
verticallayout = findViewById(R.id.verticalLayout); //got
}
public void createRow(View view){
createRow(); //got
}
private void createRow() { //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
}
private void createEditText() {
EditText editText = new EditText(this);
LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
editParams.setMargins(10, 10, 10, 10);
editText.setLayoutParams(editParams);
horizontalLayout.addView(editText);
}
private void createCheckbox() { //done
CheckBox checkBox = new CheckBox(this);
LinearLayout.LayoutParams checkParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
checkParams.setMargins(10, 10, 10, 10);
checkParams.gravity = Gravity.LEFT;
checkBox.setLayoutParams(checkParams);
horizontalLayout.addView(checkBox);
}
private void createSpinner() {
Spinner spinner = new Spinner(this);
//ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
//spinner.setAdapter(spinnerArrayAdapter);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
spinnerParams.setMargins(10, 10, 10, 10);
spinnerParams.gravity = Gravity.LEFT;
spinner.setLayoutParams(spinnerParams);
horizontalLayout.addView(spinner);
}
}
Try this one code
private void createRow() { //got
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalParams.setMargins(15, 5, 15, 5); // LEFT, TOP, RIGHT, BOTTOM
horizontalLayout.setLayoutParams(horizontalParams);
horizontalLayout.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark));
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout, horizontalParams);
}
hope so it will be useful for you. :)
Add margin to horizontal layout not in Vertical layout. check following code:
private void createRow() {
horizontalLayout = new LinearLayout(this);
LinearLayout.LayoutParams horizontalParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
horizontalParams.topMargin = 20; //add margin here.
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
horizontalLayout.setLayoutParams(horizontalParams);
createSpinner();
createCheckbox();
createEditText();
verticallayout.addView(horizontalLayout);
}
Sorry for my English. I have a dynamically created TextView and ImageView, they are added to the LinearLayout. But when I start to write a TextView that it changes its size and thus reduces the ImageView. The picture below is an example of how the size of the
final LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
row.setOrientation(LinearLayout.HORIZONTAL);
//params editTExt
android.widget.LinearLayout.LayoutParams paramsEtext = new android.widget.LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f);
paramsEtext.gravity = Gravity.LEFT;
//params image View
LinearLayout.LayoutParams paramsImageView = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.25f);
paramsImageView.gravity = Gravity.CENTER_VERTICAL;
//create edit text
final EditText etext = new EditText(QuicklyCreateQwestions.this);
etext.setLayoutParams(paramsEtext);
//add et in ArrayList
et.add(etext);
//set text in textView
etext.setText(qwestions.get(i).toString());
row.addView(etext);
//add ImageView in edit text
final ImageView imV = new ImageView(this);
imV.setLayoutParams(paramsImageView);
imV.setImageResource(R.drawable.t_del_poll);
imV.setId(i);
//set button in layaut
row.addView(imV);
//action if button click
imV.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
row.removeView(etext);
row.removeView(imV);
}
});
//add editText and button in layaut
LinearLayoutTextQwestions.addView(row);
i'm writing progrmicaly adding Linearlayout and Textview into ScrollView. but my ScrollView could not scroll to height.
My programicall Code:
if (cursor.moveToFirst()) {
do {
String last_ID = cursor.getString(cursor.getColumnIndex("lastId"));
String smsBody = cursor.getString(cursor.getColumnIndex("smsBody"));
String senderName = cursor.getString(cursor.getColumnIndex("senderName"));
String date[] = cursor.getString(cursor.getColumnIndex("receiveDate")).split("/");
CalendarTool ct =
new CalendarTool(
Integer.valueOf(date[0]),
Integer.valueOf(date[1]),
Integer.valueOf(date[2])
);
String IranianDate = ct.getIranianDate();
ScrollView SV =new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.parseColor("#f7cbad"));
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, 10);
ll.setLayoutParams(params);
TextView TV_IranianDate = new TextView(this);
TV_IranianDate.setText(IranianDate);
TV_IranianDate.setTextColor(Color.parseColor("#ffffff"));
TV_IranianDate.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
ll.addView(TV_IranianDate);
TextView TV_smsBody = new TextView(this);
TV_smsBody.setText(smsBody);
TV_smsBody.setGravity(Gravity.RIGHT);
TV_smsBody.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
);
TV_smsBody.setPadding(5, 5, 5, 5);
ll.addView(TV_smsBody);
TextView TV_spacer2 = new TextView(this);
TV_spacer2.setBackgroundColor(Color.parseColor("#000000"));
TV_spacer2.setLayoutParams(
new ViewGroup.LayoutParams
(ViewGroup.LayoutParams.FILL_PARENT,
1)
);
ll.addView(TV_spacer2);
SV.addView(ll);
((LinearLayout) linearLayout).addView(SV);
} while (cursor.moveToNext());
}
I guess the problem is the ScrollView object. You should use HorizontalScrollView instead, like this for example:
HorizontalScrollView SV = new HorizontalScrollView(this);
SV.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
like described in the API:
ScrollView only supports vertical scrolling. For horizontal scrolling, use HorizontalScrollView.
I created a table with five table rows. The first row is the header of each table.
In the following four rows each second column shell represent an image and a textview.
My problem is that my image is displayed in the center of the row.
If I add some layoutparams to the image for their width, it disappears.
I want that the alignment of my picture is left, so its right next to my first column ends.
Creating the rows:
for (int i = 0; i < 4; i++) {
TableRow tableRow = new TableRow(context);
for (int column = 1; column <= 8; column++) {
TextView textView = null;
if (column == 2) {
ImageView imgView = new ImageView(context);
imgView.setLayoutParams(new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
tableRow.addView(imgView);
textView = new TextView(context);
textView.setGravity(LEFT);
} else {
textView = new TextView(context);
}
textView.setGravity(CENTER);
textView.setTextColor(WHITE);
tableRow.addView(textView);
}
tableLayout.addView(tableRow);
}
Update the table with data:
for (int column = 0; column <= 7; column++) {
View child = tableRow.getChildAt(column);
if (child instanceof ImageView) {
ImageView flag = (ImageView) child;
flag.setImageResource(getFlagByClubName(group.getTeams().get(i).getClub()));
}
if (child instanceof TextView) {
TextView textView = (TextView) tableRow.getChildAt(column);
setContentInColumn(group.getTeams().get(i), column, textView);
}
}
You could try wrapping the imageview and textview in a relative layout. Please note I haven't tested the code below, but it's adapted from some other code of mine that works fine.
RelativeLayout wrapper = new RelativeLayout(context);
// Create imageView params
RelativeLayout.LayoutParams imageParams;
imageParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
// Create imageView
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(imageParams);
imageView.setId(1);
// Create textView params
RelativeLayout.LayoutParams textParams;
textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.LEFT_OF, imageView.getId());
// Create textView
TextView textView = new TextView(context);
textView.setLayoutParams(textParams);
// Add to the wrapper
wrapper.addView(imageView);
wrapper.addView(textView);
And then just add wrapper to your table:
tableRow.addView(wrapper);