how to place buttons in my custom view? - java

I have created a gridlayout with width=3 and height =2. i placed 6 views in each grid element with ids as view1,view2,view3,view4,view5 and view 6. now how to place buttons on each view programmatically?
View child1,child2,child3,child4,child5,child6;
child1=(View)view.findViewById(R.id.view1);
child2=(View)view.findViewById(R.id.view2);
child3=(View)view.findViewById(R.id.view3);
child4=(View)view.findViewById(R.id.view4);
child5=(View)view.findViewById(R.id.view5);
child6=(View)view.findViewById(R.id.view6);
and finally to place dynamic buttons i used the code as below.
child1 = this.getActivity().getLayoutInflater().inflate(R.layout.video,null);
video = (Button)child1.findViewById(R.id.button);
this continues for child2,3 etc.video is an xml layout with a button id as button.
the layout is as follows
<?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:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#484848"
android:layout_weight=".1">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imb1"
android:layout_weight=".07"
android:layout_marginBottom="7.5dp"
android:textColor="#ffffff"
android:src="#drawable/back"
android:adjustViewBounds="false"
android:background="#484848" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:background="#484848"
android:src="#drawable/medilearn1"
android:layout_weight=".15"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dashboard"
android:id="#+id/textView3"
android:layout_gravity="center"
android:textColor="#fbfbfb"
android:textAlignment="viewStart"
android:layout_marginLeft="20dp"
android:layout_weight="0.67" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Restart"
android:id="#+id/button3"
android:layout_weight=".07"
android:background="#d86018"
android:textColor="#ffffff"
android:layout_marginTop="7.5dp"
android:layout_marginBottom="7.5dp"
android:layout_marginRight="7.5dp" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d8d8d8"
android:layout_weight=".9">
<SearchView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/searchView"
android:background="#ffffff"
android:queryHint="search"
android:layout_gravity="center_horizontal" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:background="#d8d8d8">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:rowCount="2"
android:id="#+id/gridView">
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view1" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view2" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view3" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view4" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view5" />
<View
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_columnWeight="1"
android:id="#+id/view6" />
</GridLayout>
</ScrollView>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#484848"
android:layout_weight=".1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Version:1.0"
android:id="#+id/textView6"
android:layout_gravity="center"
android:textColor="#fbfbfb"
android:textAlignment="viewStart"
android:layout_marginLeft="20dp" />
</LinearLayout>
</LinearLayout>
This is my layout.
I found the solution. I declared linearlayout in each grid with hight and width as my wish and called child with addView().so,
LinearLayout lay1=(LinearLayout)view.findViewById(R.id.layout1);
and then
lay1.addView(child1);

You can add a Button to your view like this:
Button btn = new Button();
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btn.setId(1);
btn.setText("ButtonText");
yourView.addView(btn);
If you want it to fill your whole view, replace LayoutParams.WRAP_CONTENT with LayoutParams.MATCH_PARENT.

just set adapter for your GridView
MyGridAdapter csAdapter = new MyGridAdapter();
csGridView.setAdapter(csAdapter);
csGridView.setOnItemClickListener(gridItemClicked);
and adapter code is
public AdapterView.OnItemClickListener gridItemClicked = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//your code
}
};

Update:
Based on the layout you posted there are two options you can pursue:
Create the layout in your xml file: you could just replace the View tags with Button tags if your layout is static and will always require those buttons.
Create the layout dynamically: for this you don't need the View tags inside your layout as those will be created programmatically, so you can remove those. Then do the following for every Button you want to create (I'm assuming the layout containing the buttons is called video.xml):
View child = this.getActivity().getLayoutInflater().inflate(R.layout.video,null);
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
// set the row number this child is located in
params.rowSpec = GridLayout.spec(rowNum, 1);
// set the column number this child is located in
params.columnSpec = GridLayout.spec(colNum, 1);
child.setLayoutParams(params);
grid.addView(child);
Also, please note that a View cannot have a child View, only layouts extending ViewGroup (e.g. FrameLayout, GridLayout, ...) can have child Views!
The best way to inflate the View in your case is the following code:
GridLayout grid = view.findViewById(R.id.grid); // get the grid layout
// ...
child1 = this.getActivity().getLayoutInflater().inflate(R.layout.video, grid, true);
video = (Button)child1.findViewById(R.id.button);
By providing the parent (grid), the LayoutInflator will take care of properly inflating the requested resource into the view hierarchy, the true attaches the inflated layout to the root layout.
Also check the API docs for this:
inflate(int resource, ViewGroup root, boolean attachToRoot):
Inflate a new view hierarchy from the specified xml resource.

Related

How to set Dialog width and height in percents in XML

I need my Dialog width and height fill 70% of screen space. I'm using ConstraintLayout as root layout and tried to achieve this by using app:layout_constraintWidth_percent but it's not working.
It would be great to achieve this in XML without Java code.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/dialog_background_shape"
android:padding="#dimen/app_padding">
<TextView
android:id="#+id/textView13"
style="#style/text_plain3"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:text="#string/add_member_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/button_add_member_close"
style="#style/text_title2"
android:textStyle="bold"
android:layout_width="28dp"
android:layout_height="28dp"
android:background="#drawable/button_dialog_close"
android:gravity="center"
android:text="X"
android:textColor="#color/text_plain3"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/add_member_group_id"
style="#style/text_group_id"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView13" />
<TextView
android:id="#+id/textView17"
style="#style/text_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/add_member_annotation"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_group_id"/>
<EditText
android:id="#+id/add_member_user_id"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/add_member_hint_id"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:maxLength="28"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<Button
android:id="#+id/button_add_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:text="#string/add_member_button_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_user_id"/>
</androidx.constraintlayout.widget.ConstraintLayout>
In order to have a constraint width/height DialogFragment or (any specific width or height in general):
wrap your ConstraintLayout into another ConstraintLayout in order to make the outer take the full size of the screen, and the inner get the desired width/height percentage:
Make the android:background of the outer to be transparent
Constraint the width & height of the inner ConstraintLayout, make it centered in parent, and add the percentage constraint to the width & height:
app:layout_constraintHeight_percent="0.7"
app:layout_constraintWidth_percent="0.7"
Now the layout looks like:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/main_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_gravity="center"
android:background="#drawable/dialog_background_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.7"
android:padding="#dimen/app_padding">
<TextView
android:id="#+id/textView13"
style="#style/text_plain3"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:text="#string/add_member_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/button_add_member_close"
style="#style/text_title2"
android:textStyle="bold"
android:layout_width="28dp"
android:layout_height="28dp"
android:background="#drawable/button_dialog_close"
android:gravity="center"
android:text="X"
android:textColor="#color/text_plain3"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/add_member_group_id"
style="#style/text_group_id"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="64dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView13" />
<TextView
android:id="#+id/textView17"
style="#style/text_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/add_member_annotation"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_group_id" />
<EditText
android:id="#+id/add_member_user_id"
android:layout_width="248dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="#string/add_member_hint_id"
android:importantForAutofill="no"
android:inputType="textPersonName"
android:maxLength="28"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/textView17" />
<Button
android:id="#+id/button_add_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
android:text="#string/add_member_button_add"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/add_member_group_id"
app:layout_constraintStart_toStartOf="#+id/add_member_group_id"
app:layout_constraintTop_toBottomOf="#+id/add_member_user_id" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
In your custom DialogFragment:
Designate the width & height of the dialog to MATCH_PARENT:
dialog?.window?.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
Set no background the theme:
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#null</item>
</style>
Custom Dialog Fragment:
Java:
public class MyDialogFragment extends DialogFragment {
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull #NotNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
return inflater.inflate(
R.layout.dialog_layout, container,
false
);
}
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
#Override
public void onStart() {
// Making the dialog full screen
if (getDialog() != null)
getDialog().getWindow().setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
super.onStart();
}
}
Kotlin:
class MyDialogFragment : DialogFragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(
R.layout.dialog_layout, container,
false
)
}
override fun getTheme(): Int = R.style.NoBackgroundDialogTheme
override fun onStart() {
super.onStart()
// Making the dialog full screen
dialog?.window?.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
}
}
Note: I am assuming that the dialog layout is R.layout.dialog_layout
UPDATE
now if I click on empty space around the Dialog, it doesn't close.
This is because the dialog fragment consumes the entire screen size; you can solve this by the below workaround:
Add an ID to the outer most ConstraintLayout, assume it is root >> Updated on the top layout
Add an ID to the inner ConstraintLayout, assume it is main_layout >> Updated on the top layout
Dismiss the dialog when the root is clicked
Do nothing when the main_layout is clicked to consume the event so that it won't be dismissed:
So, update the onStart() of the MyDialogFragment to:
override fun onStart() {
super.onStart()
// Making the dialog full screen
dialog?.window?.setLayout(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
)
val root = requireView().findViewById<ConstraintLayout>(R.id.root)
root.setOnClickListener {
dismiss() // Dismiss the dialog
}
val main = requireView().findViewById<ConstraintLayout>(R.id.main_layout)
main.setOnClickListener {
// Consume the event
}
}
ConstraintLayout:
I see your "Dialog" Box contains a lot of different views. For that I made a simple example for you to show how to use Guideline for achieving percentages of Views.
I suggest to learn how this works and rebuild your layout with better constraining on this Guidelines, because now everything is wired up really bad. The idea will be to wire the ConstraintLayout to the Guideline and everything beside that (e.g. TextView etc.) to the ConstraintLayout. (Without Hardcoding!, that's how relative design work).
My example shows a TextView (could be your ConstraintLayout (frame for everything) that has exactly 70% (app:layout_constraintGuide_percent="0.7") vertically and horizontal of the screen. Important is to set the TextViews width and height to 0dp and to attach to the Guideline:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/vertical_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.7" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/horizontal_guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.7" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#2962FF"
app:layout_constraintBottom_toTopOf="#+id/horizontal_guideline"
app:layout_constraintEnd_toStartOf="#+id/vertical_guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
Result:
In addition same for LinearLayout:
You can use android:layout_weight="" to give a percent amount. So 70% means a weight of .70.
Keep in mind to set the width or/and height to 0dp
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="70 percent of screen"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70" />
</LinearLayout>

Add Defined TextInputLayout Dynamically and Programatically

I am trying to add a TextInputLayout with an EditText based on a number that the user selects from a Spinner. I already have the TextInputLayout attributes defined in XML and was hoping to simple just add them programmatically based on the number that the user selects from the spinner:
XML:
<FrameLayout
android:background="#drawable/image_border"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".525">
<Button
android:id="#+id/add_image_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Click to Add Image" />
</FrameLayout>
<LinearLayout
android:orientation="vertical"
android:padding="4dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".475">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_question_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="#string/create_poll_question" />
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/how_many_answers_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/how_many_answers_text"
android:textColor="#color/black"
android:textSize="16sp" />
<Spinner
android:id="#+id/number_of_answers_spinner"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:layout_height="24dp"
android:background="#android:drawable/btn_dropdown" />
</LinearLayout>
<!--Want to Add Programatically -->
<android.support.design.widget.TextInputLayout
android:id="#+id/create_poll_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/create_poll_answer_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
Here is the code I am currently using, but it does not add dynamically based on the view I already created:
public class YourItemSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String selected = parent.getItemAtPosition(pos).toString();
Toast.makeText(getActivity().getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
for (int i = 0; i < Integer.parseInt(selected); i++) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_linearlayout);
EditText editText = new EditText(getActivity());
editText.setHint("Poll Answer");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
}
Easiest way to do such dynamic injections of view is to use ButterKnife library by JakeWharton
http://jakewharton.github.io/butterknife/
You can use it like in your activity's declaration part:
#BindView(R.id.title) TextInputLayout textInputLayout;
Then bind it inside onCreate() like:
ButterKnife.bind(this);
This will roughly be equivalent to inflating and finding the view by id.
Moreover, the library helps to dynamically set drawables,etc. with ease as well
Add
android:id="#+id/create_poll_linearlayout"
to your root LinearLayout.

How to populate a linear layout using arraylist?

I have a linear layout which contains multiple widgets. I want to populate it using an ArrayList. I don't want to use use a listview because it didn't work in scrollview.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10" >
<TextView
android:id="#+id/xxxx"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="top|left"
/>
<TextView
android:id="#+id/tv_xxx"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:text="jshadb" />
<TextView
android:id="#+id/tv_xxx"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="464.89" />
<EditText
android:id="#+id/et_xxxx"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:hint=""
android:ems="10"
android:inputType="number" />
</LinearLayout>
Below the way
LayoutInflater linf = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
linf = LayoutInflater.from(YourActivity.this);
//Linear Layout on you want to add inflated View
LinearLayout tbl_layout=(LinearLayout)findViewById(R.id.Layoutid);
for (int i = 0; i < arrayList.size(); i++) {
View v = linf.inflate(R.layout.YourLinearLayout, null);//Pass your lineraLayout
((TextView) v.findViewById(R.id.xxxx)).setText("textS");
tbl_layout.addView(v);
}

How to add TableRow from xml to TableLayout programmatically?

Im new to Android and I'm trying to add a TableRow, which I already made with xml, to a TableLayout programmatically. I'm getting Force Closes, most are NullPointerExcpetions.
Here's my java class
public class DayFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.subjects_list, container, false);
TableLayout tl = (TableLayout) view.findViewById(R.id.tl);
TableRow tr = (TableRow) view.findViewById(R.id.tr_row);
tl.addView(tr); //not working, obviously im missing something
//xml parsing stuff
return view;
}
}
This is my layout with the TableLayout:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="5dp" >
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<include
android:id="#+id/header"
layout="#layout/table_header" />
<include android:id="#+id/h_divider"
layout="#layout/horizontal_divider"/>
</TableLayout>
</ScrollView>
</HorizontalScrollView>
And the TableRow:
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tr_row"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv_hour"
android:layout_weight="1"
android:gravity="center"
android:padding="8dp"
android:textSize="15sp"
android:textStyle="bold" />
<View
android:layout_weight="1"
android:background="#drawable/vertical_cell_divider" />
<TextView
android:id="#+id/tv_subject"
android:layout_weight="1"
android:gravity="left"
android:padding="8dp"
android:textSize="18sp" />
<View
android:layout_weight="1"
android:background="#drawable/vertical_cell_divider" />
<TextView
android:id="#+id/tv_start"
android:layout_weight="1"
android:gravity="left"
android:padding="8dp"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_bar"
android:layout_weight="1"
android:gravity="left"
android:textSize="18sp" />
<TextView
android:id="#+id/tv_end"
android:layout_weight="1"
android:gravity="left"
android:padding="8dp"
android:textSize="18sp" />
</TableRow>
I tried lots of different ways to achieve it but still nothing.
Replace code:
TableRow tr = (TableRow) view.findViewById(R.id.tr_row);
tl.addView(tr); //not working, obviously im missing something
with
View tr = inflater.inflate(R.layout.table_row_layout, null,false);
tl.addView(tr); //not working, obviously im missing something

imageview and listview

I have a listview with a ImageView that appears when i click on one of the elements of the list.
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ImageView icon = (ImageView) view.findViewById(R.id.icon);
icon.setImageResource(R.drawable.valider);
choiceDialog(view);
}
My layout file for the rows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="1dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView
android:id="#+id/nom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="15px"
android:textStyle="bold"
android:typeface="serif" />
/>
<TextView
android:id="#+id/date_heure"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="15px"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/adresse"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee"
android:textSize="15px"
android:textStyle="bold"
android:typeface="serif" />
</LinearLayout>
</LinearLayout>
The problem is that if I delete an element from the list, the ImageViewe is transferred to the element above whereas it was not clicked at first.
I tried to remove the image when the item is removed like this:
resolver.delete(DB_RDVUtils.CONTENT_URI, ligne,null);
ImageView icon = (ImageView) item.findViewById(R.id.icon);
icon.setImageResource(0);
but in this case, it removes all of the imagesview in the list
How to fix this bug please
Thank you very much
After removing an element from the list, remember to call Adapter.notifyDataSetchanged() so the ListView updates correctly.
Have you tried this?
resolver.delete(DB_RDVUtils.CONTENT_URI, ligne,null);
ImageView icon = (ImageView) item.findViewById(R.id.icon);
icon. invalidateDrawable(icon.getDrawable());

Categories