Dynamically adding buttons next to each other - RelativeLayout - java

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);

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.

Programmatically Adding EditText with weight

I've created a Vertical LinearLayout with a Horizontal LinearLayout within it in order to hold 3 EditText boxes within them for data entry. I've also placed a button below the vertical LinearLayout that I want to program to add another row of editText with every press.
I've succeeded so far and managed to programmatically add another row with 1 edittext in it. Then I tried with 3 and hit a roadblock. I tried to use weightsum and weights to get the editText boxes to be in 3 columns. However with the code below I get 2 editText boxes next to each other then a very very tiny editbox as the last one.
What am I doing wrong? Why aren't the widths of the edittext boxes being divided by 3?
XML:
<LinearLayout android:id="#+id/layout_ingredients"
android:layout_below="#id/text_recipeIngredients"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal">
<EditText android:id="#+id/edit_recipeIngredient"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/edit_recipe_ingredient"/>
<EditText android:id="#+id/edit_recipeAmount"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/edit_recipe_amount"/>
<EditText android:id="#+id/edit_recipeUnit"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/edit_recipe_unit"/>
</LinearLayout>
</LinearLayout>
<Button android:id="#+id/btn_add_ingredient"
android:background="#color/orange"
android:layout_marginTop="10dp"
android:layout_below="#id/layout_ingredients"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="#string/btn_add_ingredient"
android:onClick="addIngredients"/>
Java:
public void addIngredients(View view) {
final String ADDINGREDIENT = "ADD INGREDIENT";
Log.d(ADDINGREDIENT, ADDINGREDIENT);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout_ingredients);
LinearLayout ingredientLayout = new LinearLayout(this);
ingredientLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
ingredientLayout.setOrientation(LinearLayout.HORIZONTAL);
ingredientLayout.setWeightSum(3f);
EditText editTextIngredient = new EditText(this);
editTextIngredient.setHint("Ingredient");
editTextIngredient.setLayoutParams(new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f));
EditText editTextAmount = new EditText(this);
editTextAmount.setHint("Amount");
editTextAmount.setLayoutParams(new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f));
EditText editTextUnit = new EditText(this);
editTextAmount.setHint("Unit");
editTextAmount.setLayoutParams(new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.WRAP_CONTENT,
1f));
ingredientLayout.addView(editTextIngredient);
ingredientLayout.addView(editTextAmount);
ingredientLayout.addView(editTextUnit);
linearLayout.addView(ingredientLayout);
}
The problem here is that you are adding the weighted layouts to a layout that is not on the screen, so its width is unknown at the time you add them.
You have a couple of approaches, but the easiest one is probably to grab the widths of the elements already on the screen and simply duplicate them (since you are using a weighted layout to begin with).
You could also measure the screen and use that measurement to set the widths of each layout.
Last, you could use ViewTreeObserver to add them after the parent has been added and the width is known (see the post here: Getting the width/height of a layout in Android).
The last is the most "interesting" but the least efficient and useful since your original layout already did the calculations.

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.

How to arrange buttons programmatically?

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.

Add TextView on top of Button?

I'm building a contacts app which displays big contact photos. I need to add a label with the contacts name of each contact on top of the button (near the bottom) however I don't know how to get two views on top of each other. I cannot simply use settext since I need to add a semi-transparent background to the label.
EDIT:
I managed to get it on top but I cannot figure out how to get it on the bottom of the button.
RelativeLayout icon = new RelativeLayout(context);
// Create button
Button button = new Button(context);
button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
layout.addView(button);
// Create label
TextView label = new TextView(context);
label.setText(name);
label.setBackgroundColor(Color.argb(120, 0, 0, 0));
label.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
icon.addView(button);
icon.addView(label);
However the text appears on the top of the image and I want it to be on the bottom like this:
With xml this would be something like: android:layout_alignBottom="#+id/myButton" but I'm doing this programatically and I haven't found a way to do it. How can I place my label near the button?
Try this
<?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="match_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="John"
android:background="#drawable/button_action_active" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test textView" />
</FrameLayout>
</LinearLayout>
Dynamically you can do by
TextView lable = new TextView(this);
lable.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lable.setTextSize(25);
lable.setGravity(Gravity.CENTER);
lable.setText("John");
Button button = new Button(this);
button.setBackgroundResource(R.drawable.button_action_active);
button.setLayoutParams(new LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
FrameLayout fl = new FrameLayout(this);
fl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
fl.addView(button);
fl.addView(lable);
setContentView(fl);
To do this you can use a frame layout. The documentation for frame layout can be found here - http://developer.android.com/reference/android/widget/FrameLayout.html
However, frame layout is depreciated (if I remember correctly). So instead I would recommend using a relative layout. In your relative layout you can set the position of the button and then give the textview the attributes android:layout_alignLeft=#id/somethingand android:layout_alignRight="#id/Something"

Categories

Resources