I have lists of LinearLayouts with horizontal orientation each one containing two textviews added dynamically.
This LinearLayout is finally wrapped into master LinearLayout.
I want the second textview of each linear layout to be right aligned progrmatically. How can I do this dynamically.
Here's sample code:
LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listhosts);
//Several such layouts with 2 text views will be added to placeholder
LinearLayout l = new LinearLayout(this);
l.setClickable(true);
TextView h = new TextView(this);
h.setText("left");
h.setSingleLine(true);
TextView t = new TextView(this);
t.setText("right");
t.setSingleLine(true);
l.addview(h);
l.addview(t);
placeHolderLinearLayout.addView(l);
There is android:layout_alignParentRight attribute. But how to set this dynamically in this case. Any clue?
The android:layout_alignParentRight can only be applied to a view if its parent is a RelativeLayout. Change your container to that, and the 2 sub-views can use any of the layout_alignParent* attributes.
If you can't do this programatically (which I can't see how to do quickly), then you could always define your inner layout in xml (where you can easily get the layout correct) and inflate manually via:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View l = vi.inflate(R.layout.inner_relative_layout, null);
TextView leftTextView = (TextView) l.findViewById(R.id.left_text);
leftTextView.setText("left");
// ... fill in right text too
placeHolderLinearLayout.addView(l);
Edit: added layout definition
Use a layout like this, and inflate it in the code as above:
<RelativeLayout android:id="#+id/inner_relative_layout" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/left_text" android_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="#+id/right_text" android_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
You will be creating multiple of these layouts for each item you're adding to your list.
this one is woking and simple answer::
public class MainActivity extends Activity {
TextView text[];
TextView texto[];
CheckBox check[];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.layout);
text = new TextView[5];
texto = new TextView[5];
check = new CheckBox[5];
for (int i = 0; i < 5; i++) {
text[i] = new TextView(this);
text[i].setText("First one is::" + i);
texto[i] = new TextView(this);
texto[i].setText("sennd one ibs::" + i);
check[i] = new CheckBox(this);
((ViewGroup) view).addView(check[i]);
((ViewGroup) view).addView(text[i]);
((ViewGroup) view).addView(texto[i]);
}
}
}
Please try below
LinearLayout placeHolderLinearLayout = (LinearLayout)findViewById(R.id.listhosts);
//Several such layouts with 2 text views will be added to placeholder
LinearLayout l = new LinearLayout(this);
l.setClickable(true);
TextView h = new TextView(this);
txt1.setGravity(Gravity.LEFT);
h.setText("left");
h.setSingleLine(true);
TextView t = new TextView(this);
txt1.setGravity(Gravity.RIGHT);
t.setText("right");
t.setSingleLine(true);
l.addview(h);
l.addview(t);
placeHolderLinearLayout.addView(l);
Related
In my Project , I have 80 TextViews.
I should set their text from 1 to 80 once project runs , and they dont need to be changed in future.
Except TxtViews , I have some other things in my Layout, the TextViews are under ImagesViews. actually I have 80 imagesViews and under them are 80 TextViews. I want to set text of textViews from 1 to 80 dynamically.
I know I can do it in my layout.xml ,
but its really time consuming.
is there any way to do that by code?
for example with a for cycle or something like that?
Create a ViewGroup suitable for your needs in the layout, for example:
<LinearLayout
android:id="#+id/linear_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
Then you create you TextView instances programatically, and add them to the LinearLayout, like this:
LinearLayout layout = (LinearLayout)findViewById(R.id.linear_layout);
for(int i = 0; i < 80; i++) {
TextView textView = new TextView(getContext());
textView.setText("text" + i);
layout.addView(textView);
}
Optionally, you can add tags or whatever to locate them again. Alternatively just iterate over the layouts subviews.
If you know that 80 Textview fixed then you should take listview for that.
Listview Benefit
Memory management automatically
Listview manage indexing
If they share the same layout, except for the text, and could be displayed as a list, you could use an ArrayAdapter and pass the values from code.
http://www.mkyong.com/android/android-listview-example/
Checkout the below example,
public class MainActivity extends Activity {
LinearLayout linearLayout ;
ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (HorizontalScrollView) findViewById(R.id.scrollViewActivityMain);
}
private void populateTextViews() {
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
//add all textViews here
for(int i=0; i < 80; i++){
TextView myTextView = new TextView(this);
myTextView.setText("My TextView "+i);
myTextView.setGravity(Gravity.CENTER);
linearLayout.addView(myTextView);
}
scrollView.addView(linearLayout);
}
}
Don't forget to put that scrollView in your xml.
Let me know if it works for you...
If your TextViews are declared on the xml, wrap them on another view so you can reference it on the java code later, then simply use a for.
Something like:
View view = findViewById(R.id.your_wrapper);
for(int i=0; i<((ViewGroup)view).getChildCount(); i++) {
View nChild = ((ViewGroup)view).getChildAt(i);
TextView tv = (TextView) nChild;
tv.setText(String.valueOf(i + 1));
}
If not, you can simply create them dynamically inside your java code, and append them to a layout like LinearLayout.
Example:
xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear"
/>
Java code
LinearLayout ll = (LinearLayout) findViewById(R.id.linear);
for (int i = 1; i <= 80; i++) {
TextView tv = new TextView(this); // Assuming you're inside an Activity.
int count = ll.getChildCount();
tv.setText(String.valueOf(i));
ll.addView(tv, count, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
EDIT: But truly, you should use RecyclerView or ListView for that if your values are not going to change.
You can read more about RecyclerView here, and on ListView here.
Second edit: From what you're saying on your comments, you REALLY should be using ListView instead of your current design. The solutions above and from the other answers won't work at all for your problem.
When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...
Image to demonstrate:
I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}
You guys are right. It is much easier using a LinearLayout. For those interested
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);
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):
I checked the solution here:
Adding multiple views of the same type
Its given that, create a new View everytime you add it instead of changing only 1 view.
But i am doing this:
for (int i = 0; i < 10; i++) {
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(
CommentsActivity.LAYOUT_INFLATER_SERVICE);
View cv = vi.inflate(R.layout.item, null);
TextView textView1 = (TextView) cv.findViewById(R.id.tv1);
textView1.setText("-" + i);
TextView textView2 = (TextView) cv.findViewById(R.id.tv2);
textView2.setText("--" + i);
LinearLayout insertPoint = (LinearLayout) findViewById(R.id.layout);
insertPoint.addView(cv, 0, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
so its like creating a new inflater and view for every i. But i am only getting the last item.
ie.., only 1 inflatedView with tv1 as -9 and tv2 as --9
seems like everytime i go into the for loop, the old view is being replaced by the new view. How to add all the 10 views??
ThankYou
usually I use this
private void renewDetail(){
llDetail.removeAllViews();
for (int i = 0; i < 10; i++) {
llDetail.addView(new ChildDetailNotePieDiagram(context, "Name", 1000, 10));
}
}
the logic is first I clear all view from the parent layout and then add view to the parent layout.
where llDetail is a linear layout and I create a linear layout class ChildDetailNotePieDiagram and add it to the linear layout so basically it's a different solution from what you use now
but I think you can try my solution if you want :)
feel free to ask any question about this in the comment
How can I dynamically add a TextView to this? The commented out code doesn't work.
public class myTextSwitcher extends Activity {
private TextView myText;
public myTextSwitcher(String string){
//myText = new TextView(this);
//myText.setText("My Text");
}
}
You're creating a text view and setting its value but you're not specifying where and how it should be displayed. Your myText object needs to have a container of some sort which will make it visible.
What you're trying to do is dynamically layout a view. See here for a good starter article. From the article:
// This is where and how the view is used
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
// this part is where the containers get "wired" together
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
First of all, you shouldn't be adding it in the constructor, non-default constructors are pretty much useless for an Activity. Finally, you are correctly creating a new TextView but you are not adding it anywhere. Get ahold of some layout in your content view (probably with findViewById), and call layout.addView(myText) with it.
Did you add the your text view to the activity using setContentView(myText);
make this
myText = new TextView(this);
myText.setText("foo");
setContentView(myText);
in oncreate() method
final TextView tv1 = new TextView(this);
tv1.setText("Hii Folks");
tv1.setTextSize(14);
tv1.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout ll = (LinearLayout) findViewById(R.id.lin);
ll.addView(tv1);
Your activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
</LinearLayout>