Recyclerview dynamic textview height - java

I want to dynamically change textviews height to fit cells in other linearlayout.
I tried to make this in this way:
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, final int position) {
holder.numberCage.setText(cages.get(position).getNumberCage());
holder.eggsDescription.setText(cages.get(position).getEggsDescription());
holder.eggsFrom.setText(cages.get(position).getNumberCageFrom());
if(cages.get(position).getBasicEggs().toString().equals("true"))
holder.basicEggs.setText("Tak");
else
holder.basicEggs.setText("Nie");
holder.dateEggs.setText(cages.get(position).getDateEggs());
holder.description.setText(cages.get(position).getDescription());
holder.pigeonsInside.setText(cages.get(position).getPigeonsInside());
holder.numberCageTitle.setHeight(holder.numberCage.getHeight());
holder.eggsDescriptionTitle.setHeight(holder.eggsDescription.getHeight());
holder.eggsFromTitle.setHeight(holder.eggsFrom.getHeight());
holder.basicEggsTitle.setHeight(holder.basicEggs.getHeight());
holder.dateEggsTitle.setHeight(holder.dateEggs.getHeight());
holder.descriptionTitle.setHeight(holder.description.getHeight());
holder.pigeonsInsideTitle.setHeight(holder.pigeonsInside.getHeight());
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView numberCage,pigeonsInside,description, dateEggs, basicEggs, eggsFrom, eggsDescription, numberCageTitle, pigeonsInsideTitle, descriptionTitle, dateEggsTitle, basicEggsTitle, eggsFromTitle, eggsDescriptionTitle;
LinearLayout linearLayout, linearLayoutLeft, linearLayoutRight;
public MyViewHolder(View view){
super(view);
numberCage = view.findViewById(R.id.numberCage);
pigeonsInside = view.findViewById(R.id.pigeonsInside);
description = view.findViewById(R.id.description);
dateEggs = view.findViewById(R.id.dateEggs);
basicEggs = view.findViewById(R.id.basicEggs);
eggsFrom = view.findViewById(R.id.eggsFrom);
eggsDescription = view.findViewById(R.id.eggsDescription);
numberCageTitle = view.findViewById(R.id.numberCageTitle);
pigeonsInsideTitle = view.findViewById(R.id.pigeonsInsideTitle);
descriptionTitle = view.findViewById(R.id.descriptionTitle);
dateEggsTitle = view.findViewById(R.id.dateEggsTitle);
basicEggsTitle = view.findViewById(R.id.basicEggsTitle);
eggsFromTitle = view.findViewById(R.id.eggsFromTitle);
eggsDescriptionTitle = view.findViewById(R.id.eggsDescriptionTitle);
linearLayout = view.findViewById(R.id.linearLayout);
}
}
I have good results but my problem is that I have to scroll down and scroll up my list to "load" this heights, I don't know how to tell this so I show you screenshot.
left table is not showing
After scroll down: left table after scroll
This is my content xml:
<LinearLayout 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="wrap_content"
android:baselineAligned="true"
android:weightSum="2"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".CageList"
tools:showIn="#layout/activity_cage_list"
android:divider="#android:color/black"
android:dividerPadding="1dip"
android:showDividers="end"
android:id="#+id/linearLayout"
>
<LinearLayout
android:id="#+id/linearLayoutLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:orientation="vertical">
<TextView
android:id="#+id/numberCageTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Numer klatki:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/pigeonsInsideTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Gołębie w klatce:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/descriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Opis klatki:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/dateEggsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Data złożenia jaj:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/basicEggsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jajka macierzyste?"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/eggsFromTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jajka przełożone skąd? (klatka)"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/eggsDescriptionTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Opis do jajek:"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:background="#color/colorPrimary"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.75"
android:orientation="vertical">
<TextView
android:id="#+id/numberCage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/pigeonsInside"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/dateEggs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/basicEggs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/eggsFrom"
android:layout_width="match_parent"
android:layout_height="35.65dp" />
<TextView
android:id="#+id/eggsDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorPrimary"/>
</LinearLayout>
</LinearLayout>

create another file named sub_layout.xml which will have below code :-
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="horizontal">
<TextView
android:id="#+id/txtTitleStatic"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.45"
android:text="Jajka przełożone skąd? (klatka)"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".55" />
</LinearLayout>
and then use below code to include in main layout (You Content XML File):-
<include
android:id="#+id/numberCageLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/pigeonsInsideLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/descriptionLayout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Now in your Java File get their definition like :-
View numberCageLayout = findViewById(R.id.numberCageLayout);
View pigeonsInsideLayout = findViewById(R.id.pigeonsInsideLayout);
View descriptionLayout = findViewById(R.id.descriptionLayout);
TextView numberCageStatic =numberCageLayout.findViewById(R.id.txtTitleStatic);
TextView numberCage =numberCageLayout.findViewById(R.id.txtTitle);
TextView pigeonsInsideStatic =pigeonsInsideLayout.findViewById(R.id.txtTitleStatic);
TextView pigeonsInside =pigeonsInsideLayout.findViewById(R.id.txtTitle);
Now like this you don't have to worry about cell height.

Related

Programmatically check RadioButton

I have two radiobutton groups that need to be initialized when loading the screen, so I used this code:
mVisibilityPublic.setChecked(true);
But when I load the screen I get the following (first is the wrong behavior, second is the expected one):
I've tried with all these:
mVisibilityPublic.setSelected(true);
mVisibilityPublic.performClick();
mVisibilityPublic.setActivated(true);
mVisibilityPublic.invalidate();
mVisibilityPublic.requestLayout();
But I get the same result.
<ScrollView 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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:orientation="vertical"
android:padding="#dimen/form_vertical_spacing"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".createevent.CreateEventActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="#string/create_event_privacy"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
<RadioGroup
android:id="#+id/create_visibility_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="#+id/createevent_visibility_public"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_public_event"/>
<RadioButton
android:id="#+id/createevent_visibility_private"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_private_event"/>
<RadioButton
android:id="#+id/createevent_visibility_ppv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv"/>
</RadioGroup>
<LinearLayout
android:id="#+id/ppv_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="#string/create_event_ppv_section"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="#+id/ppv.currency.layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<com.myapp.ui.AutoCompleteCombo
android:id="#+id/ppv.currency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/create_event_ppv_currency">
</com.myapp.ui.AutoCompleteCombo>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/ppv.price.layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_weight="1">
<android.support.design.widget.TextInputEditText
android:id="#+id/ppv.price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/create_event_ppv_price"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxLines="1"
/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<RadioGroup
android:id="#+id/ppv_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="#+id/ppv_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv_allcountries"/>
<RadioButton
android:id="#+id/ppv_allexcept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv_except"/>
<RadioButton
android:id="#+id/ppv_only"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/create_event_ppv_some"/>
</RadioGroup>
<com.myapp.countries.CountriesMultiCombo
android:id="#+id/create.ppv.countrylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout>
<View
android:id="#+id/separator"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/general_info"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="-32dp"
android:layout_marginRight="-32dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#color/colorDivider"
/>
<LinearLayout
android:id="#+id/geoblock_panel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="#string/geo_blocking_settings_title"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
<RadioGroup
android:id="#+id/geo_radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<RadioButton
android:id="#+id/geo_none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/geo_none"/>
<RadioButton
android:id="#+id/geo_blockonly"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/geo_block_only"/>
<RadioButton
android:id="#+id/geo_blockexcept"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/geo_block_all_except"/>
</RadioGroup>
<com.myapp.createevent.countries.CountriesMultiCombo
android:id="#+id/create.geo.countrylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
/>
</LinearLayout> <Button
android:id="#+id/test_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CLICK ME"/>
</LinearLayout>
</ScrollView>
Note that if I select the option manually (like I did with the second example in the image) I get the proper result. Its only if I do it programmatically that its missing the inner circle.
public class TestFragment extends Fragment {
private Unbinder mUnbinder;
#BindView(R.id.createevent_visibility_public)
RadioButton mVisibilityPublic;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.fragment_advanced_opts, container, false);
mUnbinder = ButterKnife.bind(this, inflate);
return inflate;
}
public static TestFragment newInstance(int tvId, int eventId) {
TestFragment testFragment = new TestFragment();
Bundle args = new Bundle();
args.putInt(TV_ARGUMENT, tvId);
args.putInt(EVENTID_ARGUMENT, eventId);
testFragment.setArguments(args);
return testFragment;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mVisibilityPublic.setChecked(true);
}
}

How to load a picture in the ImageView of a collapsing toolbar?

I just created an Activity to show some product informations and this Activity has a Collapsing Toolbar. I wanted to load an image by Picasso as soon as the Activity opens.
Here is the current code :
public class ProductActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product);
Product p = (Product) getIntent().getExtras().getSerializable("product");
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle(p.getProductName());
TextView name = (TextView) findViewById(R.id.product_name);
TextView type = (TextView) findViewById(R.id.product_type);
TextView description = (TextView) findViewById(R.id.product_description);
TextView ingredients = (TextView) findViewById(R.id.product_ingredients);
TextView price = (TextView) findViewById(R.id.product_price);
ImageView thumbnail = (ImageView) findViewById(R.id.product_thumbnail);
name.setText(p.getProductName());
type.setText(p.getProductType().getDisplayName());
description.setText(p.getProductDescription());
ingredients.setText("Non disponible");
price.setText(p.getProductPrice().length>1 ? "À partir de "+p.getProductPrice()[0].toString()+"€" : p.getProductPrice()[0].toString()+"€");
Picasso.with(this).load(p.getProductThumbnailURL()).fit().into(thumbnail);
Picasso.with(this).load(p.getProductThumbnailURL()).fit().into((ImageView)findViewById(R.id.test), new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Toast.makeText(ProductActivity.this, "Impossible de charger l'image !", Toast.LENGTH_SHORT).show();
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home)finish();
return super.onOptionsItemSelected(item);
}
}
And here is a screen of the Activity (don't take care of the design xD)
ProductActivity
Any idea ? :p
Here is the XML file of the ProductActivity :
<android.support.design.widget.CoordinatorLayout
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"
tools:context="com.slama.hotpizza77.ProductActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
<ImageView
android:id="#+id/product_thumbnail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:contentDescription="#string/product_thumbnail_string" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="0dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="product_name"
style="#style/TextAppearance.AppCompat.Large"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/product_type"
style="#style/TextAppearance.AppCompat.Medium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="product_type"
android:textAppearance="#style/TextAppearance.AppCompat.Button"
tools:ignore="HardcodedText" />
<TextView
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Description :"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/product_description"
style="#style/TextAppearance.AppCompat.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="product_description"
android:textSize="16sp"
tools:ignore="HardcodedText" />
<TextView
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Ingrédients :"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/product_ingredients"
style="#style/TextAppearance.AppCompat.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="product_ingredients"
android:textSize="16sp"
tools:ignore="HardcodedText" />
<LinearLayout
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_product_pork"/>
<TextView
android:layout_marginLeft="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contient de la viande issue de porc !"
style="#style/TextAppearance.AppCompat.Body2"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_product_notforkid"/>
<TextView
android:layout_marginLeft="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ne convient pas aux enfants !"
style="#style/TextAppearance.AppCompat.Body2"/>
</LinearLayout>
<TextView
android:id="#+id/product_price"
android:layout_marginTop="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="product_price"
style="#style/Widget.AppCompat.Button.Colored"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="200dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Instead of wrap_content for your product_thumbnail ImageView height use match_parent or a fixed size.
Hope this helps

Android - Snackbar Cover my View

Why Snackbar cover my view
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fullview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_gray"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_centerInParent="true"
android:text="#string/reload"
android:visibility="visible" />
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="#+id/complist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fadeScrollbars="false" />
</RelativeLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="5dp"
android:orientation="horizontal">
<Button
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="#string/send"
android:textColor="#android:color/white" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/green"
android:text="#string/cancel"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
java code used to show snake bar
private CoordinatorLayout _CoordinatorLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.compition, null);
_CoordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.fullview);
}
Snackbar.make(_CoordinatorLayout , message , Snackbar.LENGTH_LONG).show();
here is screen shot
what I miss ??
The documentation here says:
Snackbars appear above all other elements on screen and only one can
be displayed at a time.
So, Snackbar will cover your view, just like a Toast.

Inflating a LinearLayout dynamically - Android

I'm trying to inflate a list of views in a LinarLayout directly in the onCreate() method from the activity. However, I'm having a problem somewhere in the code but I really can't see it, I'm turning around this problem for two days, I'm going crazy !
The problem is that when I run the activity, the linear layout is completely empty, there is only the title.
Here is the important part of the code, KeyItems activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.keyitems_layout);
initializer();
ll = (LinearLayout) findViewById(R.id.ll);
LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < list.size(); i++) {
View view = inflater.inflate(R.layout.keyitem_temp, ll, false);
final TextView ind = (TextView) view.findViewById(R.id.ind);
final TextView item = (TextView) view.findViewById(R.id.item);
ind.setText(list.get(i).get("id"));
item.setText(list.get(i).get("item"));
ll.addView(view);
}
}
Here is the keyitems_layout :
<?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:orientation="vertical"
android:background="#color/black" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/black">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/key_items"
android:textSize="36sp"
android:textColor="#color/white" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" />
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:orientation="vertical"
android:background="#color/white" >
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:text="caca"
android:padding="32dp" />
</LinearLayout>
</ScrollView>
And finally, my keyitem_temp.xml which I'm inflating :
<?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="wrap_content"
android:background="#color/black"
android:padding="4dp"
android:orientation="horizontal" >
<TextView android:id="#+id/ind"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/blue" />
<TextView android:id="#+id/item"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/white"/>
</LinearLayout>
I have an other activity called ItemsActivity which is the same code with some little differences and it works. Any suggestion? Thanks in advance.
Try using wrap_content for all of your heights except the initial ScrollView:
<?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:orientation="vertical"
android:background="#color/black" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/black">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/key_items"
android:textSize="36sp"
android:textColor="#color/white" />
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp" />
<LinearLayout android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:orientation="vertical"
android:background="#color/white" >
</LinearLayout>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:text="caca"
android:padding="32dp" />
</LinearLayout>
</ScrollView>
and:
<?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="wrap_content"
android:background="#color/black"
android:padding="4dp"
android:orientation="horizontal" >
<TextView android:id="#+id/ind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/blue" />
<TextView android:id="#+id/item"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/white"/>
</LinearLayout>

Android Layout - Adding Dynamic Textview and EditText

In one of my project I want to create a layout like given Image
In image assume that Red boxes are Textviews and Blue boxes are EditText
The number of rows is not known. It depends on web service Response. How can I achieve this.
You can try like this codes. Such as
<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="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="First Row" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="horizontal" >
<Button
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Ok" />
<Button
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Cancel" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
I expect it will be helpful for you. Best of luck!
Try adding views dynamically in linear layout which is inside scroll view.
Try this code.I know it is not exact solution.But,Hope it gives you some idea.
public class MainActivity extends Activity {
ScrollView scrollview;
LinearLayout linearLayout;
LinearLayout.LayoutParams layoutParams;
static int i;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollview = (ScrollView)findViewById(R.id.scrollview);
linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
Button button = (Button)findViewById(R.id.button);
layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
TextView view = new TextView(MainActivity.this);
view.setText(++i+" view");
linearLayout.addView(view, layoutParams);
}
});
}}
first inf_child.xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/inf_dynamicLl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/btnContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.25"
android:text="OK"/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.25"
android:text="Cancel"/>
</LinearLayout>
</LinearLayout>
second inf_dynamic_view.xml layout which will generate row dynamically:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inner_linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/firstText"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/secontText"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/ThirdText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25" />
<EditText
android:id="#+id/editText"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.25"
/>
</LinearLayout>
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.inf_child, null);
}
LinearLayout dynamicLl = (LinearLayout) convertView.findViewById(R.id.inf_dynamicLl);
for (int rowCounter = 0; rowCounter < parentData.get(groupPosition).childData.get(childPosition).price.size(); rowCounter++)
createRow(dynamicLl, parentData.get(groupPosition).childData.get(childPosition).price.get(rowCounter), groupPosition);
=> create row dynamically
private void createRow(LinearLayout lView, String price, int childPosition) {
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View l = vi.inflate(R.layout.inf_dynamic_view, null);
final TextView tvFirst, tvSecond,tvThird;
final EditText edt1;
lView.addView(l);
lView.refreshDrawableState();
}

Categories

Resources