How to align item vertically in a layout? - android

I try to dynamically populate a list and I have been following this question but for a reason my items aren't one under each other but one next to each others.
This is how I populate it using java:
public View onCreateView(#NonNull final LayoutInflater inflater,
final ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_detail_apero, container, false);
TextView name = (TextView)root.findViewById(R.id.name_apero);
name.setText(detailApero.getName());
TextView date = (TextView)root.findViewById(R.id.date_apero);
date.setText(detailApero.getDate());
LinearLayout ll = (LinearLayout)root.findViewById(R.id.vertical_layout_ingredient);
LinearLayout a = new LinearLayout(root.getContext());
a.setOrientation(LinearLayout.HORIZONTAL);
for(int i = 0; i < 20; i++)
{
Button b = new Button(root.getContext());
b.setText("Button "+i);
a.addView(b);
}
ll.addView(a);
and finally the xml structure:
<?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"
android:background="#color/colorPrimary"
tools:context=".ui.home.AperoDetailFragment">
<TextView
android:id="#+id/name_apero"
android:layout_width="156dp"
android:layout_height="53dp"
android:textSize="18sp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/date_apero"
android:layout_width="238dp"
android:layout_height="53dp"
android:textSize="18sp"
android:ems="10"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.907"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<ScrollView
android:id="#+id/ingredient_apero"
android:layout_width="409dp"
android:layout_height="426dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.111"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.399">
<LinearLayout
android:id="#+id/vertical_layout_ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
/>
</ScrollView>
<TextView
android:id="#+id/ingredient_title_apero"
android:layout_width="115dp"
android:layout_height="28dp"
android:text="Liste d'achat:"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.005"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.125" />
</androidx.constraintlayout.widget.ConstraintLayout>
I don't understand why they aren't coming one under each other, how can I do that ?

Related

Login button and inputs fields aren't work in android studio

First of all, I wanna say sorry about my English.
I am trying to build my first android application using android studio. I created the main page containing two taps one for login activity and the other one for the About section.
what I did exactly was create the Login activity for the fragments to hold the tabs and the basic design and one layout only without the java file for the about_tab_fragment section and finally the login_tab activity.
the problem is that I can not get the input data from the view and no action happened when I click on the login button.(and there is no error in the Run).
and this is my code:
Login activity (Layout)
<?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"
tools:context=".Login">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="#drawable/login_header"
app:layout_constraintHeight_percent=".27"
android:scaleType="centerCrop"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent=".78"
app:layout_constraintVertical_bias="1"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintHeight_percent=".7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tab_layout"/>
<com.google.android.material.tabs.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/desigen_for_login_page"
app:layout_constraintVertical_bias="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_google"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/google_img"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/facebook_img"
app:layout_constraintHorizontal_bias="1"
android:layout_marginRight="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/fab_google"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_insta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
android:src="#drawable/insta_img"
app:layout_constraintHorizontal_bias="0"
android:layout_marginLeft="10dp"
android:elevation="35dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fab_google"
app:layout_constraintTop_toBottomOf="#+id/view_pager" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Login activity (Java)
public class Login extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
FloatingActionButton facebook,google,instagram;
float v = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = findViewById(R.id.tab_layout);
viewPager = findViewById(R.id.view_pager);
facebook = findViewById(R.id.fab_facebook);
google = findViewById(R.id.fab_google);
instagram = findViewById(R.id.fab_insta);
tabLayout.addTab(tabLayout.newTab().setText("Login"));
tabLayout.addTab(tabLayout.newTab().setText("About"));
tabLayout.setTabGravity(tabLayout.GRAVITY_FILL);
final LoginAdapterClass adapter = new LoginAdapterClass(getSupportFragmentManager(),this,tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//For animations
facebook.setTranslationY(300);
google.setTranslationY(300);
instagram.setTranslationY(300);
tabLayout.setTranslationY(300);
facebook.setAlpha(v);
google.setAlpha(v);
instagram.setAlpha(v);
tabLayout.setAlpha(v);
facebook.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(400).start();
google.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(600).start();
instagram.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(800).start();
tabLayout.animate().translationY(0).alpha(1).setDuration(1000).setStartDelay(100).start();
}
}
about_tab_fragment (layout)
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About"
android:textSize="30dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Login_tab (Layout)
<?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"
tools:context=".LoginTab">
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="24dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_email_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:background="#drawable/custom_inputs"
android:drawableStart="#drawable/custom_lock_icon"
android:drawablePadding="12dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/email" />
<Button
android:id="#+id/loginBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.758"
app:layout_constraintWidth_percent="0.8" />
<TextView
android:id="#+id/forgetPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forget Password"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintVertical_bias="0"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/password"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/password" />
</androidx.constraintlayout.widget.ConstraintLayout>
My problem occurs here:
I can not take the text from the inputs email and password plus that I can not add event to the loginBtn. I tried to put Toast in the On Click Listener but nothing happened.
Login_tab (Java)
public class LoginTab extends AppCompatActivity {
EditText emailLogin,passwordLogin;
Button loginBtn;
TextView forgetPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_tab);
emailLogin = findViewById(R.id.email);
passwordLogin = findViewById(R.id.password);
loginBtn = findViewById(R.id.loginBtn);
forgetPassword = findViewById(R.id.forgetPassword);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(Login.this,"This is the login button",Toast.LENGTH_LONG).show();
if(emailLogin.getText().toString().isEmpty()){
emailLogin.setError("Email is required");
return;
}
if(passwordLogin.getText().toString().isEmpty()){
passwordLogin.setError("Password is required");
return;
}
}
});
}
}
Can any one help me to fix the problem.
I did a lot of searches but I could not find any answer.
I found the problem.
the problem because I have to put the action in the class loginTab that extends Fragment not AppCompatActivity.
public class LoginTabFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.activity_login_tab,container,false);
Button b = root.findViewById(R.id.loginBtn);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(root.getContext(), "This is the Login button....",Toast.LENGTH_LONG).show();
}
});
return root;
}
}

Recycleview custom layout constraint are changing for each inflated view causing problems

I have a recyclerview that inflates data fetched from json.
The issue I am facing are design issues - sometimes the text is aligned correctly and the image is shown and sometimes the text is too long, causing what I think is for the image not to be shown.
here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="395dp"
android:layout_height="170dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="#335634"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/heroImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:src="#mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/heroTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="Hero Title"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/heroDescription"
android:layout_width="200dp"
android:breakStrategy="simple"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="TextView"
android:textAlignment="center"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/heroImage"
app:layout_constraintTop_toBottomOf="#+id/heroTitle" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
and here is my adapter:
public class HeroesAdapter extends RecyclerView.Adapter<HeroesAdapter.HeroesViewHolder> {
private List<Hero> heroList;
public HeroesAdapter(List<Hero> heroList) {
this.heroList = heroList;
}
#NonNull
#Override
public HeroesViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.hero_cell, parent, false);
return new HeroesViewHolder(rowView);
}
#Override
public void onBindViewHolder(#NonNull HeroesViewHolder holder, int position) {
Hero currentHero = heroList.get(position);
String str = String.join(",", currentHero.abilities);
holder.heroTitle.setText(currentHero.title);
holder.heroAbilties.setText(str);
Picasso.get().load(currentHero.image).resize(500,500).into(holder.heroesImage);
}
#Override
public int getItemCount() {
return heroList.size();
}
public static class HeroesViewHolder extends RecyclerView.ViewHolder {
ImageView heroesImage;
TextView heroTitle;
TextView heroAbilties;
public HeroesViewHolder(#NonNull View itemView) {
super(itemView);
heroesImage = itemView.findViewById(R.id.heroImage);
heroTitle = itemView.findViewById(R.id.heroTitle);
heroAbilties = itemView.findViewById(R.id.heroDescription);
}
}
}
This is the result I am getting and this is the wanted result.
the issues I am facing are:
making each row have a clean design and act the same for all.
images are sometimes shown, sometimes not.
bottom view is "half in screen" and half out of screen.
By using LinearLayout you can design your hero_cell that will give you the desirable output. as I do it for now.
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="horizontal"
android:padding="10dp"
android:background="#335634"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/heroImage"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:src="#drawable/logo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:background="#335634"
android:layout_marginLeft="20dp"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/heroTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="Hero Title"
android:textSize="25dp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/heroDescription"
android:layout_width="match_parent"
android:breakStrategy="simple"
android:layout_height="wrap_content"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="TextView"
android:textSize="18dp" />
</LinearLayout>
According to your expectation, you should use card-view and I've updated the layout. Please check
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
app:cardCornerRadius="10dp"
android:elevation="10dp"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="395dp"
android:layout_height="170dp"
android:background="#fff"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/heroImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:src="#mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/heroTitle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:text="Hero Title"
android:textColor="#000"
android:textSize="25dp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/heroDescription"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/heroImage"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/heroDescription"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="25dp"
android:autoSizeMinTextSize="15dp"
android:breakStrategy="simple"
android:text="TextView"
android:textColor="#444"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/heroImage"
app:layout_constraintTop_toBottomOf="#+id/heroTitle" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>

Nothing happens when i try to change TextView value dynamically in a fragment

I tried everything on the internet but still couldn't update the value. but however i can update the progress bar value. i wonder why i can update progress but not the textview value
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ProgressBar prograss_bar;
int prograss = 28;
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
TextView prograss_perc = (TextView) view.findViewById(R.id.sale_prog_pres);
prograss_perc.setText(String.valueOf(prograss));
prograss_bar = (ProgressBar) view.findViewById(R.id.sales_prog);
prograss_bar.setProgress(prograss);
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
I get empty value for the Textview but progress bar updates. what can i do?
xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".tab1">
<TextView
android:id="#+id/sale_prog_pres"
android:layout_width="84dp"
android:layout_height="29dp"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="#+id/sales_prog"
app:layout_constraintEnd_toEndOf="#+id/sales_prog"
app:layout_constraintStart_toStartOf="#+id/sales_prog"
app:layout_constraintTop_toTopOf="#+id/sales_prog" />
<ProgressBar
android:id="#+id/sales_prog"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="247dp"
android:layout_height="212dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:progressDrawable="#drawable/circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.396" />
<TextView
android:id="#+id/rights"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Powered by Sinewavesoft "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
There are 2 problems with your code.
First, make sure your text color is different from background color by adding android:textColor="#FFF".
<TextView
android:id="#+id/sale_prog_pres"
android:layout_width="84dp"
android:layout_height="29dp"
android:gravity="center"
android:background="#android:color/black"
android:textColor="#FFF"
app:layout_constraintBottom_toBottomOf="#+id/sales_prog"
app:layout_constraintEnd_toEndOf="#+id/sales_prog"
app:layout_constraintStart_toStartOf="#+id/sales_prog"
app:layout_constraintTop_toTopOf="#+id/sales_prog" />
Second, change onCreateView method code to
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
ProgressBar prograss_bar;
int prograss = 28;
TextView prograss_perc = (TextView) view.findViewById(R.id.sale_prog_pres);
prograss_perc.setText(String.valueOf(prograss));
prograss_bar = (ProgressBar) view.findViewById(R.id.sales_prog);
prograss_bar.setProgress(prograss);
// This line is IMPORTANT!!!
return view;
}
From the code snippet you provided, you're setting the text value of TextView prograss_perc to the string value of the object itself. It should be:
int progress = 28;
prograss_perc.setText(String.valueOf(progress));
Edit: The TextView is behind the ProgressBar. Use this instead.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".tab1">
<ProgressBar
android:id="#+id/sales_prog"
android:background="#android:color/holo_red_dark"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="247dp"
android:layout_height="212dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:progressDrawable="#drawable/circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.396" />
<TextView
android:id="#+id/sale_prog_pres"
android:layout_width="84dp"
android:layout_height="29dp"
android:gravity="center"
android:background="#android:color/black"
app:layout_constraintBottom_toBottomOf="#+id/sales_prog"
app:layout_constraintEnd_toEndOf="#+id/sales_prog"
app:layout_constraintStart_toStartOf="#+id/sales_prog"
app:layout_constraintTop_toTopOf="#+id/sales_prog" />
<TextView
android:id="#+id/rights"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Powered by Sinewavesoft "
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>

how to show drop down of AutoCompleteTextView behind of a view?

I used an AutoCompleteTextView in my Service. after user type in AutoCompleteTextView, a drop down shown like this:
drop down is shown top of service layout but what I want is shown behind of that like this:
service use this layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
>
<View
android:id="#+id/bg"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/floating_service_bg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<android.support.constraint.ConstraintLayout
android:id="#+id/collapse_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="#id/bg"
app:layout_constraintLeft_toLeftOf="#id/bg"
app:layout_constraintTop_toTopOf="#id/bg"
>
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="8dp"
android:src="#drawable/star"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<ImageView
android:id="#+id/close_btn"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/ic_close"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="ContentDescription"/>
</android.support.constraint.ConstraintLayout>
<View
android:id="#+id/drop_down_anchor"
android:layout_width="0dp"
android:layout_height="0.5dp"
android:layout_margin="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<LinearLayout
android:id="#+id/expanded_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="horizontal"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="#id/bg"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintLeft_toRightOf="#id/collapse_view"
app:layout_constraintRight_toRightOf="#id/bg"
app:layout_constraintTop_toTopOf="#id/bg">
<AutoCompleteTextView
android:id="#+id/searchBox"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="#color/transparent"
android:dropDownAnchor="#id/drop_down_anchor"
android:gravity="left"
android:hint="#string/searchboxHint"
android:imeOptions="actionDone"
android:inputType="textPhonetic"
android:padding="15dip"
android:selectAllOnFocus="false"
android:singleLine="true"
android:textColor="#000"
android:textColorHint="#a5a5a5"
android:textIsSelectable="true"
android:textSize="16sp"
/>
<ImageView
android:id="#+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/ic_search_24dp"
android:visibility="visible"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
and DropDownAdapter class:
class DropDownAdapter extends ArrayAdapter<String> {
ArrayList<String> wordList;
ArrayList<String> meanList;
public DropDownAdapter(ArrayList<String> words, ArrayList<String> means) {
super(context, R.layout.drop_down_layout, words);
this.wordList = words;
this.meanList = means;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(LAYOUT_INFLATER_SERVICE);
View rowView = (View) inflater.inflate(R.layout.drop_down_layout,
parent, false);
TextView leftTextView = (TextView) rowView.findViewById(R.id.item_word);
TextView rightTextView = (TextView) rowView.findViewById(R.id.item_mean);
LinearLayout dropDownItem = (LinearLayout) rowView.findViewById(R.id.drop_down_item);
final int pos = position;
leftTextView.setText(wordList.get(pos));
rightTextView.setText(meanList.get(pos));
return rowView;
}
}
can you suggest me a solution, please?

adding cardviews to scroll view dynamically, not showing

I am attempting to add card views to a scroll view dynamically. so I am adding these cardviews to a linear layout which is situated inside a scrollview in my xml. However when I press the fab button I don't see anything. I don't know why. Is it something to do with layoutparams?
This is my create.java
public class create extends AppCompatActivity {
Button button;
Context context;
CardView cardview;
LayoutParams layoutparams;
TextView textview;
LinearLayout linearLayout;
ScrollView scrollView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create);
button = (Button)findViewById(R.id.button);
context = getApplicationContext();
linearLayout = (LinearLayout) findViewById(R.id.linearlayout1);
scrollView = (ScrollView)findViewById(R.id.scrollView1);
//TODO FAB BUTTON
FloatingActionButton floatingActionButton =
(FloatingActionButton) findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
createCardViewProgrammatically();
}
});
}
public void createCardViewProgrammatically(){
CardView card = new CardView(context);
// Set the CardView layoutParams
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
card.setLayoutParams(params);
// Set CardView corner radius
card.setRadius(9);
// Set cardView content padding
card.setContentPadding(15, 15, 15, 15);
// Set a background color for CardView
card.setCardBackgroundColor(Color.parseColor("#FFC6D6C3"));
// Set the CardView maximum elevation
card.setMaxCardElevation(15);
// Set CardView elevation
card.setCardElevation(9);
// Initialize a new TextView to put in CardView
TextView tv = new TextView(context);
tv.setLayoutParams(params);
tv.setText("CardView\nProgrammatically");
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
tv.setTextColor(Color.RED);
// Put the TextView in CardView
card.addView(tv);
// Finally, add the CardView in root layout
linearLayout.addView(card);
}
}
and this is my xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:background="#color/colorBackground"
android:minHeight="170dp"
tools:context=".create"
tools:layout_editor_absoluteY="81dp"
>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="438dp"
android:fillViewport="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="0dp">
<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"
tools:context="com.android_examples.cardviewprogrammatically_android_examplescom.MainActivity"
android:id="#+id/linearlayout1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to Create CardView programmatically on Button click"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="60dp"
android:layout_height="70dp"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="8dp"
android:src="#android:drawable/ic_input_add"
app:backgroundTint="#color/colorCreate"
app:elevation="6dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:pressedTranslationZ="12dp"
android:tint="#color/colorBackground"/>
<View
android:id="#+id/subheading"
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginStart="1dp"
android:layout_marginTop="2dp"
android:background="#color/colorBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
/>
<View
android:id="#+id/view"
android:layout_width="320dp"
android:layout_height="1dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="76dp"
android:background="#color/colorText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/view2"
android:layout_width="320dp"
android:layout_height="1dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="28dp"
android:background="#color/colorText"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:text="#string/done_label"
android:textColor="#color/colorText"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView4"
app:layout_constraintTop_toBottomOf="#+id/view2" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="128dp"
android:layout_marginRight="128dp"
android:layout_marginTop="8dp"
android:text="#string/aisle_label"
android:textColor="#color/colorText"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view2"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="5dp"
android:text="#string/qty_label"
android:textColor="#color/colorText"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toStartOf="#+id/textView4"
app:layout_constraintTop_toBottomOf="#+id/view2"
app:layout_constraintVertical_bias="0.7" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/item_label"
android:textColor="#color/colorText"
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toStartOf="#+id/textView3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view2"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
You need to specify the Orientation of your LinearLayout as VERTICAL
<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"
tools:context="com.android_examples.cardviewprogrammatically_android_examplescom.MainActivity"
android:id="#+id/linearlayout1">

Categories

Resources