Android dynamic layout click problems - android

i create a dynamic view ( linearlayout ) with
table.addView(view, layoutParams);
I want when I click on view1 the background color turns white,
when I click on view2 the background color of view1 turns black and the background color of view2 turns white, and if I click on view3 the background color of view2 turns black and the background color of view3 turns white ...
private void addToRow(int rowID, TimeCard timecard, int offset) {
LayoutParams tlparams = new LinearLayout.LayoutParams(TIMECARD_WIDTH,
LinearLayout.LayoutParams.WRAP_CONTENT);
tlparams.setMargins((int) offset, 150-50*rowID, 0, 0);
System.out.println("rowID= "+50*rowID);
View v = timecard.getView(context);
v.setOnClickListener(this);
rows.get(rowID).addView(v, tlparams);
}
private void addRowsToTable() {
LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (LinearLayout row : rows) {
layoutParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//table is an absolutlayout and i add many row in this layout
table.addView(row, layoutParams);
}
tableNavbar.addView(llnavBar, layoutParams);
}
#Override
public void onClick(View v) {
v.setBackgroundResource(R.drawable.cadre_blanc);
TextView t1=(TextView)v.findViewById(R.id.tc_title);
t1.setTextColor(Color.rgb(0, 139, 221));
}
how can i do this please??

Related

How to set margins to dynamically created UI

I need to set margin to dynamically created UI. I want to add margin to LinearLayout.
Below is my code
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
viewPager = (ViewPager) container;
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
this.layoutInflater = inflater;
scrollView = new ScrollView(getActivity());
linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);
//adding few UI controllers dynamically by method call to here
return scrollView;
}
I tried many ways but nothing works. Currently, it is not adding space/margin as per given dimensions.
if(layoutParams instanceof MarginLayoutParams)
{
((MarginLayoutParams) layoutParams).topMargin = 15;
((MarginLayoutParams) layoutParams).leftMargin = 15;
//... etc
}
As per this SO answer:
scrollView.setFillViewport(true);
This forces the childview to stretch to the parent scrollview. Then you can set the margins that will add space after filling the scrollview.
If you want to add margins to LinearLayout, you need to create LayoutParams of the same type of the parent, so:
scrollView = new ScrollView(getContext());
linearLayout = new LinearLayout(getContext());
linearLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView.LayoutParams layoutParams = new ScrollView.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
// I recommend you to set resolved size as margins, not pixels like you are doing here.
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);
EDIT
Remember to add the ScrollView to your layout adding LayoutParams to it.
For example in a Fragment (you are using getActivity() so i suppose you are in a Fragment):
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(15, 15, 15, 15);
scrollView.addView(linearLayout, layoutParams);
scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
return scrollView;
}
you need to call scrollView.setLayoutParams(layoutParams);
also set Layoutparams to your Scrollview and Inner LinearLayout
in your code :
scrollView = new ScrollView(getActivity());
scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
scrollView.setFillViewport(true);
linearLayout = new LinearLayout(getActivity());
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) scrollView
.getLayoutParams();
layoutParams.setMargins(15, 15, 15, 15);
scrollView.setLayoutParams(layoutParams); //add this
scrollView.addView(linearLayout, layoutParams);

How to perfectly horizontally center a RadioButton with no text in a layout?

I am trying to place RadioButtons underneath some TextViews and center them horizontally, essentially moving the label above the button.
Here is my code:
XML:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:id="#+id/someRadioGroup"/>
Java:
LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(VERTICAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);
TextView choiceTextView = new TextView(context);
choiceTextView.setText("1");
choiceTextView.setLayoutParams(layoutParams);
choiceTextView.setGravity(Gravity.CENTER);
choiceLinearLayout.addView(choiceTextView);
RadioButton choiceRadioButton = new RadioButton(context);
choiceRadioButton.setText("");
choiceRadioButton.setGravity(Gravity.CENTER);
choiceRadioButton.setLayoutParams(layoutParams);
choiceLinearLayout.addView(choiceRadioButton);
someRadioGroup.addView(choiceLinearLayout);
Please note that the above code is in a loop to add each of the seven options.
Here is what it looks like on MOST devices (tested on Android 2.3, 4.3, and 4.4):
Here's what it looks like on Android 4.1:
Please note that the TextViews are not actually off-center - they are perfectly centered. It is the RadioButtons that are too far left.
What can I do to fix this issue?
EDIT:
I have added choiceTextView.setGravity(Gravity.CENTER); to the code above. It did not do anything as the text was already centered. The text is just fine. The RadioButtons are too far to the left. Here's a screenshot with the layout bounds option enabled on my device:
//This layout is to group the options
LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);
//You can use a cicle
for (int i = 0; array.size(); i++){
//This layout is to group the label and radiobutton.
LinearLayout radioLayout = new LinearLayout(context);
radioLayout.setGravity(Gravity.CENTER);
radioLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams radioParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
radioLayout.setLayoutParams(radioParams);
TextView choiceTextView = new TextView(context);
choiceTextView.setText(i);
radioLayout.addView(choiceTextView);
RadioButton choiceRadio = new RadioButton(context);
radioLayout.addView(choiceRadio);
choiceLinearLayout.addView(radioLayout);
}
RadioButton choiceRadioButton = new RadioButton(context);
choiceRadioButton.setText("");
choiceRadioButton.setGravity(Gravity.CENTER);
choiceRadioButton.setLayoutParams(layoutParams);
choiceLinearLayout.addView(choiceRadioButton);
someRadioGroup.addView(choiceLinearLayout);
I ended up with a different solution - using ToggleButtons instead of RadioButtons. I set a StateListDrawable as the background of the toggle buttons and made sure that text was always an empty string, whether the button was toggled on or off. Here's the code I ended up with:
LinearLayout choiceLinearLayout = new LinearLayout(context);
choiceLinearLayout.setGravity(Gravity.CENTER);
choiceLinearLayout.setOrientation(VERTICAL);
LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
choiceLinearLayout.setLayoutParams(layoutParams);
TextView choiceTextView = new TextView(context);
choiceTextView.setText("1");
choiceTextView.setLayoutParams(layoutParams);
choiceTextView.setGravity(Gravity.CENTER);
choiceLinearLayout.addView(choiceTextView);
ToggleButton choiceToggleButton = new ToggleButton(context);
choiceToggleButton.setText("");
choiceToggleButton.setTextOn("");
choiceToggleButton.setTextOff("");
choiceToggleButton.setGravity(Gravity.CENTER);
StateListDrawable radioDrawable = getRadioDrawable(context); // this function creates the state list our of pngs that I've added to the project
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
choiceToggleButton.setBackgroundDrawable(radioDrawable);
} else {
choiceToggleButton.setBackground(radioDrawable);
}
LinearLayout.LayoutParams choiceToggleButtonLayoutParams = new LayoutParams(radioDrawable.getIntrinsicWidth(), radioDrawable.getIntrinsicHeight());
choiceToggleButton.setLayoutParams(choiceToggleButtonLayoutParams);
choiceLinearLayout.addView(choiceToggleButton);
choiceToggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
ToggleButton toggleButton = (ToggleButton) view;
// do not allow toggling a button off
if (!toggleButton.isChecked()) {
toggleButton.setChecked(true);
}
// uncheck all other buttons, leaving the current one checked
for (int i = 0; i < someRadioGroup.getChildCount(); i++) {
LinearLayout linearLayout = (LinearLayout) someRadioGroup.getChildAt(i);
if (linearLayout != null) {
ToggleButton tb = (ToggleButton) linearLayout.getChildAt(1);
if (tb != null && tb != toggleButton) {
tb.setChecked(false);
}
}
}
}
});
someRadioGroup.addView(choiceLinearLayout);
Note that an OnClickListener is required for each ToggleButton to mimic proper RadioButton behavior.
Here's the result on Android 4.1 (with some left and right margin applied to each ToggleButton):

Android layout dynamically issue

Trying to dynamically add a textView and a button into a LinearLayout, then add this layout into a main layout that setContentView use
So its something like this
T - text view
B - button
Ltb - layout that contains T and B
Lm - Main layout that contains Ltb
Then use this.setContentView(Lm) to show to result
Roles:
T must be on the left.
B must be on right of the screen within the layout
All element above are declared dynamically, without using layout xml
Actual result:
Display fine. but when I type in text that is longer than the screen width, the Button got pushed outside of the screen and gone.
Problem, is it something my dynamic layout doing wrong ?
Code here:
public SearchBar(Context c){
et=new EditText(c);
bt=new Button(c);
et.setHint("added et");
bt.setText("added btn");
ll=new LinearLayout(c);
setLinearLayout();
et.setLayoutParams(flowLeft());
bt.setLayoutParams(flowRight());
ll.addView(et);
ll.addView(bt);
}
private RelativeLayout.LayoutParams flowRight(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
return params;
}
private RelativeLayout.LayoutParams flowLeft(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
return params;
}
private void setLinearLayout(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(params);
}
// try this way, hope this will help you...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ltb = new LinearLayout(this);
TextView T = new TextView(this);
Button B =new Button(this);
LinearLayout.LayoutParams ltbParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams TParms = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1f);
LinearLayout.LayoutParams BParms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
T.setLayoutParams(TParms);
B.setLayoutParams(BParms);
T.setText("Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text");
B.setText("Button");
ltb.addView(T);
ltb.addView(B);
ltb.setLayoutParams(ltbParams);
setContentView(ltb);
}
Do like this. This may help you.
Ltb.setWeightSum(100);
Ltb.setOrientation(LinearLayout.HORIZONTAL);
T.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));
B.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));

managing visibility of relativelayout children

I had to implement the expand/collapse(show/hide) a particular view on click of a some other view..Finally got it working but with a small problem.
I have a relative layout with two children: first is the textview and second is a linear layout.
On the click event of the textview i set the visibility(VISIBLE or GONE) of the following linear layout.
Now the problem is that after the linear layout is visible it somehow manages to hide the textview..
I tried textview.bringToFront() but it just makes the textview overlap the first row of the linearlayout ie the textview comes on top of the linear layout content.
I tried putting the textview in a linearlayout, but it makes no difference.
I tried setting the linear layout as BELOW. All in vain..
I know the textview exists because when i click the first row(which is overlapping the textview) the click event gets fired..
All i want is that no matter what my textview should be visible and the linear layout must take its position below the textview if it is visible..
EDIT
RelativeLayout wrapperlayout = new RelativeLayout(getActivity());
//wrapperlayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
//-------------HANDLE---------------------------
TextView txtHeader = new TextView(getActivity());
txtHeader.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v;
RelativeLayout rParent = (RelativeLayout) tv.getParent();
LinearLayout lInner = (LinearLayout) rParent.getChildAt(1);
if(lInner.isShown())
{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.GONE);
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up));
}
else{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.collapse, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.VISIBLE);
lInner.setTop(tv.getBottom());
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down));
}
}
});
txtHeader.setText("Header");
txtHeader.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
txtHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
//--------------CONTENT-------------------------
LinearLayout lContent = new LinearLayout(getActivity());
lContent.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lContent.setOrientation(LinearLayout.VERTICAL);
HashMap<String, String> MySet = new HashMap<String, String>();
MySet = getData();
Iterator<String>RowItr = MySet.keySet().iterator();
int rowcnt = 0;
while (RowItr.hasNext()) {
LinearLayout lRow = new LinearLayout(getActivity());
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lRow.setLayoutParams(lparams1);
lRow.setOrientation(LinearLayout.HORIZONTAL);
TextView txtLbl = new TextView(getActivity());
txtLbl.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
TextView txtVal = new TextView(getActivity());
txtVal.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
String Lbl = RowItr.next();
txtLbl.setText(Lbl);
if(rowcnt % 2 != 0)
lRow.setBackgroundColor(Color.parseColor("#dbe4f0"));
else
lRow.setBackgroundColor(Color.parseColor("#ffffff"));
txtVal.setText(MySet.get(Lbl));
lRow.addView(txtLbl);
lRow.addView(txtVal);
lRow.setPadding(10, 10, 10, 10);
lContent.addView(lRow);
rowcnt++;
}
lContent.setVisibility(View.GONE);
wrapperlayout.addView(txtHeader);
wrapperlayout.addView(lContent);
RelativeLayout.LayoutParams rPARAMS = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//rPARAMS.addRule(RelativeLayout.ABOVE, txtHeader.getId());
//rPARAMS.addRule(RelativeLayout.BELOW, lContent.getId());
wrapperlayout.setLayoutParams(rPARAMS);
well you can store your LinearLayout as an instance variable, and simply call layout.setvisibility(View.GONE); in your onClick method. Doubt theres any other solution unless you want to save the parent(the layout that both of your views are attached to) and perform findViewById inside onClick or call getChildAt(1) orso
Got a solution.. i tried setting margins..and it all worked out well.. te handle and content both are visible without hiding any other views..

Give ALIGNMENT to the fields in relative layout at Runtime in android?

I am working to create fields at run time, like in a relative layout am adding one text field at right corner and one Check-box at the left corner.
For this am getting problem, currently i am using the following code:
ViewGroup hori_layout=new RelativeLayout(getParent());
hori_layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tv1=new TextView(getParent());
tv1.setText(_medContactNames[i]);
tv1.setTextColor(Color.BLACK);
CheckBox cb = new CheckBox(getApplicationContext());
hori_layout.addView(tv1);
hori_layout.addView(cb);
layout.addView(hori_layout);
*
/**
* GENERATING RELATIVE LAYOUT AT RUNTIME
* */
public class RL extends RelativeLayout {
public RL(Context context,int i,String flag) {
super(context);
//FIRST FIELD OF THE LAYOUT
TextView firstField = new TextView(context);
firstField.setTextColor(Color.BLACK);
if(flag.equalsIgnoreCase("LAW")){
firstField.setText(_lawContactNames[i]);
}else{
firstField.setText(_medContactNames[i]);
}
firstField.setId(1);
//SECOND FIELD OF THE LAYOUT
CheckBox secondField = new CheckBox(context);
secondField.setId(2);
//FIRST LAYOUT WHICH MUST BE PRESENT AT LEFT END == TEXT FIELD
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(firstField, lpSecond);
//SECOND LAYOUT AT RIGHT END == CHECK BOX
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpFirst.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, secondField.getId());
addView(secondField, lpFirst);
}
}
*

Categories

Resources