I am making some changes to an XML layout side of my application. There is a ImageView I am trying to bring to the front yet when I do the app crashes and I can seem to tell why.
Working XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
</LinearLayout>
<TextView
android:id="#+id/place_name"
android:textIsSelectable="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_margin="2dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:src="#drawable/offline" />
XML that crashes:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<TextView
android:id="#+id/place_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
Im just trying to bring the #place_img to the front of the #wrapper.
java.lang.NullPointerException
at com.peekatucorp.peekatu.DiscussArrayAdapter.getView(DiscussArrayAdapter.java:172)
DiscussArrayAdapter.java:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type==1 || type==3)
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
else if(type==4 || type==5)
row = inflater.inflate(R.layout.listitem_users, parent, false);
else
row = inflater.inflate(R.layout.listitem_messages, parent, false);
}
//
final OneComment coment = getItem(position);
userComment = (TextView) row.findViewById(R.id.comment);
userImage = (ImageView) row.findViewById(R.id.place_img);
userName = (TextView) row.findViewById(R.id.place_name);
userOnlineImage = (ImageView) row.findViewById(R.id.pe_profile_pic);
commentDate = (TextView) row.findViewById(R.id.place_distance);
userComment.setText(coment.comment);
ImageLoader imageLoader = ImageLoader.getInstance();
// imageLoader = ImageLoader.getInstance();
// imageLoader.init(ImageLoaderConfiguration.createDefault(convertView.getContext()));
imageLoader.displayImage(coment.image, userImage);
userImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
final TabInfo tab = navact.getCurrentTabInfo();
final ProfileFragment fragment = new ProfileFragment().setUser(coment.userid).setNAV(navact);
// fragment.setText(characters[position]);
// second, you push the fragment. It becomes visible and the up button is
// shown
navact.pushFragment(tab, fragment);
/*
Intent i = new Intent(context, ProfileActivity.class);
i.putExtra("userID", coment.userid);
// Create the view using FirstGroup's LocalActivityManager
View view = ChatTabGroup.group.getLocalActivityManager()
.startActivity("show profile", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
ChatTabGroup.group.replaceView(view);*/
}
});
if(coment.online.equalsIgnoreCase("1"))
userOnlineImage.setImageResource(R.drawable.online);
else
userOnlineImage.setImageResource(R.drawable.offline);
if(coment.gender.equalsIgnoreCase("M"))
userName.setTextColor(Color.parseColor("#878ff4"));
else if(coment.gender.equalsIgnoreCase("F"))
userName.setTextColor(Color.parseColor("#f487d6"));
else
userName.setTextColor(Color.parseColor("#969696"));
userName.setText(coment.username);
commentDate.setText(coment.time);
if(type==4){
commentDate = (TextView) row.findViewById(R.id.textView1);
SharedPreferences preferences = getContext().getSharedPreferences("MyPreferences", getContext().MODE_PRIVATE);
double distance = distFrom(Double.parseDouble(getItem(position).time.split(",")[0]),Double.parseDouble(getItem(position).time.split(",")[1]),
Double.parseDouble(preferences.getString("user_lat", "0.0")),Double.parseDouble(preferences.getString("user_lng", "0.0"))
);
commentDate.setText(""+String.format("%.2f", (distance*0.62))+"miles");
}
if(type==1 || type==3){
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
}else{
}
//
return row;
}
Thank you.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true"
android:typeface="serif"
android:autoLink="web|all"
/>
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
<TextView
android:id="#+id/place_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/wrapper"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="normal" />
Related
I want to make a page using fragment that has a CardView. Let say I have 4 cardviews. I want the cardviews to be clickable and when clicked, they will open a new fragment with a VideoView. Can I only make one fragment that can play different video based on which cardview is clicked.
Meaning, let say, I click on cardview1, it will open a fragment with video1, then i click cardview 2, it will open from the same fragment but with video2. Is it possible to use only one fragment to have more than one videoView?
If possible how can I do that? someone please help.
this is my code
fragment_exercise.xml (fragment with cardviews)
<?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"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:fillViewport="true"
tools:context=".ExerciseFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical"
android:background="#FFF8DD">
<ImageView
android:id="#+id/logo2"
android:layout_width="149dp"
android:layout_height="48dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:layout_marginRight="50dp"
android:src="#drawable/logobg" />
<androidx.cardview.widget.CardView
android:id="#+id/c1"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient16"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Running"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/running" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/c2"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="100dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient3"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Walking"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/walking" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/c3"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient22"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Aerobics"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/aerobatic" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/c4"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="250dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient13"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Yoga"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/yoga2" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/c5"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="400dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient29"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Boxing"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/boxing" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/c6"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="400dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient10"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Cycling"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/biking" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/c7"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="550dp"
android:layout_marginLeft="50dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient24"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Dancing"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/dance" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/c8"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="550dp"
android:layout_marginLeft="220dp"
app:cardCornerRadius="26dp"
app:cardElevation="10dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/gradient30"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:layout_marginTop="80dp"
android:fontFamily="#font/poppins"
android:text="Swimming"
android:textAlignment="center"
android:textColor="#color/white"
android:textSize="18dp"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:layout_width="102dp"
android:layout_height="88dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:src="#drawable/m2" />
</androidx.cardview.widget.CardView>
</RelativeLayout>
</ScrollView>
ExerciseFragment.java
public class ExerciseFragment extends Fragment implements View.OnClickListener{
View v;
CardView cv1, cv2, cv3, cv4, cv5, cv6, cv7, cv8;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_exercise, container, false);
cv1 = (CardView) v.findViewById(R.id.c1);
cv1.setOnClickListener(this);
cv2 = (CardView) v.findViewById(R.id.c2);
cv2.setOnClickListener(this);
cv3 = (CardView) v.findViewById(R.id.c3);
cv3.setOnClickListener(this);
cv4 = (CardView) v.findViewById(R.id.c4);
cv4.setOnClickListener(this);
cv5 = (CardView) v.findViewById(R.id.c5);
cv5.setOnClickListener(this);
cv6 = (CardView) v.findViewById(R.id.c6);
cv6.setOnClickListener(this);
cv7 = (CardView) v.findViewById(R.id.c7);
cv7.setOnClickListener(this);
cv8 = (CardView) v.findViewById(R.id.c8);
cv8.setOnClickListener(this);
// Inflate the layout for this fragment
return v;
}
#Override
public void onClick(View v) {
Fragment fragment1, fragment2, fragment3, fragment4, fragment5, fragment6, fragment7, fragment8;
switch (v.getId()) {
case R.id.c1:
fragment1 = new VideoFragment();
FragmentTransaction transaction1 = getParentFragmentManager().beginTransaction();
transaction1.replace(R.id.container,fragment1);
transaction1.commit();
break;
case R.id.c2:
fragment2 = new VideoFragment();
FragmentTransaction transaction2 = getParentFragmentManager().beginTransaction();
transaction2.replace(R.id.container,fragment2);
transaction2.commit();
break;
case R.id.c3:
fragment3 = new VideoFragment();
FragmentTransaction transaction3 = getParentFragmentManager().beginTransaction();
transaction3.replace(R.id.container,fragment3);
transaction3.commit();
break;
case R.id.c4:
fragment4 = new VideoFragment();
FragmentTransaction transaction4 = getParentFragmentManager().beginTransaction();
transaction4.replace(R.id.container,fragment4);
transaction4.commit();
break;
case R.id.c5:
fragment5 = new VideoFragment();
FragmentTransaction transaction5 = getParentFragmentManager().beginTransaction();
transaction5.replace(R.id.container,fragment5);
transaction5.commit();
break;
case R.id.c6:
fragment6 = new VideoFragment();
FragmentTransaction transaction6 = getParentFragmentManager().beginTransaction();
transaction6.replace(R.id.container,fragment6);
transaction6.commit();
break;
case R.id.c7:
fragment7 = new VideoFragment();
FragmentTransaction transaction7 = getParentFragmentManager().beginTransaction();
transaction7.replace(R.id.container,fragment7);
transaction7.commit();
break;
case R.id.c8:
fragment8 = new VideoFragment();
FragmentTransaction transaction8 = getParentFragmentManager().beginTransaction();
transaction8.replace(R.id.container,fragment8);
transaction8.commit();
break;
}
}
}
fragment_video.xml (fragment with videoview)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".VideoFragment">
<VideoView
android:id="#+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
</FrameLayout>
VideoFragment.java
this is what i have for now
public class VideoFragment extends Fragment {
View v;
VideoView view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = (VideoView) v.findViewById(R.id.videoView1);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(view);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()+"/raw/boxing.mp4");
view.setMediaController(mediaController);
view.setVideoURI(uri);
view.requestFocus();
view.start();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_exercise, container, false);
return v;
}
}
this code has error at line:
MediaController mediaController = new MediaController(this);
please help, how do i make one fragment that will play different videos based on the clicked cardviews without having to make multiple fragments. Is it possible, if yes please help write the code for me. Thank you so much.
I have a view in an activity as below .I want to set Margin to the view.But i used Viewgroup.LayoutParams but it doesn't change the view of the row.Below i have given custom adapter which takes data from firstpagerowitems.xml.I have given the java code for adapter .I tried doing android:layout_marginbottom="10dp" for firstpagerowitems.xml and firstpage.xml but it doesnt work.
class MyAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> rTitle;
ArrayList<String> rDescription;
ArrayList<String> rImgs;
MyAdapter (Context c, ArrayList<String> title,ArrayList<String> description,ArrayList<String> imgs) {
super(c, R.layout.firstpagerowitems, R.id.textView1, title);
this.context = c;
this.rTitle = title;
this.rDescription = description;
this.rImgs = imgs;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = layoutInflater.inflate(R.layout.firstpagerowitems, parent, false);
TextView images = row.findViewById(R.id.textView3);
TextView myTitle = row.findViewById(R.id.textView1);
TextView myDescription = row.findViewById(R.id.textView2);
Log.d("entered last","yes");
Log.d("postion", String.valueOf(position));
Log.d("rimgs", String.valueOf(rImgs));
Log.d("desc", String.valueOf(rDescription));
Log.d("title", String.valueOf(rTitle));
// now set our resources on views
images.setText(rImgs.get(position));
myTitle.setText(rTitle.get(position));
myDescription.setText(rDescription.get(position));
Random random=new Random();
int trp=random.nextInt(16);
Log.d("enteredcolor",mycolors[trp]);
// images.setBackgroundResource(R.color.lightgreen);
String fd=mycolors[trp];
// images.setBackgroundColor(Color.parseColor(fd));
// LinearLayout mylinear=row.findViewById(R.id.mylinear);
// mylinear.setBackgroundColor(Color.parseColor(fd));
row.setBackgroundColor(Color.parseColor(fd));
// ViewGroup.MarginLayoutParams margins=new ViewGroup.MarginLayoutParams(row.getLayoutParams());
// margins.setMargins(0,100,0,100);
// ViewGroup.LayoutParams layouts=new ViewGroup.LayoutParams(margins);
// row.setLayoutParams(layouts);
// ViewGroup.LayoutParams params=new ViewGroup.LayoutParams(row);
return row;
}
}
code.java
<?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:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="one"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_margin="5dp"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
firsypagerowitems.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
android:background="#color/colorWhite"
tools:context=".Firstpage">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:id="#+id/mylinear">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="10dp"
>
</ListView>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/fabs"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
app:fabSize="normal"
app:backgroundTint="#color/design_default_color_error"
app:elevation="6dp"
android:src="#android:drawable/ic_input_add"
>
</com.google.android.material.floatingactionbutton.FloatingActionButton>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
firstpage.xml
Try whether this helps...
<?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:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_marginBottom="5dp"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.2"
android:id="#+id/textView1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:layout_marginBottom="5dp"
android:textSize="20sp"
android:layout_weight="0.45"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textColor="#color/colorWhite"
android:textStyle="bold"
android:textSize="20sp"
android:layout_weight="0.35"
android:id="#+id/textView3"
/>
</LinearLayout>
Hope this helps. Feel free to ask for clarifications...
I have an ExpandableListView in my app. I want to make the child layout a bit narrower than the group header and also keep it centered. Due to some visual effects i must set layout params from code however i can't seem to find the best solutions for this tried changing layout width from xml with no luck. Any advice will be helpful.
Thanks
adapter getChild() method:
#Override
public View getChildView(final int groupPosition, final int
childPosition,
boolean isLastChild, View convertView,
ViewGroup parent) {
final Item expandedListitem = (Item) getChild(groupPosition, childPosition);
Drawable drawable= ContextCompat.getDrawable(context,R.drawable.background_border);
GradientDrawable gradientDrawable= (GradientDrawable) drawable;
gradientDrawable.setStroke(5,expandedListitem.getColor());
if (Build.VERSION.SDK_INT >= 19) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
if (!isLastChild) {
View divider = convertView.findViewById(R.id.linearfaq);
divider.setVisibility(View.INVISIBLE);
ViewGroup.LayoutParams params = convertView.getLayoutParams();
int width = (MainActivity.display.getWidth());
params.height = 110;
params.width=width-40;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
convertView.setLayoutParams(params);
View padder = convertView.findViewById(R.id.padder);
padder.setVisibility(View.INVISIBLE);
} else {
View divider = convertView.findViewById(R.id.linearfaq);
RelativeLayout.LayoutParams params = new
[![enter image description here][1]][1]RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int width = (MainActivity.display.getWidth());
params.height = 140;
params.width=width-40;
params.addRule(RelativeLayout.CENTER_IN_PARENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
convertView.setLayoutParams(params);
divider.setVisibility(View.VISIBLE);
View padder = convertView.findViewById(R.id.padder);
padder.setBackgroundColor(expandedListitem.getColor());
padder.setVisibility(View.VISIBLE);
}
}else{
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item_low, null);
}
}
Item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/back" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
>
<CheckBox
android:layout_alignParentLeft="true"
android:layout_marginTop="2dp"
android:layout_marginEnd="8dp"
android:clickable="true"
android:focusableInTouchMode="false"
android:layout_marginRight="8dp"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_height="wrap_content"
android:id="#+id/checkbox"
/>
<TextView
android:inputType="text"
android:paddingRight="8dp"
android:layout_width="wrap_content"
android:focusable="false"
android:layout_marginTop="6dp"
android:textSize="16sp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/checkbox"
android:layout_toEndOf="#+id/checkbox"
android:id="#+id/list_text"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/del"
android:src="#android:drawable/ic_menu_delete"
android:layout_marginTop="3dp"
android:background="#null"
android:visibility="gone"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkbox"
android:layout_alignBottom="#+id/checkbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:orientation="horizontal"
android:id="#+id/relativeLayout">
<Button
android:id="#+id/plus"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:background="#drawable/round_button"
android:clickable="true"
android:focusable="false"
android:text="+" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/plus"
android:layout_toRightOf="#+id/plus" />
<Button
android:id="#+id/minus"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/quantity"
android:layout_toRightOf="#+id/quantity"
android:background="#drawable/round_button"
android:clickable="true"
android:focusable="false"
android:text="-" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/linearfaq"
android:layout_marginTop="35dp"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/back"
android:orientation="vertical" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/padder"
android:layout_width="match_parent"
android:background="#color/blue"
android:visibility="gone"
android:layout_height="2dp"
android:layout_alignTop="#+id/linearfaq"
>
</RelativeLayout>
category layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/parent_border">
<TextView
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:focusable="false"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cat_text"/>
I'd put all Views in one single ConstraintLayout. By attaching the background to the parent left and right, the View is default centered. By adding margins left and right, you achieve the View being smaller.
Like in this example:
<?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">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:background="#android:color/darker_gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:buttonTint="#android:color/background_light"
android:text="CheckBox"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toStartOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:text="Stuff"
android:textColor="#android:color/background_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/checkBox"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
which looks like this:
the
android:layout_width="0dp"
actually means the view will be stretched to match the constraints. A bit like "match_parent", but within the bounds you set via its constraints.
Play around with the ConstraintLayout a bit. Once you get the hang of it, you will not want to miss it anymore :)
I have created a custom adapter for my list (JobAdapter), however, whenever I set the adapter, even if there are values (jobs arraylist parameter has a size of 2), nothing is displayed on my activity.
This is my adapter:
public class JobAdapter extends ArrayAdapter<Job> {
private final Context context;
private final ArrayList<Job> jobs;
private final int layoutResourceId;
public JobAdapter(Context context, int layoutResourceId, ArrayList<Job> jobs) {
super(context, layoutResourceId, jobs);
this.context = context;
this.jobs = jobs;
this.layoutResourceId = layoutResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService((Activity.LAYOUT_INFLATER_SERVICE));
if (view == null) {
view = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.title = (TextView)view.findViewById(R.id.tv_jobTitle);
holder.payrate = (TextView)view.findViewById(R.id.tv_payRate);
holder.startdate = (TextView)view.findViewById(R.id.tv_startDate);
holder.workinghrs = (TextView)view.findViewById(R.id.tv_duration);
holder.location = (TextView)view.findViewById(R.id.tv_location);
holder.companyname = (TextView)view.findViewById(R.id.tv_companyName);
holder.description = (TextView)view.findViewById(R.id.tv_JobDesc);
holder.experience = (TextView)view.findViewById(R.id.tv_experienceReq);
holder.equipment = (TextView)view.findViewById(R.id.tv_equipmentReq);
view.setTag(holder);
} else {
holder = (ViewHolder)view.getTag();
}
Job j = jobs.get(0);
holder.title.setText(j.getJobTitle());
holder.payrate.setText(j.getPayrate());
holder.startdate.setText(j.getDurationStart());
holder.workinghrs.setText(j.getWorkingHrs());
holder.location.setText(j.getLocation());
holder.companyname.setText("ABC Company");
holder.description.setText(j.getDescription());
holder.experience.setText("3-5 years");
holder.equipment.setText("Hardhat");
//TODO: add hardhat, boots and other field to objects mapped
return view;
}
static class ViewHolder
{
TextView title;
TextView payrate;
TextView startdate;
TextView workinghrs;
TextView location;
TextView companyname;
TextView description;
TextView experience;
TextView equipment;
}
}
This is how I am setting the adapter in the AsyncTask OnPostExecute:
protected void onPostExecute(ArrayList<Job> result) {
arrayAdapter = new JobAdapter(getApplicationContext(), R.layout.jobcard, result);
arrayAdapter.notifyDataSetChanged();
flingContainer.setAdapter(arrayAdapter);
}
What could be the problem with this? There is no error message in Logcat and the application still runs fine, just no data shown. Data should be shown in the jobcard.xml layout which is defined for the JobAdapter.
Thanks in advance for any help with this!
EDIT:
It looks like getView() in the JobAdapter is not being called, I have tried to override the getCount() method in the JobAdapter to return 1 just so I can see one element but that did not work either.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="top"
android:layout_width="match_parent"
android:layout_height="415dp"
android:padding="5dp"
android:layout_marginTop="20dp">
<LinearLayout
android:id="#+id/outerMainCardLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:background="#drawable/swipecard_shadow"
android:gravity="top"
android:layout_marginLeft="5dp">
<LinearLayout
android:id="#+id/mainCardLayout"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical"
android:weightSum="1"
android:background="#FFFFFF"
android:gravity="top">
<TextView
android:id="#+id/tv_jobTitle"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textStyle="bold"
android:textSize="16sp"
android:textColor="#505353"
android:textAlignment="center"
tools:text="Cement Pouring Guy"
android:layout_gravity="center_horizontal"/>
<TextView
android:id="#+id/tv_companyName"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#505353"
android:textAlignment="center"
tools:text="ABC Company"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="#+id/cardHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:id="#+id/tv_experienceReq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginLeft="10dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_experience"
tools:text="3-5 years" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/tv_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_location"
tools:text="Langley, BC" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="#+id/tv_payRate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_marginRight="10dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_payrate"
tools:text="125/day" />
</LinearLayout>
<TextView
android:id="#+id/tv_JobDesc"
android:textSize="14sp"
android:textColor="#505353"
android:background="#e6e7e8"
tools:text="Pour Cement, Mix Cement, Level Cement and go pick up cement bags."
android:layout_width="match_parent"
android:layout_height="150dp"
android:padding="15dp" />
<LinearLayout
android:id="#+id/additionalInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TextView
android:id="#+id/tv_startDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:layout_marginLeft="10dp"
android:drawableTop="#drawable/ic_date"
tools:text="June 1, 2016" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:drawableTop="#drawable/ic_clock"
tools:text=" 8h" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:id="#+id/tv_equipmentReq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:textSize="14sp"
android:textColor="#505353"
android:layout_marginRight="10dp"
android:drawableTop="#drawable/ic_hardhat"
tools:text="Hardhat" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/item_swipe_left_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="20dp"
android:background="#FFFFFF" />
<View
android:id="#+id/item_swipe_right_indicator"
android:alpha="0"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_margin="20dp"
android:layout_gravity="right"
android:background="#FFFFFF" />
I'm trying to make a calander app following this tut:
How can I create a weekly calendar view for an Android Honeycomb application?
But i need to add a view to the corresponding RelativeLayout but every time i try to run
the app it chrashes.
My fragment .java file:
public class Calendar extends Activity {
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class FragmentCalendar extends Fragment {
public FragmentCalendar() {
RelativeLayout activeDay = (RelativeLayout)getActivity(). findViewById(R.id.sundayRelativeLayout);
View viw = new View(null);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
viw.setBackgroundColor(Color.BLUE);
viw.setLayoutParams(lay);
activeDay.addView(viw);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar,
container, false);
return rootView;
}
}
My fragment_Calendar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.TJ.rijschool_app.Calendar"
android:orientation="vertical"
android:padding="0dp"
>
<LinearLayout
android:id="#+id/dayLabelsLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" >
<RelativeLayout
android:id="#+id/currentMonthRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="1" >
<TextView
android:id="#+id/currentMonthTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text=""
android:textSize="15dp"
android:textStyle="bold"
android:textColor="#color/medium_dark_gray" />
<TextView
android:id="#+id/currentYearTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:text=""
android:textSize="13dp"
android:textStyle="bold"
android:textColor="#color/medium_dark_gray" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/sundayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/sundayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/sundayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/sun"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
<View
android:id="#+id/dayMarkerView"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_alignParentBottom="true"
android:background="#color/strong_blue" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/mondayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/mondayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/mondayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:gravity="center"
android:text="#string/mon"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tuesdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/tuesdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tuesdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/tue"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/wednesdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/wednesdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/wednesdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/wed"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/thursdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/thursdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/thursdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/thu"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/fridayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/fridayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:layout_marginLeft="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/fridayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/fri"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/saturdayHeaderRelativeLayout"
android:layout_width="0dp"
android:layout_height="36dp"
android:layout_weight="2" >
<TextView
android:id="#+id/saturdayDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:text=""
android:textColor="#color/medium_dark_gray"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/saturdayTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="6dp"
android:text="#string/sat"
android:textColor="#color/medium_dark_gray"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
Had to delete the part about time because file was getting to big to post here.
As you can see the View called dayMarkerView has to be added dynamicly on the day it is.
Had to change:
public FragmentCalendar() {
RelativeLayout activeDay = (RelativeLayout)getActivity(). findViewById(R.id.sundayRelativeLayout);
View viw = new View(null);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
viw.setBackgroundColor(Color.BLUE);
viw.setLayoutParams(lay);
activeDay.addView(viw);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_calendar,
container, false);
return rootView;
}
TO:
public FragmentCalendar() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = new View(getActivity());
v = inflater.inflate(R.layout.fragment_calendar, container, false);
View pl = v.findViewById(R.id.thursdayHeaderRelativeLayout);
View Paper = new View(pl.getContext());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,2);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Paper.setLayoutParams(params);
Paper.setBackgroundColor(getActivity().getColor(R.color.strong_blue));
((RelativeLayout)pl).addView(Paper);
//Zet de datum op zondag
//View tv = v.findViewById(R.id.sundayDateTextView);
//((TextView)tv).setText("13");
return v;
}