I'm facing one issue with rendering a TextView and Seekbar inside a dialog.
I was practicing this tutorial from android site
The issue is inside for loop the TextView & SeekBar are supposed to be added 5 times and should be displayed in the Dialog. But only a single TextView is displayed.
Here is the code:
public void onCheckedChanged(RadioGroup rgroup, int rbutton) {
String eqSettingName = ((RadioButton) findViewById(rbutton)).getText()
.toString();
if (eqSettingName.equals("Custom")) {
Dialog dialog = new Dialog(this);
dialog.setTitle("Custom Equalizer");
LinearLayout LL = new LinearLayout(this);
short noOfBands = mEqualizer.getNumberOfBands();
final short minEQLevel = mEqualizer.getBandLevelRange()[0];
final short maxEQLevel = mEqualizer.getBandLevelRange()[1];
for (short i = 0; i < noOfBands; i++) {
short band = i;
TextView freqTV = new TextView(this);
freqTV.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
freqTV.setGravity(Gravity.CENTER_HORIZONTAL);
freqTV.setText((mEqualizer.getCenterFreq(band)) / 1000 + " Hz");
LL.addView(freqTV);
SeekBar bar = new SeekBar(this);
bar.setLayoutParams(layoutParams);
bar.setMax(maxEQLevel - minEQLevel);
bar.setProgress(mEqualizer.getBandLevel(band));
LL.addView(bar);
}
/*
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.customseekbar,
(ViewGroup) findViewById(R.id.rlCustomEqualizerSeekBar));
*/
dialog.addContentView(LL, layoutParams);
dialog.show();
}
}
Your LinearLayout defaults to orientation="horizontal". Change it's orientation to vertical and you will see what you want.
LL.setOrientation(LinearLayout.VERTICAL);
Is your LinearLayout oriented correctly?
LL.setOrientation(LinearLayout.VERTICAL);
Related
Which will be the better way to create a Vertical Lineal layout with four or more buttons in each row
The problems I have face are the following:
Setting the id of each button manually will result in a lot of repetitive code, more resources usage and you will have to change everyone to add a feature or change something (I think using an adapter will be the most efficient way, but...)
From what I know using a CustomAdapter don't help you set a unique ID to the buttons
Can you use an adapter to set a different id for each button dipending of the row?
Example:
second button of third row: r3b2
fifth button of first row: r1b5
Thanks.
You can create the buttons programmatically in our Activity class like this
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout); // the layout in which u want to display the buttons
layout.setOrientation(LinearLayout.VERTICAL);
int count = 1;
for (int i = 0; i < 5; i++) { // i = row count
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 7; j++) { // j = column count (create 7 buttons in each row)
int id = count;
final Button btnTag = new Button(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
params.weight = 1;
params.gravity = Gravity.CENTER;
btnTag.setLayoutParams(params);
btnTag.setBackgroundColor(Color.parseColor("#ffffff"));
String dateSuffix = count + getDayNumberSuffix(count);
btnTag.setTag(dateSuffix);
btnTag.setId(id);
btnTag.setText(count + "");
btnTag.setTextSize(12.0f);
btnTag.setOnClickListener(getOnClickDoSomething(btnTag, count));
row.addView(btnTag);
count++;
}
layout.addView(row);
}
View.OnClickListener getOnClickDoSomething(final Button btnTag, final int count) {
return new View.OnClickListener() {
public void onClick(View v) {
// here you can do what u want to do on button click
}
}
LinearLayout linContact = (LinearLayout) mView.findViewById(R.id.linContacts);
LinearLayout.LayoutParams leftGravityparas = new LinearLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(30, 30);
for (int i = 0; i < contactList.size(); i++) {
final ClsAdviserData contact = .contactList.get(i);
if (contact.isSelected()) {
linearLayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams linMainparam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.setBackgroundColor(getActivity().getResources().getColor(R.color.light_grey_backgeound));
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setLayoutParams(linMainparam);
linMainparam.setMargins(0, 10, 0, 0);
leftGravityparas.gravity = Gravity.LEFT;
leftGravityparas.weight = 0.9f;
TextView txtContact = new TextView(getActivity());
txtContact.setTextSize(16);
// txtContact.setBackgroundColor(getActivity().getResources().getColor(R.color.light_grey_backgeound));
txtContact.setLayoutParams(leftGravityparas);
txtContact.setId(i);
leftGravityparas.setMargins(0, 10, 0, 0);
txtContact.setPadding(20, 10, 10, 10);
txtContact.setText(contact.getName());
linearLayout.addView(txtContact, leftGravityparas);
rightGravityParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
rightGravityParams.weight = 0.1f;
final ImageView imgDelContact = new ImageView(getActivity());
imgDelContact.setLayoutParams(rightGravityParams);
imgDelContact.setTag(i);
imgDelContact.setClickable(true);
imgDelContact.setOnClickListener(this);
imgDelContact.setImageResource(R.drawable.ic_close_grey);
linearLayout.addView(imgDelContact, rightGravityParams);
// linContact.setTag(i);
linContact.addView(linearLayout);
imgDelContact.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Toast ==>" + contact.getName() + v.getTag(), Toast.LENGTH_SHORT).show();
// linContact.removeViewAt((Integer) v.getTag());
linearLayout.setVisibility(View.GONE);
// lin.removeViewAt((Integer)v.getTag());
}
});
}
}
I wrote the above code to create the textfields and buttons dynamically; But now I need to remove 2 textfields and a button when the button is clicked. How do I do that?
adding -
After initializing add subview using addView() method declared in LinearLayout
linearLayout.addView(txtContact);
linearLayout.addView(imgDelContact);
Hide -
To hide View ,so that you can get it again whenever required
imgDelContact.setVisibility(View.GONE);
txtContact.setVisibility(View.GONE);
Remove -
Or you can remove if you don't want to use it again.
linearLayout.removeView(txtContact);
linearLayout.removeView(imgDelContact);
To remove any view you can use
aLinearLayout.removeView(view)// to remove particular view
aLinearLayout.removeViewAt(position);// to remove view from particular position
If you are dynamically creating views and you just need to remove all the views just use
aLinearLayout.removeAllViews();
This will clear the layout.
I have problem in creating programmatically layout in Android. Where I want to display in list with name with check box but check box little down and not in same horizontal range. Here is the my code:
mDialog.setContentView(R.layout.exmple);
mDialog.setCancelable(true);
mDialog.setTitle("Hobbies");
mDialog.show();
final LinearLayout mLinearLayout = (LinearLayout) mDialog
.findViewById(R.id.linear);
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
final Button okButton = new Button(this);
final int size = hobbyList.size();
LinearLayout[] mLinearLayout2 = new LinearLayout[size];
LinearLayout[] mLinearLayout3 = new LinearLayout[size];
Check = new CheckBox[size];
Text = new TextView[size];
for (int i = 0; i < size; i++) {
Check[i] = new CheckBox(this);
Text[i] = new TextView(this);
mLinearLayout2[i] = new LinearLayout(this);
mLinearLayout3[i] = new LinearLayout(this);
Check[i].setId(i);
Text[i].setText(hobbyList.get(i));
Text[i].setTypeface(font_regular);
Text[i].setTextColor(color.black);
mLinearLayout2[i].addView(Text[i]);
mLinearLayout3[i].addView(Check[i]);
mLinearLayout2[i].setGravity(Gravity.LEFT);
mLinearLayout3[i].setGravity(Gravity.RIGHT);
mLinearLayout.addView(mLinearLayout2[i]);
mLinearLayout.addView(mLinearLayout3[i]);
}
okButton.setText("OK");
mLinearLayout.addView(okButton);
Here is the output pictures:
The problem is with the layout
mLinearLayout.setOrientation(LinearLayout.VERTICAL);
so the layouts are coming one after another so the checkbox is not in line
you can try having a one relative layout and placing the text and checkbox right and left it's far more easier than your current approach
I have a ScrollView and I want to insert a user specified number of HorizontalScrollViews. So what user says he wants to have a matrix of 5x5 elements, I want to insert 5 HorizontalScrollViews with 5 EditText objects each. My program adds the first line just as it's supposed to, but the rest not.
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
latout_in_scrollview.addView(row);
}
Any ideas why? Thanks!
EDIT:
The 1:1 code im using
LinearLayout dijkstra_rows;
FrameLayout.LayoutParams par1 = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams par2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_dijkstra);
dijkstra_rows = (LinearLayout) findViewById(R.id.dijkstra_rows);
Bundle extras = getIntent().getExtras();
number = extras.getInt("vertexes");
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(par2);
HorizontalScrollView row = new HorizontalScrollView(this);
row.setLayoutParams(par1);
row.addView(ll);
for (int j = 0; j < number; j++) {
EditText txt = new EditText(this);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
txt.setHint(i+","+j);
ll.addView(txt);
}
dijkstra_rows.addView(row);
}
}
ScrollView can contain only one childView. You can put any layout as per your requirement. I generally use Relative Layout...
Then add views dynamically to relative layout
viewLayout = (ViewGroup) mView.findViewById(R.id.YOUR_RELATIVE_LAYOUT_ID);
View lastCard = viewLayout.getChildAt(viewLayout.getChildCount() - 1);
// INFLATE YOUR NEW VIEW YOU WANT TO ADD
CardView cardView = (CardView)
LayoutInflater.from(getContext()).inflate(R.layout.card_nearest_stop, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Set id to view
int id = 125;
if (lastCard != null) {
params.addRule(RelativeLayout.BELOW, lastCard.getId());
id = lastCard.getId() + 125;
}
cardView.setLayoutParams(params);
cardView.setId(id);
viewLayout.addView(cardView);
ScrollView is a single element container.
A ScrollView is a FrameLayout, meaning you should place one child in
it containing the entire contents to scroll; this child may itself be
a layout manager with a complex hierarchy of objects. A child that is
often used is a LinearLayout in a vertical orientation, presenting a
vertical array of top-level items that the user can scroll through.
You are adding multiple LinearLayouts here
for (int i = 0; i < number; i++) {
LinearLayout ll = new LinearLayout(this);
.
.
}
You should have only one out of this loop. Then add this one to your scrollView, in Loop you can add muliple HorizontolScrollViews to this LinearLayout
I'm developing an Android 3.1 tablet application and I have a "problem" with EditText control.
This is an EditText without text:
But, when I type some text on it:
Those four rows were created programmatically:
LinearLayout layout = null;
LayoutParams layoutParams = null;
EditText editText = null;
RadioGroup rGroup = null;
RadioButton rButton = null;
String tag = null;
layout = new LinearLayout(mActivity);
layoutParams =
new LayoutParams(LayoutParams.FILL_PARENT, 60);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(layoutParams);
editText = new EditText(mActivity);
tag = Long.toString(eOtherChks.getEreportOthChkId()) + "_" + OTHER_DESCRIPTION_COL;
editText.setTag(tag);
layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.weight = .25f;
editText.setLayoutParams(layoutParams);
editText.setGravity(Gravity.CENTER);
editText.setText(eOtherChks.getDescription());
layout.addView(editText);
But, in another view, I have added some EditText programmatically with editText.setInputType(InputType.TYPE_CLASS_NUMBER); and they don't get bigger.
How can I avoid this effect?
You can try setting:
layoutParams.width = 0;
This should prevent the resize. Let me know if it works.
This is how I've solved the problem:
layoutParams = new LayoutParams(0, LayoutParams.WRAP_CONTENT);