I'm trying to display two RelativeLayouts in a LinearLayout, however the best result I'm getting is getting one of the two displayed.
final LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutAll = (RelativeLayout) inflate.inflate(
R.layout.activity_main, null);
final RelativeLayout menuLayout = (RelativeLayout)inflate.inflate(
R.layout.bottom_menu, null);
menuLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
layoutMain.addView(layoutAll);
layoutMain.addView(menuLayout);
layoutAll is showing, and if I change the order of the addView lines, then menuLayout is showing. So far I've tried different layout parameters (MATCH_PARENT for layoutAll or menuLayout and even both). Any suggestion ? Thanks.
Related
I search for this problem but i didn't find a convenient answer.
I try to add dynamically a TextView to a LinearLayout, but the problem is that the setGravity doesn't work.
This is my code:
TextView textView = new TextView(mContext);
textView.setTextColor(Color.BLACK);
textView.setText(colorKeyword(resources.getString(R.string.tipsbutton_tooltip)));
textView.setTextSize(35);
textView.setTextScaleX(1.1f);
textView.setPadding(textViewPadding, textViewPadding, 0, textViewPadding);
textView.setTextAppearance(mContext, R.style.fontForTooltipsTexts);
textView.setGravity(Gravity.CENTER_VERTICAL);
textView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textView.setBackgroundResource(R.drawable.tip_box);
tooltipView.setBackgroundResource(android.R.color.transparent);
ImageView arrowView = new ImageView(mContext);
arrowView.setImageResource(R.drawable.tip_arrow_down);
arrowView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
arrowView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tooltipView.setOrientation(LinearLayout.VERTICAL);
tooltipView.addView(textView);
tooltipView.addView(arrowView);
If i set the layout height of the textview to MATCH_PARENT, the gravity knows to center the text horizontal as i need, but the side effect is that all the background/ textview area is stretched to the parent height.
I now for sure is a problem related to the order in which they are laid out. Is probably something that i miss to do.
I use this for parent:
LinearLayout tooltipView = new LinearLayout(mContext);
tooltipView.setOrientation(LinearLayout.VERTICAL);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
tooltipView.setLayoutParams(lp);
tooltipView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
tooltipView.setGravity(Gravity.CENTER_VERTICAL);
I tried to inflate from a layout in which i already added the background, but i have the same problem.
The height of the TextView should match to the background image height.
Thanks for any suggestions!
So I'm having trouble getting a TextView to appear programmatically with Java. Here's the code:
LinearLayout layout=new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 0.0F);
TextView tx = new TextView(this);
tx.setText("Hello World");
layout.addView(tx);
Are you ever setting the content view for the current activity? The LinearLayout you're dynamically creating doesn't appear to ever be displayed.
Do something like this after you create it (or create the linear layout in your XML first, then dynamically add the text box):
setContentView(layout)
I want to create a TextView over an ImageView in my Android-App. I know I need a RelativeLayout for this, but I don't know how to create this in my main.java (NOT in XML).
It should be dynamic, so I want to create a for-loop which creates many ImageViews with a TextView over it in a HorizontalScrollView.
This is my approach
LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1);
for (int i=1 ; i<=3; i++){
String uri = "drawable/test"; //only one picture several times
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView iv = new ImageView (this);
iv.setBackgroundResource (imageResource);
sv.addView(iv);
}
This only add ImageViews to my HorizontalScrollView (LinearLayout is located in the HorizontalScrollView), but now I also want to add TextViews over my ImageViews. I tried lot of things but nothing works.. I despair.
Hopefully someone can help me
PS: Sorry for my spelling mistakes, I'm from Germany
If you know it to design in xml, create a XML and inflate it.
Below is the code that explains, How to use it.
Code Snippet
//Parent View
LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1);
//infalter service
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Loop to create all the child views
for (int i=0; i<=3; i++) {
//Refrence your layout xml here
LinearLayout childView= inflater. inflates (R. layout. your_xml, null);
//Get refrence to your button and imageview using childView
//add child view
sv.addView(childView);
}
//add sv to horizontal scroll view
Dude try custom views. Create one XML file and create one basic layout . And then inflate that view as many time as u want don't do with for loop. I thnk u r new in android.
http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription&aid=115&aaid=137
Here is the code snippet:
//The view is created manually
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myListView = (ListView) inflater.inflate(R.layout.my_lv_layout, null, false);
... //the ListView is prepared (that part of my program works)
RelativeLayout.LayoutParams myListViewParams =
new RelativeLayout.LayoutParams(width, height);
myListViewParams.addRule(RelativeLayout.ABOVE, R.id.button_at_the_bottom_of_the_relative_layout);
//the listView is added here, but it is not above the button, but in the upper
//left corner of the screen (the above-rule is simply ignored)
myRelativeLayout.addView(myListView, myListViewParams);
(But the rule RelativeLayout.ALIGN_PARENT_RIGHT does work here)
In the file my_lv_layout.xml are no rules defined except the width and height attributes.
How can I solve this problem?
i came across this
http://www.brighthub.com/mobile/google-android/articles/48845.aspx
i understand i can create a view. But i want to define a layout xml and put that into my view. for example in layout i will have a few textviews, i will set value to them dynamically and add them to view and display. is this possible if yes how ? if no whats the alternative ?
Look at LayoutInflater
Sample:
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout dialerLayout = (LinearLayout) layoutInflater.inflate(R.layout.phone_dialer, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
dialerLayout.setLayoutParams(params);
LinearLayout tabDialer = (LinearLayout) findViewById(R.id.tabDialer);
tabDialer.addView(dialerLayout);