I need to overlap an image view with textview. And this combined view will be repeated 100 times in a LinearLayout. I was thinking of using FrameLayout in LinearLayout and Repeating the FrameLayout in LinearLayout 100 times when FrameLayout holds the imageview and textview overlapped.
Need to do this programatically not from xml file.
I added the image and textview to framelayout first then tried to add the framelayout to linearlayout. But it says : the specified child has already a parent.. so not working. Can you please show me in code? Thanks for your help.
it is going to be like this, but need to be done programmaticaly
---linear layout--------------
------------------------------
|frame layout----------------|
||txt view on top of img view|
------------------------------
frame layout will be repeated|
---/end of linear layout------
Also here is the separated code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout dynamicview = (LinearLayout) findViewById(R.id.main_layout);
FrameLayout barFrameLayout = new FrameLayout(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
Gravity.CENTER);
barFrameLayout.setLayoutParams(params);
LinearLayout.LayoutParams slparams1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 65; i <= 75; i++) {
TextView catTV = new TextView(this);
catTV.setLayoutParams(slparams1);
catTV.setText("===" + Character.toString((char) i) + "===");
catTV.setTextSize(32);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
iv.setLayoutParams(slparams1);
barFrameLayout.addView(catTV);
barFrameLayout.addView(iv);
dynamicview.addView(barFrameLayout);
}
}
Here is the code to demonstrate what you are trying to achieve. I have used RelativeLayout, which is very flexible, you can position the elements easily relative to others.( if you need to change to FrameLayout you can change ).
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ExampleLayout extends LinearLayout{
public ExampleLayout(Context context,AttributeSet attrs){
super(context,attrs);
for(int i =0; i< 100; i++){
RelativeLayout childLayout = new RelativeLayout(context);
ImageView img = new ImageView(context);
TextView text = new TextView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
childLayout.addView(img, params);
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
childLayout.addView(text, params);
LinearLayout.LayoutParams parentParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.addView(childLayout,parentParams);
}
}
}
You can then use the ExampleLayout class to add it to any of the layout.xml file.
FrameLayout is designed to block out an area on the screen to display
a single item
(source: http://developer.android.com/reference/android/widget/FrameLayout.html).
Anyway, you have to create new FrameLayouts not use the same.
When you're doing:
barFrameLayout.addView(catTV);
barFrameLayout.addView(iv);
dynamicview.addView(barFrameLayout);
you're always adding these new objects (catTV and iv) to the same instance of a FrameLayout (barFrameLayout).
I don't think that's what you wanted to do.
Related
When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...
Image to demonstrate:
I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}
You guys are right. It is much easier using a LinearLayout. For those interested
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);
I am trying to create a horizontalscrollview in the onCreate() method of my first activity, since I want to make a large number of textviews to scroll through. Here is what I have so far:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout linscrollview;
HorizontalScrollView scrollview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (HorizontalScrollView) findViewById(R.id.scrollview_layout);
linscrollview = new LinearLayout(this);
for(int i=0; i<5; i++) {
TextView tv = new TextView(this);
tv.setWidth(LayoutParams.WRAP_CONTENT);
tv.setHeight(LayoutParams.WRAP_CONTENT);
tv.setText("" + i);
tv.setTextSize(20);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
params.setMargins(10, 0, 10, 0);
tv.setLayoutParams(params);
tv.setId(i);
linscrollview.addView(tv);
}
scrollview.addView(linscrollview);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I am not getting any errors, however no textviews are showing up.
Your problem is likely to do with the setWidth and setHeight methods. They set the exact value of the TextView width and height in pixels as described in the documentation:
Makes the TextView exactly this many pixels wide. You could do the
same thing by specifying this number in the LayoutParams.
http://developer.android.com/reference/android/widget/TextView.html#setWidth(int)
What you want to do is set the LayoutParams for the TextView as you are already going slightly further down your code. So just get rid of those two method calls and it should work.
this s a chunk of code to implement horizontal scrolling for textview, modify the same according to requirements.
textView.setHorizontallyScrolling(true);
textView.setSingleLine(true);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setHorizontalScrollBarEnabled(true);
textView.setSelected(true);
Please do like this
linscrollview .setOrientation(LinearLayout.HORIZONTAL);
and ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
My coding here. it will gives you list like horizonatal listview
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);
//add the textView and the Button to LinearLayout
li.addView(valueTV);
li.addView(valueB);
li.addView(img);
li.setLayoutParams(params1);
myLInearLayoutmain.addView(li);
}
i've created an android application which creates 50 button dynamically,which works perfectly, but the problem is i'm getting bad view when i put some background color for these buttons dynamically using code given below
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
my code is as given below, can anyone please tell me some solution for this
my Android Platform is 2.3.3
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MyMain extends Activity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mymain);
createCalender();
}
public void createCalender()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
LinearLayout rowLayout=null;
Button[][] buttons = new Button[10][5];
int count=51;
int tab=1;
for (int i = 0; i<10; i++)
{
if(count%5==1)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(5);
layoutVertical.addView(rowLayout,param);
count=count-5;
}
for(int j=0;j<5;j++)
{
buttons[i][j]=new Button(this);
buttons[i][j].setText(""+tab);
buttons[i][j].setHeight(35);
buttons[i][j].setWidth(75);
buttons[i][j].setTextColor(Color.BLACK);
buttons[i][j].setBackgroundColor(Color.GREEN);
tab++;
rowLayout.addView(buttons[i][j],param);
}
}
}
}
You should add some margin to leave these buttons some breathing room.
// First try with WRAP_CONTENT
LinearLayout.LayoutParams paramForButtons = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 1.0f);
// If the buttons look too small using WRAP_CONTENT, change to MATCH_PARENT
// Set the margins in this case
LinearLayout.LayoutParams paramForButtons = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
paramForButtons.setMargins(10, 10, 10, 10);
Replace the last line of your for loop with:
rowLayout.addView(buttons[i][j],paramForButtons);
I've RelativeLayout created in layout/activity.xml
And i want to add some elements there programmatically by following way:
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlayout.addView(CustomView,p);
And it works, but elements which were added doesn't fill all view, but i need it.
and also i want to add such elements in square (width=height), how can I do it?
To fill all view Use LayoutParams.MATCH_PARENT instead of LayoutParams.WRAP_CONTENT.
To make layout as square just create int width,height = 300; and then :
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(width, height);
or pass LayoutParams.WRAP_CONTENT into RelativeLayout.LayoutParams and change height and width of your custom view.
To make view square :
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
to make fill on all view you can use this:
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
Also you can add rules for your layout items like this :
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Best wishes.
I want to show two scrollView in one LinearLayout or RelativeLayout.
Which property should is set to show first Scrollview on the top of screen and second scrollview just below the first Scrollview ?
I have tried but in linearlayout it shows me only first scrollview and in relativelayout it shows me only second scrollView.
Yes, I want to do all this dynamically without using xml file.
Here is my code
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.widget.TableLayout.LayoutParams;
public class TableFinal extends Activity {
LinearLayout linearMain, linearScrollView, linearTextview;
RelativeLayout relativeMain;
ScrollView scrollview;
HorizontalScrollView Hscrollview;
TableLayout tablelayout;
TableRow tablerow;
TextView textview;
LinearLayout.LayoutParams linearparmas;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
linearparmas=new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
relativeMain = new RelativeLayout(this);
// First Table
linearScrollView = new LinearLayout(this);
scrollview = new ScrollView(this);
Hscrollview = new HorizontalScrollView(this);
tablelayout = new TableLayout(this);
// First Table's First Row
tablerow = new TableRow(this);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("11");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("12");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
tablelayout.addView(tablerow);
Hscrollview.addView(tablelayout);
scrollview.addView(Hscrollview);
linearScrollView.addView(scrollview);
relativeMain.addView(linearScrollView);
// first table complete
// second tabler start
linearScrollView = new LinearLayout(this);
scrollview = new ScrollView(this);
Hscrollview = new HorizontalScrollView(this);
tablelayout = new TableLayout(this);
// second Table's First Row
tablerow = new TableRow(this);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("21");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
linearTextview = new LinearLayout(this);
textview = new TextView(this);
textview.setText("22");
linearTextview.addView(textview);
tablerow.addView(linearTextview);
tablelayout.addView(tablerow);
Hscrollview.addView(tablelayout);
scrollview.addView(Hscrollview);
linearScrollView.addView(scrollview);
relativeMain.addView(linearScrollView);
// second tabler complete
setContentView(relativeMain);
}
}
Thank you.
You can use LinearLayout or RelativeLayout.
For all layouts you need to get displayHeight.
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int displayHeight = display.getHeight();
Then for all layouts set layoutParams to fill parent.
Now for scrollViews differences begin.
In LinearLayout for both scrollViews set the same layoutParams (LinearLayout.layoutParams)
scrollView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, displayHeight/2));
And add both to linearLayout
In RelativeLayout set layoutParams like in my example (RelativeLayout.layoutParams)
RelativeLayout.LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT, displayHeight);
lp1.addRule(ALIGN_PARENT_TOP);
scrollView1.setLayoutParams(lp1);
RelativeLayout.LayoutParams lp2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp2.addRule(BELOW, scrollView1.getId());
scrollView2.setLayoutParams(lp2);
And add both to layout.
Should work in all display resolution if I did not messed up.