Dynamically add scrollview in layout and linearlayout in scrollview - android

How do I dynamically add a scrollview in my layout? and once I get that scrollview added I want to add a linearlayout inside it so I can now add controls in that linearlayout?

I hope it will helpful to you.
Try this Code...
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
ScrollView sv = new ScrollView(this);
sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
ll.setOrientation(1);
sv.addView(ll);
for(int i = 0; i < 20; i++)
{
Button b = new Button(this);
b.setText("Button "+i);
ll.addView(b);
}
rl.addView(sv);
/* If you want to set entire layout as dynamically, then remove below lines in program :
* setContentView(R.layout.activity_main);
* RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
* rl.addView(sv);
*
* And Add below line :
* this.setContentView(sv);
*/
}

try this
http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/

try this it will work :
TextView tv = new TextView(this);
tv.setText(msg);
tv.setMovementMethod(new ScrollingMovementMethod());
setContentView(tv);

You can wrap your widget inside a ScrollView. Here's a simple example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/txt"/>
</LinearLayout>
</ScrollView>
If you want to do it in code:
ScrollView sv = new ScrollView(this);
//Add your widget as a child of the ScrollView.
sv.addView(wView);

Related

Layout attributes in XML code those are ignored when using addView method

I'm korean, The image under is used korean language. Well.. This fact isn't important. I tried to add the custom view, In Linear layout, And It's be in linear layout successfully. But It's not on center horizontal. I did surely set the gravity of linearlayout center horizontal. But It isn't work. I guess cause of problem is layout params. Custom view class haven't problem, And not appeared any error on debugging. How do I do??
Activity in XML code:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="NestedScrolling">
<LinearLayout
android:id="#+id/select_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<!--Here is-->
</LinearLayout>
</ScrollView>
AddView method:
ThemeManager mTheme;
Button selectTopBoardButton;
LinearLayout selectBody;
ThemeView c0001;
ThemeView c0002;
ThemeView c0003;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
gestureDetectorCompat = new GestureDetectorCompat(this, new MainActivityGoer());
NetworkDetector networkDetector = new NetworkDetector(this);
if (!networkDetector.isConnected()) {
Toast.makeText(SelectActivity.this, "네트워크에 접속할 수 없습니다.", Toast.LENGTH_SHORT).show();
}
selectTopBoardButton = (Button) findViewById(R.id.select_top_board_button);
selectBody = (LinearLayout) findViewById(R.id.select_body);
mTheme = new ThemeManager(this);
c0001 = mTheme.getThemeView("c0001");
c0002 = mTheme.getThemeView("c0002");
c0003 = mTheme.getThemeView("c0003");
selectTopBoardButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String url = getResources().getString(R.string.main_request_btn06_url);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
selectBody.addView(c0001);
}
i think your subview layout_width="match_parent" please try this code
c0001.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
selectBody.addView(c0001);
Try to set the gravity for custom views like this
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
c0001.setLayoutParams(params); //your custom ThemeView

Programmatically generate android layouts

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

how to add multiple textboxes in a single scrollbar programatically

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

Dynamically added ListView shows only the first item

I'm trying to generate a dynamic screen that contains a "textview", "edittext","button" and "listview". I'm trying to add what is written in the edittext to my listview. I'm not getting any errors and i can see added items inside my ArrayList when i debug the code. But listview shows only the very first item added into the list.
LayoutParams layoutMatchParent = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams layoutWrapContent = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams layoutMatchParentWidth = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(linearLayout);
setContentView(scrollView, layoutMatchParent);
LinearLayout vgMul=new LinearLayout(this);
vgMul.setOrientation(LinearLayout.VERTICAL);
TextView tvMul=new TextView(this);
tvMul.setText("Some Label");
tvMul.setId(1);
tvMul.setLayoutParams(layoutWrapContent);
vgMul.addView(tvMul);
EditText edtMul=new EditText(this);
edtMul.setId(2);
edtMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(edtMul);
Button btnAddMul=new Button(this);
btnAddMul.setText("Add");
btnAddMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(btnAddMul);
ListView lvMul =new ListView(this);
lvMul.setId(3);
lvListItems =new ArrayList<String>();
lvArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvListItems);
lvMul.setAdapter(lvArrayAdapter);
lvMul.setScrollContainer(false);
vgMul.addView(lvMul);
btnAddMul.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
EditText edtMul=(EditText)findViewById(2);
lvListItems.add(edtMul.getText().toString());
lvArrayAdapter.notifyDataSetChanged();}
});
linearLayout.addView(vgMul);
Is this because of the scrollview? I'm adding my controls as children of a linear layout but i'm stuck...Any help will be appreciated...Thanks...
Remove your scroll view, a listview already implements a scrollview.
Alright, so I assume you have an activity, some part of that activity has a listview, you want to scroll on the listview and also scroll the whole activity.
You can do it by creating the listview in another Relative Layout (or Linear layout). Let me write an example for you in xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
//Your text views and other stuff goes here
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/SecondRelativeLayoutDetailedPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#color/holoActionBarColor">
<ListView
android:id="#+id/myRewardsActiveList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/myActiveRewardsHeadingLinLayout">
</ListView>
</RelativeLayout>
</RelativeLayout>
Does this make more sense now?

Add layout in method android

I would like to know how can I create method to add linearlayout to other layout. I want to create method which when I use this method in activity, this method add linear layout in top of my activity layout. How can I do that?
Edit:
I do something like that:
public class SecondActivity extends Activity {
LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
layout = (LinearLayout) findViewById(R.id.lay);
Button next = (Button) findViewById(R.id.nextActivity);
layout.addView(addNewLinearLayout(getApplicationContext()));
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
}
});
}
private View addNewLinearLayout(Context context) {
LinearLayout linearLayout = new LinearLayout(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
// Do something else here on your linear layout or to customize your linear layout
return linearLayout;
}
}
and this is xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/nextActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginTop="100dp"
/>
</LinearLayout>
but my layout is not changing his color. Why?
Try something like this :
private View _addNewLinearLayout(context) {
LinearLayout linearLayout = new LinearLayout(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(params);
// Do something else here on your linear layout or to customize your linear layout
return linearLayout;
}
In the main you could call this like :
getView().addView(_addNewLinearLayout(context));
You can easily create a new LinearLayout like this:
LinearLayout linLayout = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
And then set it with:
setContentView(linLayout, layoutParams);
You use this sample method. You can add more parameters if you want.
public LinearLayout addLinearLayout(Context context) {
LinearLayout newLayout = new LinearLayout(context);
//Add stuffs here, like LayoutParams
return newLayout;
}
yourLinearLayoutName.addView(addLinearLayout(yourClassName.this));
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View detailView = inflater.inflate(R.layout.yourNewLayout, l_details, false);
where l_details is the instance of the Linear layout in which you want add another linear layout.

Categories

Resources