I have a LinearLayout in main.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="fill_parent"
android:orientation="vertical"
android:id="#+id/mainLayout" >
</LinearLayout>
I have made another XML file, called item_box.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/item_bg"
android:gravity="right" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:text="#string/item1"
android:textSize="30dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="20dp"
android:gravity="right"
android:text="#string/number1"
android:textColor="#color/number_bg"
android:textSize="30dp" />
</LinearLayout>
Basically, what I want to do from the code (programmatically), is add a couple item_box.xml into the main.xml. How can I accomplish this?
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
mainLayout.addView(itemBox);
try this linearlayout inside another linearlayout add id to linearlayout layout1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
in oncreate() call
LinearLayout firstlayout=(LinearLayout)findviewbyId(R.id.firstlayout);
LinearLayout secondlayoout=(LinearLayout)this.getLayoutInflater().inflate(R.layout.layout2,null); //inflating view from xml
TextView btn1=(TextView)secondlayoout.findviewbyId(R.id.button1);
btn1.settext("TEST");
firstlayout.addview(secondlayoout);
You are getting a Nullpointer because linearLayout1 is not in mainLayout. You need to inflate your view first and then add it. You should read this question I think it will help you.
Use LayoutInflater:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemBox = inflater.inflate(R.layout.item_box);
Related
I am trying to resize the inflated view but not working
I am adding this view to AlertDialog
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.popup_util, null,false);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200)); //Set params here
......
dialog.setView(view);
popup_util
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/setting_password_popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPopUpTitle"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:textColor="#000000" android:textSize="24dp" android:background="#color/colorAccent"/>
<TextView android:padding="10dp" android:layout_width="match_parent" android:layout_below="#+id/txtPopUpTitle" android:id="#+id/txtPopUpMessage" android:layout_height="wrap_content" android:textSize="22dp"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal|bottom">
<Button android:id="#+id/btnPopUpCancel"
android:layout_width="match_parent" android:layout_height="50dp"
android:layout_weight="1"
android:textSize="24dp"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:text="#string/btn_cancel_text" />
<Button android:id="#+id/btnPopUpOk"
android:layout_width="match_parent" android:layout_height="50dp"
android:textColor="#color/colorAccent"
android:textSize="24dp"
android:layout_centerHorizontal="true"
android:text="#string/btn_ok_text"
android:background="#android:color/transparent"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
But it is not working.. What i m missing ?
u cane try this code:
View view = inflater.inflate(R.layout.active_slide, this);
view.setMinimumWidth(200);
You can use this code its works good in my case
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
NOTE: Be sure to use the parent Layout's LayoutParams. Mine is LinearLayout.LayoutParams!
You can do that by setting the width and height to the window of the dialog itself.
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, 200);
You must do that after showing the dialog using dialog.show()
I have a work with the RecyclerView, and I use the CardView as well.I have made all these standards procedures (created the ViewHolder, implemented necessary methods) but the problem is that CardView's corners aren't rounded.I get only rectangular corners of the CardView.
Here is the XML code of Adapter's "main layout":
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="15dp"
card_view:cardElevation="10dp"
card_view:cardCornerRadius="30dp">
<RelativeLayout
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/adapterText"
android:text="#string/textCaption"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/adapterDate"
android:text="#string/date"
android:layout_marginTop="10dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/adapterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
And another layout with RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/contactList"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
And initialization of the RecyclerView:
#Override
public void onViewCreated(View root, Bundle savedInstanceState) {
if (root != null) {
RecyclerView mRecyclerContactList=(RecyclerView)(root.findViewById(R.id.contactList));
mContactAdapter=new ContactAdapter(getContext(),mContextualMultiMode,mContactList);
mRecyclerContactList.setAdapter(mContactAdapter);
RecyclerView.ItemAnimator animator=new DefaultItemAnimator();
mRecycle
rContactList.setItemAnimator(animator);
RecyclerView.ItemDecoration mVerticalDecoration=new DividerItemDecoration
(getContext(), LinearLayoutManager.VERTICAL);
mRecyclerContactList.addItemDecoration(mVerticalDecoration);
mRecyclerContactList.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
Does anybody know how to solve this issue?
Cardview as root causes strange problems to me. Try to use Relativelayout as root.
Hi try using this code
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="250dp"
android:baselineAligned="false"
android:elevation="6dp"
cardview:cardCornerRadius="6dp"
cardview:cardElevation="6dp"
cardview:cardPreventCornerOverlap="false"
cardview:cardUseCompatPadding="true"
cardview:contentPadding="0dp">
</android.support.v7.widget.CardView>
Use this code when you are using cardview
<android.support.v7.widget.CardView
android:id="#+id/cardview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:elevation="100dp"
card_view:cardBackgroundColor="#color/cardview_initial_background"
card_view:cardCornerRadius="8dp"
android:layout_marginLeft="#dimen/margin_large"
android:layout_marginRight="#dimen/margin_large"
>
I am trying to create a TextView programmatically which will be shown in another layout that doesn't belong to the same activity but the TextView isn't shown. Following is the code:
Activity:
public class LogActivity extends AppCompatActivity {
LinearLayout textLayout;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_one);
LayoutInflater layoutInflater=getLayoutInflater();
View textEntryLayout=layoutInflater.inflate(R.layout.layout_two, null);
textLayout=(LinearLayout) textEntryLayout.findViewById(R.id.layout);
textView=new TextView(this);
textView.setText("TextView");
textView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
textLayout.addView(textView);
}
Layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
android:background="#color/editTextBG">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/log"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/editTextFont"
android:textSize="35sp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#color/editTextFont"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
That's because your container layout, has other children whose heights are set to match_parent. You won't be able to see TextView you add in Java code since the other children added in XML fill all the screen.
You need to add textEntryLayout to your layout. You are modifying that inflated view without adding it to (e.g.) a layout in your layout_one.
Try:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_id_in_layout1);
layout.addView(textEntryLayout);
I am adding views dynamically like this:
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.dynamicabout, null);
TextView textOut = (TextView)addView.findViewById(R.id.aboutcontent);
textOut.setText(strings.get(index));
((ViewGroup) v).addView(addView);
However, once we get to the bottom of the screen and beyond, there's no scrollbar.
I tried using the ScrollView, but that doesn't do anything.
dynamicabout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
about.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/stdpadding"
android:paddingRight="#dimen/stdpadding"
android:paddingBottom="#dimen/stdpadding"
android:paddingTop="#dimen/stdpadding"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</LinearLayout>
What you need to do is you need to have Parent and Child view. Add child to the Parent. Parent is LinearLayout in ScrollView in dynamicabout.xml. Child is about XML. Inflate about XML on the runtime and Add it into Parent View (LinearLayout in ScrollView).
P.S. > Set an ID for the Parent Layout in order to be able to add views inside it, as far as I see it doesn't have ID.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/stdpadding"
android:paddingRight="#dimen/stdpadding"
android:paddingBottom="#dimen/stdpadding"
android:paddingTop="#dimen/stdpadding"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ScrollView>
</LinearLayout>
////////////////////////////////////////////////
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"/>
</LinearLayout>
</RelativeLayout>
//////////////////////////////////////////////////////////////
LinearLayout linear=(LinearLayout)findviewbyId(R.id.linear);
LayoutInflater layoutInflater =
(LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.dynamicabout, null);
TextView textOut = (TextView)addView.findViewById(R.id.aboutcontent);
textOut.setText(strings.get(index));
linearview.addView(addView);
please chk this code:
I try to make a ScrollView to act like a ListView. So every row will be a TextView added dynamically. but the program crashes. I have a ScrollView inside a LinearLayout. What i do wrong? Thanks.
Here is my code
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="#+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
A scrollView Can only have one child. If you try to add more than 1 child to the scroll view then it will crash. You should have the LinearLayout inside of the scrollview and then dynamically add textViews to the linear layout.
You are trying to add LinearLayout1 as its own child's child (being your own grandfather, is a confusing notion).
Change you XML around like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
You cannot add views to a scrollview. You can only add them to a viewgroup, like linear or relativelayout.
It is not exactly clear what you are trying to achieve, but what is wrong with using a listview for this? You could set a max height on the listview. Or you could try wdziemia's suggestion