No scrolling effect with ScrollView in PopupWindow - android

I want to implement a popup menu with complex effects, one of them is to support scrolling. It seems PopupMenu and AlertDialog can not meet the requirement what I need. So I tried PopupWindow with ScrollView.
Firstly, I prepared a layout which has a simple strcture just like what ApiDemo shows:
ScrollView
LinearLayout with Vertical attribute
TextView
TextView
TextView
...
Secondarily, I new a PopupWindow with this layout and 200width/300height, show it at somewhere with showAtLocation().
I can make scrollbar displayed and has scroll effect, but TextViews in LinearLayout do NOT scroll(they are in fixed position)!
Something is wrong but I have no sense on it.
(android3.0)
Thanks for any warm-heart man who can give me some tips.
-_-!!

I also faced similar issue. Some how wrapping relative layout in scrollview is not working for popupwindow.
I tried wrapping individual views under scrollview and it worked for me. PLease have a look below. Hope this helps
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#eebd9c"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView2" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:ems="15"
android:gravity="fill_horizontal"
android:minLines="6"
android:scrollbars="vertical"
android:singleLine="false"
android:text="Multiline text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView></RelativeLayout>

I think your requirement can be fullfilled by Dialog try the code i have given
protected void showInputDialog() {
final Dialog splRerDialog = new Dialog(getContext());
splRerDialog.setTitle("Special request.");
ScrollView scroll = new ScrollView(getContext());
LinearLayout lin = new LinearLayout(getContext());
lin.setLayoutParams( new LayoutParams(350,200));
lin.setGravity(Gravity.CENTER);
lin.setOrientation(LinearLayout.VERTICAL);
final EditText req = new EditText(getContext());
req.setLayoutParams( new LayoutParams(300,300));
lin.addView(req);
Button btn = new Button(getContext());
btn.setText("Ok");
btn.setLayoutParams( new LayoutParams(200,LayoutParams.WRAP_CONTENT));
btn.setGravity(Gravity.CENTER);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
splRerDialog.dismiss();
}
});
lin.addView(btn);
scroll.addView(lin);
splRerDialog.addContentView(scroll, new LayoutParams(LayoutParams.WRAP_CONTENT,200));
splRerDialog.show();
}

Related

Adding views programatically to a relative layout within a scrollview

Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !

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?

what is root layout in XML?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:scrollbars="none"
android:layout_x="0dp"
android:layout_y="0dp"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:src="#drawable/background" />
</LinearLayout>
</ScrollView>
</LinearLayout>
this is my xml file .which is pity much simple. My intention is to increase height of scroll view dynamically and the image (which is with the scroll view) view will be shown gradually.so how can i do that and What is rootLayout here and How i call rootLayout from my code ??
final Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable()
{
public void run() {
secondCounter++;
yourNewHeight += 10;
sv.getLayoutParams().height = yourNewHeight;
LinearLayout root = (LinearLayout) findViewById(R.id.rootLayout);
root.invalidate();
Log.v("", "" +sv.getLayoutParams().height);
if(secondCounter == 20){
timer.cancel();
}
}
});
}
}, delay, period);
this is my code in java file.But its not working . guys can you help me out..!!!
The root of this layout is the AbsoluteLayout.
You can obtain a reference to this root View together with all of its children in your Activity using a call like:
mRootView = ((ViewGroup)findViewById(R.id.rootLayout));
Note that AbsoluteLaoyut is long depreciated and you will probably want to replace it with an alternative ViewGroup, such as a LinearLayout, RelativeLayout, etc. depending on the rest of your layout and how you want to do things.
If you've inflated your XML appropriately in code (ie called setContentView()) you should be able to reference rootLayout using:
AbsoluteLayout root = (AbsoluteLayout) findViewById(R.id.rootLayout);
Though if you're just trying to increase the height of the ScrollView, it makes more sense to directly call:
ScrollView scroll = (ScrollView) findViewById(R.id.scrollView1);
And as above, you should probably use a RelativeLayout or LinearLayout rather than an AbsoluteLayout.
Since ScrollView can only have one Child within it, you can use ScrollView itself as your root element with a LinearLayout as a direct and only child to it, inside which u can add any views you want.
And AbsoluteLayout is deprecated, and I'd recommend not to use it

Drawing into scrollView

my problem I know is simple, but for the life of me I cant get an answer...
I have a LinearLayout that got a scrollview inside it... in the scroll view (in code) it should call a custom ScrollView to show on the application... it only shows black, nothing in it... here is my code
first the xml... simple LL with a textview, scrollview and button
<?xml version="1.0" encoding="utf-8"?>
<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_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:text="test field" android:layout_alignParentTop="true"/>
<ScrollView android:layout_width="600dp"
android:layout_height="300dp"
android:id="#+id/v1"
android:fillViewport="true"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/gfx"
android:text="Gfx"
></Button>
</LinearLayout>
Now the onCreate of that activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
cw = getWindowManager().getDefaultDisplay().getWidth();
ch = getWindowManager().getDefaultDisplay().getHeight();
v = Draw2d findViewById(R.id.v1);
context = this;
setContentView(R.layout.gfx);
mDbHelper = new DbAdapter(this);
mDbHelper.open();
}
Draw2d is the class of the custom scrollView declared like so (it does have onDraw and everything. If I set the contentview to the Draw2d object directly it'll show no problem, but I need it withen a LL)
public static class Draw2d extends ScrollView{
public Draw2d(Context context) {
super(context);
}
Please any help is appreciated
Maybe because of you have nothing in the ScrollView? Try putting something in it. Check this link, you see there is another linear layout in the scrollview with "30px" layout height making the scrollview appear. If the layout_height was wrap_content, you would not see the scrollview if there is enough place on the screen for it.

How to use the xml setting in a view of a activity?

I want to show two views in one activity. If I clicked on button in the first view I want to see the second and other way round.
The views should not have the same size as the screen so I want e.g. to center it, like you see in first.xml.
But if I add the views with
addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
the views are not centered. They are shown at top left.
How can I use the xml settings to e.g. center it?
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>
<LinearLayout android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton android:id="#+id/first_button"
android:src="#drawable/show_second"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null" />
</LinearLayout>
second.xml same as first.xml but with
<ImageButton android:id="#+id/second_button"
android:src="#drawable/show_first"
... />
ShowMe.java
public class ShowMe extends Activity {
View mFirstView = null;
View mSecondView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFirstLayout();
initSecondLayout();
showFirst();
}
private void initFirstLayout() {
LayoutInflater inflater = getLayoutInflater();
mFirstView = inflater.inflate(R.layout.first, null);
getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
firstButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ShowMe.this.showSecond();
}
});
}
private void initSecondLayout() {
// like initMaxiLayout()
}
private void showFirst() {
mSecondView.setVisibility(View.INVISIBLE);
mFirstView.setVisibility(View.VISIBLE);
}
private void showSecond() {
mFirstView.setVisibility(View.INVISIBLE);
mSecondView.setVisibility(View.VISIBLE);
}}
Hope someone can help.
Thanks
Why don't you use setContentView(R.layout.yourlayout)? I believe the new LayoutParams you're passing in addContentView() are overriding those you defined in xml.
Moreover, ViewGroup.LayoutParams lacks the layout gravity setting, so you would have to use the right one for the layout you're going to add the view to (I suspect it's a FrameLayout, you can check with Hierarchy Viewer). This is also a general rule to follow. When using methods that take layout resources as arguments this is automatic (they might ask for the intended parent).
With this consideration in mind, you could set your layout params with:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);
addContentView(mYourView, lp);
But I would recommend setContentView() if you have no particular needs.
EDIT
I mean that you create a layout like:
~~~/res/layout/main.xml~~~
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
then in your onCreate() or init...Layout():
setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);
this will keep the layout params from xml, because it knows what kind it needs. See reference.

Categories

Resources