Correct me if I am wrong, but for example when half of a sample U-235 undergoes alpha decay, it marks its half life. However, what if a sample undergoes gamma decay? Wouldn't its mass always stay the same? How do we measure the half life of radioactive materials which only undergo gamma decay?
You can measure the half-life of an isotope by measuring the reduction in the amount of the isotope, and that would not be possible for gamma radiation.
However, you can also measure the half-life of any isotope by monitoring the reduction in the level of radiation from the isotope.
Additionally, most gamma radiation is emitted as a "tidying-up" of some other apha or beta decay.
Consider the isotope Cesiom-137 https://en.wikipedia.org/wiki/Caesium-137.
This isotope undergoes beta-decay to form Ba-137, with a moderately long half-life of 30.17 years. Some of the barium is formed in an excited nuclear state; the protons and neutrons in these barium nuclei are not in their lowest possible energy configuration. The isotope is designated as barium- 137m (m for metastable).
This barium-137m reaches a final stable state by emitting a gamma ray.
This is the case in many alpha or beta decays. But in most cases the gamma ray comes out almost instantaneously, and is associated with the original parent isotope.
But the barium 137m has a half-life of 2.5 minutes; long enough to associate the gamma ray with the metastable barium isotope.
This system is the basis of a very useful Physics student experiment. https://www.clarion.edu/academics/colleges-and-schools/college-of-education-health-and-human-services/school-of-education/science-in-motion-project/available-equipment/Half-Life-of-Barium-137m.doc
A plastic vial contains some radioactive Cs 137 locked up in a stable, insoluble chemical matrix. Because of the decay described above, the vial also contains some barium 137m undergoing gamma decay. The amount of barium stays relatively constant, as the generation from cesium 137 matches the decay to stable barium 137.
WShen the vial is rinsed with water, it leaves the chemically bound cesium behind, while rinsing out the barium (both 137 and 137m).
The amount of barium is chemically undetectable. But the amount of gamma radiation can be measured, and it decreases as the barium 137m disappears, and with the same half life pf 2.5 minutes
In the meanwhile, the plastic vial is restoring the amount of barium-i37m, ready for the next batch of students. The device is often referred to as a "nuclear cow".
Wouldn't its mass always stay the same?
Because nuclei are well modeled with quantum mechanical models and special relativity, no, the mass changes because of the energy taken away with the gamma.
See the answers here.