I want to generate programmatically one layout and in layout one textview and edittext. How can I make it look like this?
There is code but it didnt work :(
RelativeLayout mRlayout = (RelativeLayout) findViewById(R.id.mRlayout);
RelativeLayout.LayoutParams mRparams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
EditText myEditText = new EditText(context);
myEditText.setLayoutParams(mRparams);
mRlayout.addView(myEditText);
first add linear layout in xml file like
<LinearLayout
android:id="#+id/horizantalLinear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horiontal" >
</LinearLayout>
and then create object of linear layout in your java code
EditText myEditText = new EditText(this); // Pass it an Activity or Context
LayoutParams editLayoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
myEditText.setLayoutParams(editLayoutParams);
myEditText.setVerticalFadingEdgeEnabled(true);
myEditText.setHint(hint);
myEditText.setId(Integer.parseInt(id));
myLayout.addView(myEditText);// myLayout is object of linear layout created in xml file
I would suggest define LinearLayout inside layout.xml and Create object in Java and then add textview the LinearLayout.
<LinearLayout
android:id="#+id/relatedChannels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
Java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LinearLayout cat_linear=(LinearLayout) findViewById(R.id.list_Category);
TextView tv = new TextView(context);
tv.setText("This is Text View");
tv.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
cat_linear.addView(tv);
EditText ed = new EditText (context);
ed.setHint("EditText");
ed.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
cat_linear.addView(ed);
}
Try this way,hope this will help you to solve your problem.
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView textView = new TextView(this);
textView.setText("TextView");
layout.addView(textView);
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1f));
editText.setHint("EditText");
layout.addView(editText);
setContentView(layout);
}
}
Related
The way my program works is that I take user input for 3 items, create a horizontal LinearLayout programmatically with those three items. Then place that LinearLayout inside a RelativeLayout that is defined in XML.
User can keep adding these 3-item horizontal LLs to the bottom of the RelativeLayout, which acts as a list in this case.
I am specifying a weight in the setLayoutParams method for each TextView object
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
ll.setId(View.generateViewId());
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
The problem is, the TextViews aren't being displayed the way I'd like them to. I'd like each of the three items to line up vertically, like a proper list.
Instead, I get something like this. You can see that the length of the strings is somehow affecting the way the layout is stored/displayed.
I know it's a big block of code, but any help would be appreciated!
Try this:-
Create a custom layout insteadof creating controls as dynamically
custom.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="90">
<TextView
android:id="#+id/textview1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:gravity="center"
android:text="text1"/>
<TextView
android:id="#+id/textview2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text2"/>
<TextView
android:id="#+id/textview3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="30"
android:text="text3"/>
</LinearLayout>
Add this view in RelativeLayout
View view = factory.inflate(R.layout.custom, null);
relativeLayout.addView(view);
You need to set the layout_width property to 0 for layout weight property to work well. I made a couple to changes to your code to make it work well for me.
Try this,
private void addNewRow(float currentIngredientMeasurementQuantity, String currentIngredientMeasurementType, String currentIngredientName) {
TextView quantityText = new TextView(MainActivity.this);
quantityText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
quantityText.setText("" + currentIngredientMeasurementQuantity);
TextView typeText = new TextView(MainActivity.this);
typeText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
typeText.setText(currentIngredientMeasurementType);
TextView nameText = new TextView(MainActivity.this);
nameText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
nameText.setText(currentIngredientName);
LinearLayout ll = new LinearLayout(MainActivity.this);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
ll.setId(View.generateViewId());
} else {
ll.setId(totalIngredientQuantity);
}
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//Find last member of RelativeLayout
View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1);
if (lastIngredient != null) {
relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId());
relativeParams.setMargins(0, 0, 0, 0);
}
ll.addView(quantityText);
ll.addView(typeText);
ll.addView(nameText);
relativeLayout.addView(ll, relativeParams);
}
You have to set the Gravity to Left (or Start) on your TextViews before adding them to the LinearLayout, just like this:
LinearLayout.LayoutParams name_lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
name_lp.gravity = Gravity.LEFT;
nameText.setLayoutParams(name_lp);
Try to tell your your Textviews to use all the available space they can get. So they will fill the whole LinearLayout.
Instead of WRAP_CONTENT use MATCH_PARENT or FILL_PARENT
In XML you could use 0dip like described here: In android layouts what is the effect/meaning of layout_height="0dip"
Another idea would be to use TableLayout or GridLayout http://developer.android.com/reference/android/widget/GridLayout.html
http://developer.android.com/reference/android/widget/TableLayout.html
Have Fun
I want to create a ImageView dynamically and place it at the center of the center. I tried doing it in xml and I was able. I tried to convert the same thing to code but the imageView always appears at the top left corner of the screen. Can somebody point out what the mistake is ? I'm still new to Android and finding my way. There might be blunders as well.
Thanks
The XML file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/myimg"
android:layout_width="100dip"
android:layout_height="100dip"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal"
android:background="#drawable/exmp"
/>
</LinearLayout>
The code
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Resources res = getResources();
String pluginPackageName = this.getApplicationContext().getPackageName();
int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(getApplicationContext());
iv.setLayoutParams(imageViewParams);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageResource(drawable_spinner);
layout.addView(iv, imageViewParams);
addContentView(layout, imageViewParams);
}
To make it exactly as in the XML layout file you have to change addContentView(layout, imageViewParams); to setContentView(layout);
EDIT:
full code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Resources res = getResources();
String pluginPackageName = this.getApplicationContext().getPackageName();
//int anim = res.getIdentifier("anim_android", "anim",pluginPackageName);
//int transparent_style = res.getIdentifier("TransparentProgressDialog", "style", pluginPackageName);
//int drawable_spinner = res.getIdentifier("exmp", "drawable", pluginPackageName);
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams((100),( 100));
imageViewParams.gravity = Gravity.CENTER_HORIZONTAL;
ImageView iv = new ImageView(getApplicationContext());
iv.setLayoutParams(imageViewParams);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setImageResource(R.mipmap.ic_launcher);
layout.addView(iv, imageViewParams);
setContentView(layout);
}
You have this as the last line in onCreate:
addContentView(layout, imageViewParams);
Perhaps you meant this?
addContentView(layout, layoutParams);
Alternatively, maybe this:
setContentView(layout, layoutParams);
Use layoutGravity insted of gravity.
imageViewParams.layoutGravity = Gravity.CENTER_HORIZONTAL;
You've to set the layout_gravity. You can do this way:
LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
layout.setLayoutParams(layoutParams);
layout.setGravity(Gravity.CENTER);
layout.setOrientation(LinearLayout.VERTICAL);
I have following code problem is when i run this all text boxes are not shown on screen so i figured that I need to add all text boxes in a scrollview but I dont know how.Also I can do this using listview but i have to do it by adding scrollview programatically in java code help anybody
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] textArray={"one","two","asdasasdf", "asdf" ,"dsdaa","fsvs","sd"};
int length=textArray.length;
LinearLayout layout = new LinearLayout(this);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);
for(int i=0;i<length;i++)
{
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
tv.setTextSize(40);
tv.setTextColor(Color.BLACK);
tv.setPadding(20, 50, 20, 50);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
layout.addView(tv);
//tv.setMovementMethod(new ScrollingMovementMethod());
}
}
}
// try this way,hope this will help you...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] textArray={"one","two","asdasasdf", "asdf" ,"dsdaa","fsvs","sd"};
int length=textArray.length;
ScrollView scrollView = new ScrollView(this);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
for(int i=0;i<length;i++)
{
TextView tv=new TextView(getApplicationContext());
tv.setText(textArray[i]);
tv.setTextSize(40);
tv.setTextColor(Color.WHITE);
tv.setPadding(20, 50, 20, 50);
tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
layout.addView(tv);
//tv.setMovementMethod(new ScrollingMovementMethod());
}
scrollView.addView(layout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
setContentView(scrollView);
}
Define one LinearLayout under ScrollView in xml file. Give id field to LinearLayout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/<ID> >
</LinearLayout>
</ScrollView>
Map LinearLayout in your class:
LinearLayout layout = (LinearLayout) findViewById(R.id.<ID>);
Add TextBox dynamically here:
TextView textBox = new TextView(context);
layout.addView(textBox);
I have a RelativeLayout defined in main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
And add a TextView and EditText programatically in onCreate:
public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);
addContentView(customGlView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
addContentView(myTextView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(myEditText, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
They both show up, but are overlapping in the top left corner. I've spend hours to figure out how to either position them just below each other, or one in the left corner of the screen and the other in the right corner. If I add them through the main.xml neither of them will show up. Can anyone give me a hint to the right direction?
For a RelativeLayout, you need to specify the relative positions of your elements, using the LayoutParams:
myTextView.setId(1);
myEditText.setId(2);
addContentView(myTextView, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.BELOW, myTextView.getId());
addContentView(myEditText, params);
Add a rule to your LayoutParams which will set one textview below the other..
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("myTextView");
params1.addRule(RelativeLayout.BELOW, tv1.getId());
TextView tv2 = new TextView(this);
I couldn't solve this with RelativeLayout, but using LinearLayout does exactly what you want to do.
addView(myEditText);
Or you can use this method to add them to any position you want. For example, you can first add myEditText and then add myTextView above myEditText:
addView(myEditText);
addView(myTextView, 0);
I can't quite get this to work, hoping to get some hints, it seems like from my research the code should work, any thoughts would be greatly appreciated...
I have an existing layout.xml file that includes:
<RelativeLayout style="#style/bodyLayout">
<ScrollView
android:id="#+id/scrollBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="false"
>
<LinearLayout
android:id="#+id/scrollLinearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Then I have programatic code as follows:
LinearLayout ll = new LinearLayout(this);
ll = (LinearLayout)findViewById(R.id.scrollLinearLayout);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(R.dimen.text_size_medium);
tv.setText("test");
tv.setLayoutParams(lp);
ll.addView(tv);
The new TextView doesn't appear when the activity is displayed, I know I a missing something obvious... the new TextView should appear in the LinearLayout section...
I have run your code works fine
LinearLayout ll = (LinearLayout)findViewById(R.id.subLinear);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setId(1);
tv.setTextSize(15);
tv.setText("test adding");
tv.setLayoutParams(lp);
ll.addView(tv);
if you add new View(anything) from any button click event then add this line
ll.invalidate();
it will refresh it your component