Setting Layouts in ViewPagerIndicator? - android

Hi there I am having a bit of struggle trying to use Jake's ViewPagerIndicator. My first problem is how can I set different layouts for different page in the ViewPagerIndicator ?.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setText("Hello!");
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.addView(text);
return layout;
}
I have edited the sample code very briefly but I do not know how to change layouts between different page swipes and I would also like to know how to set the id of the items that I am creating in the ViewPagerIndicator so I can make responses ?.
I have tried to say something like
text.onclicklistener
But I keep on having errors with this code. I am now also wondering whether or not I will have to set an ID of all my items that I create inside of his class. I also tried this but with no success. Do i have to use an ID in order to use the OnClickListener .etc?
text.setId("android:id#hello");
Thanks

You can do so by tweaking the code !
First comment this part:
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 20; i++) {
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() - 1);
Second add this: fragment.mContent = content; // below the loop
third you have to inflate your layouts inside onCreateView():
View view=null;
if(mContent.equals("title1"))
{
view = inflater.inflate(R.layout.title1, container, false);
}
else if(mContent.equals("title2"))
{
view = inflater.inflate(R.layout.title2, container, false);
}
return view;

Related

Create buttons in sequential order programmatically

I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class

Programatically add LinearLayout in fragment with padding between buttons

I want to know how to add or link with findviewID a LinearLayout with multiple buttons (programatically added) and space between them. I have tried to create a XML layout in my fragment and link it to a variable of the type LinearLayout and work from there with LayoutParams but I don't get it to work properly. I would like to know if anyone here has any suggestions. below is my code.
private GuiLoader guiloader = new GuiLoader();
private LinearLayout layout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.media_list_fragment, container, false);
layout = (LinearLayout)v.findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f);
param.setMargins(100, 100, 100, 100);
layout.setPadding(500, 86, 50, 50);
layout.setLayoutParams(param);
and the button creation throug a class
for (final VideoDevice videoDevice : videoDevices) {
Button myButton = guiloader.createButton(getActivity());
myButton.setBackgroundColor(getResources().getColor(R.color.green));
myButton.setText(videoDevice.description);
layout.addView(myButton, guiloader.buttonWidth, guiloader.buttonHeight);
Thanks in advance!
When you call setPadding and setLayoutParams on your LinearLayout - this deals with the padding/margins around the edges of the LinearLayout - not between the buttons.
It sounds like you would like to have a gap between the buttons. For that, you can:
1) add a margin to each button inside your for loop
2) consider using GridView which allows you to specify the spacing between children using android:horizontalSpacing and android:verticalSpacing

how to insert a new LinearLayout after a specific one everytime i click a button?

I want to build some sort of twitter application. In DashboardActivity i need to add a status box everytime i click the "Post" button.My dashboard xml looks like this:
<RelativeLayout>
<LinearLayout></LinearLayout> -->header layout
<LinearLayout></LinearLayout> -->some layout with some titles
<LinearLayout></LinearLayout> --> post status layout with the post button
<LinearLayout></LinearLayout> --> layout with a horizontal rule
<LinearLayout></LinearLayout> --> this is the layout with id "rootStatusBox" where i want to add the status box
</RelativeLayout>
Now, i want to be able to add a new LinearLayout after the horizontal rule layout everytime i click the "Post" button.
I tried something like this in my DashboardActivity:
postStatus.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
addUserStatusBox(firstname,lastname,status);
}});
And addUserStatusBox() looks like this:
public void addUserStatusBox(String firstname, String lastname,String status) {
LinearLayout rootStatusBox = (LinearLayout)findViewById(R.id.rootStatusBox);
LinearLayout userStatusBox = new LinearLayout(this);
userStatusBox.setOrientation(LinearLayout.HORIZONTAL);
userStatusBox.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setMargins(0, 300, 0, 0); // llp.setMargins(left, top, right, bottom);
userStatusBox.setLayoutParams(layout);
TextView friendName = new TextView(this);
TextView friendStatus = new TextView(this);
TextView dataCrearePost = new TextView(this);
friendName.setText(firstname+ " " + lastname);
friendName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
friendName.setTextSize(10);
friendName.setTypeface(null, Typeface.BOLD);
friendStatus.setText(status);
friendStatus.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(-70, 20, 0, 0); // llp.setMargins(left, top, right, bottom);
friendStatus.setLayoutParams(llp);
friendStatus.setTextSize(10);
userStatusBox.addView(friendName);
userStatusBox.addView(friendStatus);
rootStatusBox.addView(userStatusBox);
}
This is working only for the first time when i add a status.I don't know how to add more posts after the horizontal rule layout and to be able to see the old posts below my new one.I would appreciate a little bit of help.Thank you
I would use a customized list view for this purpose.
You need to create the following:
Layout for ListItem: This represents single row in the list. You can customize it by creating separate layout for this. Say you create: listitem_post.xml
Adapter: Write an adapter by extending BaseAdapter class (say: PostsAdapter.java). Fill in all the overridden methods. Most importantly, in the getView() method, inflate the post_listitem. Assign that to convertView object (which is passed in as an argument).
public View getView(int index, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.listitem_post, parent, false);
}
//Code other parts
return convertView;
}
Activity: In your xml code of activity, insert a ListView say listview_posts. In the java file for the activity, set adapter created in step 2 for listview_posts inside onCreate() method.
PostsAdapter postsListAdapter = new PostsAdapter();
ListView postsListView = (ListView) this.findViewById(R.id.listview_posts);
postsListView.setAdapter(postsListAdapter);
That is how you specify that each list element is listitem_post.
Follow this tutorial

Android: Adding multiple Views to a Linear Layout dynamically

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 to add View dynamically from right side in Android?

I need to layout the views from the RIGHT side in a Fragment in Android, however, android did not layout the sub views as what I thought.
I tried to add a TableLayout and an ImageView to a LINEARLAYOUT, the width of the ImageView was fixed and the width of TableLayout is dynamic. Furthermore, the ImageView need to be located on the right side.
Part of the source code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Context c = getActivity().getApplicationContext();
LinearLayout l = new LinearLayout(c);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 0);
params.gravity = Gravity.RIGHT;
l.setLayoutParams(params);
// l.setGravity(Gravity.RIGHT);
PankouAttachmentView pav = new PankouAttachmentView(c, null);
pav.setLayoutParams(params);
l.addView(pav);
ImageView iv = new ImageView(c);
iv.setClickable(true);
iv.setFocusable(true);
iv.setImageResource(R.drawable.testarrow);
iv.setMaxWidth(BUTTON_WIDTH);
iv.setLayoutParams(new LayoutParams(BUTTON_WIDTH,
LayoutParams.MATCH_PARENT));
l.addView(iv);
return l;
}
Any help will be appreciated.THX :)
I'm not sure I completely understand your question but have you tried using a relative layout? Relative layout lets you accomplish much easier than a linear layout. See the android hello views tutorial.

Categories

Resources