I am trying to inflate a EditText view inside a RelativeLayout in my Activity.
But getting an error says:
The specified child already has a parent. You must call removeView() on the child's parent first.
My code:
RelativeLayout relLayout= new RelativeLayout(insideActivity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.setLayoutParams(params);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.holo_edit_text, null);
EditText searchBox = (EditText)view.findViewById(R.id.holo_text);
relLayout.addView(searchBox);
And here is holo_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/holo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</RelativeLayout>
your holo_edit_text.xml should be
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
And the way you add it to the view should be like this
EDIT (use getLayoutInflater() instead)
View view = getLayoutInflater().inflate(R.layout.holo_edit_text, null);
//EditText searchBox = (EditText)view.findViewById(R.id.holo_text);
//The last sentence is not neccesary
relLayout.addView(view);
Have you tried this while adding to relative layout
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
relLayout.addView(view, params);
Try this..
View child_view = LayoutInflater.from(this).inflate(R.layout.holo_edit_text, null);
relLayout.addView(child_view);
Use a try/catch mechanism.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView != null) {
((ViewGroup) rootView.getParent()).removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.your_layout_file_here, container,false);
EditText searchBox = (EditText)rootView.findViewById(R.id.holo_text);
relLayout.addView(searchBox);
} catch (InflateException e) {
}
return rootView;
}
I have fixed my issue by simply setting the LayoutParams to searchBox before adding it to the RelativeLayout. And its working fine.
Thanks all for your response.
Related
I faced with a problem that i cannot put my dataGridView into scrollView and in case I have a lot of columns they just become so thin that it's impossible to see something there. That's why I decided to remake it and create LinearLayout with Vertical Layout for each column and each of them will have another LinearLayout with Horizontal Layout just to simulate GridView. (Hope it's a good idea)
But unfortunately I'm facing some problems during its creation. It is not being created and my application turning off. Ask you for your help
Here is my code: grid_container.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/GridScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
PageFragment.java (place where LinearLayout should be fill out)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grid_container, container, false);
LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_grid_layout);
int colCount = mPage.get(0).split(",").length;
int rowCount = mPage.size();
ArrayList<ArrayList<String>> tempNormList = createNormList(mPage);
for(int currCol=0;currCol<colCount;currCol++){
LinearLayout linearLayout = new LinearLayout(view.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(llParams);
for(int currRow=0; currRow<rowCount;rowCount++){
TextView textView = new TextView(view.getContext());
textView.setText(tempNormList.get(currCol).get(currRow));
if(currRow==0){
textView.setBackgroundResource(R.drawable.header_borders);
}
linearLayout.addView(textView);
}
mainLayout.addView(linearLayout);
}
return view;
}
Thank you in advance for your help
try my linked Answer, it will provide your solution, but you have do some changes like below
Don't Use Custom Adapter, use for loop with getView() method of
CustomAdapter to Set Data.
Cast your Linearlayout which id is main_grid_layout by `findViewById().
add convertView of getView() method to main_grid_layout
Skip Item Android Grid View in condition
hope it will help you
I have created a fragment and want to start a dialogue on a button click on which i would display n numbers of texts distributed vertically using Linear layout vertical orientation. To begin with i have created a textview which i am populating dynamically. But i get a null pointer exception saying the any of the layout i referenced here points to NULL.
View view,tempView;
TextView myName, myPhNo, myEmail, myDob;
ImageButton selectRingTone;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_profile, container, false);
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
myName = (TextView)view.findViewById(R.id.username);
myPhNo = (TextView)view.findViewById(R.id.userPhNo);
myEmail = (TextView)view.findViewById(R.id.useremailId);
myDob = (TextView)view.findViewById(R.id.userdob);
selectRingTone = (ImageButton)view.findViewById(R.id.selectRingTone);
setUserProfileData();
//setUserTextStatus();
//setUserAudioStatus();
selectRingTone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseAndSetRingTone();
}
});
return view;
}
private void chooseAndSetRingTone(){
final Dialog fbDialogue = new Dialog(view.getContext(), android.R.style.Theme_Black);
fbDialogue.getWindow().setTitle("Select your audio status song");
fbDialogue.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(100, 0, 0, 0)));
fbDialogue.setContentView(R.layout.fragment_selectsong);
getSongsAndFillDialogue();
fbDialogue.setCancelable(true);
fbDialogue.show();
}
private void getSongsAndFillDialogue(){
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = (LinearLayout) tempView.findViewById(R.id.eachRingToneSong);
TextView tv = new TextView(tempView.getContext());
tv.setText("Hi hello");
Button b = new Button(tempView.getContext());
b.setText("abcde");
container.addView(tv);
container.addView(b);
}
XML Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLyt"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollCotainerForRingToneSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/eachRingToneSong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<TextView
android:id="#+id/song1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="abcde"
/>-->
</LinearLayout>
</ScrollView>
</RelativeLayout>
you are creating View for your Dialog in onCreateView method:
tempView = inflater.inflate(R.layout.fragment_selectsong, container, false);
and fulfilling it using getSongsAndFillDialogue method, but you are setting for your Dialog another new instance passing only resource id:
fbDialogue.setContentView(R.layout.fragment_selectsong);
when you are passing just id then Dialog is inflating this layout by itself (just like you). So there are two layouts created, one in Activity - fulfilled, and one created "automatically" by setContentView method, but not touched at all by your methods (so it stays empty)
instead of passing resource id pass already prepared tempView, like below:
fbDialogue.setContentView(tempView);
I am trying to make a chat bubble, I have successfully did it with a text only, but when i want to add another text to put the time inside the bubble i had to add a RelativeLayout to put it on the actual linear layout, but this time i am getting an error.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionGroup="true">
<RelativeLayout
android:id="#+id/my_lay"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:text="test"
android:background="#drawable/sender_message_9"/>
<TextView
android:id="#+id/messageTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="12:12 PM"
android:layout_alignBottom="#id/message"
android:layout_alignRight="#id/message"
/>
</RelativeLayout>
</LinearLayout>
ADAPTER:
public class ChatAdapter extends ArrayAdapter<MessageProvider> {
private List<Message> list = new ArrayList<>();
private TextView TXT;
private RelativeLayout rLayout;
Context context;
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.source_message, parent, false);
}
...
TXT.setText(Message);
//Set Linear Changes
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if(!POSITION){ // Moves the layout to the right
params.gravity = Gravity.RIGHT;
}else { // Moves it to the Left
params.gravity = Gravity.LEFT;
}
TXT.setLayoutParams(params);
return convertView;
}
}
ERROR:
05-05 00:10:44.563 8962-8962/com.example.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
I would appreciate any answer, thanks.
change the following line
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
to
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
because you have included the textview inside a RelativeLayout parent.
The error is this line
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
you are using Linear layout params for a Relative Layout.
try using
RelativeLayout.LayoutParams params= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
I've a View declared in the XML file, and I want to define it by code, but when I establish it nothing is shown. Can you help me?
This is my XML file:
[...]
<View
android:id="#+id/marco_container"
style="#style/wrapFull"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
[...]
And I define it by this way:
setContentView(R.layout.marco);
View view = (View) findViewById(R.id.marco_container);
view.inflate(getApplicationContext(), R.layout.prueba, null);
I tried to declare it by this way too:
view = View.inflate(getApplicationContext(), R.layout.prueba, null);
This is prueba.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Prueba" style="#style/wrapContent"
android:layout_gravity="center"/>
</FrameLayout>
It's not a View view. It's your custom View, just create a custom view that extends FrameLayout and inside the constructor inflate your view.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER...);
View view = inflater.inflate(getApplicationContext(), R.layout.prueba, this);
and inside your main xml put :
<com.my.path.CustomView
android:id="#+id/marco_container"
style="#style/wrapFull"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
And in your class :
setContentView(R.layout.marco);
CustomView view = (CustomView) findViewById(R.id.marco_container);
You need to do it this way:
View view2 = view.inflate(this, R.layout.prueba, null);
((ViewGroup)view).addView(view2);
Of course view must be some kind of ViewGroup (RelativeLayout, LinearLayout, etc.)
setContentView(R.layout.marco);
LinearLayout li = (LinearLayout) findViewById(R.id.marco_container);
View view = getLayoutInflater().inflate(R.layout.prueba, li, false);
li.addView(view);
I am trying to create a fragment. In the fragment I plan to place a text view next to it a Spinner control. I tried using this code. However Spinner always is placed over textview to the left of the screen. Can anyone suggest what could be the problem?
Code:
public class FragmentTest extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.ll2, null);
RelativeLayout lref = (RelativeLayout)view.findViewById(R.id.ll2ll);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
lp.addRule(RelativeLayout.ALIGN_LEFT);
RelativeLayout rl = new RelativeLayout(getActivity());
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setLayoutParams(lp);
tv.setText("My ");
rl.addView(tv, lp);
Spinner s = new Spinner(getActivity());
lp.addRule(RelativeLayout.RIGHT_OF, tv.getId());
s.setId(2);
String currencyData[] = {"USD",
"EUR","INR"
};
ArrayAdapter<Object> currencyAdapter; currencyAdapter = new ArrayAdapter<Object>(getActivity(), android.R.layout.simple_spinner_item, currencyData);
currencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(currencyAdapter);
rl.addView(s,lp);
return rl;
}
}
XML file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:isScrollContainer="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/ll2ll"
android:layout_width="140dp"
android:layout_height="50dp"
android:textSize="#dimen/font_size" />
</RelativeLayout>
When a view is inflated from a XML you will not get the LayoutParams properly. Try using new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
your problem is this, you can't use the same layout parameters for adding all the layouts and modifying the lp.
because the lp is used after you have initialized all the layouts, that means you are using same parameters for all the layouts spinner and textview both.
try using different layout parameters for all the layouts
RelativeLayout.LayoutParams lp
RelativeLayout.LayoutParams lptextview
RelativeLayout.LayoutParams lpspinner
for all of them
if your problem is solved mark my answser as correct