How to arrange buttons programmatically? - java

I've been trying to get the control arrows in the standard D-Pad orientation centered and underneath the video stream but this is the best I've been able to do so far based on what I believe should be the correct code. Notice that the up and down arrows are in the correct location but the Left arrow doesn't show and the Right arrow is on the left. Either I'm not understanding something correctly or there is something affecting the arrangement of the arrows that I'm not catching.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
setContentView(rl, relativeLayoutParams);
mv = new MjpegView(this);
rl.addView(mv);
Button panLeft = new Button(this);
panLeft.setBackgroundResource(R.drawable.arrow_left);
int panLeftID = View.generateViewId();
panLeft.setId(panLeftID);
Button panRight = new Button(this);
panRight.setBackgroundResource(R.drawable.arrow_right);
int panRightID = View.generateViewId();
panRight.setId(panRightID);
Button tiltUp = new Button(this);
tiltUp.setBackgroundResource(R.drawable.arrow_up);
int tiltUpID = View.generateViewId();
tiltUp.setId(tiltUpID);
Button tiltDown = new Button(this);
tiltDown.setBackgroundResource(R.drawable.arrow_down);
int tiltDownID = View.generateViewId();
tiltDown.setId(tiltDownID);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(tiltDown, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.LEFT_OF, tiltUpID);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(panLeft, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, tiltDownID);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(panRight, relativeLayoutParams);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ABOVE, tiltDownID);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(tiltUp, relativeLayoutParams);
And here is what it looks like:
http://s23.postimg.org/otbyon2a3/screenie.png

What about a grid layout?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:columnCount="3"
android:orientation="horizontal"
android:id="#+id/dpad_grid"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Up"
android:id="#+id/button4"
android:layout_row="0"
android:layout_column="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right"
android:id="#+id/button3"
android:layout_row="1"
android:layout_column="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Down"
android:id="#+id/button2"
android:layout_row="2"
android:layout_column="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left"
android:id="#+id/button5"
android:layout_row="1"
android:layout_column="0"/>
</GridLayout>
To echo #Dave S you should really use layouts for something like this.

Related

Dynamically adding a view to a custom ListView item layout not working

I wish to add a custom tailored layout for each ListView item. I have an empty linear layout in the xml layout file for ListView item layout. It has horizontal orientation.
I wish to add three FrameLayouts dynamically to above mentioned LinearLayout and set custom weight for each FrameLayout.
I found a part of the solution for it, as in the code below. Problem arises when the ListView has to be updated. ListView is initially hidden. When a certain button is pressed, ListView gets updated (hash map cleared and then filled with a new data set) and its visibility set from GONE to VISIBLE. Next thing is to loop through all the items and add designed view programmatically.
If I invoke the method add_designed_views(null); right after updating ListView items, the app loops through items, creates all the new views and adds them to right items. However, they are not added. I know this because of check if the item is already added, and if I invoke this method several times in a row, it will do the same each time, as if it didn't add anything at all.
Next thing that I tried is I postponed adding views dynamically by invoking the method add_designed_views from onClick of a dummy button. When I see the list has loaded (without the custom views) I press the dummy button and the custom views appear. But every 6th is missing. If I press it again, the missing ones appear. Which makes me even more confused.
It's obvious that the views can't be added immediately after changing the visibility of ListView. Probably because it takes the system some time to inflate the ListView, or it does some background task, but I couldn't find any listener or some function that tells me that it has finished loading so that I can add my views then.
Custom ListView item layout (xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="110dp"
android:background="#drawable/textview_border_dark_line"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="21"
android:minHeight="40dp">
<TextView
android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:minHeight="90dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/path_model"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal"
android:onClick="onClick_listItem"
>
<!-- Path Model. -->
<!-- This is where I add view dynamically. -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="#+id/text2"
android:textSize="12dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_time"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/clock"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/text4_1"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_time" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_changes"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/change"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_2"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_changes"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_cost"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/coins"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_3"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_cost"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="21"
android:minHeight="38dp">
<TextView
android:id="#+id/text3"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
</LinearLayout>
add_designed_views method (Java):
public void add_designed_views(View view)
{
for(int i=0; i<hashmap_search_results_list.size(); i++)
{
/* Check if it is already added. We have to check,
because we run it twice in consecutive order. */
LinearLayout listView_item = (LinearLayout)getViewByPosition(i, listView_search_results);
View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
LinearLayout already_added_ll_path = (LinearLayout)((ViewGroup)path_model).getChildAt(0);
if(already_added_ll_path != null)
{
Log.d(TAG, "Already has linear layout: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
continue;
}
Log.d(TAG, "Doing: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
Search_Result_Item result_item = (Search_Result_Item)search_results_list.get(i);
/* How to access each view at each item position in list view. */
//LinearLayout listView_item = (LinearLayout) getViewByPosition(i, listView_search_results);
//View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
//LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
/* Setting the proportions of lines regarding walking time and riding time. */
HrMin hrmin_walking_to_entrance_time = result_item.walking_to_entrance_time();
HrMin hrmin_riding_time = result_item.travel_time();
HrMin hrmin_walking_from_exit_time = result_item.walking_from_exit_time();
int int_walking_to_entrance_time = hrmin_get_minutes(hrmin_walking_to_entrance_time);
int int_riding_time = hrmin_get_minutes(hrmin_riding_time);
int int_walking_from_exit_time = hrmin_get_minutes(hrmin_walking_from_exit_time);
int total = int_walking_to_entrance_time +
int_riding_time +
int_walking_from_exit_time;
float weight_walking_to_entrance = (float)int_walking_to_entrance_time/total;
float weight_bus_ride = (float)int_riding_time/total;
float weight_walking_from_exit = (float)int_walking_from_exit_time/total;
/* How to add a new layout, view to existing layout. */
LinearLayout ll_path = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
/* Pedestrian image. */
ImageView pedestrian = new ImageView(this);
pedestrian.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
int dimensionInPixel = 16;
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian.setLayoutParams(pedestrian_params);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_to_entrance);
View gray_line = new View(this);
gray_line.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line.setLayoutParams(gray_line_params);
frameLayout_pedestrian_container.addView(gray_line);
frameLayout_pedestrian_container.addView(pedestrian);
frameLayout_pedestrian_container.setLayoutParams(frame_layout_params);
ll_path.addView(frameLayout_pedestrian_container);
/* Bus image. */
FrameLayout frameLayout_bus_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_bus_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_bus_ride);
ImageView bus = new ImageView(this);
bus.setBackground(getResources().getDrawable(R.drawable.bus_rnd));
dimensionInPixel = 17;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams bus_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
bus.setLayoutParams(bus_params);
/* Green line. */
View green_line = new View(this);
green_line.setBackground(getResources().getDrawable(R.drawable.horizontal_green_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams green_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
green_line.setLayoutParams(green_line_params);
frameLayout_bus_container.addView(green_line);
frameLayout_bus_container.addView(bus);
frameLayout_bus_container.setLayoutParams(frame_layout_bus_params);
ll_path.addView(frameLayout_bus_container);
/* Walking from exit path. */
ImageView pedestrian2 = new ImageView(this);
pedestrian2.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
dimensionInPixel = 16;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params2 = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian2.setLayoutParams(pedestrian_params2);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container2 = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_from_exit);
View gray_line2 = new View(this);
gray_line2.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params2 = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line2.setLayoutParams(gray_line_params2);
frameLayout_pedestrian_container2.addView(gray_line2);
frameLayout_pedestrian_container2.addView(pedestrian2);
frameLayout_pedestrian_container2.setLayoutParams(frame_layout_params2);
ll_path.addView(frameLayout_pedestrian_container2);
ll_path.setLayoutParams(params);
ll_path.setOrientation(LinearLayout.HORIZONTAL);
path_model.addView(ll_path);
}
}
Part where I update the ListView and show it:
search_results.setVisibility(View.VISIBLE);
ListView is a child of two layouts:
<LinearLayout
android:id="#+id/search_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="gone"
android:paddingTop="140dp"
android:orientation="vertical"
android:onClick="onClick_nothing">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_options_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="#+id/mapView"
app:layout_constraintTop_toTopOf="#+id/mapView"
android:background="#android:color/white"
android:orientation="vertical"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarThumbVertical="#android:color/darker_gray"
/>
</RelativeLayout>
</LinearLayout>
One thing that helped is Handler.postDelayed():
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
add_designed_views (null);
}
}, 300);
This function delays method call for 300 mS. Programmatically added layouts are visible. But, still every 6th is missing. They also appear after I touch any of the items.

Dynamically adding buttons next to each other - RelativeLayout

Ok, so here's the thing. I'm trying to make an app that resembles a piano for android, also I've never really had much experience with Java or programming for Android so all of this is pretty new to me. I've managed to do this in XML but I want to make it programmaticaly so I can easily add more white and black keys also dependant of screen size. In XML it looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/white1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:id="#+id/white2"
android:layout_toRightOf="#+id/white1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:id="#+id/white3"
android:layout_toRightOf="#+id/white2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:id="#+id/white4"
android:layout_toRightOf="#+id/white3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:id="#+id/white5"
android:layout_toRightOf="#+id/white4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:id="#+id/white6"
android:layout_toRightOf="#+id/white5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:id="#+id/white7"
android:layout_toRightOf="#+id/white6"/>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginStart="-10dp"
android:layout_marginEnd="-6dp"
android:background="#000"
android:id="#+id/black1"
android:layout_toRightOf="#+id/white1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginLeft="-6dp"
android:layout_marginRight="-10dp"
android:background="#000"
android:id="#+id/black2"
android:layout_toRightOf="#+id/white2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-6dp"
android:background="#000"
android:id="#+id/black3"
android:layout_toRightOf="#+id/white4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginLeft="-8dp"
android:layout_marginRight="-8dp"
android:background="#000"
android:id="#+id/black4"
android:layout_toRightOf="#+id/white5"/>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_marginLeft="-6dp"
android:layout_marginRight="-10dp"
android:background="#000"
android:id="#+id/black5"
android:layout_toRightOf="#+id/white6"/>
And now I wanted to recreate it programmaticaly, at first I've tried linear approach but first of all I was unable to make more than 7 keys, and I didn't really knew how to make black keys on top of that. So now I've went with RelativeLayout and all is fine as long as I create two buttons, then it works fine, one is next to another. But when I try to create more than two buttons they kinda make a stack.
I was trying to make some sort of array of buttons so I could easily make a loop to create destined number of buttons. Also I wanted to change the width of buttons, so if I create 8 buttons the would have the width of screen_width/8 but I'm not quite sure if it makes any sense since it's actually not doing anything when uncommented.
I would be grateful for any tips :)
public class MainActivity extends AppCompatActivity {
final int[] whitelist = {R.id.whitebt1,R.id.whitebt2,R.id.whitebt3,R.id.whitebt4,R.id.whitebt5,
R.id.whitebt6,R.id.whitebt7,R.id.whitebt8};
Button[] whiteKeys = new Button[whitelist.length];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
final RelativeLayout pianoLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams whiteKeyParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
whiteKeys[0] = new Button(this);
whiteKeys[0].setId(View.generateViewId());
//whiteKeys[0].setHeight(height);
//whiteKeys[0].setWidth(width/8);
whiteKeys[0].setLayoutParams(whiteKeyParams1);
pianoLayout.addView(whiteKeys[0]);
whiteKeys[1] = new Button(this);
whiteKeys[1].setId(View.generateViewId());
//whiteKeys[i].setHeight(height);
RelativeLayout.LayoutParams whiteKeyParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
whiteKeyParams2.addRule(RelativeLayout.RIGHT_OF, whiteKeys[0].getId() );
whiteKeys[1].setLayoutParams(whiteKeyParams2);
pianoLayout.addView(whiteKeys[1]);
//HERE'S IS THE MOMENT WHERE I TRY TO ADD THIRD BUTTON AND THE BUTTONS START TO PILE UP
/*
whiteKeys[2] = new Button(this);
whiteKeys[2].setId(View.generateViewId());
//whiteKeys[i].setHeight(height);
//RelativeLayout.LayoutParams whiteKeyParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
whiteKeyParams2.addRule(RelativeLayout.END_OF, whiteKeys[1].getId());
whiteKeys[2].setLayoutParams(whiteKeyParams2);
pianoLayout.addView(whiteKeys[2]);*/
this.setContentView(pianoLayout);
}
}
You can add 8 same size buttons using weightsum and layoutweight with LienarLayout with horizontal orientations.
see below code it may help you to add same size buttons dynamically.
/* Add a new Linearlayout as a container for the buttons */
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//Added Weight Sum 8 in LinearLayout
linearLayout.setWeightSum(8);
/* Create a new Buttons in this container, for the status bar */
//below LayoutParams define with weight 1 for buttons.
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
Button button1 = new Button(linearLayout.getContext());
button1.setLayoutParams(param);
Button button2 = new Button(linearLayout.getContext());
button2.setLayoutParams(param);
Button button3 = new Button(linearLayout.getContext());
button3.setLayoutParams(param);
Button button4 = new Button(linearLayout.getContext());
button4.setLayoutParams(param);
Button button5 = new Button(linearLayout.getContext());
button5.setLayoutParams(param);
Button button6 = new Button(linearLayout.getContext());
button6.setLayoutParams(param);
Button button7 = new Button(linearLayout.getContext());
button7.setLayoutParams(param);
Button button8 = new Button(linearLayout.getContext());
button8.setLayoutParams(param);
With your approach before adding the view to parent layout you will have to add margins for every new key also which will prevent stacking one key over another.
params.setMargins(left, top, right, bottom);

How can I programmatically set this tablelayout to have equal column widths for Android?

I am trying to create the rows below the table heading programmatically, but they are spilling over and I am not sure how to split them up to only take up 25% of the width each?
Here is the code for the row creation:
TableRow row = new TableRow(activity);
lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1.0f);
tl = new TableLayout.LayoutParams();
tl.weight = 1;
lp.height = 100;
row.setLayoutParams(lp);
row.setWeightSum(4);
tv = new TextView(getActivity());
tv.setLayoutParams(tl);
tv.setTextSize(14);
tv.setText(whotos.get(i));
tv2 = new TextView(getActivity());
tv2.setLayoutParams(tl);
tv2.setTextSize(14);
tv2.setText(documents.get(i));
tv3 = new TextView(getActivity());
tv3.setLayoutParams(tl);
tv3.setTextSize(14);
tv3.setText(directions.get(i));
tv4 = new TextView(getActivity());
tv4.setLayoutParams(tl);
tv4.setTextSize(14);
tv4.setText(thedates.get(i));
row.addView(tv); //tp name
row.addView(tv2); //document type
row.addView(tv3); //direction
row.addView(tv4); //date
ll.addView(row, i + 1 + tableHeadingRows);
Here is my code for the layout above the programmatically created rows:
<TableRow
android:id="#+id/tableRow2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="5dip"
android:stretchColumns="1"
android:weightSum="4">
<TextView
android:id="#+id/tp"
android:layout_weight="1"
android:layout_width="0dp"
android:text="TP"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/doc"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Doc"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/dir"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Direction"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/date"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceLarge" />
</TableRow>
<!-- draw a red line -->
<View
android:layout_height="2dip"
android:background="#color/tableHorizontalRule" />
Does anyone have a solution to this issue?
I changed this over to a LinearLayout, as it simplified things a bit and allowed me to easily set column weights programmatically.

ImageView, How to set image seem at the front of another image?

I have a problem with images. How can I bring an image in front of another one.
The problem is I create the images in code. So I can not change order in Layout.
You can use ImageView.bringToFront();, maybe is the best solution.
Here's the Tutorial
Let me know if you have any issue.
You can use RelativeLayout:
RelativeLayout rv = (RelativeLayout) findViewById(R.id.my_ph);
RelativeLayout.LayoutParams params;
ImageButton im1 = new ImageButton(this);
im1.setBackgroundResource(R.drawable.lamp);
im1.setId(i);
im1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TextView tx = (TextView) findViewById(R.id.textView1);
tx.setText("lamp #" + v.getId());
}
});
params = new RelativeLayout.LayoutParams(40, 40);
params.leftMargin = x;
params.topMargin = y;
rv.addView(im1, params);
XML Layout:
<RelativeLayout
android:id="#+id/my_ph"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#drawable/map" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="#+id/image"
android:layout_alignParentLeft="true">
</TextView>
</RelativeLayout>
If you are creating images via code , you could use LayoutParams to give the second image a negative leftMargin.
RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
imageParams.leftMargin = -20; // change this value
imageParams.addRule(RelativeLayout.RIGHT_OF,image1.getId());
image2.setLayoutParams(imageParams);
This would force the second image on top of the first image. By changing the left margin you could decide on how much you want the second image to move over the first.

Write XML dynamically

I would like to know how I could dynamically write the data below in Java. I am stuck on how you write the "layout_alignParent" can set the gravity of each text view....
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:gravity="center_vertical" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignParentRight="true"
android:gravity="center_vertical" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
I know that the code below would not work. But how could I re-write it for it to work?
RelativeLayout m = (RelativeLayout)findViewById(R.id.tileContainerME);
EditText et1 = new EditText(this);
EditText et2 = new EditText(this);
EditText et3 = new EditText(this);
EditText et4 = new EditText(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
et1.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
et2.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.removeRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
et3.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
et4.setLayoutParams(params);
params.removeRule(RelativeLayout.ALIGN_PARENT_TOP);
params.removeRule(RelativeLayout.CENTER_HORIZONTAL);
et1.setGravity(Gravity.CENTER_HORIZONTAL);
et1.setHint("Test");
et2.setGravity(Gravity.CENTER_VERTICAL);
et2.setHint("Test");
et3.setGravity(Gravity.CENTER_VERTICAL);
et3.setHint("Test");
et4.setGravity(Gravity.CENTER_HORIZONTAL);
et4.setHint("Test");
m.addView(et1);
m.addView(et2);
m.addView(et3);
m.addView(et4);
I think this code will help you:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView.setLayoutParams(params);
Edited:
params.gravity = Gravity.CENTER_HORIZONTAL;
tv2.setLayoutParams(params);

Categories

Resources