I have seen some explanations on how to add two views next to each other in LinearLayout by using XML. How can this be done programatically?
I have the following two methods:
private void createListOfInstalledApplications() {
installedApplicationNames = this.getApplicationNames();
installedPackages = this.getPackageNames();
for(int i = 0; i < installedApplicationNames.size(); i++){
TextView scrollAppsView = new TextView(this);
scrollAppsView.setId(i);
scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
layout.addView(scrollAppsView);
}
}
This method adds the names of installed applications on the Android phone to the LinearLayout. On the right side of these applications, I would like to have a drop down menu where some settings can be chosen. For now the following methods adds the Spinners to the LinearLayout:
private void createDropDownMenuesForApplications() {
List<String> vibPatternNames = new ArrayList<String>();
VibrationPatternManager vibPatManager = VibrationPatternManager.getInstance();
List<VibrationPattern> vibrationPatterns = vibPatManager.getAllAvailablePatterns();
for(VibrationPattern vibrationPattern : vibrationPatterns){
vibPatternNames.add(vibrationPattern.getName());
}
for(int i = 0; i < installedApplicationNames.size(); i++){
Spinner dropDownMenu = new Spinner(this);
dropDownMenu.setId(i);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, vibPatternNames);
dropDownMenu.setAdapter(dataAdapter);
layout.addView(dropDownMenu);
}
}
The drop-down menus now appear below the application names. However, I would like them to appear to the right. So
"ApplicationName1 drop-down-menu1"
instead of
ApplicationName1
ApplicationName2
Drop-down-menu1
Drop-down-menu2
How do I do this?
Just do
layout.setOrientation(Horizontal)
I have done similar task as required by you, please have a look at my answer:
https://stackoverflow.com/a/33339420/5479863
in JSON response image URLs were provided so, at runtime I have to add views dynamically adjacent to each other.
Look at the answer to know more...
I hope that will help you...
Happy coding :)
Try the following:
1) MActivity.class--------------
public class MActivity extends AppCompatActivity {
private LinearLayout layout_horizontal;
private LinearLayout layout_vertical;
private LayoutInflater layoutInflater;
private FrameLayout fl;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Frame layout generic container --- root.
// Frame layout has as a direct child layout_vertical (Linear layout with orientation vertical).
// for each item inside installedApplicationNames, re-create a linear layout with orientation horizontal (layout_horizontal),
// with a TextView and Spinner and then add it as a direct child of the linear layout with orientation vertical.
fl = (FrameLayout) findViewById(R.id.fl);
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.layout_vertical , null);
fl.addView(view);
layout_vertical = (LinearLayout) view.findViewById(R.id.ll_v);
populate();
}
private void populate() {
layout_vertical.removeAllViews();
List<String> installedApplicationNames = new ArrayList<String>();
for (int k = 0; k < 30; k++) {
installedApplicationNames.add("N" + k);
}
for (int i = 0; i < installedApplicationNames.size(); i++) {
View view1 = layoutInflater.inflate(R.layout.layout_horizontal , null);
layout_horizontal = (LinearLayout) view1.findViewById(R.id.ll_h);
TextView scrollAppsView = new TextView(this);
scrollAppsView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT));
scrollAppsView.setId(i);
scrollAppsView.setText(installedApplicationNames.get(i) + "\n ");
layout_horizontal.addView(scrollAppsView);
// TODO: use a certain method to get vibPatternNames for the current installedApplicationName
List<String> vibPatternNames = new ArrayList<String>();
for (int j = 0; j < 4; j++) {
vibPatternNames.add("S" + j);
}
Spinner dropDownMenu = new Spinner(this);
dropDownMenu.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
dropDownMenu.setId(i);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, vibPatternNames);
dropDownMenu.setAdapter(dataAdapter);
layout_horizontal.addView(dropDownMenu);
layout_vertical.addView(layout_horizontal);
}
}
}
2) layout.xml-----------
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:id="#+id/fl">
</FrameLayout>
</ScrollView>
3) layout_vertical.xml-----------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll_v"
android:layout_gravity="center"
android:orientation="vertical">
</LinearLayout>
4) layout_horizontal.xml---------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/ll_h"
android:layout_gravity="center"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
Related
I am very new to Leanback Libraries. I want to develop Android TV home screen, where the CustomHeaderItem will be on left side and for each HeaderItem there will be one ListRow and instead of BrowseFragment, i want to render whole things on VerticalGridView. Like below picture.
To achiveve this, i tried to keep an ImageView with HorizontalGridView and both views i rendered inside VerticalGridView.
While doing so, the focus is moving from imageView to respective HorizontalGridView on clicking on Right dpad Key which is expected behaviour but coming back from respective HorizontalGridView to respective ImageView is not happening.
row_grid_view_renderer.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/header_icon"
android:layout_width="300dp"
android:paddingBottom="50dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:gravity="center"
android:focusable="true"
android:layout_weight="1"
android:clickable="true"
android:background="#drawable/selector_bg_card"
android:focusableInTouchMode="true" />
<androidx.leanback.widget.HorizontalGridView
android:id="#+id/row_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:layout_gravity="center"
android:focusableInTouchMode="true"
android:layout_weight="2"
lb:horizontalMargin="12dip"
lb:verticalMargin="24dip"
lb:numberOfRows="3"
lb:rowHeight="150dip" />
</LinearLayout>
main_grid_view_rendere.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.leanback.widget.VerticalGridView
android:id="#+id/parent_grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Below i am overriding the createRowViewHolder() method of ListRowPresenter class to set the imageView and HorizontalGridView.
public class CustomListRowPresenter extends ListRowPresenter {
private static final String TAG = CustomListRowPresenter.class.getSimpleName();
private static final boolean DEBUG = true;
private CustomListRowView rowView;
public CustomListRowPresenter() {
super();
}
public RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
rowView = new CustomListRowView(parent.getContext(),null,-1);
rowView.getImageView().setImageResource(R.drawable.ic_blur_on_black_24dp);
return new ViewHolder(rowView, rowView.getRowGridView() , this);
}
And here is my MainActivity class which is rendering ImageView and HorizontalGridView inside main_grid_view(which is holding VerticalGridView).
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_grid_view);
mVerticalGridView = (VerticalGridView) findViewById(R.id.parent_grid_view);
loadRows();
}
private void loadRows() {
cardPresenter = new CardPresenter();
rowsAdapter = new ArrayObjectAdapter(new CustomListRowPresenter());
List<Movie> list = MovieList.setupMovies();
int i;
for (i = 0; i < NUM_ROWS; i++) {
if (i != 0) {
Collections.shuffle(list);
}
ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
for (int j = 0; j < NUM_COLS; j++) {
listRowAdapter.add(list.get(j % 5));
}
HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i]);
rowsAdapter.add(new CustomListRow(header, listRowAdapter));
}
ItemBridgeAdapter itemBridgeAdapter = new ItemBridgeAdapter(rowsAdapter);
mVerticalGridView.setAdapter(itemBridgeAdapter);
}
I want to get the focus in both respective View while pressing Right and Left key of Dpad.
How i will achieve the above scenario or where i am going wrong ?
And also, How to stop header transition inside BrowseFragment ?
Thanks in advance :)
I created a layout which programmatically created buttons (see code). With this layout I have a column of buttons, but I would create something like the picture below. I tried something with linear layout but the buttons didn't go to new line automatically and didn't show, so I change it to table layout. How can I create something like the picture programmatically?
Thank you.
protected TableLayout layout;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layout = (TableLayout) findViewById(R.id.workflows_activity_layout);
private void createButton() {
loading.setVisibility(View.VISIBLE);
layout.removeAllViews();
if (items.length == 0) {
TableRow row = new TableRow(this);
TextView no_item = new TextView(this);
no_questions.setText("there are no items");
no_questions.setTextSize(35);
row.addView(no_item);
layout.addView(row);
} else {
for (int i = 0; i < items.length; i++) {
TableRow row = new TableRow(this);
final Button btn = new Button(this);
TextView tw = new TextView(this);
tw.setText(items[i].getName());
tw.setTextSize(25);
btn.setBackgroundResource(R.drawable.button_image);
btn.setId(i);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = btn.getId();
Intent openItempage = new Intent(MainActivity.this, ItemsActivity.class);
startActivity(openItempage);
}
});
row.addView(btn);
row.addView(tw);
layout.addView(row, params);
items_buttons.add(btn);
items_nameText.add(tw);
}
}
loading.setVisibility(View.GONE);
}
There's an easy way to achieve what you want. Look at this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
android:id="#+id/tab_lay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
TabLayout tab_lay = (TableLayout)view.findViewById(R.id.tab_lay);
for(int i = 1; i <= 4; ){
TableRow a = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
a.setLayoutParams(param);
a.setGravity(Gravity.CENTER_VERTICAL);
for(int y = 0; (y < 2) && (i <= 4); y++) {
Button x = new Button(getActivity());
x.setText("Text " + i);
x.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
x.setLayoutParams(par);
int ids = i;
x.setId(ids);
x.setOnClickListener(this);
a.addView(x);
i++;
}
tab_lay.addView(a);
}
If you want more buttons, then just change 4 which is compare to i to your value. Hope I help.
You can implement this using labraries, for instance
FlowLayout
Usage:
<com.wefika.flowlayout.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start|top">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />
</com.wefika.flowlayout.FlowLayout>
Another solution is:
AndroidFlowLayout
Usage:
<com.liangfeizc.flowlayout.FlowLayout
android:id="#+id/flow_layout"
flowlayout:vertical_spacing="10dp"
flowlayout:horizontal_spacing="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
I am creating n number of images with wordImage and assign a differnt Id.This happens on a button click add. There is another button remove when I click that all the images that I have created should be removed from the view(Head is the view). Plz help how to do that.
onclick of add button
for (int getwordcount = 0; getwordcount <5; getwordcount++) {
int i=0;
WordImage = new ImageView(this);
WordImage.setId(getwordcount);
WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
para.leftMargin=maragin+=54;
para.bottomMargin=25;
para.width=BtnNext.getWidth()/3;
para.height=BtnNext.getHeight();
WordImage.setLayoutParams(para);
WordImage.setTag(getSplitString);
Head.addView(WordImage);
}
onclick of remove button
only the last -------------> Head.removeView(WordImage);
create image is removed from view.
How do I remove all images created using wordimage.Any help would be greatly appreciated.
WordImage saves link just for the last ImageView. Previous link lost in this line:
WordImage = new ImageView(this);
This is why Head.removeView(WordImage) removes only the last image.
If you want delete all added image elements, the easiest way do this is to add RelativeLayout container like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout android:id="#+id/Head" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"
android:onClick="onClickAdd"
android:id="#+id/button" android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove"
android:onClick="onClickRemove"
android:layout_alignBaseline="#id/button"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Then you can delete all images only by one command:
Head.removeAllViews();
In other case, if you need to remove separate images, you have to delete your items in cycle similar to your "add" block:
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
Head.removeView(findViewById(getwordcount));
}
Activity class will be like this:
public class MyActivity extends Activity {
RelativeLayout Head;
int alphabetsimages[] = {android.R.drawable.arrow_down_float, android.R.drawable.btn_dropdown, android.R.drawable.btn_minus, android.R.drawable.ic_dialog_map};
ImageView WordImage;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickAdd(View v){
int margin = 0;
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
int i=0;
WordImage = new ImageView(this);
WordImage.setId(getwordcount);
WordImage.setBackgroundResource(alphabetsimages[getwordcount]);
RelativeLayout.LayoutParams para=new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
para.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
para.leftMargin=margin+=54;
para.bottomMargin=25;
para.width = 40;
para.height = 40;
WordImage.setLayoutParams(para);
Head= (RelativeLayout)findViewById(R.id.Head);
Head.addView(WordImage);
}
}
public void onClickRemove(View v){
//Head.removeAllViews();
for (int getwordcount = 0; getwordcount <alphabetsimages.length; getwordcount++) {
Head.removeView(findViewById(getwordcount));
}
}
}
I hope that's what you need!
I'm a first-day android programmer and here is my problem: in my app i have a supporting activity which uses static layout; my activity in onCreate method receives a list of data - how should i embed this data into my existing layout?
So far I've tried this code, but it doesn't shows anything, though there're no exceptions and there're few elements, hence loop works:
Activity class:
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.results, null);
// Find the ScrollView
LinearLayout sv = (LinearLayout) v.findViewById(R.id.lMain);
// getting data here
for (int i = 0; i < recipesList.getId().size(); i++) {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
TextView tv = new TextView(this);
tv.setText("abc");
ll.addView(tv);
sv.addView(ll);
}
results.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/lMain" >
</LinearLayout>
</ScrollView>
Your apporch is wrong. You have to do like this.
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.results);
LinearLayout sv = (LinearLayout) findViewById(R.id.lMain);
// getting data here
for (int i = 0; i < recipesList.getId().size(); i++)
{
// Add text
TextView tv = new TextView(this);
tv.setText("abc");
sv .addView(tv);
}
}
i have a complex xml layout which has list views..a row in the list view contains several text fields which are spaced evenly. i am using textview to store the text and then finally add all the items to the row...its working perfectly fine.
but now i have case where in i am not sure, how many text fields i might get from a webservice. therefore i need to create the textview dynamically on run time, populate them and then insert into the list..
is there anyway to declare,add and populate new textview fields on runtime?
or is there is anyway to implement the spacing between the two fields?
result of first call
__________________________
|_____|_____|_____|______|
result of second call
________________________________
|_____|_____|_____|______|______|
I tried implementing the solution that was provided below (Kenny), but for some reason I am unable to add views into the list.. below is my code
public class HeaderAdapter extends ArrayAdapter<Header> {
final Header[] listSymbols;
private TextView textView;
private LinearLayout row;
public HeaderAdapter(Context context, int symResourceID,
Header[] objects) {
super(context, symResourceID, objects);
listSymbols = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.header_view, parent, false);
Header headerRec = listSymbols[position];
for(int i = 0 ; i < listSymbols.length;i++){
textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
textView.setId(i);
row.add??
}
}
The main activity that calls this
setContentView(R.layout.main);
headerList.add(new Header("Symbol","Quantity","Price","Price Yesterday","52 Week High","52 Week Low","Change","Days Gain","Days Gain %","Returns"));
Header[] tmpHeaderList = headerList.toArray(new Header[headerList.size()]);
ArrayAdapter<Header> headerAdapter = new HeaderAdapter(this,R.layout.twoway_header_view,tmpHeaderList);
headerListView.setAdapter(headerAdapter);
xml layout file..the main file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scrollbars="none"
android:id="#+id/headerHv">
<ListView android:id="#+id/header_listView1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#color/white" android:cacheColorHint="#00000000"
android:smoothScrollbar="true" android:scrollbars="none" />
</HorizontalScrollView>
</LinearLayout>
the file in which the template for the row is defined
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="#+id/headerList" android:layout_width="80dp"
android:layout_height="wrap_content" android:textColor="#000000"
android:typeface="sans" android:textStyle="normal" />
</LinearLayout>
Here is the way i dynamically generate custom buttons from a list, you could do the same thing with textViews:
//Setup Buttons
layout = (LinearLayout)findViewById(R.id.layoutBars);
int count = lBars.size();
for(int i = 0; i< count;i++){
final Bar b = lBars.get(i);
BarButton button = new BarButton(DDTBars.this, R.drawable.barsmall , b.getName().toUpperCase());
button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
button.setId(i);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Run activity passing name of the bar to retrieve data
Intent i = new Intent(DDTBars.this, DDTBar.class);
i.putExtra("name", b.getName());
startActivity(i);
}
});
layout.addView(button);
}
So you could try:
//Setup TextViews
layout = (LinearLayout)findViewById(R.id.mylayout);
int count = myTextList.size();
for(int i = 0; i< count;i++){
TextView txtView = new TextView(this);
txtView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
txtView.setId(i);
layout.addView(txtView);
}
You could do it in code. Declare TextView 's in a loop and use RelativeLayout to position them wrt each other.