How to create Scrollview programmatically? - android

I have one table "TABLE_SUBJECT" which contain a number of subjects. I need to create
one horizontal scroll view with Subject.
How do I create a ScrollView with database items programmatically? If I enter 1o subject then it will be appear in scroll view as a button. Is it possible?

you may create it as below:
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
scroll.addView(yourTableView);

if you have many elements first you need to wrap-up and add in the Scroll view; for example i need a many text view inside of scrollview, so you need to create ScrollView->LinearLayout->Many textview
ScrollView scrollView = new ScrollView(context);
scrollView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
TextView textView = new TextView(context);
textView.setText("my text");
LinearLayout linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.RIGHT);
linearLayout.addView(textView);
scrollView.addView(linearLayout);

this may help you.
HorizontalScrollView hsrll = (HorizontalScrollView)findViewById(R.id.hrsll);
b = new Button(this);
for (int i = 0; i < 5; i++) {
b.setWidth(LayoutParams.WRAP_CONTENT);
b.setHeight(LayoutParams.WRAP_CONTENT);
b.setText("b"+i);
b.setId(100+i);
hsrll.addView(b);
}
instead of for loop just modify the code as your need(no of records in db). but this the code for creating buttons in dynamically.

I was doing it like this:
Create xml with LinearLayout inside the ScrollView
Create xml as item in ScrollView
In activity set main content as xml with ScrollView
Loop through all table elements with adding new View to LinearLayout form main view
For me works fine.

In Kotlin you can use the below code
val scroll = ScrollView(context)
scroll.setBackgroundColor(R.color.transparent)
scroll.layoutParams = LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT
)
scroll.addView(yourTableView)

Related

Placing Multiple Buttons to a Relative Layout without a Grid in Android

I want to add multiple buttons to a layout programmatically.
However, count of buttons is different every time and I just want them to placed next to each other with a wrap content width. After a line is filled, it should go to next line and continue that way.
What is the cleanest way to achieve that?
Thanks.
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
while(isActive) {
LinearLayout horziontalLayout = new LinearLayout(context);
horziontalLayout.setOrientation(LinearLayout.HORIZONTAL);
add buttons here..
//Example Button button = new Button(context);
//horziontalLayout.addView(button);
verticalLayout.addView(horziontalLayout);
isActive = false // when youe done filling up buttons..
}
easiest way should be having a vertical LinearLayout in wich you add multiple horizontal LinearLayout
//vertical one
LinearLayout vlinear = new LinearLayout(this);
vlinear.setOrientation(LinearLayout.VERTICAL);
LayoutParams vlinearParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
vlinear.setLayoutParams(vlinearParams);
parentgroup.addView(vlinear);
//horizontal lines
for (int i=0;i<numlines){
LinearLayout hlinear = new LinearLayout(this);
hlinear.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams vlinearParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
hlinear.setLayoutParams(hlinearParams);
vlinear.addView(hlinear);
///add the loop for adding cells
for...
hlinear.addView(cell);
}

how to implement vertical scroll on pop-up window in android?

I've tried with a piece of code but it is not displaying vertical scroll bar. my code is pasted below:
public void init() {
popupButton = (Button) findViewById(R.id.textview1);
popupText = new TextView(this);
insidePopupButton = new Button(this);
layoutOfPopup = new LinearLayout(this);
LinearLayout lt=new LinearLayout(this);
view=new ScrollView(this);
insidePopupButton.setText("OK");
popupText.setText("This is Popup Window.press OK to dismiss it.");
popupText.setBackgroundColor(Color.WHITE);
popupText.setPadding(0, 0, 0, 20);
layoutOfPopup.setOrientation(1);
lt.addView(popupText);
layoutOfPopup.addView(insidePopupButton,350,35);
layoutOfPopup.setBackgroundColor(Color.BLACK);
view.addView(lt);
layoutOfPopup.addView(view);
Thank you in advance..:)
ScrollView cannot be combine with popupWindow in android, no matter what. Sad but true.
You need to add your views in this way:
LinearLayout linearLayout = new LinearLayout(this);
// add all your views to linearLayout(or RelativeLayout)
linearLayout.addView(popupText);
linearLayout.addView(insidePopupButton);
// add linearLayout to ScrollView instance
view.addView(linearLayout);
// add ScrollView instance to main layout
layoutOfPopup.addView(view);
ScrollView is a single element container.

LinearLayout Horizontal inside LinearLayout Vertical

I am trying to put a linearlayout vertical inside the linear layout programmatically, but it seems that its not working, the buttons doesnt appear, but the text view appear...
Here is my code:
(this is for a dialog..)
LinearLayout titleLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.VERTICAL);
m_titleView = new TextView(m_context);
m_titleView.setText(title);
LinearLayout horizontalLayout = new LinearLayout(m_context);
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
Button backward = new Button(m_context);
backward.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
backward.setText("Backwards");
Button newDirButton = new Button(m_context);
newDirButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newDirButton.setText("New folder");
horizontalLayout.addView(backward);
horizontalLayout.addView(newDirButton);
titleLayout.addView(m_titleView);
titleLayout.addView(horizontalLayout);
Thanks in advance!
Try setting LayoutParams on horizontalLayout.
Anyway, I'd suggest moving to xml world, as this code is not maintainable.
Edit:
Answer found by the author:
titleLayout.setOrientation(LinearLayout.HORIZONTAL);
should be:
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);

how to add dynamic image with horizontal scrollview/listview

I have to develop five to six horizontal scrolling view,in this scrolling image add dynamic using adapter.
Feature:
1) Image can be drag and drop one scroll view to another
2) image can be move one horizontal scrolling to another view
3) image can be selected/unselected
4) with with all version
i am using this library, but sometime scrolling is not that much smooth(chopping)
Check this links may be it's useful to you.
1) Insert a view dynamically in a HorizontalScrollView in Android
2) http://android-er.blogspot.in/2012/07/implement-gallery-like.html
3) Horizontal ListView like Google Catalogs
Using this code you can add android control programatically to linear layout and just add Horizontal Scrollview to Linear Layout through xml
You will get horizontally listview.
//My coding here.
String[] name={"PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT","PRASHANT"} ;
myLInearLayoutmain =(LinearLayout) findViewById(R.id.linearLayoutmain);
for(int i =0;i<6;i++)
{
LinearLayout li=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
li.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams paramsnew = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
params1.setMargins(30, 20, 30, 0);
//add textView
valueTV = new TextView(this);
valueTV.setText(""+name[i]);
valueTV.setId(5);
valueTV.setLayoutParams(paramsnew);
valueTV.setGravity(Gravity.CENTER);
// adding Button to linear
valueB = new Button(this);
valueB.setText(""+name[i]);
valueB.setId(i);
valueB.setLayoutParams(params);
valueB.setOnClickListener(this);
valueB.setGravity(Gravity.CENTER);
// adding Imageto linear
img = new ImageView(this);
img.setImageResource(R.drawable.ic_launcher);
img.setLayoutParams(paramsnew);
//add the textView and the Button to LinearLayout
li.addView(valueTV);
li.addView(valueB);
li.addView(img);
li.setLayoutParams(params1);
myLInearLayoutmain.addView(li);
}

Problems vertically aligning a Button and horizontally oriented LinearLayout within a vertcally oriented LinearLayout

Im posting from my phone so please excuse stupid typos and formatting issues.
I have an activity which lists saved games that the player can load.
I created a simple layout xml file which defines a ScrollView. On load, I grab all the saved games and programatically add a view for each saved game to a vertically oriented LinearLayout child of the ScrollView.
The view for each game consists of a Horizontally oriented LinearLayout which in turn contains a Button and a vertically oriented LinearLayout. That LinearLayout in turn contains some TextViews and ImageViews (and one more LinearLayout which I'm ommitting here for the sake of clarity).
The hierarchy looks something like this (some details omitted).
ScrollView
LinearLayout - vertical
Each saved game:
LinearLayout - horizontal
Button - load game
LinearLayout - vertical
TextView - game name
TextView - date string
My problem:
I would like the top of the button and the "game name" texview to be vertically aligned but the TextView (or maybe it's LinearLayout parent) has some rogue padding on top that I can't get rid of. See screenshot for details.
LoadSaved class:
Note: mScrollView is badly named. It refers to the ScrollView's child LinearLayout.
public class LoadSaved extends Activity {
public LinearLayout mScrollView;
private MinerDb mDb;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.loadsaved);
mDb = new MinerDb(this);
mScrollView = (LinearLayout) findViewById(R.id.load_scroll_view);
Bundle[] savedGames = mDb.getSavedGames();
for (int i = 0; i < savedGames.length; i++) {
Bundle game = savedGames[i];
final int gameId = game.getInt("gameId");
String name = game.getString("name");
String date = game.getString("date");
Bundle player = game.getBundle("player");
int playerMoney = player.getInt("money");
int playerHealth = player.getInt("health");
LinearLayout gameContainer = new LinearLayout(getApplicationContext());
gameContainer.setPadding(5, 5, 5, 5);
gameContainer.setGravity(Gravity.TOP);
gameContainer.setOrientation(LinearLayout.HORIZONTAL);
gameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button loadButton = new Button(getApplicationContext());
loadButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
loadButton.setText("Load");
LinearLayout gameInfo = new LinearLayout(getApplicationContext());
gameInfo.setOrientation(LinearLayout.VERTICAL);
gameInfo.setPadding(10,0,10,10);
gameInfo.setGravity(Gravity.TOP);
gameInfo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView nameView = new TextView(getApplicationContext());
nameView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
nameView.setGravity(Gravity.TOP);
nameView.setText(name);
TextView dateView = new TextView(getApplicationContext());
dateView.setPadding(5,0,0,0);
dateView.setGravity(Gravity.TOP);
dateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
dateView.setText(date);
LinearLayout playerView = new LinearLayout(getApplicationContext());
playerView.setOrientation(LinearLayout.HORIZONTAL);
playerView.setPadding(5,0,0,0);
playerView.setGravity(Gravity.TOP);
playerView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView playerMoneyView = new TextView(getApplicationContext());
playerMoneyView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
playerMoneyView.setPadding(0,0,10,0);
playerMoneyView.setTextColor(Color.GREEN);
playerMoneyView.setText("$" + playerMoney);
TextView playerHealthView = new TextView(getApplicationContext());
playerHealthView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
playerHealthView.setPadding(0,0,10,0);
playerHealthView.setTextColor(Color.RED);
playerHealthView.setText(playerHealth + "%");
playerView.addView(playerMoneyView);
playerView.addView(playerHealthView);
gameInfo.addView(nameView);
gameInfo.addView(dateView);
gameInfo.addView(playerView);
gameContainer.addView(loadButton);
gameContainer.addView(gameInfo);
mScrollView.addView(gameContainer);
loadButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.e("LoadSaved", "LoadSaved::onCreate: Clicking: " + gameId);
Intent loadGameIntent = new Intent(LoadSaved.this, Miner.class);
loadGameIntent.putExtra("load_game", gameId);
startActivity(loadGameIntent);
finish();
}
});
}
}
}
loadsaved.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/load_scroll_view" />
</ScrollView>
</LinearLayout>
If you want any kind of alignment, why don't you use a RelativeLayout? That's basically designed to align one view with another. android:layout_alignTop sounds like something you want.
(And, of course, verify that the padding values are the same in all controls, but I'm sure you did that.)
Why don't you try using a ListView for that kind of gui.
You will still need to define a row xml.
+1 to the answers suggesting ListView and RelativeLayout. For this type of situation you probably want a ListView with an item layout using RelativeLayout. (ListView will scale much better if there are many items, and if this is for a list of saved games it seems like this could grow quite a bit.) For this type of UI it's recommended to have the whole row/list item clickable rather than use a small Load button, but that's a design issue and ultimately up to you.
Don't use getApplicationContext for creating your views. Activity is a Context, just pass this in your case.
By default LinearLayouts try to align child views by their text baseline if present. Note that the bottom of your button's Load text aligns perfectly with the CURRENT_GAME text in your screenshot. Try gameContainer.setBaselineAligned(false).
Normally your gameInfo layout would only report the baseline of one of its children if you set a baselineAlignedChildIndex, but it looks like this behavior changed between cupcake and eclair when creating LinearLayouts programmatically. (Link to the commit that changed it in AOSP here.)

Categories

Resources